public async Task AddRoleAssignmentAsync(string messageId, string emote, string roleToGrant, string guildId, string channelId)
        {
            var emoteBytes = Encoding.Unicode.GetBytes(emote);

            using (var ctx = new OmgSpidersDbContext())
            {
                var existingEntity = await ctx.RoleAssignmentReaction.FirstOrDefaultAsync(
                    x => x.MessageId == messageId && x.EmoteReference == emoteBytes);

                if (existingEntity != null)
                {
                    // overwrite for now
                    existingEntity.EmoteReference = emoteBytes;
                    existingEntity.RoleId         = roleToGrant;
                }
                else
                {
                    ctx.RoleAssignmentReaction.Add(new RoleAssignmentReaction
                    {
                        MessageId      = messageId,
                        EmoteReference = emoteBytes,
                        RoleId         = roleToGrant,
                        GuildId        = guildId,
                        ChannelId      = channelId
                    });
                }
                await ctx.SaveChangesAsync();

                this.InvalidateItem <RoleAssignmentMap>(RoleMapKey);
            }
        }
Ejemplo n.º 2
0
        public async Task <string> ClaimPlayer(string player, string discordMention, string friendlyName)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                var existingMap = ctx.PlayerList.FirstOrDefault(x => x.PlayerName == player);
                if (existingMap == null)
                {
                    var newRegistration = new PlayerList()
                    {
                        PlayerName = player, DiscordMention = discordMention, FriendlyName = friendlyName
                    };
                    ctx.PlayerList.Add(newRegistration);
                }
                else if (!string.IsNullOrEmpty(existingMap.DiscordMention) && discordMention != existingMap.DiscordMention)
                {
                    return($"The mapping already exists currently {existingMap.DiscordMention} owns {player}.");
                }
                else
                {
                    existingMap.DiscordMention = discordMention;
                    existingMap.FriendlyName   = friendlyName;
                    ctx.PlayerList.Update(existingMap);
                }
                await ctx.SaveChangesAsync();

                var result = $"{player} has been claimed by {discordMention} with friendlyname {friendlyName}";

                result += "\n" + await RegisterMain(discordMention, player, true);

                return(result);
            }
        }
Ejemplo n.º 3
0
 public async Task <List <SaleRun> > ListRuns()
 {
     using (var ctx = new OmgSpidersDbContext())
     {
         return(ctx.SaleRun.ToList());
     }
 }
Ejemplo n.º 4
0
 public async Task <SaleRun> GetRunDetails(int runId)
 {
     using (var ctx = new OmgSpidersDbContext())
     {
         return(ctx.SaleRun.Include(x => x.SaleRunParticipation).ThenInclude(x => x.Player).FirstOrDefault(x => x.Id == runId));
     }
 }
Ejemplo n.º 5
0
        public async Task <string> RegisterMain(string discordMention, string mainName, bool onlyIfNew = false)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                bool changeMade  = false;
                var  existingMap = ctx.MainRegistration.FirstOrDefault(x => x.DiscordMention == discordMention);
                if (existingMap == null)
                {
                    var newRegistration = new MainRegistration()
                    {
                        MainName = mainName, DiscordMention = discordMention
                    };
                    ctx.MainRegistration.Add(newRegistration);
                    changeMade = true;
                }
                else if (!string.IsNullOrEmpty(existingMap.DiscordMention) && discordMention != existingMap.DiscordMention)
                {
                    return($"The mapping already exists currently {existingMap.DiscordMention} owns {mainName}.");
                }
                else if (!onlyIfNew)
                {
                    existingMap.DiscordMention = discordMention;
                    existingMap.MainName       = mainName;
                    ctx.MainRegistration.Update(existingMap);
                    changeMade = true;
                }
                await ctx.SaveChangesAsync();

                if (changeMade)
                {
                    return($"{mainName} has been registered as the main for {discordMention} and will receive future payouts.");
                }
                return(string.Empty);
            }
        }
