Exemple #1
0
            public static APIGatewayProxyResponse Send(bool result, string msg, IEnumerable data = null)
            {
                BodyResponse body       = new BodyResponse();
                int          statusCode = 0;

                body.msg = msg;
                if (data != null)
                {
                    body.data = data;
                }
                else
                {
                    body.data = null;
                }

                if (result == false)
                {
                    statusCode = (int)HttpStatusCode.InternalServerError;
                }
                else
                {
                    statusCode = (int)HttpStatusCode.OK;
                }

                return(new APIGatewayProxyResponse
                {
                    StatusCode = statusCode,
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json" },
                        { "Access-Control-Allow-Origin", "*" },
                        { "Access-Control-Allow-Credentials", "true" }
                    },
                    Body = JsonConvert.SerializeObject(body)
                });
            }
        public async Task <BodyResponse <LevelInfo> > Handle(GetLevelInfoCommand request,
                                                             CancellationToken cancellationToken)
        {
            //读取redis account信息
            LevelInfo levelinfo = await _redis.GetLevelInfo(request.Id);

            if (levelinfo == null)
            {
                //从数据库中获取
                using (var loker = _redis.Locker(KeyGenHelper
                                                 .GenUserKey(request.Id, LevelInfo.ClassName)))
                {
                    loker.Lock();
                    levelinfo = await _levelRepository.FindAndAdd(request.Id,
                                                                  new LevelInfo(request.Id, 1, 0, _levelManager.GetNeedExp(1)));

                    _ = _redis.SetLevelInfo(levelinfo);
                }
            }

            BodyResponse <LevelInfo> response = new BodyResponse <LevelInfo>(StatusCodeDefines.Success,
                                                                             null, levelinfo);

            return(response);
        }
Exemple #3
0
        public async Task <BodyResponse <MoneyInfo> > Handle(GetMoneyCommand request,
                                                             CancellationToken cancellationToken)
        {
            var moneyInfo = await _redis.GetMoney(request.Id);

            if (moneyInfo == null)
            {
                using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(request.Id,
                                                                          MoneyInfo.ClassName)))
                {
                    await locker.LockAsync();

                    moneyInfo = await _moneyRepository.FindAndAdd(request.Id,
                                                                  new MoneyInfo(request.Id, 0, 0, 0, 0, 0));

                    _ = _redis.SetMoney(request.Id, moneyInfo);
                }

                if (moneyInfo == null)
                {
                    return(new BodyResponse <MoneyInfo>(StatusCodeDefines.GetMoneyError, null, null));
                }
            }
            BodyResponse <MoneyInfo> response = new BodyResponse <MoneyInfo>
                                                    (StatusCodeDefines.Success, null, moneyInfo);

            Log.Debug($"GetMoneyCommand:{moneyInfo.CurCoins},{moneyInfo.Carry}");
            return(response);
        }
        public async Task <BodyResponse <MatchingResponseVM> > BlindMatching(long id, long blind)
        {
            BodyResponse <DummyMatchingResponseInfo> response =
                await _bus.SendCommand(new BlindMatchingCommand(id, blind));

            return(response.MapResponse <MatchingResponseVM>(_mapper));
        }
Exemple #5
0
        public Task <BodyResponse <string> > Handle(FacebackCommand request, CancellationToken cancellationToken)
        {
            //插入一条到数据库
            BodyResponse <string> response = new BodyResponse <string>(StatusCodeDefines.Success, null, null);

            return(Task.FromResult(response));
        }
