43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using TOOHUCardAPI.Data.Repositories;
|
|
using TOOHUCardAPI.DTO;
|
|
|
|
namespace TOOHUCardAPI.Data.Services
|
|
{
|
|
public class GameConfigurationService
|
|
{
|
|
private readonly ILogger<GameConfigurationService> _logger;
|
|
private readonly CardRepository _cardRepository;
|
|
|
|
private List<string> _cardBlacklist = new()
|
|
{
|
|
"_",
|
|
"BonusEgg"
|
|
};
|
|
|
|
public GameConfigurationService(ILogger<GameConfigurationService> logger, CardRepository cardRepository)
|
|
{
|
|
_logger = logger;
|
|
_cardRepository = cardRepository;
|
|
}
|
|
|
|
public async Task<GameConfigResponse> GetGameConfiguration()
|
|
{
|
|
var random = new Random(DateTime.Now.Date.GetHashCode());
|
|
GameConfigResponse response = new GameConfigResponse();
|
|
var cards = await _cardRepository.GetAllCards();
|
|
var validCards =
|
|
cards.Where(card => _cardBlacklist.Select(bl => !card.CardName.Contains(bl)).All(res => res))
|
|
.Select(card => card.CardName).Append("all");
|
|
var enumerable = validCards as string[] ?? validCards.ToArray();
|
|
var index = random.Next(0, enumerable.Length);
|
|
response.LuckCard = enumerable.ElementAt(index);
|
|
response.GameMessage = response.GameMessage + $" Today's lucky card is {response.LuckCard}!";
|
|
return response;
|
|
}
|
|
}
|
|
} |