예제 #1
0
        private void OnHelloServer(ushort id, HelloServer helloServer)
        {
            Logger.Get().Info(this, $"Received HelloServer data from ID {id}");

            // Start by sending the new client the current Server Settings
            _netServer.GetUpdateManagerForClient(id).UpdateGameSettings(_gameSettings);

            // Create new player data object
            var playerData = new ServerPlayerData(
                helloServer.Username,
                helloServer.SceneName,
                helloServer.Position,
                helloServer.Scale,
                helloServer.AnimationClipId
                );

            // Store data in mapping
            _playerData[id] = playerData;

            foreach (var idPlayerDataPair in _playerData.GetCopy())
            {
                if (idPlayerDataPair.Key == id)
                {
                    continue;
                }

                _netServer.GetUpdateManagerForClient(idPlayerDataPair.Key).AddPlayerConnectData(
                    id,
                    helloServer.Username
                    );
            }

            OnClientEnterScene(id, playerData);
        }
예제 #2
0
        private void OnClientEnterScene(ushort id, ServerPlayerData playerData)
        {
            var alreadyPlayersInScene = false;

            foreach (var idPlayerDataPair in _playerData.GetCopy())
            {
                // Skip source player
                if (idPlayerDataPair.Key == id)
                {
                    continue;
                }

                var otherPlayerData = idPlayerDataPair.Value;

                // Send the packet to all clients on the new scene
                // to indicate that this client has entered their scene
                if (otherPlayerData.CurrentScene.Equals(playerData.CurrentScene))
                {
                    Logger.Get().Info(this, $"Sending EnterScene data to {idPlayerDataPair.Key}");

                    _netServer.GetUpdateManagerForClient(idPlayerDataPair.Key).AddPlayerEnterSceneData(
                        id,
                        playerData.Username,
                        playerData.LastPosition,
                        playerData.LastScale,
                        playerData.Team,
                        playerData.SkinId,
                        playerData.LastAnimationClip
                        );

                    Logger.Get().Info(this, $"Sending that {idPlayerDataPair.Key} is already in scene to {id}");

                    alreadyPlayersInScene = true;

                    // Also send a packet to the client that switched scenes,
                    // notifying that these players are already in this new scene.
                    _netServer.GetUpdateManagerForClient(id).AddPlayerAlreadyInSceneData(
                        idPlayerDataPair.Key,
                        otherPlayerData.Username,
                        otherPlayerData.LastPosition,
                        otherPlayerData.LastScale,
                        otherPlayerData.Team,
                        otherPlayerData.SkinId,
                        otherPlayerData.LastAnimationClip
                        );
                }
            }

            if (!alreadyPlayersInScene)
            {
                _netServer.GetUpdateManagerForClient(id).SetAlreadyInSceneHost();
            }
        }
예제 #3
0
        private void OnHelloServer(ushort id, HelloServerPacket packet)
        {
            Logger.Info(this, $"Received Hello packet from ID {id}");

            // Start by sending the new client the current Server Settings
            var settingsUpdatePacket = new GameSettingsUpdatePacket {
                GameSettings = _gameSettings
            };

            settingsUpdatePacket.CreatePacket();

            _netServer.SendTcp(id, settingsUpdatePacket);

            // Read username from packet
            var username = packet.Username;

            // Read scene name from packet
            var sceneName = packet.SceneName;

            // Read the rest of the data, since we know that we have it
            var position    = packet.Position;
            var scale       = packet.Scale;
            var currentClip = packet.AnimationClipId;

            // Create new player data object
            var playerData = new ServerPlayerData(
                username,
                sceneName,
                position,
                scale,
                currentClip
                );

            // Store data in mapping
            _playerData[id] = playerData;

            // Create PlayerConnect packet
            var playerConnectPacket = new ClientPlayerConnectPacket {
                Id       = id,
                Username = username
            };

            playerConnectPacket.CreatePacket();

            // Create PlayerEnterScene packet
            var enterScenePacket = new ClientPlayerEnterScenePacket {
                ScenePlayerData = new ScenePlayerData {
                    Id              = id,
                    Username        = playerData.Username,
                    Position        = position,
                    Scale           = scale,
                    Team            = playerData.Team,
                    AnimationClipId = currentClip,
                }
            };

            enterScenePacket.CreatePacket();

            // Create the AlreadyInScene packet
            var alreadyInScenePacket = new ClientAlreadyInScenePacket();

            // Loop over all other clients and skip over the client that just connected
            foreach (var idPlayerDataPair in _playerData.GetCopy())
            {
                if (idPlayerDataPair.Key == id)
                {
                    continue;
                }

                var otherPlayerData = idPlayerDataPair.Value;

                // Send the PlayerConnect packet to all other clients
                _netServer.SendTcp(idPlayerDataPair.Key, playerConnectPacket);

                // Send the EnterScene packet only to clients in the same scene
                if (otherPlayerData.CurrentScene.Equals(sceneName))
                {
                    Logger.Info(this, $"Sending a EnterScene packet to ID: {idPlayerDataPair.Key}");
                    _netServer.SendTcp(idPlayerDataPair.Key, enterScenePacket);

                    // Also send the source client that this player is in their scene,
                    // by adding a new player data instance to the AlreadyInScene packet
                    alreadyInScenePacket.ScenePlayerData.Add(new ScenePlayerData {
                        Id              = idPlayerDataPair.Key,
                        Username        = otherPlayerData.Username,
                        Position        = otherPlayerData.LastPosition,
                        Scale           = otherPlayerData.LastScale,
                        Team            = otherPlayerData.Team,
                        AnimationClipId = otherPlayerData.LastAnimationClip,
                    });
                }
            }

            // Now we send the AlreadyInScene packet after it is completely populated,
            // or not in case of no players in the scene already
            _netServer.SendTcp(id, alreadyInScenePacket.CreatePacket());
        }