Пример #1
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);
        }
        public async Task <IResponse> Process(IRequest request)
        {
            try
            {
                JoinOrCreateGameSessionRequest  rq       = request as JoinOrCreateGameSessionRequest;
                JoinOrCreateGameSessionResponse response = new JoinOrCreateGameSessionResponse();
                List <ActorId> NotifyList = new List <ActorId>();
                //test if there is a session
                var listproxy = new ActorId(1).Proxy <ITankGameSessionList>();
                if (await listproxy.HasSessionsAsync())
                {
                    GameSessionListItem itm = await listproxy.GetNextSessionAsync();

                    if (itm != null)
                    {
                        response.GameSessionId  = itm.GameSessionId.GetGuidId();
                        response.Status         = HttpStatusCode.OK;
                        response.waitForPlayers = false;
                        response.start          = true;
                        await listproxy.RemoveGameSessionAsync(itm.GameSessionId);

                        var sessionproxy = itm.GameSessionId.Proxy <ITankGameSession>();
                        int sequenceno   = await sessionproxy.JoinAsync(new ActorId(rq.UserId));

                        //Get players
                        List <TankSessionMemberItem> members = await sessionproxy.GetPlayersAsync();

                        foreach (TankSessionMemberItem i in members)
                        {
                            response.SessionPlayers.Add(new GameSessionPlayerItem(i.UserId.GetGuidId(), i.TankId.GetGuidId(), i.Sequence)); NotifyList.Add(i.UserId);
                        }
                    }
                    else
                    {
                        response.Status = HttpStatusCode.InternalServerError;
                        ErrorResponse errorresponse = new ErrorResponse("Failed to create gamesession");
                        return(errorresponse);
                    }
                }
                else
                {
                    Guid    newSessionid   = Guid.NewGuid();
                    ActorId sessionActorId = new ActorId(newSessionid);
                    if (await listproxy.AddGameSessionAsync(sessionActorId))
                    {
                        //Set Session Params
                        var sessionproxy = sessionActorId.Proxy <ITankGameSession>();
                        int sequenceno   = await sessionActorId.Proxy <ITankGameSession>().JoinAsync(new ActorId(rq.UserId));

                        response.GameSessionId  = newSessionid;
                        response.Status         = HttpStatusCode.OK;
                        response.waitForPlayers = true;
                        response.start          = false;
                        //Get players
                        List <TankSessionMemberItem> members = await sessionproxy.GetPlayersAsync();

                        foreach (TankSessionMemberItem i in members)
                        {
                            response.SessionPlayers.Add(new GameSessionPlayerItem(i.UserId.GetGuidId(), i.TankId.GetGuidId(), i.Sequence)); NotifyList.Add(i.UserId);
                        }
                    }
                    else
                    {
                        response.Status = HttpStatusCode.InternalServerError;
                        ErrorResponse errorresponse = new ErrorResponse("Failed to create gamesession");
                        return(errorresponse);
                    }
                }
                response.Status = HttpStatusCode.OK;
                //Notidy participants
                if (NotifyList.Count == 0)
                {
                    NotifyList.Add(new ActorId(rq.UserId));
                }
                foreach (ActorId uid in NotifyList)
                {
                    GateResponse gr = new GateResponse(this.ProcessorId, (int)System.Net.HttpStatusCode.OK, uid.GetGuidId(), response.Serialize());
                    await uid.Proxy <IUser>().SendGateResponseAsync(gr);
                }
                return(await Task.FromResult(response));
            }
            catch (Exception E)
            {
                E.Log();
                ErrorResponse errorresponse = new ErrorResponse(E.Message);
                return(errorresponse);
            }
        }