Skip to content

Commit

Permalink
Merge pull request #6 from nkjmlab/develop
Browse files Browse the repository at this point in the history
2022年度第1回の授業後のバグ修正
  • Loading branch information
yuu-nkjm authored Jun 16, 2022
2 parents 65a71aa + 894b5bb commit b49bf48
Show file tree
Hide file tree
Showing 35 changed files with 276 additions and 239 deletions.
35 changes: 25 additions & 10 deletions nkjmlab-go-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
</scm>

<properties>
<nkjmlab-utils-version>0.8.0</nkjmlab-utils-version>
<nkjmlab-utils-version>0.8.1</nkjmlab-utils-version>
<thymeleaf-version>3.0.15.RELEASE</thymeleaf-version>
<sorm4j-version>1.4.7</sorm4j-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<downloadSources>true</downloadSources>
Expand Down Expand Up @@ -58,34 +59,40 @@
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>4.3.0</version>
<version>4.6.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>8.1.0</version>
<version>8.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
<version>2.1.212</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.0</version>
<version>5.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.15.RELEASE</version>
<version>${thymeleaf-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-java8time -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
Expand All @@ -111,13 +118,13 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2</version>
<version>2.13.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.2</version>
<version>2.13.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
Expand All @@ -135,14 +142,14 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<version>5.9.0-M1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -156,6 +163,14 @@
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<includeScope>compile</includeScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.nkjmlab.util.jsonrpc.JsonRpcRequest;
import org.nkjmlab.util.jsonrpc.JsonRpcResponse;
import org.nkjmlab.util.thymeleaf.TemplateEngineBuilder;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.javalin.Javalin;
Expand Down Expand Up @@ -142,6 +144,7 @@ public GoApplication() {

this.memDbDataSource = createH2DataSource(factory.getInMemoryModeJdbcUrl(),
factory.getUsername(), factory.getPassword());
log.info("server jdbcUrl={}", factory.getServerModeJdbcUrl());
this.fileDbDataSource = createHikariDataSource(factory.getServerModeJdbcUrl(),
factory.getUsername(), factory.getPassword());
// H2Server.openBrowser(memDbDataSource, true);
Expand All @@ -162,8 +165,10 @@ public static String getJdbcUrlOfInMemoryDb(String dbName) {


private void prepareJavalin() {
JavalinThymeleaf.configure(new TemplateEngineBuilder().setPrefix("/templates/")
.setTtlMs(THYMELEAF_EXPIRE_TIME_MILLI_SECOND).build());
TemplateEngine engine = new TemplateEngineBuilder().setPrefix("/templates/")
.setTtlMs(THYMELEAF_EXPIRE_TIME_MILLI_SECOND).build();
engine.addDialect(new Java8TimeDialect());
JavalinThymeleaf.configure(engine);

this.app = Javalin.create(config -> {
config.addStaticFiles(WEBROOT_DIR_NAME, Location.CLASSPATH);
Expand All @@ -178,6 +183,8 @@ private void prepareTable(H2LocalDataSourceFactory dsFactory) {
problemsTable.dropAndInsertInitialProblemsToTable(PROBLEM_DIR);

this.loginsTable = new LoginsTable(fileDbDataSource);
loginsTable.createTableIfNotExists();
loginsTable.createIndexesIfNotExists();
loginsTable.writeCsv(new File(BACKUP_DIR, "logins-" + System.currentTimeMillis() + ".csv"));


Expand All @@ -199,7 +206,8 @@ private void prepareTable(H2LocalDataSourceFactory dsFactory) {


this.passwordsTable = new PasswordsTable(fileDbDataSource);

passwordsTable.dropTableIfExists();
passwordsTable.createTableAndIndexesIfNotExists();
try {
File f = ResourceUtils.getResourceAsFile("/conf/passwords.csv");
passwordsTable.readFromFileAndMerge(f);
Expand All @@ -211,8 +219,14 @@ private void prepareTable(H2LocalDataSourceFactory dsFactory) {


this.gameRecordsTable = new GameRecordsTable(fileDbDataSource);
gameRecordsTable.createTableIfNotExists();
gameRecordsTable.createIndexesIfNotExists();
gameRecordsTable
.writeCsv(new File(BACKUP_DIR, "game-record" + System.currentTimeMillis() + ".csv"));

gameRecordsTable.recalculateAndUpdateRank(usersTable);


this.matchingRequestsTable = new MatchingRequestsTable(memDbDataSource);

this.gameStatesTable = new GameStatesTable(fileDbDataSource);
Expand All @@ -223,14 +237,8 @@ private void prepareTable(H2LocalDataSourceFactory dsFactory) {
gameStatesTableInMem.insert(gameStatesTable.readAll().toArray(GameState[]::new));

this.gameStatesTables = new GameStatesTables(fileDbDataSource, memDbDataSource);

this.votesTable = new VotesTable(memDbDataSource);

this.gameRecordsTable = new GameRecordsTable(fileDbDataSource);
this.gameRecordsTable
.writeCsv(new File(BACKUP_DIR, "game-record" + System.currentTimeMillis() + ".csv"));


this.websoketSessionsTable = new WebsoketSessionsTable(memDbDataSource);
this.wsManager = new WebsocketSessionsManager(gameStatesTables, problemsTable,
websoketSessionsTable, usersTable, handsUpTable, matchingRequestsTable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.nkjmlab.go.javalin.model.row.User.*;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -53,9 +52,8 @@ public boolean registerAttendance(String userId, String seatId) {
User u = usersTable.readByPrimaryKey(userId);
u.setSeatId(seatId);
usersTable.merge(u);
loginsTable
.insert(new Login(userId, seatId, u.getUserName(), Timestamp.valueOf(LocalDateTime.now()),
HttpRequestUtils.getXForwardedFor(request).orElseGet(() -> request.getRemoteAddr())));
loginsTable.insert(new Login(userId, seatId, u.getUserName(), LocalDateTime.now(),
HttpRequestUtils.getXForwardedFor(request).orElseGet(() -> request.getRemoteAddr())));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import static org.nkjmlab.go.javalin.GoApplication.*;
import java.io.File;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand All @@ -30,6 +29,7 @@
import org.nkjmlab.go.javalin.websocket.WebsoketSessionsTable;
import org.nkjmlab.util.jackson.JacksonMapper;
import org.nkjmlab.util.java.Base64Utils;
import org.nkjmlab.util.java.json.JsonMapper;
import org.nkjmlab.util.java.lang.ParameterizedStringUtils;
import org.nkjmlab.util.javax.imageio.ImageIoUtils;

Expand All @@ -48,7 +48,7 @@ public class GoJsonRpcService implements GoJsonRpcServiceInterface {
private final HandsUpTable handsUpTable;
private final GameRecordsTable gameRecordsTable;

private static final JacksonMapper mapper = JacksonMapper.getDefaultMapper();
private static final JsonMapper mapper = JacksonMapper.getIgnoreUnknownPropertiesMapper();


public GoJsonRpcService(WebsocketSessionsManager wsManager, GameStatesTables gameStatesTables,
Expand Down Expand Up @@ -119,16 +119,16 @@ public ProblemJson saveProblem(String gameId, long problemId, String groupId, St
autoBackupProblemJsonToFile(ProblemJson.createFrom(p));
p.setAgehama(mapper.toJson(currentState.getAgehama()));
p.setCells(mapper.toJson(currentState.getCells()));
p.setCreatedAt(Timestamp.from(Instant.now()));
p.setCreatedAt(LocalDateTime.now());
p.setHandHistory(mapper.toJson(currentState.getHandHistory()));
p.setSymbols(mapper.toJson(currentState.getSymbols()));
p.setName(name);
p.setGroupId(groupId);
p.setMessage(message);
problemsTable.merge(p);
} else {
p = new Problem(problemId == -1 ? ProblemFactory.getNewId() : problemId,
Timestamp.from(Instant.now()), groupId, name, mapper.toJson(currentState.getCells()),
p = new Problem(problemId == -1 ? ProblemFactory.getNewId() : problemId, LocalDateTime.now(),
groupId, name, mapper.toJson(currentState.getCells()),
mapper.toJson(currentState.getSymbols()), message == null ? "" : message,
mapper.toJson(currentState.getHandHistory()), mapper.toJson(currentState.getAgehama()));
problemsTable.insert(p);
Expand Down Expand Up @@ -259,7 +259,7 @@ public void enterWaitingRoom(String userId) {
}
usersTable.update(u);

MatchingRequest matchingReq = new MatchingRequest(u, new Date());
MatchingRequest matchingReq = new MatchingRequest(u, LocalDateTime.now());
if (!matchingRequestsTable.exists(matchingReq)) {
matchingRequestsTable.insert(matchingReq);
} else {
Expand Down Expand Up @@ -321,7 +321,7 @@ public void handUp(String gameId, boolean handUp, String message) {
if (handUp) {
HandUp h = handsUpTable.readByPrimaryKey(gameId);
if (h == null) {
handsUpTable.insert(new HandUp(gameId, new Date(), message));
handsUpTable.insert(new HandUp(gameId, LocalDateTime.now(), message));
} else {
h.setMessage(h.getMessage() + "<br>" + message);
handsUpTable.update(h);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.nkjmlab.go.javalin.model.json;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,7 +28,7 @@ public class GameStateJson {
private List<HandJson> handHistory = new ArrayList<>();
private long problemId = -1;
private Map<String, Object> options = new HashMap<>();
private Date createdAt = new Date();
private LocalDateTime createdAt = LocalDateTime.now();

private static final JsonMapper mapper = JacksonMapper.getDefaultMapper();

Expand Down Expand Up @@ -67,8 +67,8 @@ public GameStateJson(GameState gameState) {
}

public GameState toGameState() {
return new GameState(new Date(), gameId, blackPlayerId, whitePlayerId, lastHand.getNumber(),
mapper.toJson(lastHand), mapper.toJson(agehama), mapper.toJson(cells),
return new GameState(LocalDateTime.now(), gameId, blackPlayerId, whitePlayerId,
lastHand.getNumber(), mapper.toJson(lastHand), mapper.toJson(agehama), mapper.toJson(cells),
mapper.toJson(symbols), mapper.toJson(handHistory), problemId, mapper.toJson(options));
}

Expand Down Expand Up @@ -167,11 +167,11 @@ public void setProblemId(long problemId) {
this.problemId = problemId;
}

public Date getCreatedAt() {
public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.nkjmlab.go.javalin.model.json;

import static org.nkjmlab.go.javalin.model.row.MatchingRequest.*;
import java.util.Date;
import java.time.LocalDateTime;
import org.nkjmlab.go.javalin.model.row.MatchingRequest;

public class MatchingRequestJson {

private String userId;
private int rank;
private Date createdAt;
private LocalDateTime createdAt;
private String seatId;
private String userName;
private String gameId = UNPAIRED;
Expand Down Expand Up @@ -47,11 +47,11 @@ public void setRank(int rank) {
this.rank = rank;
}

public Date getCreatedAt() {
public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.nkjmlab.go.javalin.model.json;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -47,7 +46,7 @@ private ProblemJson(long id, String groupId, String name, int[][] cells,
}

public Problem toProblem() {
return new Problem(problemId, Timestamp.from(Instant.now()), groupId, name, mapper.toJson(cells),
return new Problem(problemId, LocalDateTime.now(), groupId, name, mapper.toJson(cells),
mapper.toJson(symbols), message, mapper.toJson(handHistory), mapper.toJson(agehama));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.nkjmlab.go.javalin.model.row.User;
import org.nkjmlab.sorm4j.Sorm;
import org.nkjmlab.sorm4j.result.RowMap;
import org.nkjmlab.sorm4j.util.h2.BasicH2TableWithDefinition;
import org.nkjmlab.sorm4j.util.h2.BasicH2Table;
import org.nkjmlab.sorm4j.util.table_def.TableDefinition;

public class GameRecordsTable extends BasicH2TableWithDefinition<GameRecord> {
public class GameRecordsTable extends BasicH2Table<GameRecord> {

public static final String TABLE_NAME = "GAME_RECORDS";
private static final String ID = "id";
Expand All @@ -28,13 +28,10 @@ public GameRecordsTable(DataSource dataSource) {
super(Sorm.create(dataSource), GameRecord.class,
TableDefinition.builder(TABLE_NAME)
.addColumnDefinition(ID, INT, AUTO_INCREMENT, PRIMARY_KEY)
.addColumnDefinition(CREATED_AT, "TIMESTAMP AS CURRENT_TIMESTAMP")
.addColumnDefinition(USER_ID, VARCHAR).addColumnDefinition(OPPONENT_USER_ID, VARCHAR)
.addColumnDefinition(JADGE, VARCHAR).addColumnDefinition(MEMO, VARCHAR)
.addColumnDefinition(RANK, INT).addColumnDefinition(POINT, INT)
.addColumnDefinition(MESSAGE, VARCHAR).build());
createTableIfNotExists();
createIndexesIfNotExists();
.addColumnDefinition(CREATED_AT, TIMESTAMP).addColumnDefinition(USER_ID, VARCHAR)
.addColumnDefinition(OPPONENT_USER_ID, VARCHAR).addColumnDefinition(JADGE, VARCHAR)
.addColumnDefinition(MEMO, VARCHAR).addColumnDefinition(RANK, INT)
.addColumnDefinition(POINT, INT).addColumnDefinition(MESSAGE, VARCHAR).build());
}

public void recalculateAndUpdateRank(UsersTable usersTable) {
Expand Down
Loading

0 comments on commit b49bf48

Please sign in to comment.