/// <summary> /// Pay a group his weekly pay (only if is the correct day) /// </summary> /// <param name="_context">The database context</param> /// <param name="hub">The notification hub</param> public static void pay(ApplicationDBContext _context, IHubContext <NotificationHub> hub) { DayOfWeek today = DateTime.Now.DayOfWeek; int day = DateTime.Now.DayOfYear; int year = DateTime.Now.Year; _context.Group.ToList().ForEach(group => { if (today != group.dateCreated.DayOfWeek) { return; } if (day == group.dateCreated.DayOfYear && year == group.dateCreated.Year) { return; } _context.Entry(group).Collection("users").Load(); int moreCoins = group.weeklyPay; Areas.Home.Util.GroupNew.launch(null, group, null, Areas.Home.Models.TypeGroupNew.PAID_PLAYERS_GROUPS, false, _context); group.users.ToList().ForEach(async u => { u.coins += moreCoins; _context.Entry(u).Reference("User").Load(); User recv = u.User; Areas.Home.Util.GroupNew.launch(recv, group, null, Areas.Home.Models.TypeGroupNew.PAID_PLAYERS_USER, false, _context); await SendNotification.send(hub, group.name, recv, Areas.Alive.Models.NotificationType.PAID_GROUPS, _context); }); }); _context.SaveChanges(); }
/// <summary> /// Manages the action when the maker of a group leaves it /// </summary> /// <param name="members">All the members of the group</param> /// <param name="maker">The group-maker role</param> /// <param name="admin">The group-admin role</param> /// <param name="normal">The group-normal role</param> /// <param name="leave">True if the maker has leaved the group, false otherwise</param> /// <param name="_context">The database context</param> /// <param name="hub">The notification hub</param> public static async Task manageQuitMaker(List <UserGroup> members, Role maker, Role admin, Role normal, bool leave, ApplicationDBContext _context, IHubContext <NotificationHub> hub) { List <UserGroup> adminMembers = members.Where(m => m.role == admin).OrderBy(d => d.dateRole).ToList(); List <UserGroup> normalMembers = members.Where(m => m.role == normal).OrderBy(d => d.dateJoin).ToList(); UserGroup newMaster; if (adminMembers.Count() != 0) //The older admin in the group will become in the group maker { newMaster = adminMembers.First(); } else //If there isn't any admin, the older member in the group will become in the group make { newMaster = normalMembers.First(); } newMaster.role = maker; newMaster.dateRole = DateTime.Today; _context.Update(newMaster); _context.SaveChanges(); _context.Entry(newMaster).Reference("User").Load(); _context.Entry(newMaster).Reference("Group").Load(); Home.Util.GroupNew.launch(newMaster.User, newMaster.Group, null, Home.Models.TypeGroupNew.MAKE_MAKER_GROUP, leave, _context); Home.Util.GroupNew.launch(newMaster.User, newMaster.Group, null, Home.Models.TypeGroupNew.MAKE_MAKER_USER, leave, _context); await SendNotification.send(hub, newMaster.Group.name, newMaster.User, Alive.Models.NotificationType.MAKE_MAKER, _context); }
/// <summary> /// Sends the notification to the receiver /// </summary> /// <param name="user">The maker of the dm</param> /// <param name="recv">The receiver of the dm</param> /// <param name="title">The dm just created</param> private async Task sendNotification(User user, User recv, DirectMessageTitle title) { bool isAdmin = user.role == RoleManager.getAdmin(_context); NotificationType type = isAdmin ? NotificationType.OPEN_DM_FROM_ADMIN : NotificationType.OPEN_DM_FROM_USER; String recvName = isAdmin ? "" : recv.nickname; EmailSender.sendOpenCreateDMNotification(recv.email, recv.nickname, title.title); await SendNotification.send(_hub, recvName, recv, type, _context); }
// // ──────────────────────────────────────────────────────────────────────────────────── // :::::: P R I V A T E F U N C T I O N S : : : : : : : : // ──────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Send the news to group and user and the notification to the user /// </summary> /// <param name="targetUser">The member of the group that has been blocked/unblocked</param> /// <param name="group">The group where the member has been blocked/unblocked</param> /// <param name="makeUnmake">True if the user has been blocked, false otherwise</param> private async Task sendMessages(UserGroup targetUser, Group group, bool makeUnmake) { _context.Entry(targetUser).Reference("User").Load(); //Send home news Home.Util.GroupNew.launch(targetUser.User, group, null, Home.Models.TypeGroupNew.BLOCK_USER_USER, makeUnmake, _context); Home.Util.GroupNew.launch(targetUser.User, group, null, Home.Models.TypeGroupNew.BLOCK_USER_GROUP, makeUnmake, _context); //Send notifications NotificationType typeNotification = makeUnmake ? NotificationType.BLOCKED : NotificationType.UNBLOCKED; await SendNotification.send(_notificationsHub, group.name, targetUser.User, typeNotification, _context); }
/// <summary> /// Kick a user out from the group /// </summary> /// <param name="order">The info to kick a user from the group</param> /// See <see cref="Areas.GroupManage.Models.KickUser"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public async System.Threading.Tasks.Task <IActionResult> removeUserAsync([FromBody] KickUser order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } UserGroup targetUser = new UserGroup(); Group group = new Group(); if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.REMOVE_USER, false)) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { _context.Entry(targetUser).Reference("User").Load(); User sendNew = targetUser.User; Guid targetUserid = targetUser.User.id; await QuitUserFromGroup.quitUser(targetUser, _context, _notificationHub); await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub); InteractionManager.manageInteraction(targetUser.User, group, interactionType.KICKED, _context); using (var scope = _scopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>(); group = dbContext.Group.Where(g => g.name == order.groupName).First(); User recv = dbContext.User.Where(u => u.id == targetUserid).First(); user = dbContext.User.Where(u => u.id == user.id).First(); Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_USER, false, dbContext); Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_GROUP, false, dbContext); await SendNotification.send(_notificationHub, group.name, recv, NotificationType.KICKED_GROUP, dbContext); return(Ok(GroupPageManager.GetPage(user, group, dbContext))); } } catch (Exception) { return(StatusCode(500)); } }
// // ──────────────────────────────────────────────────────────────────────────────────── // :::::: P R I V A T E F U N C T I O N S : : : : : : : : // ──────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Send the news and the notifications to the group and the new admin /// </summary> /// <param name="target"></param> /// <param name="group"></param> /// <param name="makeUnmake"></param> /// <returns></returns> private async Task sendNews(UserGroup target, Group group, bool makeUnmake) { //Send home news _context.Entry(target).Reference("User").Load(); User targetUser = target.User; Home.Util.GroupNew.launch(targetUser, group, null, Home.Models.TypeGroupNew.MAKE_ADMIN_USER, makeUnmake, _context); Home.Util.GroupNew.launch(targetUser, group, null, Home.Models.TypeGroupNew.MAKE_ADMIN_GROUP, makeUnmake, _context); //Send notifications NotificationType typeNotification = makeUnmake ? NotificationType.MAKE_ADMIN : NotificationType.UNMAKE_ADMIN; await SendNotification.send(_hub, group.name, targetUser, typeNotification, _context); }
/// <summary> /// Send the news and notifications to the group members /// </summary> /// <param name="group">The group which has been banned or unbanned</param> /// <param name="ban">True if the group has been banned, false otherwise</param> private void sendNews(Group group, bool ban) { _context.Entry(group).Collection("users").Load(); group.users.ToList().ForEach(async u => { _context.Entry(u).Reference("User").Load(); User recv = u.User; Home.Util.GroupNew.launch(recv, group, null, Home.Models.TypeGroupNew.BAN_GROUP, ban, _context); NotificationType type = ban ? NotificationType.BAN_GROUP : NotificationType.UNBAN_GROUP; await SendNotification.send(_hub, group.name, recv, type, _context); }); }
/// <summary> /// Launch the news for the group and for the members on it /// </summary> /// <param name="u">The user who has launch the fb</param> /// <param name="group">The group where the new fb has been launched</param> /// <param name="fb">The fb that has been launched</param> private void launchNews(User u, Group group, FootballBet fb) { _context.Entry(group).Collection("users").Load(); Home.Util.GroupNew.launch(null, group, fb, Home.Models.TypeGroupNew.LAUNCH_FOOTBALLBET_GROUP, false, _context); group.users.Where(g => !g.blocked).ToList().ForEach(async ug => { _context.Entry(ug).Reference("User").Load(); bool isLauncher = ug.userid == u.id; User recv = ug.User; Home.Util.GroupNew.launch(recv, group, fb, Home.Models.TypeGroupNew.LAUNCH_FOOTBALLBET_USER, isLauncher, _context); await SendNotification.send(_hub, group.name, recv, Alive.Models.NotificationType.NEW_FOOTBALLBET, _context); }); }
// // ──────────────────────────────────────────────────────────────────────────────────── // :::::: P R I V A T E F U N C T I O N S : : : : : : : : // ──────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Send the news and notifications to the members of the group /// </summary> /// <param name="target">The member of the group to send the new and notification</param> /// <param name="group">The group just removed</param> /// <param name="_context">The database context</param> /// <param name="hub">The notification hub</param> /// <param name="roleMaker">The group-maker role</param> private static async Task sendNews(UserGroup target, Group group, ApplicationDBContext _context, IHubContext <NotificationHub> hub, Role roleMaker) { _context.Entry(target).Reference("User").Load(); _context.Entry(target).Reference("role").Load(); if (!target.blocked) { Home.Util.GroupNew.launch(target.User, group, null, Home.Models.TypeGroupNew.REMOVE_GROUP, target.role == roleMaker, _context); } try { await SendNotification.send(hub, group.name, target.User, NotificationType.GROUP_REMOVED, _context); } catch (Exception) { return; } }
/// <summary> /// Return the coins to the users who bet in that fb /// </summary> /// <param name="bet">The fb</param> /// <param name="group">The group where the fb was launched</param> private void getMoneyBackAndLaunchNews(FootballBet bet, Group group) { _context.Entry(bet).Collection("userBets").Load(); _context.Entry(group).Collection("users").Load(); bool isJackpot = CheckBetType.isJackpot(bet, _context); bet.userBets.ToList().ForEach(ub => { _context.Entry(ub).Reference("User").Load(); UserGroup userg = group.users.Where(u => u.userid == ub.userid).First(); int coinsBet = ub.bet; if (isJackpot || ub.valid) { userg.coins += coinsBet; } else { int retCoins = CheckBetType.calculateCancelRate(bet, coinsBet, _context); userg.coins += (coinsBet - retCoins); } _context.UserGroup.Update(userg); _context.SaveChanges(); _context.Entry(userg).Reference("User").Load(); }); _context.UserFootballBet.RemoveRange(bet.userBets.ToList()); _context.SaveChanges(); _context.Entry(group).Collection("users"); group.users.ToList().ForEach(async u => { _context.Entry(u).Reference("role").Load(); _context.Entry(u).Reference("User").Load(); User recv = u.User; Home.Util.GroupNew.launch(recv, group, bet, Home.Models.TypeGroupNew.FOOTBALLBET_CANCELLED_USER, u.role == RoleManager.getGroupMaker(_context), _context); await SendNotification.send(_hub, group.name, recv, Alive.Models.NotificationType.CANCELLED_FOOTBALLBET, _context); }); }
/// <summary> /// Launch the news and notifications for the group and users of the fb /// </summary> /// <param name="fb">The fb</param> /// <param name="_context">The database context</param> /// <param name="hub">The notification hub</param> private static void launchNews(FootballBet fb, ApplicationDBContext _context, IHubContext<NotificationHub> hub) { _context.Entry(fb).Reference("Group").Load(); Group group = fb.Group; List<User> newGroups = new List<User>(); Areas.Home.Util.GroupNew.launch(null, group, fb, Areas.Home.Models.TypeGroupNew.PAID_BETS_GROUP, false, _context); _context.Entry(fb).Collection("userBets").Load(); fb.userBets.ToList().ForEach(u => { _context.Entry(u).Reference("User").Load(); if (newGroups.All(uu => uu.id != u.userid)) newGroups.Add(u.User); }); newGroups.ForEach(async u => { Areas.Home.Util.GroupNew.launch(u, group, fb, Areas.Home.Models.TypeGroupNew.PAID_BETS_USER, false, _context); await SendNotification.send(hub, group.name, u, Areas.Alive.Models.NotificationType.PAID_BETS, _context); }); }
/// <summary> /// Send the notification to the user /// </summary> /// <param name="recv">The receiver of the notification</param> private async Task sendNotification(User recv) { await SendNotification.send(_hub, "", recv, Alive.Models.NotificationType.RECEIVED_DM, _context); }
/// <summary> /// Send the notification to the user /// </summary> /// <param name="recv">The receiver of the notification</param> /// <param name="open">True if the conversation has been opened, false otherwise</param> private async Task sendNotification(User recv, bool open) { NotificationType type = open ? NotificationType.REOPEN_DM : NotificationType.CLOSE_DM; await SendNotification.send(_hub, "", recv, type, _context); }