protected override void OnMessage(MessageEventArgs e) { if (e.IsBinary) { NetPackett packett = new NetPackett(); packett = Serializator.DeserializeNetPackett(e.RawData); // receiving ELO and starting matchmaking if (packett.messageType == MessageType.SendingElo) { int elo = int.Parse(Serializator.DeserializeString(packett.data)); StartMatchmaking(elo, ID); } } base.OnMessage(e); }
protected override void OnMessage(MessageEventArgs e) { if (e.IsBinary) { NetPackett packett = new NetPackett(); packett = Serializator.DeserializeNetPackett(e.RawData); if (packett.messageType == MessageType.GameLoaded) { string data = Serializator.DeserializeString(packett.data); string[] sortedData = data.Split(':'); Match match = getMatch(int.Parse(sortedData[1])); // connecting lobbyID and gameID to all players in a match foreach (var player in match.matchPlayers) { if (player.lobbyId == sortedData[0]) { player.gameId = ID; } } // checking if gameID is set to all players - as in: are they all loaded into a game bool allLoaded = true; foreach (var player in match.matchPlayers) { if (player.gameId.IsNullOrEmpty()) { allLoaded = false; } } // if All Loaded - send them match data and allow Player spawning if (allLoaded) { Console.WriteLine("All players loaded"); foreach (var player in match.matchPlayers) { Sessions.SendTo(Serializator.serialize(new NetPackett() { data = Serializator.serialize(match), messageType = MessageType.SpawnPlayer }), player.gameId); } } } else if (packett.messageType == MessageType.ClientMoved) { PlayerPosition pos = new PlayerPosition(); pos = Serializator.DeserializePlayerPosition(packett.data); NetPackett netPackett = new NetPackett() { messageType = MessageType.OtherPlayerMoved, data = packett.data }; // 1. in all matches finding my match through id // 2. when I find the match - sending to my new position to all players except myself foreach (var match in matches) { if (match.matchID == pos.matchID) { foreach (var player in match.matchPlayers) { if (!ID.Equals(player.gameId)) { Sessions.SendTo(Serializator.serialize(netPackett), player.gameId); } } } } } } base.OnMessage(e); }