Package org.swtpra1

Class GameserverMain


  • public class GameserverMain
    extends java.lang.Object
    Gameserver manages players and games created by the tournament-control and executes them. It receives HTTPS-requests and executes the correct methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean useHttps  
    • Constructor Summary

      Constructors 
      Constructor Description
      GameserverMain()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean checkBoard​(com.google.gson.JsonObject jsonBoard)
      Checks if a given boards size attributes and array sizes match.
      static boolean checkIfPlayerExists​(Player player)
      Checks if player exists in playerMap before new game is created.
      static boolean checkJsonKeys​(com.google.gson.JsonObject jo, java.lang.String[] keys)
      Check if passed json-object has needed keys.
      static boolean checkJsonKeys​(java.lang.String jsonString, java.lang.String[] keys)
      Check if passed json-string has needed keys.
      static boolean checkToken​(java.lang.String token)
      Checks if a http query has a token and checks if it is valid.
      static boolean checkTurnJson​(java.lang.String turnJson)
      checks turn json sent by player.
      static int createNewGame​(java.lang.String gameJson, int gameId)
      Create a new game and add to the games-list.
      static int createNewPlayer​(java.lang.String playerJson, int playerId)
      Creates new player and adds it to the players list.
      static int deleteGame​(int gameId)
      Delete game by given id.
      static int deletePlayer​(int playerId)
      Deletes player by his id and removes it from the list.
      static @Nullable java.lang.String getFullJsonGame​(int gameId)
      returns a game by its id as json object.
      static @NotNull java.lang.String getFullJsonGameList()
      Returns an array of the games present on the server as json objects.
      static java.util.SortedMap<java.lang.Integer,​Game> getGameMap()  
      static java.lang.String getJsonGame​(int gameId)
      Returns a game by its id as json object.
      static @NotNull java.lang.String getJsonGameList()
      Returns an array of the games present on the server as json objects.
      static java.util.SortedMap<java.lang.Integer,​Player> getPlayerMap()  
      static void main​(java.lang.String[] args)
      starts the game-server.
      static int parseId​(java.lang.String id)
      Return integer value of string.
      static void sendEndMessage​(java.lang.String url, int gameId, int turnId, int playerId, int winningPlayer)
      Send end message to player.
      static void sendEndMessages​(Game game, int turnId)
      Send end messages to players if game won/aborted.
      static void sendResponse​(com.sun.net.httpserver.HttpExchange exchange, int responseCode)
      Sends response with responseCode back to client.
      static void sendResponse​(com.sun.net.httpserver.HttpExchange exchange, java.lang.String json)
      Send response with json back to client.
      static int sendStartMessage​(Game game, int playerIndex)
      Sends game start message to a player.
      static boolean sendStartMessages​(Game game, int startingPlayerIndex)
      Send start messages to both players before game is played.
      static void sendTurnMessage​(Game game, int playerIndex, int turnId, java.lang.String playerUrl)
      Send turn message to player and execute the turn.
      static void startGame​(Game game)
      Starts a game after it was created.
      static void stopServer()
      Stops running https server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • useHttps

        public static boolean useHttps
    • Constructor Detail

      • GameserverMain

        public GameserverMain()
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
        starts the game-server.
      • sendResponse

        public static void sendResponse​(com.sun.net.httpserver.HttpExchange exchange,
                                        int responseCode)
        Sends response with responseCode back to client.
        Parameters:
        exchange - http exchange
        responseCode - 200/400/401
      • sendResponse

        public static void sendResponse​(com.sun.net.httpserver.HttpExchange exchange,
                                        java.lang.String json)
                                 throws java.io.IOException
        Send response with json back to client.
        Parameters:
        exchange - http exchange
        json - json object to be sent
        Throws:
        java.io.IOException - ioexception
      • checkToken

        public static boolean checkToken​(java.lang.String token)
        Checks if a http query has a token and checks if it is valid.
        Parameters:
        token - token passed from uri
        Returns:
        true if valid token, otherwise false
      • stopServer

        public static void stopServer()
        Stops running https server.
      • createNewPlayer

        public static int createNewPlayer​(java.lang.String playerJson,
                                          int playerId)
        Creates new player and adds it to the players list.
        Parameters:
        playerJson - json string of player to be created
        playerId - returns http status code 200 if playerId available, otherwise 400
        Returns:
        200 if player was created, otherwise 400
      • deletePlayer

        public static int deletePlayer​(int playerId)
        Deletes player by his id and removes it from the list. If the player is in a running game, this game will still continue.
        Parameters:
        playerId - id of player to be deleted
        Returns:
        200 if player could be deleted
      • createNewGame

        public static int createNewGame​(java.lang.String gameJson,
                                        int gameId)
        Create a new game and add to the games-list.
        Parameters:
        gameJson - json-string of new game with keys: gameId, players[], maxTurnTime, initialBoard
        gameId - Id of the new game
        Returns:
        http status code 200 if gameId available, otherwise 400
      • deleteGame

        public static int deleteGame​(int gameId)
        Delete game by given id.
        Parameters:
        gameId - id of the game to be deleted
        Returns:
        http status code 200 if gameId available, otherwise 400
      • getJsonGameList

        @NotNull
        public static @NotNull java.lang.String getJsonGameList()
        Returns an array of the games present on the server as json objects.
        Returns:
        List of games as JSON Object with keys: gameId, players[], winningPlayer, without initial board and turns, without player URLs
      • getFullJsonGameList

        @NotNull
        public static @NotNull java.lang.String getFullJsonGameList()
        Returns an array of the games present on the server as json objects.
        Returns:
        List of games as JSON Object with keys: gameId, players[], winningPlayer
      • getJsonGame

        public static java.lang.String getJsonGame​(int gameId)
        Returns a game by its id as json object.
        Parameters:
        gameId - gameId of wanted game
        Returns:
        game as JSONObject with keys: gameId, players[], maxTurnTime, initialBoard without confidential information
      • getFullJsonGame

        @Nullable
        public static @Nullable java.lang.String getFullJsonGame​(int gameId)
        returns a game by its id as json object.
        Parameters:
        gameId - Id of the requested game
        Returns:
        game as JSONObject with keys: gameId, players[], maxTurnTime, initialBoard
      • getGameMap

        public static java.util.SortedMap<java.lang.Integer,​Game> getGameMap()
      • getPlayerMap

        public static java.util.SortedMap<java.lang.Integer,​Player> getPlayerMap()
      • startGame

        public static void startGame​(Game game)
        Starts a game after it was created.
        Parameters:
        game - game to be played
      • sendStartMessages

        public static boolean sendStartMessages​(Game game,
                                                int startingPlayerIndex)
                                         throws java.io.IOException
        Send start messages to both players before game is played.
        Parameters:
        game - game-object to be played
        startingPlayerIndex - the index of the player starting the game
        Returns:
        true if players are ready (HTTP-Status-Code 200)
        Throws:
        java.io.IOException - ioexception if player not reachable
      • sendStartMessage

        public static int sendStartMessage​(Game game,
                                           int playerIndex)
                                    throws java.io.IOException
        Sends game start message to a player.
        Parameters:
        game - string url of players' server
        playerIndex - index of player in playerMap
        Returns:
        200 if player is ready
        Throws:
        java.io.IOException - ioexception if player not reachable
      • sendTurnMessage

        public static void sendTurnMessage​(Game game,
                                           int playerIndex,
                                           int turnId,
                                           java.lang.String playerUrl)
                                    throws java.io.IOException,
                                           java.lang.IllegalArgumentException
        Send turn message to player and execute the turn.
        Parameters:
        game - game played
        playerIndex - id of player who
        turnId - id of turn
        playerUrl - url of player with id id
        Throws:
        java.io.IOException - ioexception if player not reachable
        java.lang.IllegalArgumentException - illegalArgumentException
      • sendEndMessages

        public static void sendEndMessages​(Game game,
                                           int turnId)
                                    throws java.io.IOException
        Send end messages to players if game won/aborted.
        Parameters:
        game - game
        turnId - turnId
        Throws:
        java.io.IOException - ioexception if player not reachable
      • sendEndMessage

        public static void sendEndMessage​(java.lang.String url,
                                          int gameId,
                                          int turnId,
                                          int playerId,
                                          int winningPlayer)
                                   throws java.io.IOException
        Send end message to player.
        Parameters:
        url - url of player
        gameId - id of game
        turnId - id of turn
        playerId - id of player
        winningPlayer - id of player who has won
        Throws:
        java.io.IOException - ioexception if player not reachable
      • checkTurnJson

        public static boolean checkTurnJson​(java.lang.String turnJson)
        checks turn json sent by player.
        Parameters:
        turnJson - json-string of turn
        Returns:
        true if turnjson was correct
      • checkBoard

        public static boolean checkBoard​(com.google.gson.JsonObject jsonBoard)
        Checks if a given boards size attributes and array sizes match. For now only boards of size 10x10 are allowed.
        Parameters:
        jsonBoard - board to be checked
        Returns:
        true if board has correct size, false otherwise
      • parseId

        public static int parseId​(java.lang.String id)
        Return integer value of string.
        Parameters:
        id - id from json-strings
        Returns:
        integer of pId, otherwise -1
      • checkJsonKeys

        public static boolean checkJsonKeys​(java.lang.String jsonString,
                                            java.lang.String[] keys)
        Check if passed json-string has needed keys.
        Parameters:
        jsonString - JsonString passed in POST-Requests
        keys - array of keys which have to be in pJsonString
        Returns:
        true if all keys are in
      • checkJsonKeys

        public static boolean checkJsonKeys​(com.google.gson.JsonObject jo,
                                            java.lang.String[] keys)
        Check if passed json-object has needed keys.
        Parameters:
        jo - json-object
        keys - keys which has to be in the json-object
        Returns:
        true if needed keys are in the json-object
      • checkIfPlayerExists

        public static boolean checkIfPlayerExists​(Player player)
        Checks if player exists in playerMap before new game is created.
        Parameters:
        player - player to be checked
        Returns:
        true if player exists