private async Task CompleteVersus(VersusContext context, Data.Entities.Versus versus)
        {
            versus.Status = "Closed";

            if (versus.OpponentIterations > versus.InitiatorIterations)
            {
                versus.WinnerName = versus.OpponentName;
            }
            else if (versus.InitiatorIterations > versus.OpponentIterations)
            {
                versus.WinnerName = versus.InitiatorName;
            }
            else
            {
                versus.WinnerName = "Drawn Game";
            }

            context.Entry(versus).State = EntityState.Modified;
            await context.SaveChangesAsync();

            try
            {
                string winner;
                winner = versus.WinnerName == "DrawnGame" ? "DrawnGame" : versus.WinnerName;

                var socketInitId = await UserToSocket(context, versus.InitiatorId);

                var socketOppId = await UserToSocket(context, versus.OpponentId);

                if (socketInitId != null)
                {
                    await SendMessageAsync(socketInitId,
                                           JsonSerializer.Serialize(
                                               new Dictionary <string, string>()
                    {
                        { "Type", "Result" },
                        { "Winner", winner }
                    }));
                }
                if (socketOppId != null)
                {
                    await SendMessageAsync(socketOppId,
                                           JsonSerializer.Serialize(
                                               new Dictionary <string, string>()
                    {
                        { "Type", "Result" },
                        { "Winner", winner }
                    }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while complete versus by reason of unexpected disconnect of member: "
                                  + ex.Message);
            }
        }
Beispiel #2
0
 private async Task FixVersusResults(Data.Entities.Versus versus)
 {
     try
     {
         await FixUserResults(
             versus.InitiatorId,
             versus.WinnerName == versus.InitiatorName || versus.WinnerName == "DrawnGame",
             versus.Exercise,
             versus.InitiatorIterations
             );
         await FixUserResults(
             versus.OpponentId,
             versus.WinnerName == versus.OpponentName || versus.WinnerName == "DrawnGame",
             versus.Exercise,
             versus.OpponentIterations
             );
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error while fixing results after versus: " + ex.Message);
     }
 }