Beispiel #1
0
        public void RecieveClientMessage(ServerConnection client, BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.GI_ClientSend_PlayerUpdate:
            {
                var packet = (PlayerUpdatePacket)recievedPacket;
                packet.PlayerID           = client.ID;
                _playerUpdates[client.ID] = packet;
                break;
            }

            case MessageType.GI_ClientSend_PlayerFired:
            {
                var packet = (PlayerFiredPacket)recievedPacket;
                packet.PlayerID = client.ID;

                var timeDifference = (packet.SendDate - DateTime.UtcNow).TotalSeconds;

                var laser = _playerLasers[client.ID].FireLaserServer(packet.TotalGameTime, (float)timeDifference, new Vector2(packet.XPosition, packet.YPosition), packet.Rotation, packet.LaserID, packet.PlayerID);
                if (laser != null)
                {
                    for (int i = 0; i < ComponentClients.Count; i++)
                    {
                        ComponentClients[i].SendPacketToClient(packet, MessageType.GI_ServerSend_RemotePlayerFired);
                    }
                }
                break;
            }
            }
        }
Beispiel #2
0
        public void RecieveClientMessage(ServerConnection client, BasePacket packet)
        {
            switch ((MessageType)packet.MessageType)
            {
            case MessageType.LB_ClientSend_RematchReady:
            {
                _clientReadyStatus[client] = true;
                break;
            }

            case MessageType.LB_ClientSend_RematchUnready:
            {
                _clientReadyStatus[client] = false;
                break;
            }

            case MessageType.LB_ClientSend_ReturnToWaitingRoom:
            {
                client.RemoveServerComponent(this);
                RemoveClient(client);
                OnClientLeaveGameRoom(client, _gameRoomID);
                break;
            }
            }

            UpdateLobbyState();
            CheckRestartGame();
        }
        private void ClientMessenger_OnRecievedPlayerFiredPacket(BasePacket packet)
        {
            PlayerFiredPacket playerUpdate = (PlayerFiredPacket)packet;

            if (playerUpdate.PlayerID != _localPlayer.NetworkID) // Local laser has already been shot so don't shoot it again
            {
                _laserManager.FireRemoteLaserClient(new Vector2(playerUpdate.XPosition, playerUpdate.YPosition), playerUpdate.Rotation, playerUpdate.PlayerID, playerUpdate.SendDate, playerUpdate.LaserID, _playerColours[playerUpdate.PlayerID]);
            }
        }
        private void ClientMessenger_OnGameOver(BasePacket packet)
        {
            LeaderboardPacket leaderBoard = (LeaderboardPacket)packet;

            var scene = new LeaderboardScene(_client, _width, _height, leaderBoard);

            scene.Initalise(_contentManager, _graphicsDevice);
            SetNewScene(scene);
        }
        private void ClientMessenger_OnLoadNewGame(BasePacket packet)
        {
            GameInstanceInformation gameInstance = (GameInstanceInformation)packet;

            var newScene = new GameScene(_width, _height, gameInstance.PlayerCount, gameInstance.PlayerIDs, gameInstance.PlayerNames, gameInstance.PlayerColours, gameInstance.LocalPlayerID, _client);

            newScene.Initalise(_contentManager, _graphicsDevice);
            SetNewScene(newScene);
        }