Ejemplo n.º 6
0
        public async Task <Dictionary <string, long> > GetPayouts()
        {
            var payouts = new Dictionary <string, long>();

            using (var ctx = new OmgSpidersDbContext())
            {
                var payoutNeeded = ctx.SaleRunParticipation.Include(x => x.Player).Include(x => x.Run).Include(x => x.Run.SaleRunParticipation).Where(x => x.Paid == false).ToList();
                var runIds       = payoutNeeded.Select(x => x.RunId).Distinct();
                var runMap       = ctx.SaleRun.Where(x => runIds.Contains(x.Id)).ToDictionary(x => x.Id, x => x);
                var playerIds    = payoutNeeded.Select(x => x.PlayerId).Distinct();
                var mainMap      = ctx.MainRegistration.ToDictionary(x => x.DiscordMention, x => x);

                foreach (var payoutEntry in payoutNeeded)
                {
                    var playerEntry = payoutEntry.Player;
                    var mainEntry   = playerEntry.DiscordMention != null && mainMap.ContainsKey(playerEntry.DiscordMention) ? mainMap[playerEntry.DiscordMention].MainName: null;
                    var playerNameOrFriendlyName = string.IsNullOrEmpty(playerEntry.FriendlyName) ? playerEntry.PlayerName : playerEntry.FriendlyName;
                    var payoutKey = string.IsNullOrEmpty(mainEntry) ? $"**{playerNameOrFriendlyName}**" : mainEntry;

                    if (!payouts.ContainsKey(payoutKey))
                    {
                        payouts[payoutKey] = 0;
                    }
                    var run = runMap[payoutEntry.RunId];
                    payouts[payoutKey] += this.CalculatePlayerCutForRun(payoutEntry);
                }
            }

            return(payouts);
        }
Ejemplo n.º 7
0
        private static void AddCutForPlayerForTargetRun(OmgSpidersDbContext ctx, SaleRun saleRun, string player, float cutValue)
        {
            var playerEntity = ctx.PlayerList.Single(x => x.PlayerName == player);

            ctx.SaleRunParticipation.Add(new SaleRunParticipation {
                RunId = saleRun.Id, PlayerId = playerEntity.Id, CutValue = cutValue
            });
        }
Ejemplo n.º 8
0
        public async Task <long> GetHistory(string userTarget)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                userTarget = UserUtilities.ConvertUserTargetToDiscordMention(userTarget, ctx);
                var payoutNeeded = ctx.SaleRunParticipation.Include(x => x.Player).Include(x => x.Run).Where(x => x.Player.DiscordMention == userTarget).ToList();

                var totalOwed = payoutNeeded.Sum(x => x.Run.GoldTotalAfterAdCut / x.Run.PlayerCount);
                return(totalOwed.Value);
            }
        }
Ejemplo n.º 9
0
        private void AddNewPlayersNoCommit(string[] playerList, OmgSpidersDbContext ctx)
        {
            var playersInDb = ctx.PlayerList.ToArray().Select(x => x.PlayerName).Intersect(playerList, StringComparer.OrdinalIgnoreCase);

            var missingPlayers = playerList.Except(playersInDb.Select(x => x), StringComparer.OrdinalIgnoreCase)
                                 .Where(x => !decimal.TryParse(x, out var dontCareWeOnlyWantTheTryParseResult));

            ctx.PlayerList.AddRange(missingPlayers.Select(x => new PlayerList()
            {
                PlayerName = x
            }));
        }
Ejemplo n.º 10
0
        private static void AssignPlayerCuts(string[] playerAndCutEntryList, OmgSpidersDbContext ctx, SaleRun saleRun)
        {
            var cutValue = 1.0f;

            for (var i = 0; i < playerAndCutEntryList.Length; i++)
            {
                var entry = playerAndCutEntryList[i];
                if (ShouldAddACutBecauseThisEntryIsNotAFloat(cutValue, out cutValue, entry))
                {
                    AddCutForPlayerForTargetRun(ctx, saleRun, entry, cutValue);
                }
            }
        }
Ejemplo n.º 11
0
        public static string ConvertUserTargetToDiscordMention(string userTarget, OmgSpidersDbContext ctx)
        {
            if (userTarget.StartsWith("<@!"))
            {
                //noop
            }
            else
            {
                userTarget = ctx.MainRegistration.FirstOrDefault(x => x.MainName.Equals(userTarget))?.DiscordMention;
            }

            return(userTarget);
        }
Ejemplo n.º 12
0
        public async Task RemoveRunAsync(int runId)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                var saleRunToRemove = ctx.SaleRun.Include(x => x.SaleRunParticipation).FirstOrDefault(x => x.Id == runId);
                if (saleRunToRemove == null)
                {
                    throw new InvalidOperationException($"Run does not exist {runId}");
                }

                ctx.SaleRun.Remove(saleRunToRemove);


                await ctx.SaveChangesAsync();
            }
        }
Ejemplo n.º 13
0
        public async Task <long> GetBalance(string userTarget)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                userTarget = UserUtilities.ConvertUserTargetToDiscordMention(userTarget, ctx);

                if (string.IsNullOrEmpty(userTarget))
                {
                    return(0);
                }

                var payoutNeeded = ctx.SaleRunParticipation.Include(x => x.Player).Include(x => x.Run).Include(x => x.Run.SaleRunParticipation).Where(x => x.Paid == false && x.Player.DiscordMention == userTarget).ToList();

                var totalOwed = payoutNeeded.Sum(x => CalculatePlayerCutForRun(x));
                return(totalOwed);
            }
        }
