private void OnDisconnect(object sender, ConnectEventArgs e) { RegisteredGames.RemoveGameMastersGames(e.Handler); // if it was a player, tell gm about it try { ulong playerId = Clients.First(pair => pair.Value == e.Handler).Key; ConsoleDebug.Message($"ID: {playerId}"); IEnumerable <IGame> abandonedGames = RegisteredGames .Where(game => game.Players.Any(player => player.Id == playerId)); foreach (var game in abandonedGames) { ConsoleDebug.Message($"Informing game master of game {game.Id} about disconnected player, id: {playerId}"); InformGameMasterAboutDisconnectedPlayer(game.GameMaster, playerId); // remove player id occurrence game.Players.Remove(game.Players.First(player => playerId == player.Id)); } } catch (Exception exception) { // ignore: it was not a player Console.WriteLine(exception); } }
private void OnMessageReceive(object sender, MessageRecieveEventArgs eventArgs) { var socket = eventArgs.Handler as Socket; if (eventArgs.Message.Length > 0) //the message is not the keepalive packet { ConsoleDebug.Message("New message from: " + socket.GetRemoteAddress() + "\n" + eventArgs.Message); messageHandler.HandleMessage((dynamic)XmlMessageConverter.ToObject(eventArgs.Message)); } }
private void OnMessageReceive(object sender, MessageRecieveEventArgs eventArgs) { var socket = eventArgs.Handler as Socket; if (eventArgs.Message.Length > 0) //the message is not the keepalive packet { ConsoleDebug.Message("New message from:" + socket.GetRemoteAddress() + "\n" + eventArgs.Message); messageHandler.PrintBoard(); try { messageHandler.HandleMessage((dynamic)XmlMessageConverter.ToObject(eventArgs.Message), socket); } catch (Exception e) { Console.WriteLine(e.ToString()); } } }
public void HandleMessage(Data message) { if (message.PlayerLocation != null) { Player.game.Location = message.PlayerLocation; ConsoleDebug.Message($"My location: ({message.PlayerLocation.x}, {message.PlayerLocation.y})"); } if (message.TaskFields != null) { Common.SchemaWrapper.TaskField[] taskFields = new Common.SchemaWrapper.TaskField[message.TaskFields.Length]; for (int i = 0; i < taskFields.Length; i++) taskFields[i] = new Common.SchemaWrapper.TaskField(message.TaskFields[i]); Player.game.UpdateFields(taskFields); ; } if (message.GoalFields != null) { Common.SchemaWrapper.GoalField[] goalFields = new Common.SchemaWrapper.GoalField[message.GoalFields.Length]; for (int i = 0; i < goalFields.Length; i++) goalFields[i] = new Common.SchemaWrapper.GoalField(message.GoalFields[i]); Player.game.UpdateFields(goalFields); } if (message.Pieces != null) { foreach (Piece piece in message.Pieces) { lock (pieceLock) { if (piece.playerIdSpecified) { Player.game.Pieces = Player.game.Pieces.Where(piece1 => piece1.playerId != piece.playerId).ToList(); } if (Player.game.Pieces.Count(p => p.id == piece.id) == 0) Player.game.Pieces.Add(piece); else { var pp = Player.game.Pieces.Single(p => p.id == piece.id); pp.playerId = piece.playerId; pp.playerIdSpecified = piece.playerIdSpecified; pp.timestamp = piece.timestamp; if (pp.type == PieceType.unknown) pp.type = piece.type; } } } //args.PlayerClient.Pieces.Clear(); //foreach (var piece in message.Pieces) //{ // args.PlayerClient.Pieces.Add(piece); //} } if (message.gameFinished == true) { ConsoleDebug.Good("\nGame just ended\n"); BoardPrinter.Print(Player.game.Fields); Player.RegisterForNextGameAfterGameEnd(); return; } Player.ReadyForAction?.Invoke(); }