mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Making moves works. Game notifies when game has ended.
This commit is contained in:
@@ -8,6 +8,7 @@ import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThrea
|
|||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
import org.toop.framework.gameFramework.view.GUIEvents;
|
||||||
import org.toop.framework.utils.ImmutablePair;
|
import org.toop.framework.utils.ImmutablePair;
|
||||||
|
import org.toop.framework.utils.Pair;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@@ -15,19 +16,30 @@ import static org.toop.framework.gameFramework.GameState.TURN_SKIPPED;
|
|||||||
import static org.toop.framework.gameFramework.GameState.WIN;
|
import static org.toop.framework.gameFramework.GameState.WIN;
|
||||||
|
|
||||||
public class ServerThreadBehaviour extends AbstractThreadBehaviour implements Runnable {
|
public class ServerThreadBehaviour extends AbstractThreadBehaviour implements Runnable {
|
||||||
private Consumer<ImmutablePair<String, Integer>> onPlayerMove;
|
private final Consumer<ImmutablePair<String, Integer>> onPlayerMove;
|
||||||
|
private final Consumer<Pair<GameState, Integer>> onGameEnd;
|
||||||
/**
|
/**
|
||||||
* Creates a new base behaviour for the specified game.
|
* Creates a new base behaviour for the specified game.
|
||||||
*
|
*
|
||||||
* @param game the turn-based game to control
|
* @param game the turn-based game to control
|
||||||
*/
|
*/
|
||||||
public ServerThreadBehaviour(TurnBasedGame game, Consumer<ImmutablePair<String, Integer>> onPlayerMove) {
|
public ServerThreadBehaviour(TurnBasedGame game, Consumer<ImmutablePair<String, Integer>> onPlayerMove, Consumer<Pair<GameState, Integer>> onGameEnd) {
|
||||||
|
this.onPlayerMove = onPlayerMove;
|
||||||
|
this.onGameEnd = onGameEnd;
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyPlayerMove(ImmutablePair<String, Integer> pair) {
|
private void notifyPlayerMove(ImmutablePair<String, Integer> pair) {
|
||||||
|
if (onPlayerMove != null) {
|
||||||
onPlayerMove.accept(pair);
|
onPlayerMove.accept(pair);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyGameEnd(ImmutablePair<GameState, Integer> pair) {
|
||||||
|
if (onGameEnd != null) {
|
||||||
|
onGameEnd.accept(pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Starts the game loop in a new thread. */
|
/** Starts the game loop in a new thread. */
|
||||||
@Override
|
@Override
|
||||||
@@ -51,14 +63,12 @@ public class ServerThreadBehaviour extends AbstractThreadBehaviour implements Ru
|
|||||||
PlayResult result = game.play(move);
|
PlayResult result = game.play(move);
|
||||||
|
|
||||||
GameState state = result.state();
|
GameState state = result.state();
|
||||||
|
|
||||||
if (state != TURN_SKIPPED){
|
|
||||||
notifyPlayerMove(new ImmutablePair<>(currentPlayer.getName(), Long.numberOfTrailingZeros(move)));
|
notifyPlayerMove(new ImmutablePair<>(currentPlayer.getName(), Long.numberOfTrailingZeros(move)));
|
||||||
}
|
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case WIN, DRAW -> {
|
case WIN, DRAW -> {
|
||||||
isRunning.set(false);
|
isRunning.set(false);
|
||||||
|
notifyGameEnd(new ImmutablePair<>(state, game.getWinner()));
|
||||||
}
|
}
|
||||||
case NORMAL, TURN_SKIPPED -> { /* continue normally */ }
|
case NORMAL, TURN_SKIPPED -> { /* continue normally */ }
|
||||||
default -> {
|
default -> {
|
||||||
|
|||||||
@@ -38,9 +38,4 @@ public class ServerPlayer extends AbstractPlayer {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public abstract class AbstractPlayer implements Player {
|
|||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
protected AbstractPlayer(String name) {
|
protected AbstractPlayer(String name) {
|
||||||
|
System.out.println("Player " + name + " has been created");
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ public abstract class AbstractPlayer implements Player {
|
|||||||
throw new UnsupportedOperationException("Not supported yet.");
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
public final String getName(){
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.toop.framework.networking.server;
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
import org.toop.framework.game.gameThreads.ServerThreadBehaviour;
|
import org.toop.framework.game.gameThreads.ServerThreadBehaviour;
|
||||||
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
public class Game implements OnlineGame<TurnBasedGame> {
|
public class Game implements OnlineGame<TurnBasedGame> {
|
||||||
@@ -12,13 +13,31 @@ public class Game implements OnlineGame<TurnBasedGame> {
|
|||||||
|
|
||||||
public Game(TurnBasedGame game, User... users) {
|
public Game(TurnBasedGame game, User... users) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.gameThread = new ServerThreadBehaviour(game, (pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()));
|
this.gameThread = new ServerThreadBehaviour(
|
||||||
|
game,
|
||||||
|
(pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()),
|
||||||
|
(pair) -> notifyGameEnd(pair.getLeft(), pair.getRight())
|
||||||
|
);
|
||||||
this.users = users;
|
this.users = users;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyMoveMade(String speler, int move){
|
private void notifyMoveMade(String speler, int move){
|
||||||
users[0].sendMessage(String.format("SVR GAME MOVE {PLAYER: \"%s\", DETAILS: \"<reactie spel op zet>\", MOVE: \"%s\"}", speler, move));
|
for (User user : users) {
|
||||||
users[1].sendMessage(String.format("SVR GAME MOVE {PLAYER: \"%s\", DETAILS: \"<reactie spel op zet>\", MOVE: \"%s\"}", speler, move));
|
user.sendMessage(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"<reactie spel op zet>\"}\n", speler, move));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyGameEnd(GameState state, int winner){
|
||||||
|
if (state == GameState.DRAW){
|
||||||
|
for (User user : users) {
|
||||||
|
user.sendMessage(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"Client disconnected\"}\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
users[winner].sendMessage(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"Client disconnected\"}\n"));
|
||||||
|
users[(winner + 1)%2].sendMessage(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"Client disconnected\"}\n"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ public class User implements ServerUser {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Game game() {
|
public Game game() {
|
||||||
|
if (this.gamePair == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return this.gamePair.getLeft();
|
return this.gamePair.getLeft();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user