Example #1
0
        public async Task <IActionResult> OnPostInviteAsync()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var user = await _context.DbContext.Users.FirstOrDefaultAsync(u => u.QQID == InviteMemberQQ);

            if (user is null)
            {
                StatusMessage = "错误:用户不存在。";
                return(RedirectToPage());
            }
            if (user.GuildID.HasValue)
            {
                StatusMessage = "错误:用户已经所属其他公会。";
                return(RedirectToPage());
            }
            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            user.GuildID      = guild.GuildID;
            user.IsGuildAdmin = false;
            _context.DbContext.Update(user);
            await _context.DbContext.SaveChangesAsync();

            imGuild.AddUser(user.Id, 0, null);

            return(RedirectToPage());
        }
Example #2
0
        public async Task <(bool enabled, int?borrow)> GetBorrowSetting(ZhouVariant v)
        {
            var imGuild = await _context.GetGuildAsync(v.Zhou.GuildID);

            var imUser        = imGuild.GetUserById(UserID);
            var borrowPlusOne = imGuild.GetZhouVariantById(v.ZhouVariantID).UserData[imUser.Index].BorrowPlusOne;

            return(borrowPlusOne switch
            {
Example #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var userList = new List <PcrIdentityUser>();

            using (var sr = new StringReader(Input))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var qq    = ulong.Parse(line);
                    var name  = qq.ToString();
                    var email = $"{qq}@qq.com";
                    if (await _context.DbContext.Users.AnyAsync(u => u.QQID == qq || u.Email == email || u.UserName == name))
                    {
                        StatusMessage2 = $"´íÎó£ºQQ {qq} ÒѾ­×¢²á¡£";
                        return(Page());
                    }
                    var user = new PcrIdentityUser
                    {
                        QQID     = qq,
                        UserName = name,
                        Email    = email,
                        GuildID  = guild.GuildID,
                    };
                    userList.Add(user);
                    //Not creating with UserManager. This allows to create without saving.
                    _context.DbContext.Users.Add(user);
                }
            }
            await _context.DbContext.SaveChangesAsync();

            if (!string.IsNullOrEmpty(Password))
            {
                foreach (var u in userList)
                {
                    await _userManager.AddPasswordAsync(u, Password);
                }
            }

            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            foreach (var u in userList)
            {
                imGuild.AddUser(u.Id, 0, null);
            }

            StatusMessage = "Ìí¼ÓÍê³É¡£";
            return(RedirectToPage("/Guilds/Edit"));
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, InMemoryStorageContext dataContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

            var supportedCultures   = new[] { "zh-CN" };
            var localizationOptions = new RequestLocalizationOptions()
                                      .SetDefaultCulture(supportedCultures[0])
                                      .AddSupportedCultures(supportedCultures)
                                      .AddSupportedUICultures(supportedCultures);

            app.UseRequestLocalization(localizationOptions);

            dataContext.DbContext.Database.Migrate();

            if (LoadIMContext)
            {
                Console.WriteLine("Loading IM context");
                var guilds = dataContext.DbContext.Guilds.ToList();

                var comboCalculator = new FindAllCombos();

                foreach (var g in guilds)
                {
                    var imGuild = dataContext.GetGuildAsync(g.GuildID).Result;
                    comboCalculator.UpdateGuildAsync(dataContext, g.GuildID, imGuild).Wait();
                    CalcComboValues.RunAllAsync(dataContext.DbContext, g, imGuild).Wait();
                }

                Console.WriteLine("IM context loaded.");
            }
        }
