Exemple #1
0
        public static async Task<IList<Group>> LoadRandom(
            SocialGamificationAssetContext db,
            Group group,
            IList<CustomDataBase> customData,
            bool alliancesOnly = false,
            int limit = 1)
        {
            IQueryable<Group> query = db.Groups;

            if (alliancesOnly)
            {
                var alliancesList = Alliance.GetAllianceIds(db, group.Id, AllianceState.Accepted);

                query = query.Where(a => alliancesList.Contains(group.Id));
            }
            else
            {
                query = query.Where(g => g.Id != group.Id).Where(g => g.Type == GroupVisibility.Public);
            }

            // CustomData conditions
            var cQuery = Models.CustomData.ConditionBuilder(db, customData, CustomDataType.Group);
            IList<Guid> similarGroups = await cQuery.Select(c => c.ObjectId).Distinct().ToListAsync();

            // Check if Group satisfy CustomData constraints
            IList<Group> groups = await query.Where(g => similarGroups.Contains(g.Id)).ToListAsync();

            return GenericHelper.Shuffle(groups, limit);
        }
        public async Task<IActionResult> CreateGroup([FromBody] GroupFrom form)
        {
            if (!ModelState.IsValid)
            {
                return HttpResponseHelper.BadRequest(ModelState);
            }

            if (string.IsNullOrWhiteSpace(form.Name))
            {
                return HttpResponseHelper.BadRequest("Group name is required.");
            }

            if (await Group.ExistsUsername(_context, form.Name))
            {
                return HttpResponseHelper.BadRequest("Group with this name already exists.");
            }

            var group = new Group { Username = form.Name, Type = form.Type, AdminId = session.Player.Id };

            if (form.Players == null || form.Players.Count < 1)
            {
                return HttpResponseHelper.BadRequest("Group requires minimum 1 Player.");
            }

            IList<Player> players = new List<Player>();
            foreach (var playerId in form.Players)
            {
                var player = await _context.Players.FindAsync(playerId);
                if (player == null)
                {
                    return HttpResponseHelper.NotFound($"No Player with Id {playerId} exists.");
                }

                players.Add(player);
            }

            group.Players = players;

            _context.Groups.Add(group);

            var error = await SaveChangesAsync();
            if (error != null)
            {
                return error;
            }

            return CreatedAtRoute("GetGroupInfo", new { id = group.Id }, group);
        }