game data object with methods to add and remove players from the game
This commit is contained in:
parent
3906b5582c
commit
0183bb0732
|
@ -1,5 +1,103 @@
|
|||
package net.socialgamer.cah.data;
|
||||
|
||||
public class Game {
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.socialgamer.cah.data.QueuedMessage.Type;
|
||||
|
||||
|
||||
public class Game {
|
||||
private final int id;
|
||||
private final List<Player> players = new ArrayList<Player>(10);
|
||||
private final ConnectedUsers connectedUsers;
|
||||
private Player host;
|
||||
private BlackDeck blackDeck;
|
||||
private WhiteDeck whiteDeck;
|
||||
|
||||
/**
|
||||
* TODO Injection here would be much nicer, but that would need a Provider for the id... Too much
|
||||
* work for now.
|
||||
*
|
||||
* @param id
|
||||
* @param connectedUsers
|
||||
*/
|
||||
public Game(final int id, final ConnectedUsers connectedUsers) {
|
||||
this.id = id;
|
||||
this.connectedUsers = connectedUsers;
|
||||
}
|
||||
|
||||
public void addPlayer(final User user) {
|
||||
final Player player = new Player(user);
|
||||
synchronized (players) {
|
||||
players.add(player);
|
||||
if (host == null) {
|
||||
host = player;
|
||||
}
|
||||
}
|
||||
|
||||
final HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("event", "game_player_join");
|
||||
data.put("game_id", id);
|
||||
data.put("nickname", user.getNickname());
|
||||
broadcastToPlayers(Type.GAME_PLAYER_EVENT, data);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param user
|
||||
* @return True if {@code user} was the last player in the game.
|
||||
*/
|
||||
public boolean removePlayer(final User user) {
|
||||
synchronized (players) {
|
||||
final Iterator<Player> iterator = players.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final Player player = iterator.next();
|
||||
if (player.getUser() == user) {
|
||||
iterator.remove();
|
||||
final HashMap<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("event", "game_player_leave");
|
||||
data.put("game_id", id);
|
||||
data.put("nickname", user.getNickname());
|
||||
broadcastToPlayers(Type.GAME_PLAYER_EVENT, data);
|
||||
if (host == player) {
|
||||
if (players.size() > 0) {
|
||||
host = players.get(0);
|
||||
} else {
|
||||
host = null;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return players.size() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void broadcastToPlayers(final Type type, final HashMap<String, Object> masterData) {
|
||||
connectedUsers.broadcastToList(playersToUsers(), type, masterData);
|
||||
}
|
||||
|
||||
public User getHost() {
|
||||
if (host == null) {
|
||||
return null;
|
||||
}
|
||||
return host.getUser();
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return playersToUsers();
|
||||
}
|
||||
|
||||
private List<User> playersToUsers() {
|
||||
final List<User> users;
|
||||
synchronized (players) {
|
||||
users = new ArrayList<User>(players.size());
|
||||
for (final Player player : players) {
|
||||
users.add(player.getUser());
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package net.socialgamer.cah.data;
|
||||
|
||||
public class Player {
|
||||
private final User user;
|
||||
|
||||
public Player(final User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package net.socialgamer.cah.data;
|
||||
|
||||
import static org.easymock.EasyMock.anyObject;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.eq;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.socialgamer.cah.data.QueuedMessage.Type;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class GameTest {
|
||||
|
||||
private Game game;
|
||||
private ConnectedUsers cmMock;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
cmMock = createMock(ConnectedUsers.class);
|
||||
game = new Game(0, cmMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePlayer() {
|
||||
cmMock.broadcastToList(anyObject(Collection.class), eq(Type.GAME_PLAYER_EVENT),
|
||||
anyObject(HashMap.class));
|
||||
expectLastCall().anyTimes();
|
||||
replay(cmMock);
|
||||
|
||||
final User user1 = new User("test1");
|
||||
final User user2 = new User("test2");
|
||||
game.addPlayer(user1);
|
||||
game.addPlayer(user2);
|
||||
|
||||
assertEquals(user1, game.getHost());
|
||||
assertFalse(game.removePlayer(user1));
|
||||
assertEquals(user2, game.getHost());
|
||||
assertTrue(game.removePlayer(user2));
|
||||
assertEquals(null, game.getHost());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue