mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Tournament now returns result to clients
This commit is contained in:
@@ -6,8 +6,10 @@ import org.toop.app.gameControllers.*;
|
|||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
import org.toop.app.widget.WidgetContainer;
|
import org.toop.app.widget.WidgetContainer;
|
||||||
import org.toop.app.widget.complex.LoadingWidget;
|
import org.toop.app.widget.complex.LoadingWidget;
|
||||||
|
import org.toop.app.widget.complex.PopupWidget;
|
||||||
import org.toop.app.widget.popup.ChallengePopup;
|
import org.toop.app.widget.popup.ChallengePopup;
|
||||||
import org.toop.app.widget.popup.ErrorPopup;
|
import org.toop.app.widget.popup.ErrorPopup;
|
||||||
|
import org.toop.app.widget.popup.GameOverPopup;
|
||||||
import org.toop.app.widget.popup.SendChallengePopup;
|
import org.toop.app.widget.popup.SendChallengePopup;
|
||||||
import org.toop.app.widget.view.ServerView;
|
import org.toop.app.widget.view.ServerView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
@@ -22,6 +24,7 @@ import org.toop.framework.networking.server.gateway.NettyGatewayServer;
|
|||||||
import org.toop.framework.game.players.LocalPlayer;
|
import org.toop.framework.game.players.LocalPlayer;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -160,7 +163,8 @@ public final class Server {
|
|||||||
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false, "game-result")
|
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false, "game-result")
|
||||||
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false, "game-move")
|
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false, "game-move")
|
||||||
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false, "your-turn")
|
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false, "your-turn")
|
||||||
.listen(NetworkEvents.ClosedConnection.class, this::closedConnection, false, "closed-connection");
|
.listen(NetworkEvents.ClosedConnection.class, this::closedConnection, false, "closed-connection")
|
||||||
|
.listen(NetworkEvents.TournamentResultResponse.class, this::handleTournamentResult, false, "tournament-result");
|
||||||
|
|
||||||
connectFlow = a;
|
connectFlow = a;
|
||||||
}
|
}
|
||||||
@@ -239,6 +243,12 @@ public final class Server {
|
|||||||
gameController.gameFinished(response);
|
gameController.gameFinished(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleTournamentResult(NetworkEvents.TournamentResultResponse response) {
|
||||||
|
IO.println(response.gameType());
|
||||||
|
IO.println(Arrays.toString(response.names()));
|
||||||
|
IO.println(Arrays.toString(response.scores()));
|
||||||
|
}
|
||||||
|
|
||||||
private void handleReceivedMove(NetworkEvents.GameMoveResponse response) {
|
private void handleReceivedMove(NetworkEvents.GameMoveResponse response) {
|
||||||
if (gameController == null) {
|
if (gameController == null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -65,6 +65,9 @@ public class NetworkEvents extends EventsBase {
|
|||||||
public record GameResultResponse(long clientId, String condition)
|
public record GameResultResponse(long clientId, String condition)
|
||||||
implements GenericEvent {}
|
implements GenericEvent {}
|
||||||
|
|
||||||
|
public record TournamentResultResponse(long clientId, String gameType, String[] names, Integer[] scores)
|
||||||
|
implements GenericEvent {}
|
||||||
|
|
||||||
/** Indicates that a game move has been processed or received. */
|
/** Indicates that a game move has been processed or received. */
|
||||||
public record GameMoveResponse(long clientId, String player, String move, String details)
|
public record GameMoveResponse(long clientId, String player, String move, String details)
|
||||||
implements GenericEvent {}
|
implements GenericEvent {}
|
||||||
@@ -219,4 +222,5 @@ public class NetworkEvents extends EventsBase {
|
|||||||
/** Response to a {@link ChangeAddress} event, carrying the success result. */
|
/** Response to a {@link ChangeAddress} event, carrying the success result. */
|
||||||
public record ChangeAddressResponse(boolean successful, long identifier)
|
public record ChangeAddressResponse(boolean successful, long identifier)
|
||||||
implements ResponseToUniqueEvent {}
|
implements ResponseToUniqueEvent {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.toop.framework.networking.connection.handlers;
|
|||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.regex.MatchResult;
|
import java.util.regex.MatchResult;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -94,6 +95,9 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
case "HELP":
|
case "HELP":
|
||||||
helpHandler(recSrvRemoved);
|
helpHandler(recSrvRemoved);
|
||||||
return;
|
return;
|
||||||
|
case "RESULTS":
|
||||||
|
resultsHandler(recSrvRemoved);
|
||||||
|
return;
|
||||||
default:
|
default:
|
||||||
// return
|
// return
|
||||||
}
|
}
|
||||||
@@ -103,6 +107,34 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String extract(String input, String key) {
|
||||||
|
Pattern p = Pattern.compile(
|
||||||
|
key + "\\s*:\\s*(\\[[^]]*]|\"[^\"]*\")",
|
||||||
|
Pattern.CASE_INSENSITIVE
|
||||||
|
);
|
||||||
|
Matcher m = p.matcher(input);
|
||||||
|
return m.find() ? m.group(1) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resultsHandler(String rec) {
|
||||||
|
IO.println(rec);
|
||||||
|
|
||||||
|
String gameTypeRaw = extract(rec, "GAMETYPE");
|
||||||
|
String usersRaw = extract(rec, "USERS");
|
||||||
|
String scoresRaw = extract(rec, "SCORES");
|
||||||
|
|
||||||
|
String[] users = Arrays.stream(usersRaw.substring(1, usersRaw.length() - 1).split(","))
|
||||||
|
.map(s -> s.trim().replace("\"", ""))
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
Integer[] scores = Arrays.stream(scoresRaw.substring(1, scoresRaw.length() - 1).split(","))
|
||||||
|
.map(String::trim)
|
||||||
|
.map(Integer::parseInt)
|
||||||
|
.toArray(Integer[]::new);
|
||||||
|
|
||||||
|
eventBus.post(new NetworkEvents.TournamentResultResponse(this.connectionId, gameTypeRaw, users, scores));
|
||||||
|
}
|
||||||
|
|
||||||
private void gameMoveHandler(String rec) {
|
private void gameMoveHandler(String rec) {
|
||||||
String[] msg =
|
String[] msg =
|
||||||
Pattern.compile(
|
Pattern.compile(
|
||||||
|
|||||||
@@ -292,7 +292,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
|||||||
String scores = gson.toJson(s);
|
String scores = gson.toJson(s);
|
||||||
|
|
||||||
String msg = String.format(
|
String msg = String.format(
|
||||||
"SVR RESULTS {GAMETYPE: \"%s\", USERS: %s, SCORES: %s}",
|
"SVR RESULTS {GAMETYPE: \"%s\", USERS: %s, SCORES: %s, TOURNAMENT: 1}",
|
||||||
gameType,
|
gameType,
|
||||||
users,
|
users,
|
||||||
scores
|
scores
|
||||||
|
|||||||
Reference in New Issue
Block a user