Ejemplo n.º 1
0
        public bool InitializeAndConnect(string UserName, string Password)
        {
            try
            {
                _UserName = UserName;
                _Password = Password;

                m_client = new SignalRHubClient(ServerUrl, SignaRHubName, false);
                //Map hub events
                m_client.On <string>("Exec", s =>
                {
                    ClientGateResponse resp = s.Deserialize <ClientGateResponse>();
                    _LastResponse           = resp;
                    this.ProcessResponse(resp);
                });
                m_client.On <string>("Map", s =>
                {
                    _isMapped = true;
                    JoinOrCreateSession();
                });
                //Open connection
                m_client.Open();
                return(true);
            }
            catch (Exception E)
            {
                throw (E);
            }
        }
Ejemplo n.º 2
0
        public void ProcessResponse(ClientGateResponse response)
        {
            _recievedResonses++;
            if (response.ResultCode == (int)HttpStatusCode.OK)
            {
                switch ((RequestProcessorEnum)response.Kind)
                {
                case RequestProcessorEnum.Session:
                {
                    CreateSessionResponse csresp = response.JsonPayload.Deserialize <CreateSessionResponse>();
                    if (csresp.Status == HttpStatusCode.OK)
                    {
                        _SessionKey = csresp.SessionKey;
                        m_client.Invoke("Map", _UserId.ToString());
                    }
                }
                break;

                case RequestProcessorEnum.UserExists:
                {
                    UserExistsResponse r = response.JsonPayload.Deserialize <UserExistsResponse>();
                    if (!r.Exists)
                    {
                        //Create user
                        CreateUserRequest cr = new CreateUserRequest();
                        cr.UserName = _UserName;
                        cr.Password = _Password;
                        m_client.Invoke("Exec", cr.CreateRequest(RequestProcessorEnum.CreateUser, _UserId).Serialize());
                    }
                    else
                    {
                        LoginUserRequest lr = new LoginUserRequest();
                        lr.UserName = _UserName;
                        lr.Password = _Password;
                        m_client.Invoke("Exec", lr.CreateRequest(RequestProcessorEnum.LoginUser, _UserId).Serialize());
                    }
                }
                break;

                case RequestProcessorEnum.CreateUser:
                    CreateUserResponse cresp = response.JsonPayload.Deserialize <CreateUserResponse>();
                    if (cresp.Sucessful)
                    {
                        _UserId = cresp.UserId;
                        CreateSessionRequest csesr = new CreateSessionRequest();
                        csesr.UserId     = _UserId;
                        csesr.SessionKey = string.Empty;
                        m_client.Invoke("Exec", csesr.CreateRequest(RequestProcessorEnum.Session, _UserId).Serialize());
                    }
                    break;

                case RequestProcessorEnum.LoginUser:
                {
                    LoginUserResponse lur = response.JsonPayload.Deserialize <LoginUserResponse>();
                    if (lur.Status == HttpStatusCode.OK)
                    {
                        _UserId = lur.UserId;
                        CreateSessionRequest csesr = new CreateSessionRequest();
                        csesr.UserId     = _UserId;
                        csesr.SessionKey = string.Empty;
                        m_client.Invoke("Exec", csesr.CreateRequest(RequestProcessorEnum.Session, _UserId).Serialize());
                    }
                }
                break;

                case RequestProcessorEnum.TankPosition:
                {
                    TankPosistionResponse tr = response.JsonPayload.Deserialize <TankPosistionResponse>();
                    if (OnActionTrack != null)
                    {
                        OnActionTrack(tr.TankId, new Vector3(tr.x, tr.y, tr.z), tr.r);
                    }
                }
                break;

                case RequestProcessorEnum.FireShell:
                {
                    FireShellResponse fr = response.JsonPayload.Deserialize <FireShellResponse>();
                    if (OnActionFire != null)
                    {
                        OnActionFire(new Vector3(fr.pos.x, fr.pos.y, fr.pos.z),
                                     new Quaternion(fr.rot.x, fr.rot.y, fr.rot.z, fr.rot.w),
                                     new Vector3(fr.vel.x, fr.vel.y, fr.vel.z));
                    }
                }
                break;

                case RequestProcessorEnum._System_MapConnection:
                {
                    //Map
                    _isMapped = true;
                }
                break;

                case RequestProcessorEnum.TakeDamage:
                {
                    TakeDamageResponse tdr = response.JsonPayload.Deserialize <TakeDamageResponse>();
                    if (OnActionSetDamage != null)
                    {
                        OnActionSetDamage(tdr.TankId, tdr.Health);
                    }
                }
                break;

                case RequestProcessorEnum.JoinOrCreateGame:
                {
                    //Join or create game session
                    JoinOrCreateGameSessionResponse jcr = response.JsonPayload.Deserialize <JoinOrCreateGameSessionResponse>();
                    //Test if to run or wait
                    _CurrentGameSessionId = jcr.GameSessionId;
                    if (jcr.start)
                    {
                        int cnt = jcr.SessionPlayers.Count;
                        if (OnStartSession != null)
                        {
                            OnStartSession(jcr.SessionPlayers);
                        }
                    }
                    else
                    {
                        //used for single instance debug
                        //Create second request and link to same session enable single instance tests
                        //JoinOrCreateGameSessionRequest jcrr = new JoinOrCreateGameSessionRequest();
                        //jcrr.UserId = _UserId;
                        //m_client.Invoke("Que", jcrr.CreateRequest( RequestProcessorEnum.JoinOrCreateGame, _UserId).Serialize());
                    }
                }
                break;

                case RequestProcessorEnum.StartRound:
                {
                    StartRoundResponse srr = response.JsonPayload.Deserialize <StartRoundResponse>();
                    if (OnStartRound != null)
                    {
                        OnStartRound(srr.TankId, srr.RoundNum);
                    }
                }
                break;

                case RequestProcessorEnum.BeginRound:
                {
                    BeginRounResponse brr = response.JsonPayload.Deserialize <BeginRounResponse>();
                    if (OnRoundBegins != null)
                    {
                        OnRoundBegins(brr.RoundNum);
                    }
                }
                break;

                case RequestProcessorEnum.GetCanStartRound:
                {
                    CanStartRoundResponse crr = response.JsonPayload.Deserialize <CanStartRoundResponse>();
                    if (OnRoundBegins != null)
                    {
                        OnRoundBegins(crr.RoundNum);
                    }
                }
                break;

                default:
                    break;
                }
            }
            if (response.Kind != (int)RequestProcessorEnum.TankPosition)
            {
                UnityEngine.Debug.Log(response.Kind.ToString() + " time:" + response.TimeTaken.ToString() + " total calls:" + _recievedResonses.ToString() + " Json:" + response.JsonPayload);
            }
            //UnityEngine.Debug.Log(response.Kind.ToString() + " time:" + response.TimeTaken.ToString() + " Json:" + response.JsonPayload);
        }