Skip to content

Commit 3c85bb0

Browse files
Adding game persistence
Adding game persistance, and an endpoint for creating new games. For now it uses a hardcoded "Alfonso" user. This needs to be updated when authentication is implemented I also had to update the spring-data-dynamodb dependency due to this error: derjust/spring-data-dynamodb#267
1 parent ca4b797 commit 3c85bb0

File tree

7 files changed

+226
-8
lines changed

7 files changed

+226
-8
lines changed

minesweeper-api/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
</dependency>
3636

3737
<dependency>
38-
<groupId>com.github.derjust</groupId>
38+
<groupId>io.github.boostchicken</groupId>
3939
<artifactId>spring-data-dynamodb</artifactId>
40-
<version>5.1.0</version>
40+
<version>5.2.1</version>
4141
</dependency>
4242
</dependencies>
4343

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.minesweeper.controller;
2+
3+
import com.example.minesweeper.controller.request.NewGameRequest;
4+
import com.example.minesweeper.model.Game;
5+
import com.example.minesweeper.model.User;
6+
import com.example.minesweeper.repository.GameRepository;
7+
import com.example.minesweeper.repository.SavedGame;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
public class GameController {
15+
16+
@Autowired
17+
private GameRepository gameRepository;
18+
19+
@PostMapping("/game")
20+
public SavedGame startNewGame(@RequestBody NewGameRequest newGameRequest) {
21+
22+
// TODO:
23+
// Until I implement the authentication, this endpoint will work with a hardcoded user
24+
User user = new User("alfonso", 0);
25+
26+
Game game = new Game(user, newGameRequest.rows, newGameRequest.columns, newGameRequest.mines);
27+
SavedGame savedGame = new SavedGame(game);
28+
return gameRepository.save(savedGame);
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.minesweeper.controller.request;
2+
3+
public class NewGameRequest {
4+
5+
public int rows;
6+
public int columns;
7+
public int mines;
8+
9+
}

minesweeper-api/src/main/java/com/example/minesweeper/model/Game.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@ public class Game {
1010
public static final char FLAG_MARK = 'x';
1111
public static final char QUESTION_MARK = '?';
1212

13-
public int rows;
14-
public int columns;
15-
public int[][] cellMines; // numbers from 0 to 8 or a '*' sign that represents a mine
16-
public char[][] cellState; // ' ' for uncovered cells, '.' for hidden cells, 'x' for mine marks, '?' for question marks
13+
private User player;
1714

18-
public Game(int rows, int columns, int mines) {
15+
private String id;
16+
17+
private int rows;
18+
19+
private int columns;
20+
21+
private char[][] cellMines; // numbers from 0 to 8 or a '*' sign that represents a mine
22+
23+
private char[][] cellState; // ' ' for uncovered cells, '.' for hidden cells, 'x' for mine marks, '?' for question marks*/
24+
25+
public Game(User player, int rows, int columns, int mines) {
26+
this.player = player;
1927
this.rows = rows;
2028
this.columns = columns;
2129

22-
if(rows * columns > mines) {
30+
if(mines > rows * columns) {
2331
throw new RuntimeException("Invalid amount of mines");
2432
}
2533

34+
cellMines = new char[rows][columns];
35+
cellState = new char[rows][columns];
36+
2637
for(int x = 0; x < rows; x++) {
2738
for(int y = 0; y < columns; y++) {
2839
cellMines[x][y] = '0';
@@ -53,6 +64,30 @@ public Game(int rows, int columns, int mines) {
5364
}
5465
}
5566

67+
public User getPlayer() {
68+
return player;
69+
}
70+
71+
public String getId() {
72+
return id;
73+
}
74+
75+
public int getRows() {
76+
return rows;
77+
}
78+
79+
public int getColumns() {
80+
return columns;
81+
}
82+
83+
public char[][] getCellMines() {
84+
return cellMines;
85+
}
86+
87+
public char[][] getCellState() {
88+
return cellState;
89+
}
90+
5691
/**
5792
* Uncovers a the cell in the position (x, y)
5893
*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.minesweeper.repository;
2+
3+
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
@EnableScan
7+
public interface GameRepository extends CrudRepository<SavedGame, SavedGameId> {
8+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.example.minesweeper.repository;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.*;
4+
import com.example.minesweeper.model.Game;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import org.springframework.data.annotation.Id;
8+
9+
@DynamoDBTable(tableName = "MineSweeperGame")
10+
public class SavedGame {
11+
12+
@Id
13+
private SavedGameId savedGameId;
14+
15+
private String player;
16+
17+
private String id;
18+
19+
private int rows;
20+
21+
private int columns;
22+
23+
private String cellMines;
24+
25+
private String cellState;
26+
27+
public SavedGame() {
28+
System.out.println();
29+
}
30+
31+
public SavedGame(Game game) {
32+
player = game.getPlayer().getName();
33+
id = game.getId();
34+
savedGameId = new SavedGameId(player, id);
35+
rows = game.getRows();
36+
columns = game.getColumns();
37+
try {
38+
ObjectMapper objectMapper = new ObjectMapper();
39+
cellMines = objectMapper.writeValueAsString(game.getCellMines());
40+
cellState = objectMapper.writeValueAsString(game.getCellState());
41+
}
42+
catch(JsonProcessingException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
@DynamoDBHashKey(attributeName = "player")
48+
public String getPlayer() {
49+
return player;
50+
}
51+
52+
public void setPlayer(String player) {
53+
this.player = player;
54+
}
55+
56+
@DynamoDBRangeKey(attributeName = "id")
57+
@DynamoDBAutoGeneratedKey
58+
public String getId() {
59+
return id;
60+
}
61+
62+
public void setId(String id) {
63+
this.id = id;
64+
}
65+
66+
@DynamoDBAttribute(attributeName = "rows")
67+
public int getRows() {
68+
return rows;
69+
}
70+
71+
public void setRows(int rows) {
72+
this.rows = rows;
73+
}
74+
75+
@DynamoDBAttribute(attributeName = "columns")
76+
public int getColumns() {
77+
return columns;
78+
}
79+
80+
public void setColumns(int columns) {
81+
this.columns = columns;
82+
}
83+
84+
@DynamoDBAttribute(attributeName = "cellMines")
85+
public String getCellMines() {
86+
return cellMines;
87+
}
88+
89+
public void setCellMines(String cellMines) {
90+
this.cellMines = cellMines;
91+
}
92+
93+
@DynamoDBAttribute(attributeName = "cellState")
94+
public String getCellState() {
95+
return cellState;
96+
}
97+
98+
public void setCellState(String cellState) {
99+
this.cellState = cellState;
100+
}
101+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.minesweeper.repository;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
5+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
6+
7+
public class SavedGameId {
8+
9+
private String player;
10+
private String id;
11+
12+
public SavedGameId(String player, String id) {
13+
this.player = player;
14+
this.id = id;
15+
}
16+
17+
@DynamoDBHashKey(attributeName = "player")
18+
public String getPlayer() {
19+
return player;
20+
}
21+
22+
public void setPlayer(String player) {
23+
this.player = player;
24+
}
25+
26+
@DynamoDBRangeKey(attributeName = "id")
27+
@DynamoDBAutoGeneratedKey
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
}

0 commit comments

Comments
 (0)