public async Task <HolderAccount> GetOrCreateHolderAccount(string userId, BotPlatform platform) { if (platform == BotPlatform.Discord) { throw new ArgumentException($"Cannot create a holder account for platform {platform.GetValueName()}", nameof(platform)); } var existing = await _db.HolderAccounts .FirstOrDefaultAsync(x => x.DeletedAt == null && x.UserId == userId && x.Platform == platform ); if (existing != null) { return(existing); } Log.Debug("Creating new holder account for {UserId} on {Platform}", userId, platform.GetValueName()); var account = new HolderAccount { UserId = userId, Platform = platform, }; await _db.HolderAccounts.AddAsync(account); await _db.SaveChangesAsync(); return(account); }
public async Task MineCoin(string userId, BotPlatform platform, bool isBruteForce) { var linkedDiscordAccount = await GetLinkedAccount(userId, platform); if (platform == BotPlatform.Discord || linkedDiscordAccount != null) { var discordId = linkedDiscordAccount != null ? linkedDiscordAccount.DiscordUserId : userId; Log.Debug("{UserId} ({Platform}) mined a buttcoin (bruteforce? {IsBruteForce}) in their buttcoin account ({DiscordId})", userId, platform.GetValueName(), isBruteForce, discordId); var account = await GetOrCreateAccount(discordId); account.Balance += 1; account.Stats.AmountBruteforced += isBruteForce ? 1ul : 0ul; account.Stats.AmountMined += 1; } else { Log.Debug("{UserId} ({Platform}) mined a buttcoin (bruteforce? {IsBruteForce}) in their holder account", userId, platform.GetValueName(), isBruteForce); var account = await GetOrCreateHolderAccount(userId, platform); account.AmountMined += 1; account.AmountBruteforced += isBruteForce ? 1ul : 0ul; } await _db.SaveChangesAsync(); }
public async Task <EmptyResponse> LinkAccounts(string discordId, BotPlatform platform, string platformId) { if (await HolderAccountExists(platformId, platform)) { Log.Debug("Transferring buttcoins from {PlatformId} ({Platform}) to buttcoin account {AccountId}", platformId, platform.GetValueName(), discordId); var account = await GetOrCreateAccount(discordId); var holderAccount = await GetOrCreateHolderAccount(platformId, platform); account.Balance += holderAccount.AmountMined; account.Stats.AmountBruteforced += holderAccount.AmountBruteforced; holderAccount.MarkDeleted(); await _db.SaveChangesAsync(); } if (!await LinkedAccountExists(discordId, platform, platformId)) { await _db.LinkedAccounts.AddAsync(new LinkedAccounts { DiscordId = discordId, Platform = platform, PlatformId = platformId, }); await _db.SaveChangesAsync(); Log.Debug("Linked {PlatformId} ({Platform}) with buttcoin account {AccountId}", platformId, platform.GetValueName(), discordId); } return(new EmptyResponse()); }