Beispiel #6
0
        public BasePacket MakeBasePacket()
        {
            BasePacket packet = new BasePacket
            {
                SendDate = DateTime.UtcNow
            };

            return(packet);
        }
        private void ClientMessenger_OnPlayerDefeatedPacket(BasePacket packet)
        {
            PlayerDefeatedPacket playerDefeatedPacket = (PlayerDefeatedPacket)packet;

            _GUI.UpdatePlayerScore(playerDefeatedPacket.CollidedPlayerID, playerDefeatedPacket.CollidedPlayerNewScore);

            _laserManager.DeactivateLaser(playerDefeatedPacket.CollidedLaserID);

            var player = _players[playerDefeatedPacket.CollidedPlayerID];

            _explosionManager.AddExplosion(player.Position);
        }
        private void OnGameCompleted(BasePacket packet)
        {
            _gameInstance.OnGameCompleted -= OnGameCompleted;

            roomState = GameRoomState.Leaderboards;
            OnRoomStateChanged();

            for (int i = 0; i < ComponentClients.Count; i++)
            {
                ComponentClients[i].RemoveServerComponent(_gameInstance);
            }

            _gameLeaderboard = new ServerLeaderboard(ComponentClients, ID);
        }
        public void RecieveClientMessage(ServerConnection client, BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.WR_ClientRequest_LeaveRoom:
            {
                StringPacket leavePacket = (StringPacket)recievedPacket;
                client.SendPacketToClient(NetworkPacketFactory.Instance.MakeStringPacket(leavePacket.String), MessageType.WR_ServerResponse_SuccessLeaveRoom);
                RemoveClient(client);
                Logger.Instance.Info(client.Name + " left " + RoomName);
                break;
            }

            case MessageType.GR_ClientRequest_Ready:
            {
                // Can this request fail?
                client.SendPacketToClient(new BasePacket(), MessageType.GR_ServerResponse_SuccessReady);
                _clientReadyStatus[client] = true;

                Logger.Instance.Info(client.Name + " readied up in " + RoomName + ". (" + GetReadyCount() + "/" + ComponentClients.Count + ") players ready");

                if (GetReadyCount() == ComponentClients.Count)
                {
                    // TODO: Introduce a countdown here

                    LaunchGameInstance();
                    Logger.Instance.Info(RoomName + " has started a game instance");
                }

                OnRoomStateChanged();

                break;
            }

            case MessageType.GR_ClientRequest_Unready:
            {
                client.SendPacketToClient(new BasePacket(), MessageType.GR_ServerResponse_SuccessUnready);
                _clientReadyStatus[client] = false;

                Logger.Instance.Info(client.Name + " unreadied in " + RoomName + ". (" + GetReadyCount() + "/" + ComponentClients.Count + ") players ready");

                OnRoomStateChanged();
                break;
            }
            }
        }
        public void RecieveServerResponse(BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.WR_ServerSend_WaitingRoomFullInfo:
                var waitingRooms = (WaitingRoomInformation)recievedPacket;
                Client_OnWaitingRoomInformationRecieved(waitingRooms);
                break;

            case MessageType.WR_ServerResponse_FailJoinRoom:
                Console.WriteLine("FAILED TO JOIN ROOM");
                break;

            case MessageType.WR_ServerResponse_FailCreateRoom:
                Console.WriteLine("FAILED TO CREATE ROOM");
                break;

            case MessageType.WR_ServerResponse_SuccessJoinRoom:
            {
                StringPacket lobbyID = (StringPacket)recievedPacket;
                ClientMessageReciever_OnRoomSuccessfullyJoined(lobbyID.String);
                break;
            }

            case MessageType.WR_ServerResponse_SuccessLeaveRoom:
            {
                StringPacket lobbyID = (StringPacket)recievedPacket;
                ClientMessageReciever_OnRoomSuccessfullyLeft(lobbyID.String);
                break;
            }

            case MessageType.GR_ServerResponse_SuccessReady:
            {
                ClientMessenger_OnRoomSuccessfullyReady();
                break;
            }

            case MessageType.GR_ServerResponse_SuccessUnready:
            {
                ClientMessenger_OnRoomSuccessfullyUnready();
                break;
            }
            }
        }
