コード例 #1
0
        public async Task TryMove(User u, string gK, string xy1xy2)
        {
            if (xy1xy2.Length < 4)
            {
                return;
            }
            int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

            if (!int.TryParse(xy1xy2[0].ToString(), out x1) ||
                !int.TryParse(xy1xy2[1].ToString(), out y1) ||
                !int.TryParse(xy1xy2[2].ToString(), out x2) ||
                !int.TryParse(xy1xy2[3].ToString(), out y2))
            {
                return;
            }

            var g = this.Games[gK];

            if (g == null || g.gameEnd)
            {
                return;
            }
            Side uSide = Side.none;

            if (u.userId == g.host.userId)
            {
                uSide = g.options.direction == GameDirection.hostWhite ? Side.white : Side.black;
            }
            else if (u.userId == g.guest.userId)
            {
                uSide = g.options.direction != GameDirection.hostWhite ? Side.white : Side.black;
            }
            else
            {
                return;
            }

            var f  = g.board[x1][y1];
            var nf = g.board[x2][y2];

            if (f.side != uSide || nf.side == uSide || g.moveCount % 2 != (int)uSide)
            {
                return;
            }

            g.board[x2][y2] = f;
            g.board[x1][y1] = new Figure {
                side = Side.none, type = FigureType.empty
            };

            g.moveCount++;

            var arr = new object[1];

            arr[0] = "Success";//JsonConvert.SerializeObject(true);
            var xy1  = Number2String(y1 + 1, true) + (x1 + 1).ToString();
            var xy2  = Number2String(y2 + 1, true) + (x2 + 1).ToString();
            var kill = nf.type != FigureType.empty ? "X" : "";

            g.history += GetLogMove(string.Format("{0} moved {1} {2} =>{4} {3}", u.login, f.type.ToString(), xy1, xy2, kill));

            if (nf.type == FigureType.king)
            {
                g.history += GetLogMove(string.Format("{0} killed enemy king, game over", u.login));
                g.gameEnd  = true;
                if (g.options.isRaiting)
                {
                    UserDataProvider userData = new UserDataProvider();
                    var host  = userData.GetUser(g.host.userId);
                    var guest = userData.GetUser(g.guest.userId);

                    if (host != null && guest != null)
                    {
                        host.score  += u.userId == host.userId ? 25 : -25;
                        guest.score += u.userId == guest.userId ? 25 : -25;
                        userData.EditUser(host);
                        userData.EditUser(guest);
                    }
                }
            }
            await Startup.gameHandler.InvokeClientMethodAsync(g.hostCS, "sendMoveResp", arr);
        }