コード例 #1
0
        /// <summary>
        /// Creates a new player on the server
        /// </summary>
        /// <param name="i_player">Player name</param>
        /// <param name="i_pass">Player password</param>
        /// <returns>True if player was able to be created, false otherwise</returns>
        public Task <ServerResponseInfo <bool, SqlException> > CreatePlayer(string i_player, string i_pass)
        {
            ServerResponseInfo <bool, SqlException> res = new ServerResponseInfo <bool, SqlException>();

            try
            {
                //Connects to the SQL Server and close when exiting
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    //Opens SQL connection
                    connection.Open();
                    //Creates query string
                    string query = "INSERT INTO Players VALUES('" + i_player + "','" + i_pass + "',0)";
                    //Creates SQL Command using query and connection
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        //Executes command
                        command.ExecuteNonQuery();
                        //Returns true
                        res.info = true;
                        return(Task.FromResult(res));
                    }
                }
            }
            //If SQL Exception caught
            catch (SqlException e)
            {
                //Returns false and exception
                res.info      = false;
                res.exception = e;
                return(Task.FromResult(res));
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets player position
        /// </summary>
        /// <param name="i_player">Player name</param>
        /// <returns>Player position vector</returns>
        public async Task <ServerResponseInfo <int[]> > GetPlayerPosAsync(string i_player)
        {
            ServerResponseInfo <int[]> res = new ServerResponseInfo <int[]>();
            //Gets "gamesession" from StateManager
            GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession");

            //Returns player position
            res.info = gameSession.GetPlayerPos(i_player);
            return(res);
        }
コード例 #3
0
        /// <summary>
        /// Tries to connect player to GameSession
        /// </summary>
        /// <param name="i_player">Player name</param>
        /// <returns>True if player could be connected, false if game is full or started and false with exception if game was removed (or other reasons for exception throw)</returns>
        public async Task <ServerResponseInfo <bool, Exception> > ConnectPlayerAsync(string i_player, byte[] i_address)
        {
            ServerResponseInfo <bool, Exception> res = new ServerResponseInfo <bool, Exception>();

            try
            {
                //Get GameSession from StateManager
                GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession");

                //Adds player to game if posible
                if (gameSession.AddPlayer(i_player, i_address))
                {
                    //If first player in lobby
                    if (gameSession.playerCount == 1)
                    {
                        await this.UnregisterReminderAsync(GetReminder("RemoveIfEmpty"));

                        //Registers LobyCheck reminder
                        await this.RegisterReminderAsync("LobbyCheck", null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
                    }
                    //Saves "gamesession" state
                    await this.StateManager.SetStateAsync("gamesession", gameSession);

                    ActorEventSource.Current.Message(ServiceUri.AbsoluteUri);
                    ILoginService login = ServiceProxy.Create <ILoginService>(new Uri(ServiceUri.AbsoluteUri.Replace("GameManagerActorService", "LoginService")));
                    //Adds player to server (SQL Register)
                    await login.AddPlayerAsync(Id.ToString());

                    //Returns true
                    res.info = true;
                    return(res);
                }
                //Saves "gamesession" state
                await this.StateManager.SetStateAsync("gamesession", gameSession);

                //Returns false (player couldn't be added: game full or started)
                res.info = false;
                return(res);
            }
            //Catch exception if "gamesesion" state doesn't exist
            catch (Exception e)
            {
                //Returns false and exception (player couldn't be added: game removed)
                res.info      = false;
                res.exception = e;
                return(res);
            }
        }
コード例 #4
0
        /// <summary>
        /// Returns a list with all game session definitions on the server
        /// </summary>
        public Task <ServerResponseInfo <bool, SqlException, List <GameDefinition> > > GetGameList()
        {
            ServerResponseInfo <bool, SqlException, List <GameDefinition> > res = new ServerResponseInfo <bool, SqlException, List <GameDefinition> >();
            List <GameDefinition> gameList = new List <GameDefinition>();

            try
            {
                //Connects to the SQL Server and close when exiting
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    //Opens SQL connection
                    connection.Open();
                    //Creates query string
                    string query = "SELECT * FROM Games;";
                    //Creates SQL Command using query and connection
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        //Creates SQL reader and executes query
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            //While data to read
                            while (reader.Read())
                            {
                                //Creates new GameDefinition adds data and adds it to gameList
                                GameDefinition gameInfo = new GameDefinition();
                                gameInfo.id         = reader.GetString(0);
                                gameInfo.maxPlayers = reader.GetInt32(1);
                                gameInfo.players    = reader.GetInt32(2);
                                gameList.Add(gameInfo);
                            }
                        }
                    }
                }
                //Returns true and gameList
                res.info           = true;
                res.additionalInfo = gameList;
                return(Task.FromResult(res));
            }
            //If exception caught
            catch (SqlException e)
            {
                //Returns false ande exception
                res.info      = false;
                res.exception = e;
                return(Task.FromResult(res));
            }
        }
