Пример #1
0
        private void Server_DataReceived(string ID, byte[] Data)
        {
            //Console.WriteLine(ID + ": " + Network.ConvertBytesToString(Data));
            string received = Network.ConvertBytesToString(Data);

            if (received == "getAllCreatures")
            {
                string data = NetContainer.GetAllCreatures(ID);
                server.SendData(ID, Network.ConvertStringToBytes(data));
            }
            else
            {
                foreach (var player in connectedPlayers)
                {
                    if (player.playerId == ID)
                    {
                        player.ParceFromServer(Network.ConvertBytesToString(Data));
                        break;
                    }
                }
            }
        }
Пример #2
0
        private void Client_DataReceived(byte[] Data, string ID)
        {
            string responce = Network.ConvertBytesToString(Data);

            Console.WriteLine(responce + " is recieved!");
            if (responce.Contains("gac"))
            {
                responce = responce.Replace("gac \n", "");
                var list = responce.Split('\n');

                foreach (var item in list)
                {
                    if (item == "")
                    {
                        continue;
                    }
                    string itemCopy = item;
                    bool   isMe     = false;
                    if (itemCopy[0] == 'p')
                    {
                        isMe     = true;
                        itemCopy = itemCopy.Remove(0, 1);
                    }
                    int Id = Convert.ToInt32(Network.MiniParce(ref itemCopy, ':'));
                    int x  = Convert.ToInt32(Network.MiniParce(ref itemCopy, ','));
                    int y  = Convert.ToInt32(Network.MiniParce(ref itemCopy, ','));
                    itemCopy = itemCopy.Remove(0, 1);
                    char sym = itemCopy[0];//Convert.ToChar(itemCopy);

                    if (isMe)
                    {
                        var player = new Player(sym, new Vector(x, y), RLColor.White);
                        player.ID = Id;
                    }
                    else
                    {
                        var crea = new Creature(sym, new Vector(x, y), RLColor.White);
                        crea.ID = Id;
                    }
                }
            }
            else if (responce.Contains("dead"))
            {
                string copy = responce;
                int    Id   = Convert.ToInt32(Network.MiniParce(ref copy, ':'));

                var crea = CreaturesContainer.GetCreature(Id);
                if (crea != null)
                {
                    CreaturesContainer.Remove(crea);
                    crea = null;
                }
            }
            else
            {
                string copy = responce;
                int    Id   = Convert.ToInt32(Network.MiniParce(ref copy, ':'));
                int    x    = Convert.ToInt32(Network.MiniParce(ref copy, ','));
                int    y    = Convert.ToInt32(Network.MiniParce(ref copy, ';'));

                var crea = CreaturesContainer.GetCreature(Id);
                if (crea != null)
                {
                    crea.UpdateData(new Vector(x, y));
                }
            }
        }