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

+73-67
Large diffs are not rendered by default.

src/main/java/scorekeep/Game.java

+1-1
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

+4-8
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

+3-6
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

+6-12
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 {
+13
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

+11-16
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

+3-7
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

+14-13
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

+2-4
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

src/main/java/scorekeep/Rules.java

+43-19
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,79 @@
22

33
public class Rules {
44

5-
private final String id;
6-
private final String name;
7-
private final String[] categories;
8-
private final Integer[] users;
9-
private final Integer teams;
10-
private final String[] phases;
11-
private final String[] moves;
12-
private final String initialState;
13-
14-
public Rules(String id, String name, String[] categories, Integer[] users, Integer teams, String[] phases, String[] moves, String initialState) {
15-
this.id = id;
16-
this.name = name;
17-
this.categories = categories;
18-
this.users = users;
19-
this.teams = teams;
20-
this.phases = phases;
21-
this.moves = moves;
22-
this.initialState = initialState;
23-
}
5+
private String id;
6+
private String name;
7+
private String[] categories;
8+
private Integer[] users;
9+
private Integer teams;
10+
private String[] phases;
11+
private String[] moves;
12+
private String initialState;
13+
14+
public Rules() {
15+
};
2416

2517
public String getId() {
2618
return id;
2719
}
20+
public Rules setId(String id){
21+
this.id = id;
22+
return this;
23+
}
2824

2925
public String getName() {
3026
return name;
3127
}
28+
public Rules setName(String name){
29+
this.name = name;
30+
return this;
31+
}
3232

3333
public String[] getCategories() {
3434
return categories;
3535
}
36+
public Rules setCategories(String[] categories){
37+
this.categories = categories;
38+
return this;
39+
}
3640

3741
public Integer[] getUsers() {
3842
return users;
3943
}
44+
public Rules setUsers(Integer[] users){
45+
this.users = users;
46+
return this;
47+
}
4048

4149
public Integer getTeams() {
4250
return teams;
4351
}
52+
public Rules setTeams(Integer teams){
53+
this.teams = teams;
54+
return this;
55+
}
4456

4557
public String[] getPhases() {
4658
return phases;
4759
}
60+
public Rules setPhases(String[] phases){
61+
this.phases = phases;
62+
return this;
63+
}
4864

4965
public String[] getMoves() {
5066
return moves;
5167
}
68+
public Rules setMoves(String[] moves){
69+
this.moves = moves;
70+
return this;
71+
}
5272

5373
public String getInitialState() {
5474
return initialState;
5575
}
76+
public Rules setInitialState(String initialState){
77+
this.initialState = initialState;
78+
return this;
79+
}
5680
}

src/main/java/scorekeep/RulesController.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
import org.springframework.web.bind.annotation.PathVariable;
1010
import org.springframework.web.bind.annotation.CrossOrigin;
1111

12-
import java.security.SecureRandom;
13-
import java.math.BigInteger;
14-
1512
@RestController
1613
@RequestMapping(value="/api/rules")
1714
public class RulesController {
18-
private SecureRandom random = new SecureRandom();
19-
private RulesFactory rulesFactory = new RulesFactory();
15+
private final RulesFactory rulesFactory = new RulesFactory();
2016

2117
/* GET /rules */
2218
@RequestMapping(method=RequestMethod.GET)

0 commit comments

Comments
 (0)