/// <summary> /// Method called when the server sends a message that another player disconnected /// </summary> /// <param name="parts"></param> private void PlayerDisconnectResolve(string[] parts) { PlayerColor disconnectedPlayer = Enum.Parse <PlayerColor>(parts[1]); PlayerInfos.Remove(disconnectedPlayer); if (_playerOrder != null) { _playerOrder.Remove(disconnectedPlayer); } if (_orderManager != null) { _orderManager.Properties.Remove(disconnectedPlayer); PlacesChanged?.Invoke(this, EventArgs.Empty); } PlayerDisconnected?.Invoke(this, disconnectedPlayer); }
/// <summary> /// Method called when the server sends a message to signal that the building stage ended /// </summary> /// <param name="parts"></param> private void BuildingEndedResolve(string[] parts) { if (!IsReady) { _pingTimer.Stop(); _pingTimer.Dispose(); throw new OutOfSyncException(); } IsReady = false; foreach (PlayerInfo info in PlayerInfos.Values) { info.IsReady = false; } for (int i = 1; i < parts.Length; ++i) { _playerOrder.Add(Enum.Parse <PlayerColor>(parts[i])); } _orderManager = new PlayerOrderManager(_playerOrder, GameStage); _orderManager.PlacesChanged += (sender, e) => { PlacesChanged?.Invoke(this, e); }; _orderManager.PlayerCrashed += (sender, e) => { if (e == Player) { Crashed = true; } PlayerInfos[e].IsFlying = false; PlayerCrashed?.Invoke(this, e); }; _serverStage = ServerStage.PastBuild; BuildingEnded?.Invoke(this, EventArgs.Empty); }
public void AddDistance(PlayerColor player, int value) { if (!Properties.ContainsKey(player)) { return; } _sem.WaitOne(); //if the player moves 0 distance, they crash if (value == 0) { //remove the player from the grid _playerPlaces[_properties[player].PlaceValue] = null; //OnPlayerCrashed removes the player from the properties list OnPlayerCrashed(player); _sem.Release(); return; } bool negative = value < 0; _playerPlaces[_properties[player].PlaceValue] = null; int nextPlace = _properties[player].PlaceValue; for (int i = 0; i < Math.Abs(value); ++i) { nextPlace += negative ? -1 : 1; if (nextPlace >= LapSize) { nextPlace = 0; ++_properties[player].LapCount; } else if (nextPlace < 0) { nextPlace = LapSize - 1; --_properties[player].LapCount; } while (_playerPlaces[nextPlace] != null) { PlaceProperty occupier = _playerPlaces[nextPlace]; //if the moving player steps forwards and ups a lap on someone else if (occupier.LapCount < _properties[player].LapCount && !negative) { //also remove the other player from the grid _playerPlaces[nextPlace] = null; OnPlayerCrashed(occupier.Player); } //if the moving player steps backwards and gets upped a lap on else if (occupier.LapCount > _properties[player].LapCount && negative) { OnPlayerCrashed(player); _sem.Release(); return; } nextPlace += negative ? -1 : 1; if (nextPlace >= LapSize) { nextPlace = 0; ++_properties[player].LapCount; } else if (nextPlace < 0) { nextPlace = LapSize - 1; --_properties[player].LapCount; } } } _properties[player].PlaceValue = nextPlace; _playerPlaces[nextPlace] = _properties[player]; PlacesChanged?.Invoke(this, EventArgs.Empty); _sem.Release(); }
private void OnPlayerCrashed(PlayerColor player) { Properties.Remove(player); PlayerCrashed?.Invoke(this, player); PlacesChanged?.Invoke(this, EventArgs.Empty); }