mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Compare commits
3 Commits
8867d5a1ea
...
6e2ea82a32
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e2ea82a32 | ||
|
|
34c85ec472 | ||
| 2d9b34b7f6 |
@@ -184,7 +184,7 @@ public final class Server {
|
|||||||
|
|
||||||
gameController = null;
|
gameController = null;
|
||||||
|
|
||||||
//if (!isPolling) return;
|
// if (!isPolling) return;
|
||||||
|
|
||||||
String gameType = extractQuotedValue(response.gameType());
|
String gameType = extractQuotedValue(response.gameType());
|
||||||
if (response.clientId() == clientId) {
|
if (response.clientId() == clientId) {
|
||||||
@@ -235,8 +235,9 @@ public final class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (gameController != null) {
|
if (gameController != null) {
|
||||||
primary.reEnableButton();
|
primary.reEnableButton(); // Re enable subscribe button
|
||||||
gameController.start();
|
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){
|
private void notifyGameEnd(GameState state, int winner){
|
||||||
if (state == GameState.DRAW){
|
if (state == GameState.DRAW){
|
||||||
for (NettyClient client : clients) {
|
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{
|
else{
|
||||||
clients[winner].send(String.format("SVR GAME WIN {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: \"NettyClient disconnected\"}\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
|
@Override
|
||||||
|
|||||||
@@ -128,21 +128,15 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startGame(String gameType, NettyClient... clients) {
|
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;
|
if (!gameTypesStore.all().containsKey(gameType)) return;
|
||||||
|
|
||||||
IO.println("------------------------------------------");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ServerPlayer[] players = new ServerPlayer[clients.length];
|
ServerPlayer[] players = new ServerPlayer[clients.length];
|
||||||
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
|
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
|
||||||
|
|
||||||
for (int i = 0; i < clients.length; i++) {
|
for (int i = 0; i < clients.length; i++) {
|
||||||
players[i] = new ServerPlayer(clients[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");
|
System.out.println("Starting OnlineTurnBasedGame");
|
||||||
|
|
||||||
@@ -216,8 +210,23 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
|||||||
String userLeft = userNames.get(left);
|
String userLeft = userNames.get(left);
|
||||||
String userRight = userNames.get(right);
|
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 first = Math.max(left, right);
|
||||||
int second = Math.min(left, right);
|
int second = Math.min(left, right);
|
||||||
|
|
||||||
userNames.remove(first);
|
userNames.remove(first);
|
||||||
userNames.remove(second);
|
userNames.remove(second);
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public interface Client<G, P> {
|
|||||||
OnlineTurnBasedGame game();
|
OnlineTurnBasedGame game();
|
||||||
P player();
|
P player();
|
||||||
|
|
||||||
void addGame(Pair<G, P> gamePair);
|
void setGame(Pair<G, P> gamePair);
|
||||||
void clearGame();
|
void clearGame();
|
||||||
|
|
||||||
void send(String message);
|
void send(String message);
|
||||||
|
|||||||
@@ -27,10 +27,8 @@ public class NettyClient implements Client<OnlineTurnBasedGame, ServerPlayer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
|
public void setGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
|
||||||
if (this.gamePair == null) {
|
this.gamePair = gamePair;
|
||||||
this.gamePair = gamePair;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user