Esempio n. 1
0
        void Update()
        {
            if (!string.IsNullOrEmpty(_socket.Error))
            {
                Debug.LogWarning(_socket.Error);
                Start();
                return;
            }

            Profiler.BeginSample("socket.recv");
            byte[] data = _socket.Recv ();
            Profiler.EndSample();

            if (data == null || data.Length == 0) return;

            if (_state == ConnectionState.UNKNOWN)
                _state = ConnectionState.CONNECTED;

            if (data.Length < 4)
            {
                Debug.Log("partial message!");
                _storeAddPartial(data);
            }

            if (_partial_message != null)
            {
                Debug.Log("reconstructing partial message!");
                data = ArrayUtil.sumArrays(_partial_message, data);
                _partial_message = null;
            }

            int messageLen = BitConverter.ToInt32(data, 0);
            int realLen = data.Length - 5;

            if (realLen < messageLen)
            {
                _storeAddPartial(data);
                return;
            }
            else if (realLen > messageLen)
            {
                Debug.LogError("AHAHAH");
                return;
            }

            if (data[4] == DebugPackage.ID)
            {
                var dm = new DebugPackage(data);
                Debug.Log("debug message from: " + dm.sender + "::" + dm.message);
            }

            if (_state == ConnectionState.CONNECTED)
            {
                if (data[4] == Welcome.ID)
                {
                    _game.showWelcomeData(new Welcome(data));
                    _state = ConnectionState.WELCOMED;
                }
                else
                {
                    Debug.LogError("expected welcome, got :" + data[4].ToString() + " instead");
                    return;
                }
            }
            else if (_state == ConnectionState.AUTHORIZING)
            {
                if (data[4] == ResponseEnterWorld.ID)
                {
                    var res = new ResponseEnterWorld(data);
                    if (res.status == EnterWorldStatus.ENTER_SUCCESS)
                    {
                        Debug.Log("authorize success at world " + _lastWorldAttempt +
                        " with name " + _lastNameAttempt + "::" + res.myId.ToString());
                        _state = ConnectionState.AUTHORIZED;
                        _game.rememberMe(_lastNameAttempt, res.myId, _lastWorldAttempt);
                    }
                    else
                    {
                        Debug.Log("authorize failed!");
                        _game.showWelcomeData();
                        _state = ConnectionState.WELCOMED;
                    }
                }
                else
                {
                    Debug.LogError("expected auth response or new seed, got :" + data[4].ToString() + " instead");
                    return;
                }
            }
            else if (_state == ConnectionState.AUTHORIZED)
            {
                if (data[4] == WorldData.ID)
                {
                    Debug.Log("world data received!");
                    var wData = new WorldData(data);
                    _game.initializeWorld(wData);
                    _state = ConnectionState.IN_WORLD;

                    _socket.Send(new DebugPackage(_game.myName, "привет друзья").encode());
                }
                else
                {
                    Debug.LogError("expected world data, got :" + data[4].ToString() + " instead");
                    return;
                }
            }
            else if (_state == ConnectionState.IN_WORLD)
            {
                if (data[4] == WorldSnapshot.ID)
                {
                    Profiler.BeginSample("recv.snapshot");
            //                    Debug.Log("snapshot received!");
                    Profiler.BeginSample("unpack snapshot");
                    var snap = new WorldSnapshot(data);
                    Profiler.EndSample();

                    Profiler.BeginSample("decode snapshot");
                    var s = new byte[snap.snapshot.Length];
                    for (int i = 0; i < snap.snapshot.Length; i++)
                    {
                        s[i] = (byte.Parse(snap.snapshot[i].ToString()));
                    }
                    Profiler.EndSample();

                    Profiler.BeginSample("push snapshot");
                    _game.pushSnapshot(s);
                    Profiler.EndSample();
                    Profiler.EndSample();
                }
                else
                if (data[4] == NewSeed.ID)
                {
                    Debug.Log("new seed received ");
                    var seed = new NewSeed(data);
                    _game.pushSeed(seed.location, seed.owner);
                }
                else
                if (data[4] == SeedDestroyed.ID)
                {
                    var seedD = new SeedDestroyed(data);
                    Debug.Log("seed dest: " + seedD.location);
                    _game.destroySeed(seedD.location);
                }
                else
                if (data[4] == DeployBomb.ID)
                {
                    var enemy_bomb = new DeployBomb(data);
                    Debug.Log("enemy bomb: " + enemy_bomb.target);
                    _game.pushBomb(enemy_bomb.target);
                }
            }
        }
Esempio n. 2
0
 public void deployBomb(int location)
 {
     var d = new DeployBomb(location);
     _socket.Send(d.encode());
 }