Exemple #1
0
        private void DispatchPacket(AgarPacket p)
        {
            Console.WriteLine("Received {0} packet with {1} bytes of data.", (ServerPacketType)p.OpCode, p.Payload.Length);

            switch ((ServerPacketType)p.OpCode)
            {
            case ServerPacketType.GameAreaSize:
                // TODO Assert PayloadLength == 32
                double min_x = bc.ToDouble(p.Payload, 0);
                double min_y = bc.ToDouble(p.Payload, 8);
                double max_x = bc.ToDouble(p.Payload, 16);
                double max_y = bc.ToDouble(p.Payload, 24);
                world.SetBounds(min_x, min_y, max_x, max_y);
                break;

            case ServerPacketType.WorldUpdate:
                // Read eat events
                uint       count = bc.ToUInt16(p.Payload, 0);
                EatEvent[] eats  = new EatEvent[count];
                for (int i = 0; i < count; i++)
                {
                    eats[i] = new EatEvent()
                    {
                        eater_id  = bc.ToUInt32(p.Payload, i * 8),
                        victim_id = bc.ToUInt32(p.Payload, i * 8 + 4)
                    };
                }

                // Read cell updates (names, size, position)
                List <UpdateEvent> updates = new List <UpdateEvent>();
                UpdateEvent        current;
                int offset = 0;

                while (true)
                {
                    current.blob_id = bc.ToUInt32(p.Payload, offset);
                    if (current.blob_id == 0)
                    {
                        break;
                    }

                    current.x = bc.ToUInt32(p.Payload, offset + 4);
                    current.y = bc.ToUInt32(p.Payload, offset + 8);

                    current.radius = bc.ToUInt16(p.Payload, offset + 10);
                    current.red    = p.Payload[offset + 11];
                    current.green  = p.Payload[offset + 12];
                    current.blue   = p.Payload[offset + 13];
                    current.flags  = p.Payload[offset + 14];

                    if ((current.flags & 0x02) != 0)
                    {
                        offset += 4;         // Unsure about this...
                    }
                    current.skin_url = null; // Just to fully initialize the struct

                    if ((current.flags & 0x04) != 0)
                    {
                        current.skin_url = bc.ToNullStr8(p.Payload, offset + 15, ref offset);     // Will increment offset by num bytes
                    }
                    current.name = bc.ToNullStr16(p.Payload, offset + 15, ref offset);

                    updates.Add(current);
                }

                // Read cell removals (out of sight, disconnect, etc)
                count = bc.ToUInt32(p.Payload, offset + 15);
                uint[] removals = new uint[count];
                for (int i = 0; i < count; i++)
                {
                    removals[i] = bc.ToUInt32(p.Payload, offset + 17 + i * 4);
                }

                world.RegisterEats(eats);
                world.RegisterUpdates(updates);
                world.RegisterRemovals(removals);

                break;

            case ServerPacketType.FFALeaderboard:
                count  = bc.ToUInt32(p.Payload, 0);
                offset = 4;
                for (uint i = 0; i < count; i++)
                {
                    uint   blob_id = bc.ToUInt32(p.Payload, offset); offset += 4;
                    string name    = bc.ToNullStr16(p.Payload, offset, ref offset);

                    // We got the data, add it to a list or something. TODO create a leaderboard class
                }
                break;
            }
        }