コード例 #5
0
        /// <summary>
        /// Tries to log in player to the server
        /// </summary>
        /// <param name="i_player">Player name</param>
        /// <param name="i_pass">Player password</param>
        /// <returns>True if player was able to be logged in, false otherwise</returns>
        public Task <ServerResponseInfo <bool, SqlException> > Login(string i_player, string i_pass)
        {
            ServerResponseInfo <bool, SqlException> res = new ServerResponseInfo <bool, SqlException>();

            try
            {
                //Connects to the SQL Server and close when exiting
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    //Opens SQL connection
                    connection.Open();
                    //Creates query string
                    string query = "SELECT Pass FROM Players WHERE Player = '" + i_player + "'";
                    //Creates SQL Command using query and connection
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        //Creates SQL reader and executes query
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            //If data to read
                            if (reader.Read())
                            {
                                //Returns if pass equals to pass read from SQL
                                res.info = reader.GetString(0).Equals(i_pass) ? true : false;
                                return(Task.FromResult(res));
                            }
                            else
                            {
                                //If no data (no player with name "i_player" in SQL Server) returns false
                                res.info = false;
                            }
                            return(Task.FromResult(res));
                        }
                    }
                }
            }
            //If SQL exception caught
            catch (SqlException e)
            {
                //Returns false
                res.info      = false;
                res.exception = e;
                return(Task.FromResult(res));
            }
        }
コード例 #6
0
        /// <summary>
        /// Manages player's radar, returns info to player and notifies other players about it
        /// </summary>
        /// <param name="i_player">Player that used radar</param>
        /// <returns>Map info for this player</returns>
        public async Task <ServerResponseInfo <bool, Exception, CellContent[][]> > RadarActivatedAsync(string i_player)
        {
            ServerResponseInfo <bool, Exception, CellContent[][]> response = new ServerResponseInfo <bool, Exception, CellContent[][]>();

            response.info = true;
            try
            {
                //Gets "gamesession" from StateManager
                GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession");

                //Gets radar result
                RadarResult result = gameSession.RadarActivated(i_player);
                //If player wasn't connected
                if (!result.success)
                {
                    response.info = false;
                }
                //Otherwise
                else
                {
                    //If player wasn't dead
                    if (result.mapInfo != null)
                    {
                        List <string> message = new List <string>();
                        message.Add("RadarUsed");
                        message.Add(gameSession.GetPlayerPos(i_player).SerializeObject());
                        foreach (string player in gameSession.playerList)
                        {
                            SocketClient socket = new SocketClient();
                            socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                        }
                        //Adds map info to response
                        response.additionalInfo = result.mapInfo;
                    }
                }
            }
            catch (Exception e)
            {
                response.info      = false;
                response.exception = e;
            }
            return(response);
        }
