Exemplo n.º 1
0
    override public void HandleMessage(ref string msg)
    {
        base.HandleMessage(ref msg);
        MsgStructure   data = JsonUtility.FromJson <MsgStructure>(msg);
        FrontendEvents evt  = (FrontendEvents)data.evt;

        Debug.Log("EVENT: " + evt);
        User user = JsonUtility.FromJson <User>(data.user);

        switch (evt)
        {
        case FrontendEvents.SignedIn:
            Client.user               = user;
            ClientSignedIn.hasEvent   = data.hasEvent;
            ClientSignedIn.spanishMsg = data.spanishMsg;
            ClientSignedIn.englishMsg = data.englishMsg;
            Client.SetState(Client.signedInState);
            break;

        case FrontendEvents.SignedUp:
            Client.user = user;
            Client.SetState(Client.signedUpState);
            break;

        case FrontendEvents.WrongData:
            wrongDataEvent.Invoke(data.error);
            break;
        }
    }
Exemplo n.º 2
0
    public override void HandleMessage(ref string msg)
    {
        base.HandleMessage(ref msg);
        MsgStructure data = JsonUtility.FromJson <MsgStructure>(msg);

        if (data.roomEvt)
        {
            ClientInRoom.queuedMessages.Add(msg);
            return;
        }
        FrontendEvents evt = (FrontendEvents)data.evt;

        Debug.Log("EVENT: " + evt);
        switch (evt)
        {
        case FrontendEvents.SignedOut:
            Client.SetState(Client.connectedState);
            signedOutEvent?.Invoke();
            break;

        case FrontendEvents.Error:
            error = data.error;
            GameManager.instance.ReleaseUI();
            Debug.Log("SignedIn error: " + data.error);
            //TODO: DISPLAY ERROR IN LOBBY
            break;

        case FrontendEvents.InRoom:
            GameManager.levelId  = data.level;
            GameManager.isHost   = data.isHost;
            GameManager.isClient = data.isClient;
            Client.SetState(Client.inRoomState);
            break;

        case FrontendEvents.GetRooms:
            var            roomInfos = new List <PlayerRoomInfo>();
            PlayerRoomInfo info;
            foreach (string json in data.players)
            {
                info = JsonUtility.FromJson <PlayerRoomInfo>(json);
                Debug.Log("Room info: " + json);
                if (info != null)
                {
                    roomInfos.Add(info);
                }
            }
            getRoomsEvent?.Invoke(roomInfos);
            break;
        }
    }
Exemplo n.º 3
0
    override public void HandleMessage(ref string msg)
    {
        base.HandleMessage(ref msg);
        MsgStructure   data = JsonUtility.FromJson <MsgStructure>(msg);
        FrontendEvents evt  = (FrontendEvents)data.evt;

        switch (evt)
        {
        case FrontendEvents.Error:
            Debug.Log("ROOM ERROR");
            error = data.error;
            Client.SetState(Client.signedInState);
            break;

        case FrontendEvents.Exit:
            Client.SetState(Client.signedInState);
            break;

        case FrontendEvents.AddPlayer:
            var info = new UserInfo();
            info.id       = data.id;
            info.isHost   = data.isHost;
            info.isClient = data.isClient;
            info.bodyType = data.avatar_bodyType;
            info.skinTone = data.avatar_skinTone;
            info.color    = data.avatar_color;
            info.face     = data.avatar_face;
            info.hat      = data.avatar_hat;
            info.frame    = data.avatar_frame;
            waitingDict.Add(info.id, info);
            if (data.spawned)
            {
                goto case FrontendEvents.Spawn;
            }
            break;

        case FrontendEvents.Spawn:
            Debug.Log("Spawning " + data.id);
            UserInfo userInfo;
            if (waitingDict.TryGetValue(data.id, out userInfo))
            {
                waitingDict.Remove(data.id);
                PlayerJoiner.queuedUsers.Enqueue(userInfo);
            }
            break;

        case FrontendEvents.RemovePlayer:
            //TODO: sacar de waiting dict
            if (waitingDict.ContainsKey(data.id))
            {
                waitingDict.Remove(data.id);
            }
            Player player;
            if (players.TryGetValue(data.id, out player))
            {
                GameObject.Destroy(player.gameObject);
                players.Remove(data.id);
            }
            break;

        case FrontendEvents.GetInfo:
            NetworkObj.BasicStructure structure;
            ObjsStructure             objsStructure = JsonUtility.FromJson <ObjsStructure>(msg);
            if (objsStructure.ms < lastMs && GameManager.isClient)
            {
                Debug.Log("Ignoring info msg");
                return;
            }
            lastMs = objsStructure.ms;
            NetworkObj obj;
            foreach (var str in objsStructure.objs)
            {
                structure = JsonUtility.FromJson <NetworkObj.BasicStructure>(str);
                if (NetworkObj.objsDict.TryGetValue(structure.key, out obj))
                {
                    obj.SetInfo(str);
                }
            }
            break;

        case FrontendEvents.ChangeScene:
            if (GameManager.isClient)
            {
                SceneLoader.instance.LoadScene(data.id);
            }
            break;
        }
    }