|
2 | 2 | using Maple2.Model.Enum; |
3 | 3 | using Maple2.Model.Game; |
4 | 4 | using Maple2.Tools.Extensions; |
| 5 | +using Microsoft.EntityFrameworkCore; |
5 | 6 | using Z.EntityFramework.Plus; |
6 | 7 |
|
7 | 8 | namespace Maple2.Database.Storage; |
@@ -42,6 +43,140 @@ public IList<GuildMember> GetGuildMembers(IPlayerInfoProvider provider, long gui |
42 | 43 | .ToList(); |
43 | 44 | } |
44 | 45 |
|
| 46 | + |
| 47 | + public IList<Guild> SearchGuilds(IPlayerInfoProvider provider, string guildName = "", GuildFocus? focus = null, int limit = 50) { |
| 48 | + IQueryable<Model.Guild> query = Context.Guild; |
| 49 | + if (!string.IsNullOrWhiteSpace(guildName)) { |
| 50 | + query = query.Where(guild => EF.Functions.Like(guild.Name, $"%{guildName}%")); |
| 51 | + } |
| 52 | + if (focus.HasValue && (int) focus.Value != 0) { |
| 53 | + query = query.Where(guild => guild.Focus == focus.Value); |
| 54 | + } |
| 55 | + |
| 56 | + List<long> guildIds = query.OrderBy(guild => guild.Name) |
| 57 | + .Take(limit) |
| 58 | + .Select(guild => guild.Id) |
| 59 | + .ToList(); |
| 60 | + |
| 61 | + var result = new List<Guild>(); |
| 62 | + foreach (long id in guildIds) { |
| 63 | + Guild? guild = LoadGuild(id, string.Empty); |
| 64 | + if (guild == null) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + foreach (GuildMember member in GetGuildMembers(provider, id)) { |
| 69 | + guild.Members.TryAdd(member.CharacterId, member); |
| 70 | + guild.AchievementInfo += member.Info.AchievementInfo; |
| 71 | + } |
| 72 | + result.Add(guild); |
| 73 | + } |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + public GuildApplication? CreateGuildApplication(IPlayerInfoProvider provider, long guildId, long applicantId) { |
| 79 | + Guild? guild = LoadGuild(guildId, string.Empty); |
| 80 | + PlayerInfo? applicant = provider.GetPlayerInfo(applicantId); |
| 81 | + if (guild == null || applicant == null) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (Context.GuildApplication.Any(app => app.GuildId == guildId && app.ApplicantId == applicantId)) { |
| 86 | + Model.GuildApplication existing = Context.GuildApplication.First(app => app.GuildId == guildId && app.ApplicantId == applicantId); |
| 87 | + return new GuildApplication { |
| 88 | + Id = existing.Id, |
| 89 | + Guild = guild, |
| 90 | + Applicant = applicant, |
| 91 | + CreationTime = existing.CreationTime.ToEpochSeconds(), |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + var app = new Model.GuildApplication { |
| 96 | + GuildId = guildId, |
| 97 | + ApplicantId = applicantId, |
| 98 | + }; |
| 99 | + Context.GuildApplication.Add(app); |
| 100 | + if (!SaveChanges()) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return new GuildApplication { |
| 105 | + Id = app.Id, |
| 106 | + Guild = guild, |
| 107 | + Applicant = applicant, |
| 108 | + CreationTime = app.CreationTime.ToEpochSeconds(), |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + public GuildApplication? GetGuildApplication(IPlayerInfoProvider provider, long applicationId) { |
| 113 | + Model.GuildApplication? app = Context.GuildApplication.FirstOrDefault(app => app.Id == applicationId); |
| 114 | + if (app == null) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + Guild? guild = LoadGuild(app.GuildId, string.Empty); |
| 119 | + PlayerInfo? applicant = provider.GetPlayerInfo(app.ApplicantId); |
| 120 | + if (guild == null || applicant == null) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + return new GuildApplication { |
| 125 | + Id = app.Id, |
| 126 | + Guild = guild, |
| 127 | + Applicant = applicant, |
| 128 | + CreationTime = app.CreationTime.ToEpochSeconds(), |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + public IList<GuildApplication> GetGuildApplications(IPlayerInfoProvider provider, long guildId) { |
| 133 | + List<Model.GuildApplication> applications = Context.GuildApplication.Where(app => app.GuildId == guildId) |
| 134 | + .OrderByDescending(app => app.CreationTime) |
| 135 | + .ToList(); |
| 136 | + |
| 137 | + return applications |
| 138 | + .Select(app => { |
| 139 | + Guild? guild = LoadGuild(app.GuildId, string.Empty); |
| 140 | + PlayerInfo? applicant = provider.GetPlayerInfo(app.ApplicantId); |
| 141 | + if (guild == null || applicant == null) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + return new GuildApplication { |
| 146 | + Id = app.Id, |
| 147 | + Guild = guild, |
| 148 | + Applicant = applicant, |
| 149 | + CreationTime = app.CreationTime.ToEpochSeconds(), |
| 150 | + }; |
| 151 | + }) |
| 152 | + .WhereNotNull() |
| 153 | + .ToList(); |
| 154 | + } |
| 155 | + |
| 156 | + public IList<GuildApplication> GetGuildApplicationsByApplicant(IPlayerInfoProvider provider, long applicantId) { |
| 157 | + List<Model.GuildApplication> applications = Context.GuildApplication.Where(app => app.ApplicantId == applicantId) |
| 158 | + .OrderByDescending(app => app.CreationTime) |
| 159 | + .ToList(); |
| 160 | + |
| 161 | + return applications |
| 162 | + .Select(app => { |
| 163 | + Guild? guild = LoadGuild(app.GuildId, string.Empty); |
| 164 | + PlayerInfo? applicant = provider.GetPlayerInfo(app.ApplicantId); |
| 165 | + if (guild == null || applicant == null) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + return new GuildApplication { |
| 170 | + Id = app.Id, |
| 171 | + Guild = guild, |
| 172 | + Applicant = applicant, |
| 173 | + CreationTime = app.CreationTime.ToEpochSeconds(), |
| 174 | + }; |
| 175 | + }) |
| 176 | + .WhereNotNull() |
| 177 | + .ToList(); |
| 178 | + } |
| 179 | + |
45 | 180 | public Guild? CreateGuild(string name, long leaderId) { |
46 | 181 | BeginTransaction(); |
47 | 182 |
|
@@ -155,20 +290,18 @@ public bool DeleteGuildApplications(long characterId) { |
155 | 290 | public bool SaveGuildMembers(long guildId, ICollection<GuildMember> members) { |
156 | 291 | Dictionary<long, GuildMember> saveMembers = members |
157 | 292 | .ToDictionary(member => member.CharacterId, member => member); |
158 | | - IEnumerable<Model.GuildMember> existingMembers = Context.GuildMember |
| 293 | + HashSet<long> existingMembers = Context.GuildMember |
159 | 294 | .Where(member => member.GuildId == guildId) |
160 | | - .Select(member => new Model.GuildMember { |
161 | | - CharacterId = member.CharacterId, |
162 | | - }); |
| 295 | + .Select(member => member.CharacterId) |
| 296 | + .ToHashSet(); |
163 | 297 |
|
164 | | - foreach (Model.GuildMember member in existingMembers) { |
165 | | - if (saveMembers.Remove(member.CharacterId, out GuildMember? gameMember)) { |
| 298 | + foreach ((long characterId, GuildMember gameMember) in saveMembers) { |
| 299 | + if (existingMembers.Contains(characterId)) { |
166 | 300 | Context.GuildMember.Update(gameMember); |
167 | 301 | } else { |
168 | | - Context.GuildMember.Remove(member); |
| 302 | + Context.GuildMember.Add(gameMember); |
169 | 303 | } |
170 | 304 | } |
171 | | - Context.GuildMember.AddRange(saveMembers.Values.Select<GuildMember, Model.GuildMember>(member => member)); |
172 | 305 |
|
173 | 306 | return SaveChanges(); |
174 | 307 | } |
|
0 commit comments