Beispiel #1
0
        private void OnTimer(object state)
        {
            if (ActiveWallets.IsEmpty)
            {
                return;
            }

            var instanceId = Guid.NewGuid().ToString("N");
            var logs       = new List <PointLog>();

            foreach (var wallet in ActiveWallets.Values)
            {
                var log = _points.CreateAsync(new PointLog
                {
                    Timestamp   = DateTime.UtcNow,
                    UserId      = wallet.Id,
                    SenderId    = instanceId,
                    EarningType = EarningType.Activity,
                    Amount      = 5
                }).GetAwaiter().GetResult();

                wallet.Balance += log.Amount;
                _points.ModifyAsync(wallet).GetAwaiter().GetResult();

                logs.Add(log);
            }

            ActiveWallets.Clear();
            foreach (var action in Actions.Values)
            {
                action(logs);
            }
        }
        private Task OnMessageReceivedAsync(SocketMessage s)
        {
            _ = Task.Run(async() =>
            {
                //bool plonked = await _root.IsBannedAsync(msg.Author);
                //if (plonked || msg.Author.IsBot) return;

                if (!(s is SocketUserMessage msg))
                {
                    return;
                }
                if (msg.Author.IsBot)
                {
                    return;
                }

                var context   = new DogeyCommandContext(_discord, msg);
                string prefix = await _root.GetPrefixAsync(context.Guild);

                if (_commands.IsCommand(context, prefix, out int argPos))
                {
                    return;
                }

                var wallet = await _points.GetOrCreateWalletAsync(msg.Author);

                try
                {
                    int earning = 0;

                    // Bonus points for a prime id
                    if (MathHelper.IsPrime(msg.Id))
                    {
                        earning += 25;
                    }

                    // Add 3-5x points for concecutive numbers
                    for (int mult = 3; mult <= 5; mult++)
                    {
                        int repeats = StringHelper.RepeatingChars(msg.Id.ToString(), mult);
                        if (repeats > 0)
                        {
                            earning += repeats * (mult - 1);
                        }
                    }

                    if (wallet.Multiplier != null)
                    {
                        var multiplied = earning * wallet.Multiplier.Value;
                        earning       += (int)Math.Round(multiplied, MidpointRounding.AwayFromZero);
                    }

                    if (earning > 0)
                    {
                        var log = await _points.CreateAsync(new PointLog
                        {
                            Timestamp   = msg.Timestamp.DateTime,
                            UserId      = msg.Author.Id,
                            SenderId    = msg.Id,
                            EarningType = EarningType.Message,
                            Amount      = earning
                        });

                        wallet.Balance += log.Amount;
                        await _points.ModifyAsync(wallet);

                        foreach (var action in Actions.Values)
                        {
                            await action(log);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Unable to add points: {ex}", ex);
                }
            }, _cancellationToken);
            return(Task.CompletedTask);
        }