Exemple #6
0
        public static object GetBodyResponse(int statusCode, object obj)
        {
            BodyResponse oBodyResponse = new BodyResponse();

            oBodyResponse.status = statusCode;
            oBodyResponse.result = obj;
            return(oBodyResponse);
        }
        public async Task <BodyResponse <GameSampleMatchingResponseInfo> > MatchingRoom(long id, long blind, string curRoomId)
        {
            //获取这个玩家的redis锁
            using (var locker = _redis.Locker(KeyGenHelper.GenUserKey(id, UserRoomInfo.className)))
            {
                if (!await locker.TryLockAsync())
                {
                    return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.IsMatching, null, null));
                }

                //查询redis是否有这个玩家
                RoomInfo roomInfo     = null;
                var      userRoomInfo = await _redis.GetUserRoomInfo(id);

                if (userRoomInfo != null)
                {
                    roomInfo = new RoomInfo(userRoomInfo.RoomId, 0, userRoomInfo.GameKey, userRoomInfo.Blind);
                }
                else
                {
                    roomInfo = await _roomManager.GetRoom(id, blind);
                }
                try
                {
                    BodyResponse <JoinGameRoomMqResponse> roomResponse =
                        await _roomManager.SendToGameRoom <JoinGameRoomMqCommand, BodyResponse <JoinGameRoomMqResponse> >
                            (roomInfo.GameKey, new JoinGameRoomMqCommand(id, roomInfo.RoomId, roomInfo.GameKey));

                    if (roomResponse.StatusCode == StatusCodeDefines.Success)
                    {
                        _ = _redis.SetUserRoomInfo(new UserRoomInfo(id, roomInfo.RoomId, roomInfo.GameKey, blind, MatchingStatus.Success));
                        return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.Success, null,
                                                                                 new GameSampleMatchingResponseInfo(id, roomInfo.RoomId, roomInfo.Blind, roomInfo.GameKey)));
                    }
                    else
                    {
                        if (roomResponse.Body != null)
                        {
                            RoomInfo newRoomInfo = new RoomInfo(roomResponse.Body.RoomId, roomResponse.Body.UserCount,
                                                                roomResponse.Body.GameKey, roomResponse.Body.Blind);
                            _roomManager.UpdateRoom(newRoomInfo);
                        }

                        _ = _redis.DeleteUserRoomInfo(id);
                        return(new BodyResponse <GameSampleMatchingResponseInfo>(roomResponse.StatusCode, roomResponse.ErrorInfos, null));
                    }
                }

                catch
                {
                    _ = _redis.DeleteUserRoomInfo(id);
                    Log.Error($"user {id} join room {roomInfo.RoomId} error");
                    return(new BodyResponse <GameSampleMatchingResponseInfo>(StatusCodeDefines.BusError, null, null));
                }
            }
        }
        public static TokenData tokenData(BodyRequest request, BodyResponse response, int role)
        {
            TokenData tokenData = null;

            if (!Configs.DEBUG_MODE)
            {
                //var thread = new Thread((object t) =>
                //{
                int  times  = 3;
                bool fail   = true;
                var  client = new HttpClient();

                CheckTokenRequest ctRequest = new CheckTokenRequest();
                ctRequest.Token         = request.Token;
                ctRequest.TokenPassword = Configs.TOKEN_PASSWORD;
                ctRequest.Role          = role;

                do
                {
                    try
                    {
                        var result = client.PostAsync(Configs.CHECK_TOKEN, new StringContent(JsonConvert.SerializeObject(ctRequest), Encoding.UTF8, "application/json")).Result.Content.ReadAsAsync <CheckTokenResponse>().Result;
                        fail = result == null;
                        if (!fail)
                        {
                            response.IsTokenTimeout = result.IsTokenTimeout;
                            if (result.IsError)
                            {
                                response.Errors.Add("Không thể truy cập đến máy chủ.");
                                response.IsError = true;
                            }
                            else
                            {
                                tokenData = result.Data;
                            }
                            break;
                        }
                    }
                    catch { }
                } while (fail && --times > 0);
                //(t as Thread).Abort();
                //});
                //thread.Start(thread);
            }
            return(tokenData);
        }
        void PostBuildUpdates(BodyResponse result)
        {
            // update starmap
            mapTab.StarMap.CelestialObjects.Remove(Body);
            var level = Body.OrbitNestingLevel;

            Body = result.Body;
            Body.OrbitNestingLevel = level;
            mapTab.StarMap.CelestialObjects.Add(Body);
            // update planet stats
            InfoText.DataContext = Body;
            // update lists
            Ships.ItemsSource      = Body.CelestialObjectShips;
            Structures.ItemsSource = Body.CelestialObjectStructures;
            // display updated resources
            App.Player = result.Player;
            // display new build options
            StructureBuildOptions.ItemsSource = result.NewStructureOptions;
            ShipBuildOptions.ItemsSource      = result.NewShipOptions;
        }
       public async Task<BodyResponse<GameInfo>> Handle(GetGameInfoCommand request, 
           CancellationToken cancellationToken)
       {
 
           GameInfo gameinfo = await _redis.GetGameInfo(request.Id);
           if (gameinfo == null)
           {
               using (var loker = _redis.Locker(KeyGenHelper
               .GenUserKey(request.Id, GameInfo.ClassName)))
               {
                   loker.Lock();
                   gameinfo = await _gameRepository.FindAndAdd(request.Id, new GameInfo(request.Id, 0, 0, 0));
                   _ = _redis.SetGameInfo(gameinfo);
               }
           }
           
           BodyResponse<GameInfo> response = new BodyResponse<GameInfo>(StatusCodeDefines.Success,
               null, gameinfo);
           
           return response;
       }
