Skip to content

Commit

Permalink
stuff from defence (zoned attemptInfo, sorted history table, separate…
Browse files Browse the repository at this point in the history
…d attemptInfo into multiple entities)
  • Loading branch information
ThatDraenGuy committed Oct 27, 2022
1 parent ed3f1c7 commit 3a6cecb
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 67 deletions.
Binary file modified report/Report.odt
Binary file not shown.
Binary file modified report/Report.pdf
Binary file not shown.
57 changes: 20 additions & 37 deletions src/main/java/domain/AttemptInfo.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,50 @@
package domain;


import jakarta.enterprise.inject.spi.CDI;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ui.TimeZoneBean;

import java.time.*;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Entity
@Table(name = "attempts")
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class AttemptInfo {
@Setter
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private double x;
private double y;
private double r;
private boolean res;
private String message;
private Duration execTime;
private LocalDateTime currTime;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "coord_id", referencedColumnName = "id")
private CoordInfo coords;

public AttemptInfo() {}
private AttemptInfo(double x, double y, double r, boolean res, String message, Duration execTime, LocalDateTime currTime) {
this.x=x;
this.y=y;
this.r=r;
this.res=res;
this.message=message;
this.execTime=execTime;
this.currTime=currTime;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "shot_id", referencedColumnName = "id")
private ShotInfo shot;

public static AttemptInfo fromHit(Instant startTime, double x, double y, double r, boolean res, String message) {
return new AttemptInfo(x, y, r, res, message, getDiff(startTime), LocalDateTime.now());
}
private ZonedDateTime currTime;

public static AttemptInfo empty() {
return new AttemptInfo(0,0,0,false,"",Duration.ZERO,LocalDateTime.MIN);
}

private static Duration getDiff(Instant start) {
Instant finish = Instant.now();
return Duration.between(start, finish);
public static AttemptInfo fromHit(CoordInfo coordInfo, ShotInfo shotInfo) {
String timezone = CDI.current().select(TimeZoneBean.class).get().getTimezone();
return new AttemptInfo(null, coordInfo, shotInfo, ZonedDateTime.now(ZoneId.of(timezone)));
}

@Override
public String toString() {
return "AttemptInfo{" +
"x='" + x + '\'' +
", y='" + y + '\'' +
", r='" + r + '\'' +
", res=" + res +
", message='" + message + '\'' +
", execTime=" + execTime.toNanos() + "ns" +
", currTime=" + currTime.format(DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm:ss")) +
"id=" + id +
", coordInfo=" + coords +
", shotInfo=" + shot +
", currTime=" + currTime +
'}';
}
}
36 changes: 36 additions & 0 deletions src/main/java/domain/CoordInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "coord-attempts")
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class CoordInfo {
@Setter
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private double x;
private double y;
private double r;

@Override
public String toString() {
return "CoordInfo{" +
"id=" + id +
", x=" + x +
", y=" + y +
", r=" + r +
'}';
}

public static CoordInfo create(double x, double y, double r) {
return new CoordInfo(null,x,y,r);
}
}
45 changes: 45 additions & 0 deletions src/main/java/domain/ShotInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package domain;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.Duration;
import java.time.Instant;

@Entity
@Table(name = "shot-attempts")
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ShotInfo {
@Setter
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private boolean res;
private String message;
private Duration execTime;

public static ShotInfo create(boolean res, String message, Instant startTime) {
return new ShotInfo(null,res,message,getDiff(startTime));
}

private static Duration getDiff(Instant start) {
Instant finish = Instant.now();
return Duration.between(start, finish);
}

@Override
public String toString() {
return "ShotInfo{" +
"id=" + id +
", res=" + res +
", message='" + message + '\'' +
", execTime=" + execTime +
'}';
}
}

5 changes: 4 additions & 1 deletion src/main/java/logic/HistoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.Serializable;

import java.util.Comparator;
import java.util.List;

@Named("historyBean")
Expand All @@ -24,7 +25,9 @@ public void add(AttemptInfo attemptInfo) {
}

public List<AttemptInfo> getAttempts() {
return attemptInfoDao.getAll();
List<AttemptInfo> history = attemptInfoDao.getAll();
history.sort((o1, o2) -> o2.getCurrTime().compareTo(o1.getCurrTime()));
return history;
}

