Ejemplo n.º 1
0
        private void HandlePacket(Packet p)
        {
            Debug.WriteLine("Received packet: " + p.ToString(), "IN");
            p.Seek(1);
            if (p.Header == "")
            {
                return;
            }
            if (p.Header == "INIT")
            {
                int uid = p.ReadInt();
                string username = p.ReadString();
                int x = p.ReadInt(), y = p.ReadInt();
                int mass = p.ReadInt();

            }
            if (p.Header == "2") // set map area
            {
                int wid = p.ReadInt(), hei = p.ReadInt();
                if (Program.GameManager.Screen == null)
                {
                    Program.GameManager.Screen = new UI.Screen(new Size(wid, hei), Program.GForm.PanelSize);
                }
                else
                {
                    Program.GameManager.Screen.SetPlatformArea(wid, hei);
                }
                Program.GameManager.OnMapAreaModified();
            }
            if (p.Header == "A") // add something
            {
                int x, y;
                int id;
                string subhead = p.ReadString();
                switch (subhead)
                {
                    case "P":
                        string username = p.ReadString();
                        id = p.ReadInt();
                        int cells = p.ReadInt();
                        byte[] color = new[] { p.ReadByte(), p.ReadByte(), p.ReadByte() };

                        Color c = Color.FromArgb(color[0], color[1], color[2]);

                        Player player = new Player(id, username, c, cells);
                        if (!Program.GameManager.Players.ContainsKey(id))
                        {
                            Program.GameManager.Players.Add(id, player);
                        }
                        else
                        {
                            Program.GameManager.Players[id] = player;
                        }
                        Program.GameManager.OnPlayerJoinedGame(player);
                        break;

                    case "p":
                        int ownerID = p.ReadInt();
                        int cellID = p.ReadInt();
                        int mass = p.ReadInt();
                        x = p.ReadInt();
                        y = p.ReadInt();
                        lock (Program.GameManager.Players)
                        {
                            if (Program.GameManager.Players.ContainsKey(ownerID))
                            {
                                lock (Program.GameManager.PlayerCells)
                                {
                                    Player owner = Program.GameManager.Players[ownerID];
                                    if (!Program.GameManager.PlayerCells.ContainsKey(cellID))
                                    {
                                        owner.AddCell(new PlayerCell(mass, x, y, cellID, owner));
                                    }
                                    else
                                    {
                                        var cell = Program.GameManager.PlayerCells[cellID];
                                        cell.SetMass(mass);
                                        cell.SetXY(x, y);
                                        if (ownerID != cell.Owner.PID) // did this cell get eaten?
                                        {
                                            cell.Owner.RemoveCell(cell);
                                            owner.AddCell(cell);
                                        }
                                    }
                                }
                            }
                        }

                        break;

                    case "f":
                        x = p.ReadInt();
                        y = p.ReadInt();
                        id = p.ReadInt();

                        FoodCell food = new FoodCell(id, x, y);
                        food.SetMass(p.ReadInt());
                        lock (Program.GameManager.OtherCells)
                        {
                            if (Program.GameManager.OtherCells.ContainsKey(id))
                            {
                                Program.GameManager.OtherCells[id] = food;
                            }
                            else Program.GameManager.OtherCells.Add(id, food);
                        }

                        break;

                    case "v":
                        throw new NotImplementedException();
                    default:
                        break;
                }
            }
            if (p.Header == "R") // remove
            {
                string subhead = p.ReadString();
                switch (subhead)
                {
                    case "F": // remove eaten food
                        int count = p.ReadInt();
                        lock (Program.GameManager.OtherCells)
                        {
                            while (--count >= 0)
                            {
                                Program.GameManager.OtherCells.Remove(p.ReadInt());
                            }
                        }

                        break;
                    default:
                        break;
                }
            }
        }
Ejemplo n.º 2
0
        private void NetworkAction()
        {
            byte[] buff = new byte[2048];
            int bytes = 0;
            while (this.sock.Connected)
            {
                if (!Receive(buff, ref bytes)) { break; }

                Packet packet;
                string[] packetArray = enc.GetString(buff, 0, bytes).Split('\n');

                foreach (var pstr in packetArray)
                {
                    using (packet = new Packet(pstr))
                    {
                        HandlePacket(packet);
                    }
                }
            }
            //if (this.ConnectionLost != null && Kicked == false)
            //    this.ConnectionLost();
        }