mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Compare commits
3 Commits
8867d5a1ea
...
6e2ea82a32
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e2ea82a32 | ||
|
|
34c85ec472 | ||
| 2d9b34b7f6 |
@@ -184,7 +184,7 @@ public final class Server {
|
||||
|
||||
gameController = null;
|
||||
|
||||
//if (!isPolling) return;
|
||||
// if (!isPolling) return;
|
||||
|
||||
String gameType = extractQuotedValue(response.gameType());
|
||||
if (response.clientId() == clientId) {
|
||||
@@ -235,8 +235,9 @@ public final class Server {
|
||||
}
|
||||
|
||||
if (gameController != null) {
|
||||
primary.reEnableButton();
|
||||
primary.reEnableButton(); // Re enable subscribe button
|
||||
gameController.start();
|
||||
isPolling = true; // Fixes server getting stuck
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,14 +31,18 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
|
||||
private void notifyGameEnd(GameState state, int winner){
|
||||
if (state == GameState.DRAW){
|
||||
for (NettyClient client : clients) {
|
||||
client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
||||
client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
|
||||
}
|
||||
}
|
||||
else{
|
||||
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
||||
clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
||||
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
|
||||
clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
|
||||
}
|
||||
|
||||
// Remove game fromt clients
|
||||
for(NettyClient client : clients) {
|
||||
client.clearGame();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -128,21 +128,15 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
|
||||
@Override
|
||||
public void startGame(String gameType, NettyClient... clients) {
|
||||
IO.println("------------------------------------------");
|
||||
|
||||
IO.println("USERS: " + clients.length + " " + Arrays.stream(clients).toList().toString());
|
||||
|
||||
if (!gameTypesStore.all().containsKey(gameType)) return;
|
||||
|
||||
IO.println("------------------------------------------");
|
||||
|
||||
try {
|
||||
ServerPlayer[] players = new ServerPlayer[clients.length];
|
||||
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
|
||||
|
||||
for (int i = 0; i < clients.length; i++) {
|
||||
players[i] = new ServerPlayer(clients[i]);
|
||||
clients[i].addGame(new ImmutablePair<>(game, players[i]));
|
||||
clients[i].setGame(new ImmutablePair<>(game, players[i]));
|
||||
}
|
||||
System.out.println("Starting OnlineTurnBasedGame");
|
||||
|
||||
@@ -216,8 +210,23 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
||||
String userLeft = userNames.get(left);
|
||||
String userRight = userNames.get(right);
|
||||
|
||||
// Remove user if he is in a game
|
||||
boolean userInGame = false;
|
||||
|
||||
if (getUser(userLeft).game() != null) {
|
||||
userNames.remove(userLeft);
|
||||
userInGame = true;
|
||||
} else if (getUser(userRight).game() != null) {
|
||||
userNames.remove(userRight);
|
||||
userInGame = true;
|
||||
}
|
||||
|
||||
if (userInGame) { continue; }
|
||||
//
|
||||
|
||||
int first = Math.max(left, right);
|
||||
int second = Math.min(left, right);
|
||||
|
||||
userNames.remove(first);
|
||||
userNames.remove(second);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ public interface Client<G, P> {
|
||||
OnlineTurnBasedGame game();
|
||||
P player();
|
||||
|
||||
void addGame(Pair<G, P> gamePair);
|
||||
void setGame(Pair<G, P> gamePair);
|
||||
void clearGame();
|
||||
|
||||
void send(String message);
|
||||
|
||||
@@ -27,10 +27,8 @@ public class NettyClient implements Client<OnlineTurnBasedGame, ServerPlayer> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
|
||||
if (this.gamePair == null) {
|
||||
this.gamePair = gamePair;
|
||||
}
|
||||
public void setGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
|
||||
this.gamePair = gamePair;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user