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);
            }
        }
示例#2
0
 public VersusController(
     VersusContext context,
     UserManager <User> um,
     IMobileMessagingClient mmc,
     MessagesHandler messagesHandler)
 {
     _context         = context;
     _userManager     = um;
     _mmc             = mmc;
     _messagesHandler = messagesHandler;
 }
示例#3
0
 public MessagesController(
     IMobileMessagingClient mmc,
     UserManager <User> um,
     VersusContext vc,
     MessagesHandler messagesHandler)
 {
     _mmc             = mmc;
     _userManager     = um;
     _messagesHandler = messagesHandler;
     _context         = vc;
 }
        private async Task <string> UserToSocket(VersusContext context, Guid userId)
        {
            var socket = await context.UserSockets
                         .FirstOrDefaultAsync(us => us.UserId == userId);

            if (socket == null)
            {
                return(null);
            }

            return(socket.SocketId);
        }
        public async Task FindJob(VersusContext context, UserManager <User> userManager, string exercise,
                                  Guid versusId, string userName)
        {
            bool flag = true;

            while (flag)
            {
                var result = await Find(context, userManager, exercise, versusId, userName);

                if (result == 404)
                {
                    var versus = await context.Versus.FindAsync(versusId);

                    versus.Status = "BotReady";
                    await context.SaveChangesAsync();

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

                    if (socketId != null)
                    {
                        await SendMessageAsync(socketId,
                                               JsonSerializer.Serialize(
                                                   new Dictionary <string, string>
                        {
                            { "Type", "NotFound" }
                        }));
                    }

                    flag = false;
                }
                else if (result == 200)
                {
                    flag = false;
                }
            }
        }
 public ExercisesController(VersusContext context, UserManager <User> um)
 {
     _context     = context;
     _userManager = um;
 }
示例#7
0
 public PhotoController(UserManager <User> userManager, IWebHostEnvironment appEnvironment, VersusContext context)
 {
     _userManager    = userManager;
     _appEnvironment = appEnvironment;
     _context        = context;
 }
 public NotificationsController(VersusContext context, UserManager <User> um)
 {
     _context     = context;
     _userManager = um;
 }
示例#9
0
 public SettingsController(VersusContext context, UserManager <User> um)
 {
     _context     = context;
     _userManager = um;
 }
示例#10
0
        private async Task FixUserResults(UserManager <User> _userManager, VersusContext _context,
                                          Guid userId, bool isWin, string ex, int num)
        {
            if (ex == "pushups")
            {
                var user = await _userManager.Users
                           .Include(u => u.Exercises)
                           .ThenInclude(e => e.PushUps)
                           .FirstOrDefaultAsync(u => u.Id == userId);

                if (isWin)
                {
                    user.Exercises.PushUps.Wins++;
                }
                else
                {
                    user.Exercises.PushUps.Losses++;
                }
                if (num > user.Exercises.PushUps.HighScore)
                {
                    user.Exercises.PushUps.HighScore = num;
                }
                await _userManager.UpdateAsync(user);

                await _context.SaveChangesAsync();
            }
            else if (ex == "pullups")
            {
                var user = await _userManager.Users
                           .Include(u => u.Exercises)
                           .ThenInclude(e => e.PullUps)
                           .FirstOrDefaultAsync(u => u.Id == userId);

                if (isWin)
                {
                    user.Exercises.PullUps.Wins++;
                }
                else
                {
                    user.Exercises.PullUps.Losses++;
                }
                if (num > user.Exercises.PullUps.HighScore)
                {
                    user.Exercises.PullUps.HighScore = num;
                }
                await _userManager.UpdateAsync(user);

                await _context.SaveChangesAsync();
            }
            else if (ex == "abs")
            {
                var user = await _userManager.Users
                           .Include(u => u.Exercises)
                           .ThenInclude(e => e.Abs)
                           .FirstOrDefaultAsync(u => u.Id == userId);

                if (isWin)
                {
                    user.Exercises.Abs.Wins++;
                }
                else
                {
                    user.Exercises.Abs.Losses++;
                }
                if (num > user.Exercises.Abs.HighScore)
                {
                    user.Exercises.Abs.HighScore = num;
                }
                await _userManager.UpdateAsync(user);

                await _context.SaveChangesAsync();
            }
            else
            {
                var user = await _userManager.Users
                           .Include(u => u.Exercises)
                           .ThenInclude(e => e.Squats)
                           .FirstOrDefaultAsync(u => u.Id == userId);

                if (isWin)
                {
                    user.Exercises.Squats.Wins++;
                }
                else
                {
                    user.Exercises.Squats.Losses++;
                }
                if (num > user.Exercises.Squats.HighScore)
                {
                    user.Exercises.Squats.HighScore = num;
                }
                await _userManager.UpdateAsync(user);

                await _context.SaveChangesAsync();
            }
        }
示例#11
0
        private async Task <int> Find(VersusContext context, UserManager <User> userManager, string exercise,
                                      Guid versusId, string userName)
        {
            var user = await userManager.FindByNameAsync(userName);

            var versus = await context.Versus
                         .Include(v => v.RejectedUser)
                         .FirstOrDefaultAsync(v =>
                                              v.Id == versusId);

            var opponent = await userManager.Users
                           .Include(u => u.Settings)
                           .FirstOrDefaultAsync(u => u.UserName != user.UserName && u.Online &&
                                                !context.Versus.Any(v => (v.InitiatorId == u.Id || v.OpponentId == u.Id ||
                                                                          v.LastInvitedId == versusId) && v.Status != "Canceled" && v.Status != "Closed" ||
                                                                    v.LastInvitedId == u.Id && v.Status == "Searching")
                                                // && u.Settings.IsNotifications
                                                && !versus.RejectedUser.Select(rj => rj.UserId).Contains(u.Id));

            if (opponent == null)
            {
                versus.Status = "Canceled";
                context.Entry(versus).State = EntityState.Modified;
                await context.SaveChangesAsync();

                return(404);
            }

            try
            {
                context.VersusUsers.Add(new VersusUser
                {
                    UserId   = opponent.Id,
                    VersusId = versus.Id,
                });
                versus.LastInvitedId        = opponent.Id;
                context.Entry(versus).State = EntityState.Modified;
                await context.SaveChangesAsync();

                var socketId = await UserToSocket(context, opponent.Id);

                if (socketId != null)
                {
                    await SendMessageAsync(socketId,
                                           JsonSerializer.Serialize(
                                               new Dictionary <string, string>()
                    {
                        { "Type", "Invite" },
                        { "Exercise", exercise },
                        { "UserName", userName }
                    }));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An errof while send invite message " + ex.Message);
                return(500);
            }

            return(200);
        }
示例#12
0
 public AuthController(IAuthService auth, VersusContext vc, UserManager <User> um)
 {
     _auth        = auth;
     _context     = vc;
     _userManager = um;
 }