/// <summary> /// To be called when a user performs an illegal action on this computer. /// </summary> public void PerformIllegalAction() { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed if (this != World.World.GetInstance().Player.PlayerComp) { ActiveTracer.GetInstance().StartTrace(TraceTime); // TODO: base tracetime on computer difficulty or something } #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed OnIllegalAction?.Invoke(this, new IllegalActionEventArgs(World.World.GetInstance().CurrentGameTime)); }
private void OnReceived(string json) { var packet = JsonNet.Deserialize <PacketS2C>(json); _logger.LogTrace($"Received packet: {packet}"); switch (packet.Type) { case Packet.PacketTypeS2C.AuthResult: { var state = packet.GetPayload <AuthCompleted>(); OnLoggedIn?.Invoke(this, new LoggedInEventArgs(this, state.Motd, state.GameModes)); } break; case Packet.PacketTypeS2C.GameAction: { var gameAction = packet.GetPayload <LiveGameAction>(); if (_games.ContainsKey(gameAction.GameIdentifier)) { var game = _games[gameAction.GameIdentifier]; var ownerOfEntity = game.GetOwnerOfEntity(gameAction.ControlledEntityIdentifier); if (ownerOfEntity == null) { _logger.LogError($"Corrupted game state - cannot find entity: {gameAction.ControlledEntityIdentifier} in game {gameAction.GameIdentifier}"); Send(Packet.PacketTypeC2S.ForceResync, gameAction.GameIdentifier); } else { _games[gameAction.GameIdentifier].AddActionWithoutNetworking(ownerOfEntity, gameAction); } } else { _logger.LogError($"Action in unknown game: {gameAction.GameIdentifier}"); Send(Packet.PacketTypeC2S.ForceResync, gameAction.GameIdentifier); } } break; case Packet.PacketTypeS2C.IllegalAction: { var illegalAction = packet.GetPayload <IllegalAction>(); if (!illegalAction.InsideGame) { OnIllegalAction?.Invoke(this, new MessageEventArgs(illegalAction.Message)); } else if (illegalAction.GameIdentifier != null && _games.ContainsKey(illegalAction.GameIdentifier.Value)) { _games[illegalAction.GameIdentifier.Value].HandleServerIllegalAction(illegalAction.Message); } else if (illegalAction.GameIdentifier != null) { _logger.LogError($"Server refused action in unknown game: {illegalAction.GameIdentifier}"); Send(Packet.PacketTypeC2S.ForceResync, illegalAction.GameIdentifier); } else { _logger.LogWarning("Cannot handle server packet (Claims illegal action in game, but does not specify the game identifier)"); } } break; case Packet.PacketTypeS2C.NextRound: { var gameIdentifier = packet.GetPayload <long>(); if (_games.ContainsKey(gameIdentifier)) { _games[gameIdentifier].DoNextTurn(); } else { _logger.LogError($"Server sent turn end in unknown game: {gameIdentifier}"); Send(Packet.PacketTypeC2S.ForceResync, gameIdentifier); } } break; case Packet.PacketTypeS2C.GameState: { var state = packet.GetPayload <GameState>(); _games[state.GameIdentifier] = state.State; state.State.SetGameDetails(state.YourIdentifier, state.GameIdentifier, this); OnJoinedGameInternal?.Invoke(this, new GameEventArgs(state.State)); state.State.RequestClientActions(); } break; case Packet.PacketTypeS2C.GameEnd: { var end = packet.GetPayload <GameEnd>(); if (_games.ContainsKey(end.GameIdentifier)) { var game = _games[end.GameIdentifier]; _games.Remove(end.GameIdentifier); OnLeftGameInternal?.Invoke(this, new GameEventArgs(game)); } } break; case Packet.PacketTypeS2C.QueueState: var args = new CountChangedEventArgs(packet.GetPayload <int>()); _lastQueueCount = args.Count; OnServersideQueueCountChangedInternal?.Invoke(this, args); break; default: throw new ArgumentOutOfRangeException(); } }