Beispiel #11
0
        public void RecieveClientMessage(ServerConnection client, BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.WR_ClientRequest_WaitingRoomInfo:
            {
                client.SendPacketToClient(GetWaitingRoomInformation(), MessageType.WR_ServerSend_WaitingRoomFullInfo);
                break;
            }

            case MessageType.WR_ClientRequest_CreateRoom:
            {
                if (_activeRooms.Count < Server.MAX_ROOMS)
                {
                    Logger.Instance.Info("New Game Room created by " + client.Name);
                    CreateNewRoom(client.Name + "'s Room");
                    GameRoom_OnRoomStateChanged();
                }
                else
                {
                    client.SendPacketToClient(new BasePacket(), MessageType.WR_ServerResponse_FailCreateRoom);
                }
                break;
            }

            case MessageType.WR_ClientRequest_JoinRoom:
            {
                StringPacket joinPacket = (StringPacket)recievedPacket;
                GameRoom     joinedRoom = GetGameRoomFromID(joinPacket.String);
                if (joinedRoom.ComponentClients.Count < MAX_PEOPLE_PER_ROOM)
                {
                    client.SendPacketToClient(NetworkPacketFactory.Instance.MakeStringPacket(joinPacket.String), MessageType.WR_ServerResponse_SuccessJoinRoom);
                    joinedRoom.AddClientToRoom(client);
                    GameRoom_OnRoomStateChanged();
                    Logger.Instance.Info(client.Name + " joined " + joinedRoom.RoomName);
                }
                else
                {
                    client.SendPacketToClient(new BasePacket(), MessageType.WR_ServerResponse_FailJoinRoom);
                }
                break;
            }
            }
        }
        public void RecieveServerResponse(BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.LB_ServerSend_UpdateLeaderboard:
            {
                var packet = (LeaderboardUpdatePacket)recievedPacket;
                _isReadyRematch        = packet.IsClientReady;
                _readyRematchCount     = packet.PlayerReadyCount;
                _playersLeftInInstance = packet.PlayerCount;

                if (_playersLeftInInstance == 1)
                {
                    _sceneState = LeaderboardSceneState.WaitingForExit;
                }

                ReformatButtons();
                break;
            }
            }
        }
        public void RecieveServerResponse(BasePacket recievedPacket)
        {
            switch ((MessageType)recievedPacket.MessageType)
            {
            case MessageType.GI_ServerSend_UpdateRemotePlayer:
            {
                var playerPacket = (PlayerUpdatePacket)recievedPacket;
                OnRecievedPlayerUpdatePacket(playerPacket);
                break;
            }

            case MessageType.GI_ServerSend_RemotePlayerFired:
            {
                var playerPacket = (PlayerFiredPacket)recievedPacket;
                ClientMessenger_OnRecievedPlayerFiredPacket(playerPacket);
                break;
            }

            case MessageType.GI_ServerSend_EnemySpawn:
            {
                var enemyPacket = (EnemySpawnedPacket)recievedPacket;
                ClientMessenger_OnEnemySpawnedPacket(enemyPacket);
                break;
            }

            case MessageType.GI_ServerSend_EnemyDefeated:
            {
                var enemyPacket = (EnemyDefeatedPacket)recievedPacket;
                ClientMessenger_OnEnemyDefeatedPacket(enemyPacket);
                break;
            }

            case MessageType.GI_ServerSend_PlayerDefeated:
            {
                var enemyPacket = (PlayerDefeatedPacket)recievedPacket;
                ClientMessenger_OnPlayerDefeatedPacket(enemyPacket);
                break;
            }
            }
        }
 public void SendMessageToTheServer(BasePacket packet, MessageType messageType)
 {
     Client.SendMessageToServer(packet, messageType);
 }
        private void ClientMessenger_OnEnemySpawnedPacket(BasePacket packet)
        {
            EnemySpawnedPacket enemySpawn = (EnemySpawnedPacket)packet;

            _enemyManager.AddEnemy(new Vector2(enemySpawn.XPosition, enemySpawn.YPosition), enemySpawn.EnemyID);
        }
        private void OnRecievedPlayerUpdatePacket(BasePacket packet)
        {
            PlayerUpdatePacket serverUpdate = (PlayerUpdatePacket)packet;

            if (Application.APPLY_SERVER_RECONCILLIATION &&
                serverUpdate.PlayerID == _localPlayer.NetworkID && serverUpdate.SequenceNumber >= 0 &&
                _updatePackets.Count > 0)
            {
                PlayerUpdatePacket localUpdate = GetUpdateAtSequenceNumber(serverUpdate.SequenceNumber);

                if (localUpdate.XPosition != serverUpdate.XPosition ||
                    localUpdate.YPosition != serverUpdate.YPosition ||
                    localUpdate.Rotation != serverUpdate.Rotation ||
                    localUpdate.Speed != serverUpdate.Speed)
                {
                    // Create a new queue with 'serverUpdate' as the first update
                    var newQueue   = new Queue <PlayerUpdatePacket>();
                    var updateList = new List <PlayerUpdatePacket>();

                    PlayerUpdatePacket removedPacket = _updatePackets.Dequeue(); // Remove the first one which we are replacing with the serverUpdate

                    serverUpdate.DeltaTime = removedPacket.DeltaTime;
                    newQueue.Enqueue(serverUpdate);
                    updateList.Add(serverUpdate);

                    while (_updatePackets.Count > 0)
                    {
                        PlayerUpdatePacket updatePacket = _updatePackets.Dequeue();
                        newQueue.Enqueue(updatePacket);
                        updateList.Add(updatePacket);
                    }

                    _updatePackets = newQueue; // Set the new queue

                    if (updateList.Count == 0)
                    {
                        return;
                    }

                    _localPlayer.SetPlayerState(updateList[0]);
                    _localPlayer.Update(updateList[0].DeltaTime);

                    if (updateList.Count == 1)
                    {
                        return;
                    }

                    // Now we must perform the previous inputs again
                    for (int i = 1; i < updateList.Count; i++)
                    {
                        _localPlayer.ApplyInputToPlayer(updateList[i].Input, updateList[i].DeltaTime);
                        _localPlayer.Update(updateList[i].DeltaTime);
                    }
                }
            }
            else
            {
                RemotePlayer remotePlayer = _players[serverUpdate.PlayerID] as RemotePlayer;
                remotePlayer.SetUpdatePacket(serverUpdate);
            }
        }