Skip to content

Commit 777f4b7

Browse files
committed
Merge branch 'master' into xray-gettingstarted
2 parents 7fb1d0d + bbbb844 commit 777f4b7

29 files changed

+215
-225
lines changed

README.md

Lines changed: 73 additions & 67 deletions
Large diffs are not rendered by default.

src/main/java/scorekeep/Game.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
1212
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
1313

14-
@DynamoDBTable( tableName = Constants.GAME_TABLE )
14+
@DynamoDBTable( tableName = TableNames.GAME_TABLE )
1515
public class Game {
1616

1717
private String id;

src/main/java/scorekeep/GameController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919

20-
import java.security.SecureRandom;
21-
import java.math.BigInteger;
22-
2320
@RestController
2421
@RequestMapping(value="/api/game/{sessionId}")
2522
/** Routes for game CRUD
@@ -28,11 +25,10 @@
2825
Use GameModel to persist updated Game objects to DynamoDB
2926
**/
3027
public class GameController {
31-
private SecureRandom random = new SecureRandom();
32-
private GameFactory gameFactory = new GameFactory();
33-
private RulesFactory rulesFactory = new RulesFactory();
34-
private StateFactory stateFactory = new StateFactory();
35-
private GameModel model = new GameModel();
28+
private final GameFactory gameFactory = new GameFactory();
29+
private final RulesFactory rulesFactory = new RulesFactory();
30+
private final StateFactory stateFactory = new StateFactory();
31+
private final GameModel model = new GameModel();
3632
private final Logger logger = LoggerFactory.getLogger(this.getClass());
3733
/* GET /game/SESSION/ */
3834
@RequestMapping(method=RequestMethod.GET)

src/main/java/scorekeep/GameFactory.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package scorekeep;
22
import java.util.*;
3-
import java.security.SecureRandom;
4-
import java.math.BigInteger;
53
import java.lang.Exception;
64

75
public class GameFactory {
8-
private SecureRandom random = new SecureRandom();
96
private final HashMap<String, Game> allGames = new HashMap<String, Game>(1);
10-
private GameModel model = new GameModel();
11-
private SessionController sessionController = new SessionController();
7+
private final GameModel model = new GameModel();
8+
private final SessionController sessionController = new SessionController();
129

1310
public GameFactory(){
1411
}
1512

1613
public Game newGame(String sessionId) throws SessionNotFoundException, GameNotFoundException {
17-
String gameId = new BigInteger(40, random).toString(32).toUpperCase();
14+
String gameId = Identifiers.random();
1815
Game game = new Game(gameId, sessionId);
1916
model.saveGame(game);
2017
// Register game to session

src/main/java/scorekeep/GameModel.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scorekeep;
22

3-
import com.amazonaws.regions.Regions;
43
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
54
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
65
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
@@ -15,22 +14,17 @@
1514
public class GameModel {
1615
/** AWS SDK credentials. */
1716
private AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
18-
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
1917
.build();
2018
private DynamoDBMapper mapper = new DynamoDBMapper(client);
21-
private SessionModel sessionModel = new SessionModel();
19+
private final SessionModel sessionModel = new SessionModel();
2220

2321
public void saveGame(Game game) throws SessionNotFoundException {
24-
try {
25-
// check session
26-
String sessionId = game.getSession();
27-
if (sessionModel.loadSession(sessionId) == null ) {
28-
throw new SessionNotFoundException(sessionId);
29-
}
30-
mapper.save(game);
31-
} catch (Exception e) {
32-
throw e;
22+
// check session
23+
String sessionId = game.getSession();
24+
if (sessionModel.loadSession(sessionId) == null ) {
25+
throw new SessionNotFoundException(sessionId);
3326
}
27+
mapper.save(game);
3428
}
3529

3630
public Game loadGame(String gameId) throws GameNotFoundException {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package scorekeep;
2+
3+
import java.security.SecureRandom;
4+
import java.math.BigInteger;
5+
6+
public class Identifiers {
7+
private static final SecureRandom secureRandom = new SecureRandom();
8+
9+
public static String random() {
10+
return new BigInteger(40, secureRandom).toString(32).toUpperCase();
11+
}
12+
13+
}

src/main/java/scorekeep/Move.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
1010
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
1111

12-
@DynamoDBTable( tableName = Constants.MOVE_TABLE )
12+
@DynamoDBTable( tableName = TableNames.MOVE_TABLE )
1313
public class Move {
1414

1515
private String id;
@@ -21,53 +21,48 @@ public class Move {
2121
public Move() {
2222
}
2323

24-
public Move(String id, String session, String game, String user, String move) {
25-
this.id = id;
26-
this.session = session;
27-
this.game = game;
28-
this.user = user;
29-
this.move = move;
30-
}
31-
3224
@DynamoDBHashKey(attributeName="id")
3325
public String getId() {
3426
return id;
3527
}
36-
public void setId(String id){
28+
public Move setId(String id){
3729
this.id = id;
30+
return this;
3831
}
3932

4033
@DynamoDBIndexHashKey(globalSecondaryIndexName="game-index",attributeName="game")
4134
public String getGame() {
4235
return game;
4336
}
44-
public void setGame(String gameId){
37+
public Move setGame(String gameId){
4538
this.game = gameId;
39+
return this;
4640
}
4741

4842
@DynamoDBAttribute(attributeName="session")
4943
public String getSession() {
5044
return session;
5145
}
52-
public void setSession(String sessionId) {
46+
public Move setSession(String sessionId) {
5347
this.session = sessionId;
48+
return this;
5449
}
5550

5651
@DynamoDBAttribute(attributeName="user")
5752
public String getUser() {
5853
return user;
5954
}
60-
public void setUser(String userId) {
55+
public Move setUser(String userId) {
6156
this.user = userId;
57+
return this;
6258
}
6359

6460
@DynamoDBAttribute(attributeName="move")
6561
public String getMove() {
6662
return move;
6763
}
68-
public void setMove(String moveId) {
64+
public Move setMove(String moveId) {
6965
this.move = moveId;
66+
return this;
7067
}
71-
72-
7368
}

src/main/java/scorekeep/MoveController.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
import org.springframework.web.bind.annotation.CrossOrigin;
1515
import org.springframework.web.bind.annotation.RequestBody;
1616

17-
import java.security.SecureRandom;
18-
import java.math.BigInteger;
19-
2017
@RestController
2118
@RequestMapping(value="/api/move/{sessionId}/{gameId}")
2219
public class MoveController {
23-
private SecureRandom random = new SecureRandom();
24-
private MoveFactory moveFactory = new MoveFactory();
25-
private MoveModel model = new MoveModel();
26-
private GameController gameController = new GameController();
20+
private final MoveFactory moveFactory = new MoveFactory();
21+
private final MoveModel model = new MoveModel();
22+
private final GameController gameController = new GameController();
2723
/* GET /move/SESSION/GAME */
2824
@RequestMapping(method=RequestMethod.GET)
2925
public List<Move> getMoves(@PathVariable String sessionId, @PathVariable String gameId) throws SessionNotFoundException, GameNotFoundException {

src/main/java/scorekeep/MoveFactory.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scorekeep;
22

3-
import java.math.BigInteger;
4-
import java.security.SecureRandom;
53
import java.util.HashMap;
64
import java.util.List;
75
import java.util.Set;
@@ -12,22 +10,25 @@
1210
import org.slf4j.LoggerFactory;
1311

1412
public class MoveFactory {
15-
private static final Logger logger = LoggerFactory.getLogger("scorekeep.MoveFactory");
16-
private SecureRandom random = new SecureRandom();
13+
private static final Logger logger = LoggerFactory.getLogger(MoveFactory.class);
1714
private final HashMap<String, Move> allMoves = new HashMap<String, Move>(1);
18-
private MoveModel moveModel = new MoveModel();
19-
private StateModel stateModel = new StateModel();
20-
private GameController gameController = new GameController();
21-
private StateController stateController = new StateController();
22-
private RulesFactory rulesFactory = new RulesFactory();
15+
private final MoveModel moveModel = new MoveModel();
16+
private final StateModel stateModel = new StateModel();
17+
private final GameController gameController = new GameController();
18+
private final StateController stateController = new StateController();
19+
private final RulesFactory rulesFactory = new RulesFactory();
2320

2421
public MoveFactory(){
2522
}
2623

2724
public Move newMove(String sessionId, String gameId, String userId, String moveText) throws SessionNotFoundException, GameNotFoundException, StateNotFoundException, RulesException {
28-
String moveId = new BigInteger(40, random).toString(32).toUpperCase();
29-
String stateId = new BigInteger(40, random).toString(32).toUpperCase();
30-
Move move = new Move(moveId, sessionId, gameId, userId, moveText);
25+
String moveId = Identifiers.random();
26+
String stateId = Identifiers.random();
27+
Move move = new Move().setId(moveId)
28+
.setSession(sessionId)
29+
.setGame(gameId)
30+
.setUser(userId)
31+
.setMove(moveText);
3132
String newStateText = "";
3233
// load game state
3334
Game game = gameController.getGame(sessionId, gameId);
@@ -57,7 +58,7 @@ public Move newMove(String sessionId, String gameId, String userId, String moveT
5758
State newState = new State(stateId, sessionId, gameId, newStateText, newTurn);
5859
// send notification on game end
5960
if ( newStateText.startsWith("A") || newStateText.startsWith("B")) {
60-
Utils.sendNotification("Scorekeep game completed", "Winner: " + userId);
61+
Sns.sendNotification("Scorekeep game completed", "Winner: " + userId);
6162
}
6263
// register state and move id to game
6364
gameController.setGameMove(sessionId, gameId, moveId);

src/main/java/scorekeep/MoveModel.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scorekeep;
22

3-
import com.amazonaws.regions.Regions;
43
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
54
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
65
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
@@ -14,11 +13,10 @@
1413
public class MoveModel {
1514
/** AWS SDK credentials. */
1615
private AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
17-
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
1816
.build();
1917
private DynamoDBMapper mapper = new DynamoDBMapper(client);
20-
private SessionModel sessionModel = new SessionModel();
21-
private GameModel gameModel = new GameModel();
18+
private final SessionModel sessionModel = new SessionModel();
19+
private final GameModel gameModel = new GameModel();
2220

2321
public void saveMove(Move move) throws SessionNotFoundException, GameNotFoundException {
2422
// check session

0 commit comments

Comments
 (0)