コード例 #7
0
        /// <summary>
        /// Creates a new game session on the server
        /// </summary>
        /// <param name="i_gameDef">Game session definition</param>
        /// <returns>True if game session was able to be created, false otherwise</returns>
        public async Task <ServerResponseInfo <bool, Exception> > CreateGameAsync(string i_gameId, int i_maxPlayers)
        {
            ServerResponseInfo <bool, Exception> res = new ServerResponseInfo <bool, Exception>();

            try
            {
                //Connects to the SQL Server and close when exiting
                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    //Opens SQL connection
                    connection.Open();
                    //Creates query string
                    string query = "INSERT INTO Games VALUES('" + i_gameId + "'," + i_maxPlayers + "," + 0 + ")";
                    //Creates SQL Command using query and connection
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        //Executes command
                        command.ExecuteNonQuery();
                    }
                }
                //Creates game actor
                IGameManagerActor actor = ActorProxy.Create <IGameManagerActor>(new ActorId(i_gameId));
                //Initializes game actor
                await actor.InitializeGameAsync(i_maxPlayers);

                //Returns true
                res.info = true;
                return(res);
            }
            //If exception caught
            catch (Exception e)
            {
                //REMOVE GAME

                //Returns false with exception
                res.info      = false;
                res.exception = e;
                return(res);
            }
        }
コード例 #8
0
        /// <summary>
        /// Manages player attack
        /// </summary>
        /// <param name="i_player">Player name</param>
        public async Task <ServerResponseInfo <bool, Exception> > PlayerAttacksAsync(string i_player)
        {
            ServerResponseInfo <bool, Exception> response = new ServerResponseInfo <bool, Exception>();

            response.info = true;
            try
            {
                //Gets "gamesession" from StateManager
                GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession");

                //Manages player attack
                AttackResult result = gameSession.PlayerAttacks(i_player, PLAYER_ATTACK_RATE);
                //If player wasn't connected
                if (!result.success)
                {
                    response.info = false;
                }
                //Otherwise
                else
                {
                    List <string> message = new List <string>();
                    message.Add("BombHits");
                    message.Add(result.hitPoints.SerializeObject());
                    foreach (string player in gameSession.playerList)
                    {
                        SocketClient socket = new SocketClient();
                        socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                    }
                    //If there are players killed by the attack
                    if (result.killedPlayersDict.Count > 0)
                    {
                        //For each player
                        foreach (string killedPlayerId in result.killedPlayersDict.Keys)
                        {
                            message = new List <string>();
                            message.Add("PlayerKilled");
                            message.Add(killedPlayerId.SerializeObject());
                            message.Add(i_player.SerializeObject());
                            message.Add(result.killedPlayersDict[killedPlayerId].SerializeObject());
                            message.Add(DeathReason.PlayerHit.SerializeObject());
                            foreach (string player in gameSession.playerList)
                            {
                                SocketClient socket = new SocketClient();
                                socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                            }
                        }
                    }
                    //Saves "gamesession" state
                    await this.StateManager.SetStateAsync("gamesession", gameSession);

                    //If there's only one player alive
                    if (gameSession.AlivePlayers().Count == 1)
                    {
                        try
                        {
                            await this.UnregisterReminderAsync(GetReminder("TurretAim"));
                        }
                        catch (Exception e)
                        { }
                        try
                        {
                            await this.UnregisterReminderAsync(GetReminder("TurretShot"));
                        }
                        catch (Exception e)
                        { }
                        message = new List <string>();
                        message.Add("GameFinished");
                        message.Add(gameSession.AlivePlayers()[0].SerializeObject());
                        foreach (string player in gameSession.playerList)
                        {
                            SocketClient socket = new SocketClient();
                            socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                        }
                        string[] playerList = new string[gameSession.playerList.Count];
                        gameSession.playerList.CopyTo(playerList);
                        foreach (string player in playerList)
                        {
                            gameSession.RemovePlayer(player);
                            ILoginService login = ServiceProxy.Create <ILoginService>(new Uri(ServiceUri.AbsoluteUri.Replace("GameManagerActorService", "LoginService")));
                            await login.RemovePlayerAsync(Id.ToString());
                        }
                        InitializeGameAsync(gameSession.maxPlayers).Wait();
                    }
                }
            }
            catch (Exception e)
            {
                response.info      = false;
                response.exception = e;
            }
            return(response);
        }
