As part of NTNU's increased focus on security, all users are now forced to enable and use Two-factor authentication.

Commit 9dfbbe33 authored by Stefano Grisendi's avatar Stefano Grisendi
Browse files

added more stuff to server

parent 5e4090d7
package com.mygdx.game.controllers;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.controllers.commands.cPlayerPieceMove;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.tokens.StarPiece;
......@@ -19,7 +22,7 @@ public class GameController {
private final PlayerController playerController;
private final List<List<Float>> playerNameTextFieldCoordinates;
public GameController(Game model, PlayView view, PlayerController playerController) {
public GameController(Game model, PlayView view, final PlayerController playerController) {
this.model = model;
this.view = view;
this.playerController = playerController;
......@@ -35,6 +38,19 @@ public class GameController {
this.view.initializePieces(this.model.getStartFieldCoordinates());
this.view.placePlayerNames(usedBoardSlots, playerNameTextFieldCoordinates);
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
playerController.getLobby().setGame(model); //set game in playerController
playerController.updateGame(); //send to server
Thread thread = new Thread(){
public void run(){
while (playerController.getLastCommand() == null) {
System.out.println("Waiting for game update");
}
GameState gs = (GameState) playerController.getLobby().getGame();
System.out.println("gameUpdate received: " + gs);
}
};
thread.start();
}
public void handleClick(float x, float y) {
......@@ -62,7 +78,10 @@ public class GameController {
this.view.movePiece(activePiece, cubeCoordinates);
activePiece.setRotateHead(false);
activePiece = null;
playerController.getNetWorkController().sendToServer(new cPlayerPieceMove(previousCoordinateClicked, cubeCoordinates, playerController.getLobby().getID(), playerController.getPlayer().getID()));
//playerController.updateGame();
previousCoordinateClicked = null;
this.view.placeTurnIndicator(playerNameTextFieldCoordinates.get(this.model.getPlayerTurnSlot()));
......
......@@ -5,8 +5,15 @@ import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.mygdx.game.controllers.commands.Command;
import com.mygdx.game.controllers.commands.*;
import com.mygdx.game.model.DefaultBoard;
import com.mygdx.game.model.Field;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameMode;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Piece;
import com.mygdx.game.model.Player;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import java.io.IOException;
import java.util.ArrayList;
......@@ -23,7 +30,8 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.0.136"; //122 //"192.168.87.20";
//192.168.0.136
String IP4_LAN_ADDRESS = "192.168.10.154"; //122 //"192.168.87.20";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......@@ -53,6 +61,18 @@ public class NetworkController {
kryo.register(cSetPlayerName.class, 27);
kryo.register(cStartGame.class, 28);
kryo.register(Vector3.class, 29);
kryo.register(cUpdateGame.class, 30);
kryo.register(Game.class, 31);
kryo.register(GameState.class, 32);
kryo.register(GameMode.class, 33);
kryo.register(Piece.class, 34);
kryo.register(Field.class, 35);
kryo.register(DefaultBoard.class, 36);
kryo.register(DefaultRules.class, 37);
kryo.register(java.util.HashMap.class, 38);
kryo.register(java.util.Arrays.asList().getClass(), 39);
kryo.register(kotlin.collections.EmptyList.class, 40); //works anyway ignore
kryo.register(java.util.LinkedHashSet.class, 41);
}
public Client getClient() { return client; }
......
......@@ -5,9 +5,6 @@ import com.esotericsoftware.kryonet.Listener;
import com.mygdx.game.controllers.commands.*;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import org.javatuples.Pair;
import java.util.ArrayList;
import java.util.LinkedList;
......@@ -69,6 +66,8 @@ public class PlayerController {
public void leaveLobby(int id) { sendCommand(new cLobbyLeave(id)); }
public void updateGame(){ sendCommand(new cUpdateGame(lobby.getGame(), lobby.getID()));}
public Lobby getLobby(){ return lobby; }
public ArrayList<Lobby> getLobbyList(){ return lobbies; }
......
......@@ -30,9 +30,12 @@ public class Command{
@Override
public String toString() {
String toString;
if(data == null) toString = "null";
else toString = data.toString();
return "Command{" +
"text='" + text + '\'' +
"data='" + data.toString() + '\'' +
"data='" + toString + '\'' +
'}';
}
}
\ No newline at end of file
package com.mygdx.game.controllers.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controllers.PlayerController;
import java.util.ArrayList;
public class cUpdateGame extends Command{
public cUpdateGame() { super("cUpdateGame"); }
public cUpdateGame(Object game, int lobbyId ) {
super("cUpdateGame");
ArrayList<Object> data = new ArrayList<Object>();
data.add(game);
data.add(lobbyId);
this.data = data;
}
@Override
public void execute(PlayerController playerController, Connection connection){
playerController.getLobby().setGame(data); //gameState as object, need to cast before using
if(data == null) System.out.println("gameState update received and it is null");
else System.out.println("gameState update received correctly");
}
}
......@@ -52,4 +52,5 @@ class GameState(gameMode: GameMode) {
fun setFinished() {
isFinished = true
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ public class Lobby {
private int MAX_PLAYERS;
private ArrayList<Player> players;
private boolean lobbyGameStarted = false;
private Object game;
public Lobby() {}
......@@ -24,6 +25,7 @@ public class Lobby {
this.name = name;
this.MAX_PLAYERS = MAX_PLAYERS;
players = new ArrayList<Player>();
game = null;
}
public int getID(){ return ID; }
......@@ -62,6 +64,10 @@ public class Lobby {
return players;
}
public void setGame(Object gameState){ this.game = gameState; }
public Object getGame(){ return this.game; }
public LinkedHashSet<Integer> getPlayersID(){
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
for(Player player : players){
......
<component name="libraryTable">
<library name="Gradle: CheckersServer.desktop.desktop-1.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/desktop/build/libs/desktop-1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
......@@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/modules/CheckersServer.iml" filepath="$PROJECT_DIR$/.idea/modules/CheckersServer.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/CheckersServer.iml" filepath="$PROJECT_DIR$/.idea/CheckersServer.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/android/CheckersServer.android.iml" filepath="$PROJECT_DIR$/.idea/modules/android/CheckersServer.android.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/core/CheckersServer.core.iml" filepath="$PROJECT_DIR$/.idea/modules/core/CheckersServer.core.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/desktop/CheckersServer.desktop.iml" filepath="$PROJECT_DIR$/.idea/modules/desktop/CheckersServer.desktop.iml" />
......
package com.mygdx.game;
import java.util.HashMap;
public class Constants {
public static final int PLAYER_NAME_TEXT_FIELD_WIDTH = 340;
public static final int PLAYER_NAME_TEXT_FIELD_HEIGHT = 80;
public static final HashMap<Integer, String> AVAILABLEAVATARSHASHMAP = new HashMap<Integer, String>() {{
put(0, "RegularGuy");
put(1, "HipsterGirl");
put(2, "HighSchoolGuy");
put(3, "RocketGuy");
put(4, "AsianGirl");
put(5, "Grandma");
}};
}
package com.mygdx.game.controller;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.mygdx.game.controller.commands.*;
import com.mygdx.game.model.DefaultBoard;
import com.mygdx.game.model.Field;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameMode;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Piece;
import com.mygdx.game.model.Player;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class NetworkController {
......@@ -27,7 +32,7 @@ public class NetworkController {
private Kryo kryo;
public NetworkController(){
server = new Server();
server = new Server(16384,3072);
server.start();
try {
server.bind(54555, 54777);
......@@ -124,6 +129,16 @@ public class NetworkController {
return new Lobby(-1);
}
public Object updateGame(Game game, int lobbyID){
Lobby lobby = getLobby(lobbyID);
if (lobby.getID() != -1){
lobby.setGame(game);
lobbies.set(lobbies.indexOf(lobby), lobby);
return game;
}
return null;
}
public Lobby getLobby(int lobbyID){
int lobby_idx = lobbies.indexOf(new Lobby(lobbyID));
if (lobby_idx != -1){
......@@ -160,7 +175,18 @@ public class NetworkController {
kryo.register(cSetPlayerReady.class, 26);
kryo.register(cSetPlayerName.class, 27);
kryo.register(cStartGame.class, 28);
kryo.register(Vector3.class, 29);
kryo.register(cUpdateGame.class, 30);
kryo.register(Game.class, 31);
kryo.register(GameState.class, 32);
kryo.register(GameMode.class, 33);
kryo.register(Piece.class, 34);
kryo.register(Field.class, 35);
kryo.register(DefaultBoard.class, 36);
kryo.register(DefaultRules.class, 37);
kryo.register(java.util.HashMap.class, 38);
kryo.register(java.util.Arrays.asList().getClass(), 39);
kryo.register(kotlin.collections.EmptyList.class, 40); //works anyway ignore
kryo.register(java.util.LinkedHashSet.class, 41);
}
public NetworkController getNetworkController() { return this; }
......
......@@ -26,9 +26,12 @@ public class Command{
@Override
public String toString() {
String toString;
if(data == null) toString = "null";
else toString = data.toString();
return "Command{" +
"text='" + text + '\'' +
"data='" + data.toString() + '\'' +
"data='" + toString + '\'' +
'}';
}
}
\ No newline at end of file
package com.mygdx.game.controller.commands;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import java.util.ArrayList;
public class cPlayerPieceMove extends Command {
Vector3 fromCoordinates;
Vector3 toCordinates;
int lobby_id;
int playerId;
public cPlayerPieceMove() {
super("cPlayerPieceMove");
}
/*public cPlayerPieceMove(Vector3 fromCoordinates, Vector3 toCoordinates, int lobbyId, int playerId) {
super("cPlayerPieceMove");
this.fromCoordinates = fromCoordinates;
this.toCordinates = toCoordinates;
this.lobby_id = lobbyId;
this.playerId = playerId;
ArrayList<Object> data = new ArrayList<Object>();
data.add(fromCoordinates);
data.add(toCoordinates);
data.add(playerId);
this.data = data;
}*/
@Override
public void execute(NetworkController net, Connection connection){
if (data instanceof ArrayList) {
System.out.println("Checking if valid move.. If valid, moving player piece from A to B... Updating model.. Sending move to clients....");
}
for (Connection c : net.getConnections(net.getLobby(lobby_id))) {
if (c != connection) {
c.sendTCP(this);
}
}
System.out.println("Checking if valid move.. If valid, moving player piece from A to B... Updating model.. Sending move to clients....");
connection.sendTCP(this);
}
}
......
package com.mygdx.game.controller.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.Lobby;
import java.util.ArrayList;
public class cUpdateGame extends Command{
public cUpdateGame() { super("cUpdateGame"); }
public cUpdateGame(Game game) { super("cUpdateGame", game); }
@Override
public void execute(NetworkController net, Connection connection){
if(data instanceof ArrayList){
Game gameState = (Game)((ArrayList) data).get(0);
int lobbyID = (int) ((ArrayList) data).get(1);
data = net.updateGame(gameState, lobbyID);
for (Connection c : net.getConnections(net.getLobby(lobbyID))) {
c.sendTCP(this);
}
}
System.out.println("Game update request received");
}
}
\ No newline at end of file
//Inside gameState, data for a specific field
package com.mygdx.game.model
import java.lang.Exception
class Field {
private var piece: Piece?
//private var powerup: Powerup?
init {
this.piece = null
}
fun setPiece(piece: Piece) {
if (this.piece != null) {
throw Exception("Field already holds a piece.")
} else {
this.piece = piece
}
}
fun getPiece(): Piece? {
return this.piece
}
fun hasPiece(): Boolean {
return this.piece != null
}
fun removePiece() {
this.piece = null
}
}
\ No newline at end of file
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
class Game(gameState: GameState, playerIds: LinkedHashSet<Int>) {
private var gameState: GameState
private var playerIds: LinkedHashSet<Int>
private var playerTurnId: Int
private var playerTurnIndex: Int = 0 // TODO: Random from server
private var playerTurnSlot: Int
private var playerFinishedIds: List<Int> = listOf()
private var usedBoardSlots: List<Int>
init {
this.gameState = gameState
this.playerIds = playerIds
this.gameState.getBoardState().placePiecesAtStart(this.gameState.getRules(), this.playerIds)
usedBoardSlots = this.gameState.getRules().getUsedBoardSlots(playerIds.size)
playerTurnSlot = usedBoardSlots[0]
playerTurnId = playerIds.elementAt(playerTurnIndex)
}
}
\ No newline at end of file
package com.mygdx.game.model//Holds a ruleset and a board
class GameMode(rules:AbstractRules, board:AbstractBoard) {
private var rules: AbstractRules
private var board: AbstractBoard
init { }
init {
this.rules = rules
this.board = board
}
fun setBoard(board:AbstractBoard) {
this.board = board
}
fun getBoard(): AbstractBoard {
return this.board
}
fun setRules(rules: AbstractRules) {
this.rules = rules
}
fun getRules(): AbstractRules {
return this.rules
}
}
\ No newline at end of file
package com.mygdx.game.model
import com.mygdx.game.model.gamemodes.rules.DefaultRules
/*
Holds the state of the game and all data related to this.
*/
class GameState(gameMode: GameMode) {
private var boardState: AbstractBoard
private var rules: AbstractRules
private var isStarted: Boolean
private var winner: Int?
private var isFinished: Boolean = false
init { }
init {
this.rules = gameMode.getRules()
this.boardState = gameMode.getBoard()
this.isStarted = false
this.winner = null
}
fun getBoardState(): AbstractBoard {
return boardState
}
fun getRules(): AbstractRules {
return rules
}
fun isStarted(): Boolean {
return isStarted
}
fun setStarted() {
this.isStarted = true
}
fun isFinished(): Boolean {
return isFinished
}
fun getWinner(): Int? {
return this.winner
}
fun setWinner(winner: Int) {
this.winner = winner
}
fun setFinished() {
isFinished = true
}
}
\ No newline at end of file
package com.mygdx.game.model;
import java.util.ArrayList;
import java.util.HashSet;
......@@ -9,6 +10,7 @@ public class Lobby {
private String name;
private int MAX_PLAYERS;
private ArrayList<Player> players;
private Game game;
public Lobby() {}
......@@ -22,6 +24,7 @@ public class Lobby {
this.name = name;
this.MAX_PLAYERS = MAX_PLAYERS;
players = new ArrayList<Player>();
game = null;
}
public int playerJoin(Player player){
......@@ -84,6 +87,10 @@ public class Lobby {
}
}
public void setGame(Game game){ this.game = game; }
public Object getGame(){ return this.game; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
......