Ejemplo n.º 14
0
        public async Task <string> GetDiscordMentionForCharacter(string characterOrDiscordMention)
        {
            if (characterOrDiscordMention.Contains("@"))
            {
                return(characterOrDiscordMention);
            }
            using (var ctx = new OmgSpidersDbContext())
            {
                var player = await ctx.PlayerList.FirstOrDefaultAsync(x => x.PlayerName == characterOrDiscordMention);

                if (player is null)
                {
                    throw new InvalidOperationException($"{characterOrDiscordMention} is an invalid character");
                }
                return(player.DiscordMention);
            }
        }
Ejemplo n.º 15
0
        public async Task PayoutPlayer(string userTarget)
        {
            using (var ctx = new OmgSpidersDbContext())
            {
                IQueryable <SaleRunParticipation> payoutNeeded = null;

                userTarget = UserUtilities.ConvertUserTargetToDiscordMention(userTarget, ctx);

                payoutNeeded = ctx.SaleRunParticipation.Where(x => x.Paid == false && x.Player.DiscordMention == userTarget);
                foreach (var runEntry in payoutNeeded)
                {
                    runEntry.Paid = true;
                }

                ctx.SaleRunParticipation.UpdateRange(payoutNeeded);
                await ctx.SaveChangesAsync();
            }
        }
        public async Task RemoveRoleAssignmentAsync(string messageId, string emote, string roleToGrant)
        {
            var emoteBytes = Encoding.Unicode.GetBytes(emote);

            using (var ctx = new OmgSpidersDbContext())
            {
                var existingEntity = await ctx.RoleAssignmentReaction.FirstOrDefaultAsync(x => x.MessageId == messageId && x.EmoteReference == emoteBytes && x.RoleId == roleToGrant);

                if (existingEntity == null)
                {
                    return;
                }

                ctx.RoleAssignmentReaction.Remove(existingEntity);

                await ctx.SaveChangesAsync();

                this.InvalidateItem <RoleAssignmentMap>(RoleMapKey);
            }
        }
        public RoleAssignmentMap GetRoleAssignmentMap()
        {
            var cachedRoleMap = this.RoleMap;

            if (cachedRoleMap != null)
            {
                return(cachedRoleMap);
            }

            var roleMap = new RoleAssignmentMap();

            using (var ctx = new OmgSpidersDbContext())
            {
                foreach (var reaction in ctx.RoleAssignmentReaction)
                {
                    var messageIdentifier = reaction.GetMessageIdentifier();
                    var emoteString       = Encoding.Unicode.GetString(reaction.EmoteReference);
                    if (roleMap.ContainsKey(messageIdentifier))
                    {
                        roleMap[messageIdentifier][emoteString] = reaction.RoleId;
                    }
                    else
                    {
                        roleMap[messageIdentifier] =
                            new EmoteRoleMap()
                        {
                            {
                                emoteString, reaction.RoleId
                            }
                        };
                    }
                }
            }

            this.AddOrUpdateItem(RoleMapKey, roleMap);
            return(roleMap);
        }
Ejemplo n.º 18
0
        public async Task <int> AddRunAsync(string title, long goldAmount, string[] playerAndCutEntryList)
        {
            for (int idx = 0; idx < playerAndCutEntryList.Length; ++idx)
            {
                playerAndCutEntryList[idx] = playerAndCutEntryList[idx].Trim();
            }

            using (var ctx = new OmgSpidersDbContext())
            {
                var saleRun = new SaleRun()
                {
                    RunName             = title,
                    GoldTotalAfterAdCut = goldAmount,
                    RunDate             = (DateTime?)DateTime.Now,
                    PlayerCount         = playerAndCutEntryList.Length
                };

                ctx.SaleRun.Add(saleRun);
                await ctx.SaveChangesAsync();

                saleRun = ctx.SaleRun.Single(
                    x => x.GoldTotalAfterAdCut == saleRun.GoldTotalAfterAdCut &&
                    x.RunName == saleRun.RunName &&
                    x.RunDate == saleRun.RunDate);

                this.AddNewPlayersNoCommit(playerAndCutEntryList, ctx);
                await ctx.SaveChangesAsync();

                // load the players we need now.
                AssignPlayerCuts(playerAndCutEntryList, ctx, saleRun);

                await ctx.SaveChangesAsync();

                return(saleRun.Id);
            }
        }