org.xproduct.server.core.Game.java Source code

Java tutorial

Introduction

Here is the source code for org.xproduct.server.core.Game.java

Source

/*
 * Copyright (C) 2013 Christopher Burke <krslynx@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.xproduct.server.core;

import com.google.gson.Gson;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.http.ServerWebSocket;
import org.xproduct.server.core.exceptions.GameProtocolException;
import org.xproduct.server.core.helpers.Converters;
import org.xproduct.server.core.model.Player;
import org.xproduct.server.core.net.PacketController;
import org.xproduct.server.core.net.controllers.ChatMessageController;
import org.xproduct.server.core.net.controllers.ClockSyncController;
import org.xproduct.server.core.net.controllers.LoginController;
import org.xproduct.server.core.net.controllers.PlayerDiedController;
import org.xproduct.server.core.net.controllers.PositionController;
import org.xproduct.server.core.net.controllers.PrizeCollectedController;
import org.xproduct.server.core.net.controllers.QueryNameController;
import org.xproduct.server.core.net.controllers.RegisterNameController;
import org.xproduct.server.core.net.controllers.SetPresenceController;
import org.xproduct.server.core.net.controllers.ShipChangeController;
import org.xproduct.server.core.net.controllers.StartGameController;
import org.xproduct.server.core.net.packets.C2SPacketType;

/**
 *
 * @author Christopher Burke <krslynx@gmail.com>
 */
public class Game {

    /**
     * The logger for the class
     */
    private Logger log = LoggerFactory.getLogger(Game.class);
    /**
     * The gameResourcesString JSON data
     */
    private String gameResourcesString;
    /**
     * The gameSettingsString JSON data
     */
    private String gameSettingsString;
    /**
     * The gameMapDataString JSON data
     */
    private String gameMapDataString;
    /**
     * The gameMapPropertiesString JSON data
     */
    private String gameMapPropertiesString;

    /**
     * Contains all Players, and their corresponding WebSocket
     */
    private HashMap<ServerWebSocket, Player> playerMap;
    /**
     * Is the server in development mode
     */
    private boolean development;

    /**
     * The Game constructor, which passes in the JSON data of the games
     * resources, settings, map data and tile properties.
     *
     * @param gameResources
     * @param gameSettings
     * @param gameMapData
     * @param gameMapProperties
     */
    public Game(String gameResources, String gameSettings, String gameMapData, String gameMapProperties) {
        this.gameResourcesString = gameResources;
        this.gameSettingsString = gameSettings;
        this.gameMapDataString = gameMapData;
        this.gameMapPropertiesString = gameMapProperties;
        this.playerMap = new HashMap<ServerWebSocket, Player>();
    }

    /**
     * The processRequest method consumes the incoming packet from the provided
     * WebSocket, and takes the socket identifier.
     *
     * @param packetData packetData from the socket
     * @param webSocket the socket
     * @return the response back to the user (immediate) if any
     * @throws GameProtocolException
     */
    public void processRequest(String packetData, ServerWebSocket webSocket) {
        Object[] incomingObject = new Gson().fromJson(packetData, Object[].class);

        int packetTypeIdentifier = Converters.getInteger(0, incomingObject);
        C2SPacketType packetType = C2SPacketType.valueOf(packetTypeIdentifier);

        PacketController handler = null;

        switch (packetType) {
        case LOGIN:
            log.trace("Received LOGIN packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new LoginController(incomingObject, this, webSocket);
            break;
        case START_GAME:
            log.trace("Received START_GAME packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new StartGameController(incomingObject, this, webSocket);
            break;
        case POSITION:
            log.trace("Received POSITION packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new PositionController(incomingObject, this, webSocket);
            break;
        case CLOCK_SYNC:
            log.trace("Received CLOCK_SYNC packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new ClockSyncController(incomingObject, this, webSocket);
            break;
        case PLAYER_DIED:
            log.trace("Received PLAYER_DIED packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new PlayerDiedController(incomingObject, this, webSocket);
            break;
        case CHAT_MESSAGE:
            log.trace("Received CHAT_MESSAGE packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new ChatMessageController(incomingObject, this, webSocket);
            break;
        case SHIP_CHANGE:
            log.trace("Received SHIP_CHANGE packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new ShipChangeController(incomingObject, this, webSocket);
            break;
        case QUERY_NAME:
            log.trace("Received QUERY_NAME packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new QueryNameController(incomingObject, this, webSocket);
            break;
        case REGISTER_NAME:
            log.trace("Received REGISTER_NAME packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new RegisterNameController(incomingObject, this, webSocket);
            break;
        case PRIZE_COLLECTED:
            log.trace("Received PRIZE_COLLECTED packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new PrizeCollectedController(incomingObject, this, webSocket);
            break;
        case SET_PRESENCE:
            log.trace("Received SET_PRESENCE packet: " + Arrays.deepToString(incomingObject) + " from socket: "
                    + webSocket.textHandlerID);
            handler = new SetPresenceController(incomingObject, this, webSocket);
            break;
        }
        try {
            handler.process();
        } catch (GameProtocolException ex) {
            log.error("Error", ex);
        }
    }

    public HashMap<ServerWebSocket, Player> getPlayerMap() {
        return playerMap;
    }

    public Entry<ServerWebSocket, Player> getPlayerBySocialIdentifier(String socialIdentifier) {
        Iterator<Entry<ServerWebSocket, Player>> playerMapIterator = playerMap.entrySet().iterator();
        while (playerMapIterator.hasNext()) {
            Entry<ServerWebSocket, Player> entry = playerMapIterator.next();
            if (entry.getValue().getSocialIdentifier().getId().equalsIgnoreCase(socialIdentifier)) {
                return entry;
            }
        }
        return null;
    }

    public Player addPlayer(ServerWebSocket socket) {
        Player player = new Player(socket);
        return playerMap.put(socket, player);
    }

    public Player removePlayer(ServerWebSocket socket) {
        if (playerMap.containsKey(socket)) {
            return playerMap.remove(socket);
        } else {
            return null;
        }
    }

    /**
     * @return the Resources JSON data, as a Map
     * @throws ParseException
     */
    public Map getGameResources() throws ParseException {
        return Converters.jsonStringToMap(gameResourcesString);
    }

    /**
     * @return the Settings JSON data, as a Map
     * @throws ParseException
     */
    public Map getGameSettings() throws ParseException {
        return Converters.jsonStringToMap(gameSettingsString);
    }

    /**
     * @return the Game Map Data JSON data, as a Map
     * @throws ParseException
     */
    public Map getGameMapData() throws ParseException {
        return Converters.jsonStringToMap(gameMapDataString);
    }

    /**
     * @return the Game Map Properties JSON data, as a Map
     * @throws ParseException
     */
    public Object getGameMapProperties() throws ParseException {
        return Converters.jsonStringToObject(gameMapPropertiesString);
    }

    /**
     * Sets the server into development mode
     */
    public void setToDevelopment() {
        this.development = true;
    }

    public boolean isDevelopment() {
        return development;
    }
}