Beispiel #1
0
        public static async Task<IList<Player>> LoadRandom(
            SocialGamificationAssetContext db,
            Player player,
            IList<CustomDataBase> customData,
            bool alliancesOnly = false,
            int limit = 1)
        {
            var query = db.Players.Where(a => a.Role == AccountType.Player);

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

                query = query.Where(p => alliancesList.Contains(p.Id));
            }
            else
            {
                query = query.Where(p => p.Id != player.Id);
            }

            // CustomData conditions
            var customDataQuery = Models.CustomData.ConditionBuilder(db, customData, CustomDataType.Player);
            if (customDataQuery != null)
            {
                IList<Guid> similarPlayers = await customDataQuery.Select(c => c.ObjectId).Distinct().ToListAsync();

                // Check if Players satisfy CustomData constraints
                query = query.Where(p => similarPlayers.Contains(p.Id));
            }

            IList<Player> players = await query.ToListAsync();

            return GenericHelper.Shuffle(players, limit);
        }
        public async Task<IActionResult> AddPlayer([FromBody] UserForm register)
        {
            if (!ModelState.IsValid)
            {
                return HttpResponseHelper.BadRequest(ModelState);
            }

            if (string.IsNullOrWhiteSpace(register.Username) && string.IsNullOrWhiteSpace(register.Email))
            {
                return HttpResponseHelper.BadRequest("Either Username or Email is required.");
            }

            if (string.IsNullOrWhiteSpace(register.Password))
            {
                return HttpResponseHelper.BadRequest("Password is required.");
            }

            var player = new Player();

            if (!string.IsNullOrWhiteSpace(register.Username))
            {
                if (await Player.ExistsUsername(_context, register.Username))
                {
                    return HttpResponseHelper.BadRequest("Player with this Username already exists.");
                }

                player.Username = register.Username;
            }

            if (!string.IsNullOrWhiteSpace(register.Email))
            {
                if (await Player.ExistsEmail(_context, register.Email))
                {
                    return HttpResponseHelper.BadRequest("Player with this Email already exists.");
                }

                player.Email = register.Email;
            }

            player.Password = PasswordHelper.HashPassword(register.Password);

            _context.Players.Add(player);

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

            // Add or Update the CustomData
            error = await player.AddOrUpdateCustomData(_context, register.CustomData);
            if (error != null)
            {
                return error;
            }

            return CreatedAtRoute("GetPlayer", new { id = player.Id }, player);
        }