Example #5
0
        public async Task <IActionResult> OnGet()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }
            Guild = guild;
            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            var stages = await _context.DbContext.BattleStages.OrderBy(s => s.StartLap).ToListAsync();

            FirstLapForStages       = stages.Select(s => s.StartLap).ToArray();
            FirstLapForStagesString = JsonConvert.SerializeObject(FirstLapForStages);

            BossNames = new();
            foreach (var s in stages)
            {
                var bosses = await _context.DbContext.Bosses
                             .Where(b => b.BattleStageID == s.BattleStageID)
                             .OrderBy(b => b.BossID)
                             .Select(b => b.Name)
                             .ToListAsync();

                BossNames.Add(bosses);
            }
            BossNamesString = JsonConvert.SerializeObject(BossNames);

            LastUpdateTime            = imGuild.LastCalculation.ToString("MM-dd HH:mm");
            (CurrentLap, CurrentBoss) = ConvLap(guild.BossIndex);
            CurrentLap      += 1; //convert to 1-based.
            CurrentBoss     += 1;
            CurrentBossRatio = guild.BossDamageRatio;

            PlanRatio = guild.DamageCoefficient;

            return(Page());
        }
        public async Task <IActionResult> OnPostSyncAsync()
        {
            if (!_signInManager.IsSignedIn(User))
            {
                return(RedirectToPage("/Home/Index"));
            }
            var user = await _userManager.GetUserAsync(User);

            if (user is null || !user.GuildID.HasValue || !user.IsGuildAdmin)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var g = await _context.DbContext.Guilds
                    .FirstOrDefaultAsync(m => m.GuildID == user.GuildID.Value);

            var imGuild = await _context.GetGuildAsync(g.GuildID);

            await YobotSync.RunSingleAsync(_context, g, imGuild, forceRecalc : true);

            return(RedirectToPage("/Home/Index"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            var guildID = await CheckUserPrivilege();

            if (!guildID.HasValue)
            {
                return(RedirectToPage("/Guild/Index"));
            }
            var imGuild = await _context.GetGuildAsync(guildID.Value);

            if (string.IsNullOrEmpty(Input))
            {
                StatusMessage2 = "错误:输入为空。";
                return(Page());
            }

            var parser = _parserFactory.GetParser(_context.DbContext, guildID.Value);
            await parser.ReadDatabase();

            var list          = new List <Zhou>();
            var newConfigList = new List <CharacterConfig>();
            int lineNum       = 0;
            var errorMsg      = new StringBuilder();

            errorMsg.AppendLine("错误:读取轴表失败。");
            var       errorCount  = 0;
            const int MaxErrorMsg = 5;

            var allLines = DuplicateCheckBehavior == ZhouParserDuplicateCheckBehavior.Return ? new List <string>() : null;

            using var reader = new StringReader(Input);
            string line;

            while ((line = reader.ReadLine()) is not null)
            {
                allLines?.Add(line);
                lineNum += 1;
                try
                {
                    var z = parser.Parse(line, HasName, CreateConfigs, newConfigList);
                    if (z is not null)
                    {
                        list.Add(z);
                    }
                }
                catch (Exception e)
                {
                    if (++errorCount <= MaxErrorMsg)
                    {
                        errorMsg.AppendLine($"  第{lineNum}行:{e.Message}。");
                    }
                    list.Add(null);
                }
            }

            if (errorCount != 0)
            {
                if (errorCount > MaxErrorMsg)
                {
                    errorMsg.AppendLine($"  以及{errorCount - MaxErrorMsg}个其他错误。");
                }
                errorMsg.AppendLine("轴表没有被修改。");
                StatusMessage2 = errorMsg.ToString();
                return(Page());
            }
            errorMsg.Clear();
            errorMsg.AppendLine("检测到重复的轴:");

            var unsavedMergeCheck  = new List <Zhou>();
            var unsavedDupCheck    = new List <ZhouVariant>();
            var returnInputContent = new StringBuilder();
            var updatedZV          = new HashSet <ZhouVariant>(); //Each zv can only be updated once. Use this to check.
            var dupUpdateCount     = 0;

            var allUserConfigs = await _context.DbContext.UserCharacterConfigs
                                 .Include(ucc => ucc.CharacterConfig)
                                 .Where(ucc => ucc.CharacterConfig.GuildID == guildID)
                                 .ToListAsync();

            var pendingIMAdd = new List <(Zhou, ZhouVariant)>();

            foreach (var cc in newConfigList)
            {
                await Guilds.ConfigsModel.CheckAndAddRankConfigAsync(_context.DbContext, cc, allUserConfigs);
            }

            for (int i = 0; i < list.Count; ++i)
            {
                var z = list[i];
                if (z is null)
                {
                    continue;            //i is to keep the line number. If parsing fails it will be null.
                }
                var v = ((List <ZhouVariant>)z.Variants)[0];

                Zhou existingSameZhou = null;
                if (Merge || DuplicateCheckBehavior != ZhouParserDuplicateCheckBehavior.Ignore)
                {
                    existingSameZhou = await _context.DbContext.Zhous
                                       .FirstOrDefaultAsync(zz =>
                                                            zz.GuildID == guildID.Value &&
                                                            zz.BossID == z.BossID &&
                                                            zz.C1ID == z.C1ID &&
                                                            zz.C2ID == z.C2ID &&
                                                            zz.C3ID == z.C3ID &&
                                                            zz.C4ID == z.C4ID &&
                                                            zz.C5ID == z.C5ID); //Assuming same order (by range).

                    if (existingSameZhou is null)
                    {
                        existingSameZhou = unsavedMergeCheck.FirstOrDefault(zz =>
                                                                            zz.BossID == z.BossID &&
                                                                            zz.C1ID == z.C1ID &&
                                                                            zz.C2ID == z.C2ID &&
                                                                            zz.C3ID == z.C3ID &&
                                                                            zz.C4ID == z.C4ID &&
                                                                            zz.C5ID == z.C5ID);
                    }
                }

                //Dup check.
                if (DuplicateCheckBehavior != ZhouParserDuplicateCheckBehavior.Ignore && existingSameZhou is not null)
                {
                    if (!HasName || existingSameZhou.Name == z.Name)
                    {
                        //Name check passed.
                        //Check configs.
                        if (existingSameZhou.ZhouID != 0)
                        {
                            await _context.DbContext.Entry(existingSameZhou).Collection(zz => zz.Variants).LoadAsync();
                        }

                        bool dupCheckResult = true;
                        foreach (var existingV in existingSameZhou.Variants)
                        {
                            if (existingV.ZhouVariantID != 0)
                            {
                                await _context.DbContext.Entry(existingV).Collection(vv => vv.CharacterConfigs).LoadAsync();
                            }

                            if (existingV.CharacterConfigs.Count != v.CharacterConfigs.Count)
                            {
                                continue;
                            }
                            bool checkFailed = false;
                            foreach (var existingConfig in existingV.CharacterConfigs)
                            {
                                if (existingConfig.CharacterConfigID == 0)
                                {
                                    //This is a new zv. Compare cc instance.
                                    if (!v.CharacterConfigs.Any(vcc => vcc.CharacterConfig == existingConfig.CharacterConfig))
                                    {
                                        checkFailed = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    //This is a db zv. Compare cc id.
                                    if (!v.CharacterConfigs.Any(vcc => vcc.CharacterConfigID == existingConfig.CharacterConfigID))
                                    {
                                        checkFailed = true;
                                        break;
                                    }
                                }
                            }
                            if (!checkFailed)
                            {
                                switch (DuplicateCheckBehavior)
                                {
                                case ZhouParserDuplicateCheckBehavior.Error:
                                    dupCheckResult = false;
                                    errorCount    += 1;
                                    errorMsg.AppendLine($"  第{i + 1}行:检测到重复的轴。");
                                    break;

                                case ZhouParserDuplicateCheckBehavior.Modify:
                                    if (existingV.ZhouVariantID == 0 || updatedZV.Contains(existingV))
                                    {
                                        errorCount += 1;
                                        errorMsg.AppendLine($"  第{i + 1}行:输入中包含重复的轴,无法更新。");
                                    }
                                    else
                                    {
                                        existingV.CharacterConfigs.Clear();
                                        foreach (var ncc in v.CharacterConfigs)
                                        {
                                            existingV.CharacterConfigs.Add(ncc);
                                        }
                                        existingV.Content = v.Content;
                                        existingV.Damage  = v.Damage;
                                        existingV.IsDraft = AsDraft;
                                        existingV.Name    = v.Name; //This should be null.
                                        updatedZV.Add(existingV);
                                    }
                                    dupCheckResult = false;
                                    break;

                                case ZhouParserDuplicateCheckBehavior.Return:
                                    returnInputContent.AppendLine(allLines[i]);
                                    dupCheckResult = false;
                                    break;
                                }
                                break;
                            }
                        }
                        if (!dupCheckResult)
                        {
                            //Process next.
                            continue;
                        }
                    }
                }

                //Merge check.
                Zhou mergedInto = null;
                if (Merge)
                {
                    mergedInto = existingSameZhou;
                    if (HasName && mergedInto is not null && mergedInto.Name != z.Name)
                    {
                        //Only merge when name is the same.
                        mergedInto = null;
                    }
                }

                //Add the new item.
                v.IsDraft = AsDraft;
                if (mergedInto is not null)
                {
                    v.ZhouID = mergedInto.ZhouID;
                    v.Zhou   = mergedInto;
                    _context.DbContext.ZhouVariants.Add(v);
                    pendingIMAdd.Add((mergedInto, v));
                }
                else
                {
                    _context.DbContext.Zhous.Add(z);
                    pendingIMAdd.Add((z, v));
                    unsavedMergeCheck.Add(z);
                }
            }

            if (errorCount != 0)
            {
                //Error or modify.
                //Modify can also generate error (cannot modify a new zv).
                errorMsg.AppendLine("轴表没有被修改。");
                StatusMessage2 = errorMsg.ToString();
                return(Page());
            }

            await _context.DbContext.SaveChangesAsync();

            //Update IM context only after saving of DB context succeeded.
            foreach (var(z, v) in pendingIMAdd)
            {
                imGuild.AddZhouVariant(allUserConfigs, v, z, v.CharacterConfigs);
            }
            foreach (var v in updatedZV)
            {
                imGuild.UpdateZhouVariant(v);
            }

            if (DuplicateCheckBehavior == ZhouParserDuplicateCheckBehavior.Return)
            {
                StatusMessage2 = "轴表已被修改。重复的项目如下,这些项目未被添加。";
                Input          = returnInputContent.ToString();
                return(Page());
            }

            StatusMessage = "导入成功。";
            if (dupUpdateCount > 0)
            {
                StatusMessage += $"{dupUpdateCount}个已存在的重复轴被更新。";
            }
            return(RedirectToPage());
        }
Example #8
0
        public async Task <IActionResult> OnPostAsync()
        {
            var guild = await CheckUserPrivilege();

            if (guild is null)
            {
                return(RedirectToPage("/Home/Index"));
            }

            var currentCount = await _context.DbContext.Users
                               .Where(u => u.GuildID == guild.GuildID)
                               .CountAsync();

            if (CloneCount <= 0)
            {
                StatusMessage2 = "错误:克隆数量无效。";
                return(Page());
            }
            if (CloneCount + currentCount > 30)
            {
                StatusMessage2 = "错误:公会人数不得超过30人。";
                return(Page());
            }

            var templateUser = await _context.DbContext.Users
                               .FirstOrDefaultAsync(u => u.QQID == TemplateUserQQ);

            if (templateUser is null)
            {
                StatusMessage2 = "错误:模板用户不存在。";
                return(Page());
            }
            if (templateUser.GuildID != guild.GuildID)
            {
                StatusMessage2 = "错误:模板用户不在本公会。";
                return(Page());
            }

            const string EmailFormat = "clone_{0}_{1}@example.com";
            var          cloneTime   = DateTime.UtcNow.Ticks.ToString();

            var templateUserConfigs = await _context.DbContext.UserCharacterConfigs
                                      .Where(cc => cc.UserID == templateUser.Id).ToListAsync();

            var newUserList = new List <PcrIdentityUser>();

            for (int i = 0; i < CloneCount; ++i)
            {
                var email = string.Format(EmailFormat, cloneTime, i).ToUpperInvariant();
                var user  = new PcrIdentityUser()
                {
                    Email          = null,
                    UserName       = email,
                    GameID         = templateUser.GameID + "_" + (i + 1).ToString(),
                    EmailConfirmed = true,
                    GuildID        = guild.GuildID,
                };
                newUserList.Add(user);
                if (await _context.DbContext.Users.AnyAsync(u => u.NormalizedEmail == email))
                {
                    StatusMessage2 = $"错误:用户{email}已存在。请重新尝试。";
                    return(Page());
                }
            }

            var imGuild = await _context.GetGuildAsync(guild.GuildID);

            for (int i = 0; i < CloneCount; ++i)
            {
                _context.DbContext.Users.Add(newUserList[i]);
                {
                    //Clone configs.
                    foreach (var cc in templateUserConfigs)
                    {
                        _context.DbContext.UserCharacterConfigs.Add(new()
                        {
                            User = newUserList[i],
                            CharacterConfigID = cc.CharacterConfigID,
                        });
                    }
                }
                newUserList.Add(newUserList[i]);
            }

            await _context.DbContext.SaveChangesAsync();

            foreach (var user in newUserList)
            {
                imGuild.AddUser(user.Id, 0, templateUser.Id);
            }

            StatusMessage = "克隆完成。";
            return(RedirectToPage("/Guilds/Edit"));
        }