コード例 #1
0
        public async Task <Round> MakeMove(string roomId, string sessionId, int move)
        {
            var tasks = Task.Factory.StartNew(async() =>
            {
                var accountId = _accountManager.GetActiveAccountBySessionId(sessionId).Id;
                ActiveRounds.TryGetValue(roomId, out var thisRound);

                if (thisRound == null)
                {
                    return(null); //todo: exception;
                }
                if (thisRound.IsFinished)
                {
                    return(thisRound);
                }

                //************************************************************************************************************************************
                var elapsedTime = DateTime.Now.Subtract(thisRound.TimeFinished);
                if (elapsedTime.Seconds >= 200 &&
                    thisRound.PlayerMoves.Any(x => x.Value.Equals(RequiredGameMove.Default)))
                {
                    var dictionary = thisRound.PlayerMoves;
                    var first      = dictionary.Keys.First();
                    var last       = dictionary.Keys.Last();

                    if (dictionary[first] == dictionary[last])
                    {
                        thisRound.IsDraw = false;
                    }
                    else if (dictionary[first] == RequiredGameMove.Default)
                    {
                        thisRound.LoserId  = first;
                        thisRound.WinnerId = last;
                    }
                    else
                    {
                        thisRound.LoserId  = last;
                        thisRound.WinnerId = first;
                    }
                    thisRound.TimeFinished = DateTime.Now;
                    thisRound.IsFinished   = true;

                    await UpdateRound(thisRound);
                    return(thisRound);
                }

                thisRound.TimeFinished = DateTime.Now;

                //************************************************************************************************************************

                var botPlays = false;
                if (thisRound.PlayerMoves.Any(x => x.Key.Equals("Bot")))
                {
                    thisRound.PlayerMoves = RockPaperScissors.ChangeBotState(thisRound.PlayerMoves);
                    botPlays = true;
                }
                thisRound.PlayerMoves = RockPaperScissors.UpdateMove(thisRound.PlayerMoves, accountId, move);

                if (thisRound.PlayerMoves.Values.All(x => x != RequiredGameMove.Default))
                {
                    var winner = RockPaperScissors.MoveComparator(thisRound.PlayerMoves);

                    if (string.IsNullOrEmpty(winner))
                    {
                        thisRound.IsDraw   = false;
                        thisRound.WinnerId = "DRAW";
                        thisRound.LoserId  = "DRAW";
                        await UpdateRound(thisRound);
                    }

                    if (botPlays)
                    {
                        thisRound.IsFinished  = true;
                        var thisPlayerKey     =
                            thisRound.LoserId = thisRound.PlayerMoves
                                                .FirstOrDefault(x => x.Key != "Bot").Key;
                        if (winner == "Bot")
                        {
                            thisRound.WinnerId = winner;
                            thisRound.LoserId  = _accountManager.AccountsActive.FirstOrDefault(x => x.Value.Id == thisPlayerKey).Value.Login;
                        }
                        else
                        {
                            thisRound.WinnerId = _accountManager.AccountsActive.FirstOrDefault(x => x.Value.Id == winner).Value.Login;
                            thisRound.LoserId  = "Bot";
                        }
                    }

                    else
                    {
                        var loserId = thisRound.PlayerMoves.FirstOrDefault(x => x.Key != winner).Key;

                        ////////////////////////////////////////////////////////////////////////////
                        if (thisRound.WinnerId == "DRAW" || thisRound.LoserId == "DRAW")
                        {
                            thisRound.IsFinished   = true;
                            thisRound.WinnerId     = "DRAW";
                            thisRound.LoserId      = "DRAW";
                            thisRound.TimeFinished = DateTime.Now;
                        }
                        else
                        {
                            thisRound.IsFinished   = true;
                            thisRound.WinnerId     = _accountManager.AccountsActive.FirstOrDefault(x => x.Value.Id == winner).Value.Login;
                            thisRound.LoserId      = _accountManager.AccountsActive.FirstOrDefault(x => x.Value.Id == loserId).Value.Login;
                            thisRound.TimeFinished = DateTime.Now;
                        }
                        _storageRounds.Add(thisRound);
                        await FillStatistics(thisRound);
                    }
                }

                await UpdateRound(thisRound);

                return(thisRound);
            });

            return(await await tasks);  //AWAIT AWAIT?
        }