<commit_before>﻿using NadekoBot.Core.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace NadekoBot.Core.Services.Database.Repositories.Impl
{
    public class XpRepository : Repository<UserXpStats>, IXpRepository
    {
        public XpRepository(DbContext context) : base(context)
        {
        }

        public UserXpStats GetOrCreateUser(ulong guildId, ulong userId)
        {
            var usr = _set.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId);

            if (usr == null)
            {
                _context.Add(usr = new UserXpStats()
                {
                    Xp = 0,
                    UserId = userId,
                    NotifyOnLevelUp = XpNotificationType.None,
                    GuildId = guildId,
                });
            }

            return usr;
        }

        public UserXpStats[] GetUsersFor(ulong guildId, int page)
        {
            return _set.Where(x => x.GuildId == guildId)
                .OrderByDescending(x => x.Xp + x.AwardedXp)
                .Skip(page * 9)
                .Take(9)
                .ToArray();
        }

        public async Task<int> GetUserGuildRankingAsync(ulong userId, ulong guildId)
        {
            if (!_set.Where(x => x.GuildId == guildId && x.UserId == userId).Any())
            {
                var cnt = await _set.CountAsync(x => x.GuildId == guildId).ConfigureAwait(false);
                if (cnt == 0)
                    return 1;
                else
                    return cnt;
            }
            return await _set
                .Where(x => x.GuildId == guildId)
                .CountAsync(x => x.Xp > (_set
                    .Where(y => y.UserId == userId && y.GuildId == guildId)
                    .Select(y => y.Xp)
                    .DefaultIfEmpty()
                    .Sum())).ConfigureAwait(false) + 1;<commit_msg>.xp server ranking will now correctly account for awarded xp, closes #2454<commit_after>﻿using NadekoBot.Core.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace NadekoBot.Core.Services.Database.Repositories.Impl
{
    public class XpRepository : Repository<UserXpStats>, IXpRepository
    {
        public XpRepository(DbContext context) : base(context)
        {
        }

        public UserXpStats GetOrCreateUser(ulong guildId, ulong userId)
        {
            var usr = _set.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId);

            if (usr == null)
            {
                _context.Add(usr = new UserXpStats()
                {
                    Xp = 0,
                    UserId = userId,
                    NotifyOnLevelUp = XpNotificationType.None,
                    GuildId = guildId,
                });
            }

            return usr;
        }

        public UserXpStats[] GetUsersFor(ulong guildId, int page)
        {
            return _set.Where(x => x.GuildId == guildId)
                .OrderByDescending(x => x.Xp + x.AwardedXp)
                .Skip(page * 9)
                .Take(9)
                .ToArray();
        }

        public async Task<int> GetUserGuildRankingAsync(ulong userId, ulong guildId)
        {
            if (!_set.Where(x => x.GuildId == guildId && x.UserId == userId).Any())
            {
                var cnt = await _set.CountAsync(x => x.GuildId == guildId).ConfigureAwait(false);
                if (cnt == 0)
                    return 1;
                else
                    return cnt;
            }
            return await _set
                .Where(x => x.GuildId == guildId)
                .CountAsync(x => (x.Xp + x.AwardedXp) > (_set
                    .Where(y => y.UserId == userId && y.GuildId == guildId)
                    .Select(y => y.Xp + y.AwardedXp)
                    .DefaultIfEmpty()
                    .Sum())).ConfigureAwait(false) + 1;
{'fraction_non_alphanumeric': 0.10532899674756067, 'fraction_numerical': 0.0040030022516887665, 'mean_word_length': 1.4098854731766124, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115694', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1142, 'n_words': 234}