mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Added pairs
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.game.players.ServerPlayer;
|
||||
|
||||
import java.util.Map;
|
||||
import org.toop.framework.utils.Pair;
|
||||
|
||||
public interface ServerUser {
|
||||
long id();
|
||||
String name();
|
||||
Game game();
|
||||
ServerPlayer serverPlayer();
|
||||
void addGame(Game game, ServerPlayer player);
|
||||
void addGame(Pair<Game, ServerPlayer> gamePair);
|
||||
void removeGame();
|
||||
void setName(String name);
|
||||
void sendMessage(String message);
|
||||
|
||||
@@ -2,14 +2,12 @@ package org.toop.framework.networking.server;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.toop.framework.game.players.ServerPlayer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.toop.framework.utils.Pair;
|
||||
|
||||
public class User implements ServerUser {
|
||||
final private long id;
|
||||
private String name;
|
||||
private final Map<Game, ServerPlayer> game = new HashMap<>();
|
||||
private Pair<Game, ServerPlayer> gamePair;
|
||||
private ChannelHandlerContext connectionContext;
|
||||
|
||||
public User(long userId, String name) {
|
||||
@@ -28,24 +26,24 @@ public class User implements ServerUser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGame(Game game, ServerPlayer player) {
|
||||
if (this.game.isEmpty()) {
|
||||
this.game.put(game, player);
|
||||
public void addGame(Pair<Game, ServerPlayer> gamePair) {
|
||||
if (this.gamePair == null) {
|
||||
this.gamePair = gamePair;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeGame() {
|
||||
this.game.clear();
|
||||
this.gamePair = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game game() {
|
||||
return this.game.keySet().iterator().next();
|
||||
return this.gamePair.getLeft();
|
||||
}
|
||||
|
||||
public ServerPlayer serverPlayer() {
|
||||
return this.game.values().iterator().next();
|
||||
return this.gamePair.getRight();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public class ImmutablePair <T, K> implements Pair<T,K> {
|
||||
final T left;
|
||||
final K right;
|
||||
|
||||
public ImmutablePair(T left, K right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getRight() {
|
||||
return right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public class MutablePair<T, K> implements Pair<T,K> {
|
||||
T left;
|
||||
K right;
|
||||
|
||||
public MutablePair(T left, K right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(T left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(K right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public interface Pair<T, K> {
|
||||
T getLeft();
|
||||
K getRight();
|
||||
}
|
||||
@@ -1,167 +1,167 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class ServerTest {
|
||||
|
||||
static class TurnBasedGameMock implements TurnBasedGame {
|
||||
private Player[] players;
|
||||
|
||||
public TurnBasedGameMock() {}
|
||||
|
||||
@Override
|
||||
public void init(Player[] players) {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentTurn() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPlayerCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWinner() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] getBoard() {
|
||||
return new long[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public TurnBasedGame deepCopy() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLegalMoves() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayResult play(long move) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayer(int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestUser implements ServerUser {
|
||||
|
||||
final private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public TestUser(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game[] games() {
|
||||
return new Game[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGame(Game game) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeGame(Game game) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Server server;
|
||||
private Duration waitTime = Duration.ofSeconds(2);
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
|
||||
var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
|
||||
games.put("tictactoe", TurnBasedGameMock.class);
|
||||
games.put("reversi", TurnBasedGameMock.class);
|
||||
|
||||
server = new Server(games, waitTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGameTypes() {
|
||||
String[] expected = {"tictactoe", "reversi"};
|
||||
String[] actual = server.gameTypes();
|
||||
|
||||
Arrays.sort(expected);
|
||||
Arrays.sort(actual);
|
||||
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChallenge() {
|
||||
server.addUser(new TestUser(1, "test1"));
|
||||
server.addUser(new TestUser(2, "test2"));
|
||||
server.challengeUser("test1", "test2", "tictactoe");
|
||||
|
||||
IO.println(server.gameChallenges());
|
||||
|
||||
Assertions.assertEquals(1, server.gameChallenges().size());
|
||||
|
||||
try {
|
||||
Thread.sleep(waitTime.plusMillis(100));
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Assertions.assertEquals(0, server.gameChallenges().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStartGame() {
|
||||
server.startGame("tictactoe", new User(0, "A"), new User(1, "B"));
|
||||
Assertions.assertEquals(1, server.ongoingGames().size());
|
||||
server.startGame("reversi", new User(0, "A"), new User(1, "B"));
|
||||
Assertions.assertEquals(2, server.ongoingGames().size());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//package org.toop.framework.networking.server;
|
||||
//
|
||||
//import org.junit.jupiter.api.Assertions;
|
||||
//import org.junit.jupiter.api.BeforeEach;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
//import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
//import org.toop.framework.gameFramework.model.player.Player;
|
||||
//
|
||||
//import java.time.Duration;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.concurrent.ConcurrentHashMap;
|
||||
//
|
||||
//public class ServerTest {
|
||||
//
|
||||
// static class TurnBasedGameMock implements TurnBasedGame {
|
||||
// private Player[] players;
|
||||
//
|
||||
// public TurnBasedGameMock() {}
|
||||
//
|
||||
// @Override
|
||||
// public void init(Player[] players) {
|
||||
// this.players = players;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getCurrentTurn() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getPlayerCount() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getWinner() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public long[] getBoard() {
|
||||
// return new long[0];
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public TurnBasedGame deepCopy() {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public long getLegalMoves() {
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public PlayResult play(long move) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Player getPlayer(int index) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// static class TestUser implements ServerUser {
|
||||
//
|
||||
// final private long id;
|
||||
//
|
||||
// private String name;
|
||||
//
|
||||
// public TestUser(long id, String name) {
|
||||
// this.id = id;
|
||||
// this.name = name;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public long id() {
|
||||
// return id;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String name() {
|
||||
// return name;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Game[] games() {
|
||||
// return new Game[0];
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void addGame(Game game) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void removeGame(Game game) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void setName(String name) {
|
||||
// this.name = name;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void sendMessage(String message) {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private Server server;
|
||||
// private Duration waitTime = Duration.ofSeconds(2);
|
||||
//
|
||||
// @BeforeEach
|
||||
// void setup() {
|
||||
//
|
||||
// var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
|
||||
// games.put("tictactoe", TurnBasedGameMock.class);
|
||||
// games.put("reversi", TurnBasedGameMock.class);
|
||||
//
|
||||
// server = new Server(games, waitTime);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testGameTypes() {
|
||||
// String[] expected = {"tictactoe", "reversi"};
|
||||
// String[] actual = server.gameTypes();
|
||||
//
|
||||
// Arrays.sort(expected);
|
||||
// Arrays.sort(actual);
|
||||
//
|
||||
// Assertions.assertArrayEquals(expected, actual);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testChallenge() {
|
||||
// server.addUser(new TestUser(1, "test1"));
|
||||
// server.addUser(new TestUser(2, "test2"));
|
||||
// server.challengeUser("test1", "test2", "tictactoe");
|
||||
//
|
||||
// IO.println(server.gameChallenges());
|
||||
//
|
||||
// Assertions.assertEquals(1, server.gameChallenges().size());
|
||||
//
|
||||
// try {
|
||||
// Thread.sleep(waitTime.plusMillis(100));
|
||||
// } catch (InterruptedException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
//
|
||||
// Assertions.assertEquals(0, server.gameChallenges().size());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testStartGame() {
|
||||
// server.startGame("tictactoe", new User(0, "A"), new User(1, "B"));
|
||||
// Assertions.assertEquals(1, server.ongoingGames().size());
|
||||
// server.startGame("reversi", new User(0, "A"), new User(1, "B"));
|
||||
// Assertions.assertEquals(2, server.ongoingGames().size());
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user