Exemple #11
0
        public async Task <BodyResponse <AccountResponse> > Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var newAccountInfo = request.Info;
            //根据PlatformAccount字段读取redis,判断该账号是否已经注册
            var loginCheckInfo = await _redis.GetLoginCheckInfo(request.Info.PlatformAccount);

            AccountInfo accountInfo = null;
            bool        isRegister  = false;

            if (loginCheckInfo != null)
            {
                //直接通过ID去查找这个玩家信息
                accountInfo = await _redis.GetAccountInfo(loginCheckInfo.Id);

                if (accountInfo == null)
                {
                    accountInfo = await _accountRepository.GetByIdAsync(loginCheckInfo.Id);
                }
            }

            else
            {
                //查找数据库中是否有这个账号
                accountInfo = await _accountRepository.GetByPlatform(newAccountInfo.PlatformAccount);

                if (accountInfo == null)
                {
                    //注册新账号
                    isRegister = true;
                    long newUid = await _genRepository.GenNewId();

                    accountInfo = new AccountInfo(newUid, newAccountInfo.PlatformAccount,
                                                  newAccountInfo.UserName, newAccountInfo.Sex, newAccountInfo.HeadUrl,
                                                  newAccountInfo.Type, DateTime.Now, GetAccountBaseInfoMqResponse.SomeFlags.None);
                    await _accountRepository.AddAsync(accountInfo);
                }
            }

            if (accountInfo != null)
            {
                newAccountInfo.Id = accountInfo.Id;
                string          token          = TokenHelper.GenToken(accountInfo.Id);
                AccountResponse accounResponse = null;

                bool isNeedUpdate = false;
                //如果登录信息有更新, 那么更新数据库
                if (!isRegister && accountInfo != newAccountInfo)
                {
                    isNeedUpdate = true;
                }
                if (isRegister)
                {
                    var mqResponse = await _moneyAddClient.GetResponseExt <AddMoneyMqCommand, BodyResponse <MoneyMqResponse> >
                                         (new AddMoneyMqCommand(accountInfo.Id, _initRewardInfo.RewardCoins, 0, AddReason.InitReward));

                    var moneyInfo = mqResponse.Message.Body;

                    accounResponse = new AccountResponse(newAccountInfo.Id,
                                                         newAccountInfo.PlatformAccount,
                                                         newAccountInfo.UserName,
                                                         newAccountInfo.Sex,
                                                         newAccountInfo.HeadUrl,
                                                         token, new MoneyInfo(moneyInfo.CurCoins + moneyInfo.Carry,
                                                                              moneyInfo.CurDiamonds,
                                                                              moneyInfo.MaxCoins,
                                                                              moneyInfo.MaxDiamonds),
                                                         _hostManager.GetOneHost(), true, newAccountInfo.Type);
                }
                else
                {
                    //查询玩家金币
                    MoneyMqResponse moneyResponse = null;

                    var mqResponse = await _moneyClient.GetResponseExt <GetMoneyMqCommand, BodyResponse <MoneyMqResponse> >
                                         (new GetMoneyMqCommand(accountInfo.Id));

                    moneyResponse  = mqResponse.Message.Body;
                    accounResponse = new AccountResponse(newAccountInfo.Id,
                                                         newAccountInfo.PlatformAccount,
                                                         newAccountInfo.UserName,
                                                         newAccountInfo.Sex,
                                                         newAccountInfo.HeadUrl,
                                                         token, new MoneyInfo(moneyResponse.CurCoins + moneyResponse.Carry,
                                                                              moneyResponse.CurDiamonds,
                                                                              moneyResponse.MaxCoins,
                                                                              moneyResponse.MaxDiamonds),
                                                         _hostManager.GetOneHost(), false, newAccountInfo.Type);
                }

                _ = _bus.RaiseEvent <LoginEvent>(new LoginEvent(Guid.NewGuid(),
                                                                accounResponse, isRegister, isNeedUpdate, newAccountInfo));
                BodyResponse <AccountResponse> retRresponse =
                    new BodyResponse <AccountResponse>(StatusCodeDefines.Success, null, accounResponse);
                return(retRresponse);
            }

            BodyResponse <AccountResponse> response = new BodyResponse <AccountResponse>(StatusCodeDefines.LoginError,
                                                                                         null, null);

            return(response);
        }
        public Task <BodyResponse <GameSampleInfo> > Handle(GameSampleCommand request, CancellationToken cancellationToken)
        {
            BodyResponse <GameSampleInfo> response = new BodyResponse <GameSampleInfo>(StatusCodeDefines.LoginError, null, null);

            return(Task.FromResult(response));
        }
