コード例 #1
0
        public async Task PlayGame(RequestContext ctx)
        {
            //System.Diagnostics.Debugger.Break();

            string replyMsgName = "play_game_reply";

            string reqstr = ctx.Data.ToString();

            if (reqstr.Trim().Length <= 0)
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    msg           = replyMsgName,
                    error_code    = -1,
                    error_message = "Invalid request"
                }));

                return;
            }

            ctx.Logger.Info("Play Cards - " + reqstr);

            dynamic req = ctx.JsonHelper.ToJsonObject(reqstr);

            string playerId     = req.player_id;
            string merchantCode = req.merchant_code;
            string currencyCode = req.currency_code;
            string tableCode    = req.table_code;
            string gameInput    = req.game_input;

            string frontEnd = m_LocalNode.GetName();

            if (string.IsNullOrEmpty(frontEnd))
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    msg           = replyMsgName,
                    error_code    = -2,
                    error_message = "Service not available"
                }));

                return;
            }

            var remoteInfo = "";
            var rets       = await RemoteCaller.BroadcastCall(ctx.RemoteServices, "game-table", "find-game-table", tableCode);

            foreach (var item in rets)
            {
                if (item.Value == "ok")
                {
                    remoteInfo = item.Key;
                    break;
                }
            }
            if (string.IsNullOrEmpty(remoteInfo))
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    msg           = replyMsgName,
                    error_code    = -2,
                    error_message = "Service not available"
                }));

                return;
            }

            var tablereq = new
            {
                player_id     = playerId,
                merchant_code = merchantCode,
                currency_code = currencyCode,
                table_code    = tableCode,
                game_input    = gameInput
            };

            var reply = await RemoteCaller.SpecifiedCall(ctx.RemoteServices, RemoteCaller.GetServerNameFromRemoteInfo(remoteInfo),
                                                         "game-table", "play-game", ctx.JsonHelper.ToJsonString(tablereq));

            if (string.IsNullOrEmpty(reply))
            {
                reply = ctx.JsonHelper.ToJsonString(new
                {
                    msg           = replyMsgName,
                    error_code    = -8,
                    error_message = "Failed to call play game function"
                });
            }
            else
            {
                dynamic ret = ctx.JsonHelper.ToJsonObject(reply);
                ret.msg = replyMsgName;
                reply   = ctx.JsonHelper.ToJsonString(ret);
            }

            await ctx.Session.Send(reply);
        }