Skip to content

Commit

Permalink
Adding game persistence
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AlfonsoEsteves committed Apr 14, 2021
1 parent ca4b797 commit 3c85bb0
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 8 deletions.
4 changes: 2 additions & 2 deletions minesweeper-api/pom.xml
Expand Up @@ -35,9 +35,9 @@
</dependency>

<dependency>
<groupId>com.github.derjust</groupId>
<groupId>io.github.boostchicken</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>5.1.0</version>
<version>5.2.1</version>
</dependency>
</dependencies>

Expand Down
@@ -0,0 +1,30 @@
package com.example.minesweeper.controller;

import com.example.minesweeper.controller.request.NewGameRequest;
import com.example.minesweeper.model.Game;
import com.example.minesweeper.model.User;
import com.example.minesweeper.repository.GameRepository;
import com.example.minesweeper.repository.SavedGame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GameController {

@Autowired
private GameRepository gameRepository;

@PostMapping("/game")
public SavedGame startNewGame(@RequestBody NewGameRequest newGameRequest) {

// TODO:
// Until I implement the authentication, this endpoint will work with a hardcoded user
User user = new User("alfonso", 0);

Game game = new Game(user, newGameRequest.rows, newGameRequest.columns, newGameRequest.mines);
SavedGame savedGame = new SavedGame(game);
return gameRepository.save(savedGame);
}
}
@@ -0,0 +1,9 @@
package com.example.minesweeper.controller.request;

public class NewGameRequest {

public int rows;
public int columns;
public int mines;

}
Expand Up @@ -10,19 +10,30 @@ public class Game {
public static final char FLAG_MARK = 'x';
public static final char QUESTION_MARK = '?';

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

public Game(int rows, int columns, int mines) {
private String id;

private int rows;

private int columns;

private char[][] cellMines; // numbers from 0 to 8 or a '*' sign that represents a mine

private char[][] cellState; // ' ' for uncovered cells, '.' for hidden cells, 'x' for mine marks, '?' for question marks*/

public Game(User player, int rows, int columns, int mines) {
this.player = player;
this.rows = rows;
this.columns = columns;

if(rows * columns > mines) {
if(mines > rows * columns) {
throw new RuntimeException("Invalid amount of mines");
}

cellMines = new char[rows][columns];
cellState = new char[rows][columns];

for(int x = 0; x < rows; x++) {
for(int y = 0; y < columns; y++) {
cellMines[x][y] = '0';
Expand Down Expand Up @@ -53,6 +64,30 @@ public Game(int rows, int columns, int mines) {
}
}

public User getPlayer() {
return player;
}

public String getId() {
return id;
}

public int getRows() {
return rows;
}

public int getColumns() {
return columns;
}

public char[][] getCellMines() {
return cellMines;
}

public char[][] getCellState() {
return cellState;
}

/**
* Uncovers a the cell in the position (x, y)
*
Expand Down
@@ -0,0 +1,8 @@
package com.example.minesweeper.repository;

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;

@EnableScan
public interface GameRepository extends CrudRepository<SavedGame, SavedGameId> {
}
@@ -0,0 +1,101 @@
package com.example.minesweeper.repository;

import com.amazonaws.services.dynamodbv2.datamodeling.*;
import com.example.minesweeper.model.Game;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.annotation.Id;

@DynamoDBTable(tableName = "MineSweeperGame")
public class SavedGame {

@Id
private SavedGameId savedGameId;

private String player;

private String id;

private int rows;

private int columns;

private String cellMines;

private String cellState;

public SavedGame() {
System.out.println();
}

public SavedGame(Game game) {
player = game.getPlayer().getName();
id = game.getId();
savedGameId = new SavedGameId(player, id);
rows = game.getRows();
columns = game.getColumns();
try {
ObjectMapper objectMapper = new ObjectMapper();
cellMines = objectMapper.writeValueAsString(game.getCellMines());
cellState = objectMapper.writeValueAsString(game.getCellState());
}
catch(JsonProcessingException e) {
e.printStackTrace();
}
}

@DynamoDBHashKey(attributeName = "player")
public String getPlayer() {
return player;
}

public void setPlayer(String player) {
this.player = player;
}

@DynamoDBRangeKey(attributeName = "id")
@DynamoDBAutoGeneratedKey
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@DynamoDBAttribute(attributeName = "rows")
public int getRows() {
return rows;
}

public void setRows(int rows) {
this.rows = rows;
}

@DynamoDBAttribute(attributeName = "columns")
public int getColumns() {
return columns;
}

public void setColumns(int columns) {
this.columns = columns;
}

@DynamoDBAttribute(attributeName = "cellMines")
public String getCellMines() {
return cellMines;
}

public void setCellMines(String cellMines) {
this.cellMines = cellMines;
}

@DynamoDBAttribute(attributeName = "cellState")
public String getCellState() {
return cellState;
}

public void setCellState(String cellState) {
this.cellState = cellState;
}
}
@@ -0,0 +1,35 @@
package com.example.minesweeper.repository;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;

public class SavedGameId {

private String player;
private String id;

public SavedGameId(String player, String id) {
this.player = player;
this.id = id;
}

@DynamoDBHashKey(attributeName = "player")
public String getPlayer() {
return player;
}

public void setPlayer(String player) {
this.player = player;
}

@DynamoDBRangeKey(attributeName = "id")
@DynamoDBAutoGeneratedKey
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

0 comments on commit 3c85bb0

Please sign in to comment.