Fixed that won effect did not work if winning player was not first player in player list.

This commit is contained in:
LevelX2 2013-07-23 17:22:49 +02:00
parent a0c3f32b9b
commit 94109b5e2e
2 changed files with 26 additions and 16 deletions

View file

@ -439,7 +439,7 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
if (winnerId == null) { if (winnerId == null) {
return "Game is a draw"; return "Game is a draw";
} }
return "Player " + state.getPlayer(winnerId).getName() + " is the winner"; return new StringBuilder("Player ").append(state.getPlayer(winnerId).getName()).append(" is the winner").toString();
} }
@Override @Override
@ -708,14 +708,18 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
protected UUID findWinnersAndLosers() { protected UUID findWinnersAndLosers() {
UUID winner = null; UUID winner = null;
for (Player player: state.getPlayers().values()) { for (Player player: state.getPlayers().values()) {
if (player.hasWon() || (!player.hasLost() && !player.hasLeft())) { if (player.hasWon()) {
winner = player.getId();
break;
}
if (!player.hasLost() && !player.hasLeft()) {
player.won(this); player.won(this);
winner = player.getId(); winner = player.getId();
break; break;
} }
} }
for (Player player: state.getPlayers().values()) { for (Player player: state.getPlayers().values()) {
if (winner != null && !player.getId().equals(winner)) { if (winner != null && !player.getId().equals(winner) && !player.hasLost()) {
player.lost(this); player.lost(this);
} }
} }
@ -1937,12 +1941,12 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
@Override @Override
public Date getStartTime() { public Date getStartTime() {
return startTime; return new Date(startTime.getTime());
} }
@Override @Override
public Date getEndTime() { public Date getEndTime() {
return endTime; return new Date(endTime.getTime());
} }
@Override @Override

View file

@ -111,10 +111,10 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
private static Random rnd = new Random(); private static Random rnd = new Random();
/** /**
* Means what exactly? * Used to cancel waiting requests send to the player
* No more actions?
*/ */
protected boolean abort; protected boolean abort;
protected final UUID playerId; protected final UUID playerId;
protected String name; protected String name;
protected boolean human; protected boolean human;
@ -1333,17 +1333,23 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
public void won(Game game) { public void won(Game game) {
if (!game.replaceEvent(new GameEvent(GameEvent.EventType.WINS, null, null, playerId))) { if (!game.replaceEvent(new GameEvent(GameEvent.EventType.WINS, null, null, playerId))) {
if (!this.loses) { if (!this.loses) {
//20100423 - 800.6, 801.16 //20130501 - 800.7, 801.16
if (game.getPlayers().size() > 2) { // all opponents in range loose the game
for (UUID opponentId: game.getOpponents(playerId)) { for (UUID opponentId: game.getOpponents(playerId)) {
Player opponent = game.getPlayer(opponentId); Player opponent = game.getPlayer(opponentId);
if (opponent != null && !opponent.hasLost()) { if (opponent != null && !opponent.hasLost()) {
opponent.lost(game); opponent.lost(game);
}
} }
this.wins = true;
} }
else { // if no more opponents alive, you win and the game ends
int opponentsAlive = 0;
for (UUID opponentId: game.getOpponents(playerId)) {
Player opponent = game.getPlayer(opponentId);
if (!opponent.hasLost()) {
opponentsAlive++;
}
}
if (opponentsAlive == 0) {
this.wins = true; this.wins = true;
game.end(); game.end();
} }