121 lines
4.4 KiB
C#
121 lines
4.4 KiB
C#
namespace qtc_api.Services.CurrencyGamesService
|
|
{
|
|
public class CurrencyGamesService : IHostedService, IDisposable
|
|
{
|
|
private Timer? Timer { get; set; }
|
|
private int CurrentPricePerStock { get; set; }
|
|
|
|
private IServiceScopeFactory _scopeFactory;
|
|
private ILogger<CurrencyGamesService> logger;
|
|
public CurrencyGamesService(IServiceScopeFactory scopeFactory, ILogger<CurrencyGamesService> logger)
|
|
{
|
|
_scopeFactory = scopeFactory;
|
|
this.logger = logger;
|
|
}
|
|
|
|
// Stock Market
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
Timer = new Timer(Timer_Elapsed, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
|
|
|
|
logger.LogInformation("Stock Market Loop Started");
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
Timer?.Change(Timeout.Infinite, 0);
|
|
|
|
logger.LogInformation("Stock Market Loop Stopped");
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Timer?.Dispose();
|
|
}
|
|
|
|
public ServiceResponse<int> GetCurrentPricePerStock() { return new ServiceResponse<int> { Success = true, Data = CurrentPricePerStock }; }
|
|
|
|
public async Task<ServiceResponse<UserStockActionResultDto>> BuyStock(User user, int amount)
|
|
{
|
|
using (var scope = _scopeFactory.CreateScope())
|
|
{
|
|
var userServiceScoped = scope.ServiceProvider.GetRequiredService<IUserService>();
|
|
|
|
int total = 0;
|
|
for (int i = 0; i < amount; i++)
|
|
{
|
|
total += CurrentPricePerStock;
|
|
}
|
|
|
|
if (total > user.CurrencyAmount) return new ServiceResponse<UserStockActionResultDto> { Success = false, Message = "Cannot Afford Stock(s)" };
|
|
|
|
var currencyResult = await userServiceScoped.RemoveCurrencyFromUser(user.Id, total);
|
|
var stockResult = await userServiceScoped.AddStockToUser(user.Id, amount);
|
|
|
|
return new ServiceResponse<UserStockActionResultDto> { Success = true, Data = new UserStockActionResultDto { StockAmount = stockResult.Data, CurrencyAmount = currencyResult.Data } };
|
|
}
|
|
}
|
|
|
|
public async Task<ServiceResponse<UserStockActionResultDto>> SellStock(User user, int amount)
|
|
{
|
|
using (var scope = _scopeFactory.CreateScope())
|
|
{
|
|
var userServiceScoped = scope.ServiceProvider.GetRequiredService<IUserService>();
|
|
|
|
if (amount > user.StockAmount) return new ServiceResponse<UserStockActionResultDto> { Success = false, Message = "Not Enough Stocks" };
|
|
|
|
int total = 0;
|
|
for (int i = 0; i < amount; i++)
|
|
{
|
|
total += CurrentPricePerStock;
|
|
}
|
|
|
|
var currencyResult = await userServiceScoped.AddCurrencyToUser(user.Id, total, false);
|
|
var stockResult = await userServiceScoped.RemoveStockFromUser(user.Id, amount);
|
|
|
|
return new ServiceResponse<UserStockActionResultDto> { Success = true, Data = new UserStockActionResultDto { StockAmount = stockResult.Data, CurrencyAmount = currencyResult.Data } };
|
|
}
|
|
}
|
|
|
|
// Number Guess
|
|
|
|
public ServiceResponse<int> GetRandomNumber()
|
|
{
|
|
Random rnd = new Random();
|
|
return new ServiceResponse<int> { Success = true, Data = rnd.Next(1, 100) };
|
|
}
|
|
|
|
public ServiceResponse<NumberGuessResult> GuessRandomNumber(int original, int guess)
|
|
{
|
|
var response = new ServiceResponse<NumberGuessResult> { Success = true };
|
|
|
|
if (original - guess == 10) response.Data = NumberGuessResult.Lower;
|
|
else if (guess - original == 10) response.Data = NumberGuessResult.Higher;
|
|
else if (guess == original) response.Data = NumberGuessResult.Correct;
|
|
else response.Data = NumberGuessResult.Incorrect;
|
|
|
|
return response;
|
|
}
|
|
|
|
private void Timer_Elapsed(object? state)
|
|
{
|
|
Random rnd = new Random();
|
|
CurrentPricePerStock = rnd.Next(5, 200);
|
|
logger.LogInformation($"Current Price Per Stock - {CurrentPricePerStock}");
|
|
}
|
|
|
|
public enum NumberGuessResult
|
|
{
|
|
Higher,
|
|
Lower,
|
|
Correct,
|
|
Incorrect
|
|
}
|
|
}
|
|
}
|