コード例 #9
0
        /// <summary>
        /// Moves player
        /// </summary>
        /// <param name="i_dir">Movement vector</param>
        /// <param name="i_player">Player name</param>
        public async Task <ServerResponseInfo <bool, Exception> > PlayerMovesAsync(int[] i_dir, string i_player)
        {
            ServerResponseInfo <bool, Exception> response = new ServerResponseInfo <bool, Exception>();

            response.info = true;
            try
            {
                //Gets "gamesession" from StateManager
                GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession");

                //Moves player
                MovementResult result = gameSession.MovePlayer(i_dir, i_player);
                //If player wasn't connected
                if (result.type.Equals(MovementResultType.PlayerNotConnected))
                {
                    response.info = false;
                    //Saves "gamesession" state
                    await this.StateManager.SetStateAsync("gamesession", gameSession);
                }
                //If something happened
                else if (!result.type.Equals(MovementResultType.Nothing))
                {
                    //If player died
                    if (result.type.Equals(MovementResultType.PlayerDied))
                    {
                        List <string> message = new List <string>();
                        message.Add("PlayerDead");
                        message.Add(i_player.SerializeObject());
                        message.Add(result.playerPos.SerializeObject());
                        message.Add(DeathReason.Hole.SerializeObject());
                        foreach (string player in gameSession.playerList)
                        {
                            SocketClient socket = new SocketClient();
                            socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                        }
                    }
                    //If player killed other player
                    else
                    {
                        List <string> message = new List <string>();
                        message.Add("PlayerKilled");
                        message.Add(result.killedPlayer.SerializeObject());
                        message.Add(i_player.SerializeObject());
                        message.Add(result.playerPos.SerializeObject());
                        message.Add(DeathReason.PlayerSmash.SerializeObject());
                        foreach (string player in gameSession.playerList)
                        {
                            SocketClient socket = new SocketClient();
                            socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                        }
                    }
                    //If there's only one player alive
                    if (gameSession.AlivePlayers().Count == 1)
                    {
                        try
                        {
                            await this.UnregisterReminderAsync(GetReminder("TurretAim"));
                        }
                        catch (Exception e)
                        { }
                        try
                        {
                            await this.UnregisterReminderAsync(GetReminder("TurretShot"));
                        }
                        catch (Exception e)
                        { }
                        List <string> message = new List <string>();
                        message.Add("GameFinished");
                        message.Add(gameSession.AlivePlayers()[0].SerializeObject());
                        foreach (string player in gameSession.playerList)
                        {
                            SocketClient socket = new SocketClient();
                            socket.StartGameSessionClient(gameSession.GetPlayerAddress(player), message.SerializeObject() + "<EOF>");
                        }
                        string[] playerList = new string[gameSession.playerList.Count];
                        gameSession.playerList.CopyTo(playerList);
                        foreach (string player in playerList)
                        {
                            gameSession.RemovePlayer(player);
                            ILoginService login = ServiceProxy.Create <ILoginService>(new Uri(ServiceUri.AbsoluteUri.Replace("GameManagerActorService", "LoginService")));
                            await login.RemovePlayerAsync(Id.ToString());
                        }
                        InitializeGameAsync(gameSession.maxPlayers).Wait();
                    }
                    else
                    {
                        //Saves "gamesession" state
                        await this.StateManager.SetStateAsync("gamesession", gameSession);
                    }
                }
            }
            catch (Exception e)
            {
                response.info      = false;
                response.exception = e;
            }
            return(response);
        }