public boolean isEmpty() {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/logic/QuadrantsBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.AttemptInfo;
import domain.CoordInfo;
import domain.ShotInfo;
import logic.quadrants.Quadrant;
import logic.quadrants.QuadrantsInfo;
import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -35,13 +37,14 @@ private void init() {

public AttemptInfo doCheck(double x, double y, double r) {
Instant startTime = Instant.now();
if (r<=0) return AttemptInfo.fromHit(startTime,x,y,r,false,"Invalid R value");
CoordInfo coordInfo = CoordInfo.create(x,y,r);
if (r<=0) return AttemptInfo.fromHit(coordInfo, ShotInfo.create(false, "Invalid R value", startTime));
for (Quadrant quadrant : quadrants) {
boolean res = quadrant.checkHit(x,y,r);
if (res) {
return AttemptInfo.fromHit(startTime,x,y,r, true, "That's a hit!");
return AttemptInfo.fromHit(coordInfo, ShotInfo.create(true, "That's a hit!", startTime));
}
}
return AttemptInfo.fromHit(startTime,x,y,r, false, "That's a miss!");
return AttemptInfo.fromHit(coordInfo, ShotInfo.create(false, "That's a miss", startTime));
}
}
11 changes: 4 additions & 7 deletions src/main/java/ui/ClockBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.io.Serializable;
Expand All @@ -11,18 +12,14 @@
@Named("clockBean")
@SessionScoped
public class ClockBean implements Serializable {

private String timezone = "America/New_York";
@Inject
private TimeZoneBean timeZoneBean;

public String getTime() {
return formattedTime();
}

private String formattedTime() {
return DateTimeFormatter.ofPattern("HH:mm:ss VV").format(ZonedDateTime.now(ZoneId.of(timezone)));
}

public void updateTimezone() {
timezone = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("timezone");
return DateTimeFormatter.ofPattern("HH:mm:ss VV").format(ZonedDateTime.now(ZoneId.of(timeZoneBean.getTimezone())));
}
}
19 changes: 19 additions & 0 deletions src/main/java/ui/TimeZoneBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ui;

import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import lombok.Getter;

import java.io.Serializable;

@SessionScoped
@Named("timeZoneBean")
public class TimeZoneBean implements Serializable {
@Getter
private String timezone = "America/New_York";

public void updateTimezone() {
timezone = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("timezone");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ui;

import jakarta.enterprise.inject.spi.CDI;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
Expand All @@ -10,15 +11,16 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@FacesConverter("localeDateTimeConverter")
public class LocaleDateTimeConverter implements Converter<LocalDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
public class ZonedDateTimeConverter implements Converter<ZonedDateTime> {

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss O").withZone(ZoneId.of(CDI.current().select(TimeZoneBean.class).get().getTimezone()));
@Override
public LocalDateTime getAsObject(FacesContext context, UIComponent component, String value) {
return LocalDateTime.parse(value, formatter);
public ZonedDateTime getAsObject(FacesContext context, UIComponent component, String value) {
return ZonedDateTime.parse(value, formatter);
}

@Override
public String getAsString(FacesContext context, UIComponent component, LocalDateTime value) {
public String getAsString(FacesContext context, UIComponent component, ZonedDateTime value) {
return formatter.format(value);
}
}
7 changes: 7 additions & 0 deletions src/main/webapp/WEB-INF/classes/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@

<!-- Make all annotated entities part of this unit configuration: -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>-->
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="true"/>
<!-- Is nighthack for WildFly: https://issues.jboss.org/browse/WFLY-2727 -->
<!-- <property name="wildfly.jpa.twophasebootstrap" value="false" />-->
</properties>
</persistence-unit>
</persistence>
10 changes: 5 additions & 5 deletions src/main/webapp/WEB-INF/templates/history.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@
tableStyleClass="history-table" rowStyleClass="history-table-cell" headerClass="header-history-cell">
<p:column>
<f:facet name="header">X</f:facet>
<h:outputText value="#{attempt.x}"/>
<h:outputText value="#{attempt.coords.x}"/>
</p:column>
<p:column>
<f:facet name="header">Y</f:facet>
<h:outputText value="#{attempt.y}"/>
<h:outputText value="#{attempt.coords.y}"/>
</p:column>
<p:column>
<f:facet name="header">R</f:facet>
<h:outputText value="#{attempt.r}"/>
<h:outputText value="#{attempt.coords.r}"/>
</p:column>
<p:column>
<f:facet name="header">Result</f:facet>
<h:outputText value="#{attempt.message}"/>
<h:outputText value="#{attempt.shot.message}"/>
</p:column>
<p:column>
<f:facet name="header">Execution time</f:facet>
<h:outputText value="#{attempt.execTime}">
<h:outputText value="#{attempt.shot.execTime}">
<f:converter converterId="nanosDurationConverter"/>
</h:outputText>
</p:column>
Expand Down
21 changes: 21 additions & 0 deletions src/main/webapp/WEB-INF/templates/timezone.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:my="http://xmlns.jcp.org/jsf/composite/mycomponents/graph"
>
<ui:composition>
<h:form>
<p:remoteCommand name="setTimezone" action="#{timeZoneBean.updateTimezone()}" update="#{listener}"/>
<p:remoteCommand name="updateTimezone" update="#{listener}"/>
</h:form>
<script type="text/javascript">
setTimezone([{name: "timezone", value: Intl.DateTimeFormat().resolvedOptions().timeZone}]);
updateTimezone();
</script>
</ui:composition>
</html>
5 changes: 5 additions & 0 deletions src/main/webapp/main.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<h:outputStylesheet library="style" name="main.css"/>
<h:outputStylesheet library="style" name="content.css"/>
<h:body>
<h:panelGroup id="timezone">
<ui:include src="WEB-INF/templates/timezone.xhtml">
<ui:param name="listener" value=":history-table"/>
</ui:include>
</h:panelGroup>
<table class="main-table">
<colgroup>
<col class="left"/>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/resources/mycomponents/clock.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h:form>
<p:poll interval="#{cc.attrs.interval}" update="clock"/>
<h:outputText styleClass="clock-text" id="clock" value="#{clockBean.time}"/>
<p:remoteCommand name="setTimezone" action="#{clockBean.updateTimezone()}" update="clock"/>
<p:remoteCommand name="setTimezone" action="#{timeZoneBean.updateTimezone()}" update="clock"/>
</h:form>
<script type="text/javascript">
setTimezone([{name: "timezone", value: Intl.DateTimeFormat().resolvedOptions().timeZone}]);
Expand Down
Loading

0 comments on commit 3a6cecb

Please sign in to comment.