コード例 #1
0
        // NETPACKETT
        // custom modified to read Message Type
        public static byte[] serialize(NetPackett netpackett)
        {
            var s  = new MemoryStream();
            var bW = new BinaryWriter(s);

            bW.Write((int)netpackett.messageType);
            bW.Write(netpackett.data.Length);
            foreach (var item in netpackett.data)
            {
                bW.Write(item);
            }
            return(s.ToArray());
        }
コード例 #2
0
        // custom modified to read Message Type
        private static NetPackett DeserializeNetPackett(ref byte[] b, ref MemoryStream s, ref BinaryReader bR)
        {
            var obj = new NetPackett();

            obj.messageType = (MessageType)bR.ReadInt32();
            int dataArraySize = bR.ReadInt32();

            obj.data = new Byte[dataArraySize];
            for (int i = 0; i < dataArraySize; i++)
            {
                obj.data[i] = bR.ReadByte();
            }
            return(obj);
        }
コード例 #3
0
 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);
 }
コード例 #4
0
        // custom modified to read Message Type
        public static NetPackett DeserializeNetPackett(byte[] b)
        {
            var s   = new MemoryStream(b);
            var bR  = new BinaryReader(s);
            var obj = new NetPackett();

            obj.messageType = (MessageType)bR.ReadInt32();
            int dataArraySize = bR.ReadInt32();

            obj.data = new Byte[dataArraySize];
            for (int i = 0; i < dataArraySize; i++)
            {
                obj.data[i] = bR.ReadByte();
            }
            return(obj);
        }
コード例 #5
0
            public void StartMatchmaking(int elo, string playerID)
            {
                Console.WriteLine("Player entering matchmaking");
                Player current = new Player();

                // finding player through lobby ID, enabling his isMatchmaking and setting elo
                foreach (var player in players)
                {
                    if (player.lobbyId == playerID)
                    {
                        player.elo           = elo;
                        player.isMatchmaking = true;
                        current = player;
                    }
                }

                Match match = new Match();

                match.matchPlayers.Add(current);
                foreach (var player in players)
                {
                    //(Math.Abs(elo - player.elo) calculates "distance"
                    if ((player.lobbyId != playerID) && (Math.Abs(elo - player.elo) <= minimumMatchmakingRange) && player.isMatchmaking)
                    {
                        match.matchPlayers.Add(player);
                        // if 3 players are in a match, add it to a list and give it id -> Send match id to those players and let them know they can play
                        if (match.matchPlayers.Count == 3)
                        {
                            Console.WriteLine("Found a match");
                            match.matchID = counter++;
                            matches.Add(match);
                            foreach (var p in match.matchPlayers)
                            {
                                NetPackett packett = new NetPackett()
                                {
                                    data        = Serializator.serialize(match.matchID.ToString()),
                                    messageType = MessageType.StartTheGame
                                };
                                Sessions.SendTo(Serializator.serialize(packett), p.lobbyId);
                            }
                        }
                    }
                }
            }
コード例 #6
0
            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);
            }