コード例 #1
0
        static void WebServerOnData(int id, ArraySegment <byte> data)
        {
            _bitBuffer.Clear();
            _bitBuffer.FromArray(data.Array, data.Count);

            byte messageId = _bitBuffer.ReadByte();

            switch (messageId)
            {
            case 1:
            {
                uint qX = _bitBuffer.ReadUInt();
                uint qY = _bitBuffer.ReadUInt();

                PlayerData playerData = _playerDatas[id];
                playerData.qX = qX;
                playerData.qY = qY;

                // Send this position to everyone next state packet
                _dataToSend.Enqueue(playerData);

                break;
            }

            case 8:
            {
                string name = _bitBuffer.ReadString();
                _playerDatas[id].name       = name;
                _playerDatas[id].handshaked = true;

                _handshakenClientCount += 1;

                // Tell new client their id and the game state and everyone's name and points
                _bitBuffer.Clear();
                _bitBuffer.AddByte(2);
                _bitBuffer.AddUShort((ushort)id);
                _bitBuffer.AddByte((byte)_currentState);

                _bitBuffer.AddUShort((ushort)_playerDatas.Count);
                foreach (var playerData in _playerDatas.Values)
                {
                    _bitBuffer.AddUShort(playerData.id);
                    _bitBuffer.AddString(playerData.name);
                    _bitBuffer.AddShort(playerData.points);
                }

                _bitBuffer.ToArray(_buffer);
                _webServer.SendOne(id, new ArraySegment <byte>(_buffer, 0, 5 + _playerDatas.Count * 20));

                // Tell all the players this new client's name
                _bitBuffer.Clear();
                _bitBuffer.AddByte(9);
                _bitBuffer.AddUShort(_playerDatas[id].id);
                _bitBuffer.AddString(_playerDatas[id].name);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 23));

                break;
            }

            case 10:
            {
                ushort objectId = _bitBuffer.ReadUShort();
                ushort newX     = _bitBuffer.ReadUShort();
                ushort newY     = _bitBuffer.ReadUShort();
                _movedObjects[objectId] = new Tuple <ushort, ushort>(newX, newY);

                break;
            }

            case 11:
            {
                short pointChange = _bitBuffer.ReadShort();
                _playerDatas[id].points += pointChange;

                // If points are 0 or less, give builder a point
                if (pointChange <= 0)
                {
                    _playerDatas[_builderId].points += 1;
                }

                break;
            }

            case 12:
            {
                _bitBuffer.Clear();
                _bitBuffer.AddByte(13);
                _bitBuffer.AddUShort((ushort)id);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 3));

                break;
            }
            }
        }
コード例 #2
0
        static void WebServerOnData(int id, ArraySegment <byte> data)
        {
            _bitBuffer.Clear();
            _bitBuffer.FromArray(data.Array, data.Count);

            byte messageId = _bitBuffer.ReadByte();

            switch (messageId)
            {
            case 1:
            {
                uint qX = _bitBuffer.ReadUInt();
                uint qY = _bitBuffer.ReadUInt();

                PlayerData playerData = _playerDatas[id];
                playerData.qX = qX;
                playerData.qY = qY;

                // Send this position to everyone next state packet
                _dataToSend.Enqueue(playerData);

                break;
            }

            case 8:
            {
                string name = _bitBuffer.ReadString();
                _playerDatas[id].name = name;

                // Tell all the players this new client's name
                _bitBuffer.Clear();
                _bitBuffer.AddByte(9);
                _bitBuffer.AddUShort(_playerDatas[id].id);
                _bitBuffer.AddString(_playerDatas[id].name);
                _bitBuffer.ToArray(_buffer);
                _webServer.SendAll(_connectedIds, new ArraySegment <byte>(_buffer, 0, 23));

                break;
            }

            case 10:
            {
                ushort objectId = _bitBuffer.ReadUShort();
                _movedObjects.Add(objectId);

                break;
            }

            case 11:
            {
                short pointChange = _bitBuffer.ReadShort();
                _playerDatas[id].points += pointChange;

                // If points are 0 or less, give builder a point
                if (pointChange <= 0)
                {
                    _playerDatas[_builderId].points += 1;
                }

                break;
            }
            }
        }