Skip to content

Commit

Permalink
minor fix, update project version to 1.3-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
otter authored and otter committed Nov 9, 2017
1 parent 469f0fb commit 57896f1
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 58 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# websocket-gameroom
# websocket-gameroom
A game room implement by Spring WebSocket, a bootstrap for group users to different room with maximum number of users for each room.

## Getting Started
Not design for run alone, see the quick start below to apply it to your project.

## Quick Start
1. add this annotation
```@EnableConfigurationProperties(WebSocketGameRoom.class)```
2. Build your own game model extends AbstractGame
3. Build your own game model builder extends AeroplaneChessBuilder
4. Implement GameService interface and pass the your own game model as type
5. Configure the destination prefix of the application and the message broker by the configuration

## Configuration
| name | type | default | description |
| ---- |:----:| :-------:| ----------:|
| websocket.destination.prefix.broker | String | null | prefix of the message broker |
| websocket.destination.application | String | null | prefix of application |
| websocket.stomp.endpoint | String | null | stomp client end point |
| gameroom.config.numof.player | int | 0 | max num of player for each room |

## Demo
A [aeroplane chess](https://github.com/kan01234/aeroplanes-chess) game built by this project.

## Prerequisites
* JAVA 8 Runtime
* Maven 3.3 or above
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>websocket-gameroom</artifactId>

<version>1.3-m2</version>
<version>1.3-rc</version>

<properties>
<java.version>1.8</java.version>
Expand Down
2 changes: 1 addition & 1 deletion script/deploy-local.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cd ..

ver=1.3-m2;
ver=1.3-rc;
base=$(pwd);
mvn clean package
rm -rf ${base}/../aeroplanes-chess/repo/com/dotterbear/websocket-gameroom/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public class GameRoomConfig {

@Bean(name = "playerGameMap")
public Map<String, String> getPlayerGameMap() {
return new ConcurrentHashMap<String, String>();
return new ConcurrentHashMap<>();
}

@Bean(name = "playingGameMap")
public Map<String, AbstractGame> getPlayingGameMap() {
return new ConcurrentHashMap<String, AbstractGame>();
return new ConcurrentHashMap<>();
}

@Bean(name = "waitingGameMap")
public Map<String, AbstractGame> getWaitingGameMap() {
return new ConcurrentHashMap<String, AbstractGame>();
return new ConcurrentHashMap<>();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.dotterbear.websocket.gameroom.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
Expand All @@ -16,8 +14,6 @@ public class PlayerWebSocketController {
@Autowired
private PlayerService playerService;

private Logger logger = LoggerFactory.getLogger(PlayerWebSocketController.class);

@MessageMapping("/join/{gameId}/{name}")
public void join(@Header("simpSessionId") String sessionId, @DestinationVariable(value = "gameId") String gameId, @DestinationVariable(value = "name") String name) {
if (gameId.equals("null")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class AbstractGame {

private String id;
protected Player[] players;
private Set<String> readyPlayers = new HashSet<String>();
private Set<String> readyPlayers = new HashSet<>();
private AtomicInteger joinCount = new AtomicInteger(0);

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.dotterbear.websocket.gameroom.repository;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dotterbear.websocket.gameroom.model.AbstractGame;
import com.dotterbear.websocket.gameroom.model.Player;

@Repository
public class GameRepository<T extends AbstractGame> {
Expand Down Expand Up @@ -80,7 +82,7 @@ public Map<String, T> getPlayingGameMap() {
public T removePlayingGame(String gameId) {
T t = playingGameMap.remove(gameId);
if (t != null)
playerRepository.removePlayer(Stream.of(t.getPlayers()).filter(p -> p != null).map(p -> p.getSessionId()).collect(Collectors.toList()));
playerRepository.removePlayer(Stream.of(t.getPlayers()).filter(Objects::nonNull).map(Player::getSessionId).collect(Collectors.toList()));
return t;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public void sendTo(String path, String sessionId, String key, Object value) {
}

private void send(String path, String[] keys, Object[] values) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
logger.info("send to path: " + path + ", data: " + map);
logger.info("send to path: ", path, ", data: ", map);
simpMessagingTemplate.convertAndSend(brokerDestinationPrefix + "/" + path, map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
@Service
public class PlayerService extends AbstractWebSocketService {

Logger logger = LoggerFactory.getLogger(PlayerService.class);
private Logger logger = LoggerFactory.getLogger(PlayerService.class);

@Autowired
PlayerRepository playerRepository;
private PlayerRepository playerRepository;

@Autowired
GameRepository<AbstractGame> gameRepository;
private GameRepository<AbstractGame> gameRepository;

@Autowired
GameService<AbstractGame> gameService;
private GameService<AbstractGame> gameService;

@Autowired
PlayerUtils playerUtils;
private PlayerUtils playerUtils;

@Value(value = "${websocket.gameroom.config.numof.player}")
int numOfPlayer;
private int numOfPlayer;

public String addPlayer(String sessionId, String name) {
logger.info("addPlayer, sessionId: " + sessionId);
logger.info("addPlayer, sessionId: ", sessionId);
Map<String, AbstractGame> waitingGameMap = gameRepository.getWaitingGameMap();
AbstractGame abstractGame = null;
abstractGame = waitingGameMap.values().stream().filter(g -> g.getJoinCount().getAndIncrement() <= numOfPlayer).findFirst().orElse(null);
Expand All @@ -48,39 +48,39 @@ public String addPlayer(String sessionId, String name) {
}

public String addPlayer(String sessionId, String gameId, String name) {
logger.info("addPlayer, sessionId: " + sessionId + ", gameId: " + gameId + ", name: " + name);
logger.info("addPlayer, sessionId: ", sessionId, ", gameId: ", gameId, ", name: ", name);
AbstractGame abstractGame = gameRepository.getWaitingGame(gameId);
if (abstractGame != null && abstractGame.getJoinCount().incrementAndGet() <= numOfPlayer)
return addPlayer(new Player(name, sessionId), abstractGame);
synchronized (abstractGame) {
return addPlayer(new Player(name, sessionId), abstractGame);
}
else
sendTo("joined", sessionId, "error", true);
return null;
}

String addPlayer(Player player, AbstractGame abstractGame) {
private String addPlayer(Player player, AbstractGame abstractGame) {
String gameId = null;
int i = 0;
synchronized (abstractGame) {
logger.info("addPlayer, player: " + player + ", game: " + abstractGame);
if (abstractGame == null)
return null;
Player[] players = abstractGame.getPlayers();

for (; i < players.length; i++) {
if (players[i] == null) {
players[i] = player;
break;
}
logger.info("addPlayer, player: ", player, ", game: ", abstractGame);
if (abstractGame == null)
return null;
Player[] players = abstractGame.getPlayers();

for (; i < players.length; i++) {
if (players[i] == null) {
players[i] = player;
break;
}
gameId = abstractGame.getId();
playerRepository.addPlayer(player.getSessionId(), gameId);
}
gameId = abstractGame.getId();
playerRepository.addPlayer(player.getSessionId(), gameId);
sendTo("joined", player.getSessionId(), new String[] { "error", "game-id", "index" }, new Object[] { false, gameId, i });
return gameId;
}

public void removePlayer(String sessionId) {
logger.info("removePlayer, sessionId: " + sessionId);
logger.info("removePlayer, sessionId: ", sessionId);
Map<String, String> playerGameMap = playerRepository.getPlayerGameMap();
if (!playerGameMap.containsKey(sessionId))
return;
Expand All @@ -91,37 +91,37 @@ public void removePlayer(String sessionId) {
abstractGame = gameRepository.getPlayingGame(gameId);
isWaiting = false;
}
removePlayer(sessionId, isWaiting, abstractGame);
synchronized (abstractGame) {
removePlayer(sessionId, isWaiting, abstractGame);
}
}

void removePlayer(String sessionId, Boolean isWaiting, AbstractGame abstractGame) {
logger.info("removePlayer, sessionId: " + sessionId + ", isWaiting: " + isWaiting + ", abstractGame: " + abstractGame);
private void removePlayer(String sessionId, Boolean isWaiting, AbstractGame abstractGame) {
logger.info("removePlayer, sessionId: ", sessionId, ", isWaiting: ", isWaiting, ", abstractGame: ", abstractGame);
Player[] players;
String gameId = abstractGame.getId();
synchronized (abstractGame) {
playerRepository.removePlayer(sessionId);
players = abstractGame.getPlayers();
int i;
for (i = 0; i < players.length; i++) {
if (players[i] != null && players[i].getSessionId().equals(sessionId)) {
players[i] = null;
break;
}
playerRepository.removePlayer(sessionId);
players = abstractGame.getPlayers();
int i;
for (i = 0; i < players.length; i++) {
if (players[i] != null && players[i].getSessionId().equals(sessionId)) {
players[i] = null;
break;
}
abstractGame.removeReadyPlayer(sessionId);
if (!isWaiting) {
if (abstractGame.getReadyPlayersSize() == 1) {
gameService.playerWin(gameId, playerUtils.getLastPlayerIndex(players));
return;
}
gameService.playerLeaved(abstractGame, i);
}
abstractGame.removeReadyPlayer(sessionId);
if (!isWaiting) {
if (abstractGame.getReadyPlayersSize() == 1) {
gameService.playerWin(gameId, playerUtils.getLastPlayerIndex(players));
return;
}
gameService.playerLeaved(abstractGame, i);
}
send("player-list", gameId, "players", players);
}

public void ready(String sessionId, String gameId) {
logger.info("ready, sessionId: " + sessionId + " , gam");
logger.info("ready, sessionId: ", sessionId, " , gameId: ", gameId);
AbstractGame abstractGame = gameRepository.getWaitingGame(gameId);
if (abstractGame == null)
return;
Expand Down

0 comments on commit 57896f1

Please sign in to comment.