Exemple #13
0
        public static Message Receive(System.IO.Stream reader)
        {
            int totalRecv  = 0;
            int sizeToRead = 16;

            byte[] hBuffer = new byte[sizeToRead];

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];
                int    recv   = reader.Read(buffer, 0, sizeToRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(hBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            Header header = new Header(hBuffer);

            totalRecv = 0;
            byte[] bBuffer = new byte[header.BODYLEN];
            sizeToRead = (int)header.BODYLEN;

            while (sizeToRead > 0)
            {
                byte[] buffer = new byte[sizeToRead];
                int    recv   = reader.Read(buffer, 0, sizeToRead);
                if (recv == 0)
                {
                    return(null);
                }

                buffer.CopyTo(bBuffer, totalRecv);
                totalRecv  += recv;
                sizeToRead -= recv;
            }

            ISerializable body = null;

            switch (header.MSGTYPE)
            {
            case CONSTANTS.REQ_FILE_SEND:
                body = new BodyRequest(bBuffer);
                break;

            case CONSTANTS.REP_FILE_SEND:
                body = new BodyResponse(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_DATA:
                body = new BodyData(bBuffer);
                break;

            case CONSTANTS.FILE_SEND_RES:
                body = new BodyResult(bBuffer);
                break;

            default:
                throw new System.Exception(System.String.Format("Unknown MSGTYPE : {0}" + header.MSGTYPE));
            }

            return(new Message()
            {
                Header = header, Body = body
            });
        }
        public async Task <BodyResponse <MatchingResponseVM> > Playnow(long id)
        {
            BodyResponse <DummyMatchingResponseInfo> response = await _bus.SendCommand(new DummyPlaynowCommand(id));

            return(response.MapResponse <MatchingResponseVM>(_mapper));
        }