Beispiel #1
0
        /**
         * Called when the game settings are updated, and need to be broadcast
         */
        public void OnUpdateGameSettings()
        {
            if (!_netServer.IsStarted)
            {
                return;
            }

            var settingsUpdatePacket = new GameSettingsUpdatePacket {
                GameSettings = _gameSettings
            };

            settingsUpdatePacket.CreatePacket();

            _netServer.BroadcastTcp(settingsUpdatePacket);
        }
Beispiel #2
0
        private void OnGameSettingsUpdated(GameSettingsUpdatePacket packet)
        {
            var pvpChanged           = false;
            var bodyDamageChanged    = false;
            var displayNamesChanged  = false;
            var alwaysShowMapChanged = false;
            var onlyCompassChanged   = false;
            var teamsChanged         = false;

            // Check whether the PvP state changed
            if (_gameSettings.IsPvpEnabled != packet.GameSettings.IsPvpEnabled)
            {
                pvpChanged = true;

                var message = $"PvP is now {(packet.GameSettings.IsPvpEnabled ? "enabled" : "disabled")}";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Check whether the body damage state changed
            if (_gameSettings.IsBodyDamageEnabled != packet.GameSettings.IsBodyDamageEnabled)
            {
                bodyDamageChanged = true;

                var message =
                    $"Body damage is now {(packet.GameSettings.IsBodyDamageEnabled ? "enabled" : "disabled")}";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Check whether the always show map icons state changed
            if (_gameSettings.AlwaysShowMapIcons != packet.GameSettings.AlwaysShowMapIcons)
            {
                alwaysShowMapChanged = true;

                var message =
                    $"Map icons are now{(packet.GameSettings.AlwaysShowMapIcons ? "" : " not")} always visible";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Check whether the wayward compass broadcast state changed
            if (_gameSettings.OnlyBroadcastMapIconWithWaywardCompass !=
                packet.GameSettings.OnlyBroadcastMapIconWithWaywardCompass)
            {
                onlyCompassChanged = true;

                var message =
                    $"Map icons are {(packet.GameSettings.OnlyBroadcastMapIconWithWaywardCompass ? "now only" : "not")} broadcast when wearing the Wayward Compass charm";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Check whether the display names setting changed
            if (_gameSettings.DisplayNames != packet.GameSettings.DisplayNames)
            {
                displayNamesChanged = true;

                var message = $"Names are {(packet.GameSettings.DisplayNames ? "now" : "no longer")} displayed";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Check whether the teams enabled setting changed
            if (_gameSettings.TeamsEnabled != packet.GameSettings.TeamsEnabled)
            {
                teamsChanged = true;

                var message = $"Team are {(packet.GameSettings.TeamsEnabled ? "now" : "no longer")} enabled";

                UI.UIManager.InfoBox.AddMessage(message);
                Logger.Info(this, message);
            }

            // Update the settings so callbacks can read updated values
            _gameSettings.SetAllProperties(packet.GameSettings);

            // Only update the player manager if the either PvP or body damage have been changed
            if (pvpChanged || bodyDamageChanged || displayNamesChanged)
            {
                _playerManager.OnGameSettingsUpdated(pvpChanged || bodyDamageChanged, displayNamesChanged);
            }

            if (alwaysShowMapChanged || onlyCompassChanged)
            {
                if (!_gameSettings.AlwaysShowMapIcons && !_gameSettings.OnlyBroadcastMapIconWithWaywardCompass)
                {
                    _mapManager.RemoveAllIcons();
                }
            }

            // If the teams setting changed, we invoke the registered event handler if they exist
            if (teamsChanged)
            {
                // If the team setting was disabled, we reset all teams
                if (!_gameSettings.TeamsEnabled)
                {
                    _playerManager.ResetAllTeams();
                }

                TeamSettingChangeEvent?.Invoke();
            }
        }
Beispiel #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());
        }