Example #1
0
        /// <summary>
        /// 强制退出游戏
        /// </summary>
        /// <param name="req"><see cref="StopGameRequest"/></param>
        /// <returns><see cref="StopGameResponse"/></returns>
        public StopGameResponse StopGameSync(StopGameRequest req)
        {
            JsonResponseModel <StopGameResponse> rsp = null;

            try
            {
                var strResp = this.InternalRequestSync(req, "StopGame");
                rsp = JsonConvert.DeserializeObject <JsonResponseModel <StopGameResponse> >(strResp);
            }
            catch (JsonSerializationException e)
            {
                throw new TencentCloudSDKException(e.Message);
            }
            return(rsp.Response);
        }
Example #2
0
        public async Task <StopGameResponse> StopGameAsync(StopGameRequest request)
        {
            LogInfo($"Stop game: ConnectionId = {CurrentRequest.RequestContext.ConnectionId}");

            // fetch game record from table
            var gameRecord = await _table.GetAsync <GameRecord>(request.GameId);

            if (gameRecord == null)
            {
                LogInfo("No game found to stop");

                // game is already stopped, nothing further to do
                return(new StopGameResponse());
            }

            // check if game state machine needs to be stopped
            if (gameRecord.GameLoopArn != null)
            {
                LogInfo($"Stopping Step Function: Name = {gameRecord.GameLoopArn}");
                try {
                    await _stepFunctionsClient.StopExecutionAsync(new StopExecutionRequest {
                        ExecutionArn = gameRecord.GameLoopArn,
                        Cause        = "user requested game to be stopped"
                    });
                } catch (Exception e) {
                    LogErrorAsInfo(e, "unable to stop state-machine");
                }
            }

            // delete game record
            LogInfo($"Deleting game record: ID = {request.GameId}");
            await _table.DeleteAsync <GameRecord>(request.GameId);

            // update game state to indicated it was stopped
            gameRecord.Game.Status = GameStatus.Finished;
            ++gameRecord.Game.TotalTurns;
            gameRecord.Game.Messages.Add(new Message {
                GameTurn = gameRecord.Game.TotalTurns,
                Text     = "Game stopped."
            });

            // return final game state
            return(new StopGameResponse {
                Game = gameRecord.Game
            });
        }