forked from MS2Community/Maple2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStorage.Web.cs
More file actions
238 lines (205 loc) · 9.75 KB
/
GameStorage.Web.cs
File metadata and controls
238 lines (205 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using Maple2.Database.Model.Ranking;
using Maple2.Model.Game;
using Character = Maple2.Database.Model.Character;
namespace Maple2.Database.Storage;
public partial class GameStorage {
public partial class Request {
#region Ranking
// This is not an efficient way to fetch rankings. This is only done for proof of concept.
// Ideally, we would store the rank records in db and fetch that. any current season data should be refreshed on a daily basis.
public TrophyRankInfo? GetTrophyRankInfo(long characterId) {
Character? character = Context.Character.Find(characterId);
if (character == null) {
return null;
}
AchievementInfo achievementInfo = GetAchievementInfo(character.AccountId, character.Id);
// Get all characters with their account IDs
var allCharacters = Context.Character
.Select(c => new {
CharacterId = c.Id,
AccountId = c.AccountId,
})
.ToList();
// Calculate total trophies for each character
var characterTrophies = new List<(long CharacterId, int TotalTrophies)>();
foreach (var characterEntry in allCharacters) {
AchievementInfo info = GetAchievementInfo(characterEntry.AccountId, characterEntry.CharacterId);
characterTrophies.Add((characterEntry.CharacterId, info.Total));
}
// Sort by trophy count (descending) and find our character's position
characterTrophies = characterTrophies.OrderByDescending(ct => ct.TotalTrophies).ToList();
int rank = characterTrophies.FindIndex(ct => ct.CharacterId == character.Id) + 1;
// If character not found (shouldn't happen), default to last place
if (rank == 0) {
rank = characterTrophies.Count + 1;
}
return new TrophyRankInfo(
Rank: rank,
CharacterId: character.Id,
Name: character.Name,
Profile: character.Profile?.Picture ?? string.Empty,
Trophy: achievementInfo);
}
public TrophyRankInfo? GetTrophyRankInfo(string name) {
long characterId = GetCharacterId(name);
if (characterId == default) {
return null;
}
return GetTrophyRankInfo(characterId);
}
public IList<TrophyRankInfo> GetTrophyRankings() {
// Get all characters with their account IDs
var characters = Context.Character
.Select(c => new {
CharacterId = c.Id,
AccountId = c.AccountId,
Name = c.Name,
Profile = c.Profile.Picture,
})
.ToList();
// Calculate total trophies for each character (including account-wide trophies)
var characterRankings = new List<(int Rank, long CharacterId, string Name, string Profile, AchievementInfo Trophy)>();
foreach (var character in characters) {
// Get achievement info for this character (combines account and character trophies)
AchievementInfo achievementInfo = GetAchievementInfo(character.AccountId, character.CharacterId);
if (achievementInfo.Total <= 0) {
continue;
}
// Add to our list
characterRankings.Add((0, character.CharacterId, character.Name, character.Profile ?? string.Empty, achievementInfo));
}
// Sort by total trophy count and assign ranks
characterRankings = characterRankings
.OrderByDescending(r => r.Trophy.Total)
.Select((r, index) => (index + 1, r.CharacterId, r.Name, r.Profile, r.Trophy))
.Take(200)
.ToList();
// Convert to TrophyRankInfo objects
return characterRankings
.Select(r => new TrophyRankInfo(
Rank: r.Rank,
CharacterId: r.CharacterId,
Name: r.Name,
Profile: r.Profile,
Trophy: r.Trophy))
.ToList();
}
public GuildTrophyRankInfo? GetGuildTrophyRankInfo(long guildId) {
var guild = Context.Guild
.Where(g => g.Id == guildId)
.Select(g => new { g.Id, g.Name, g.Emblem, g.LeaderId })
.FirstOrDefault();
if (guild == null) {
return null;
}
string leaderName = Context.Character
.Where(c => c.Id == guild.LeaderId)
.Select(c => c.Name)
.FirstOrDefault() ?? string.Empty;
AchievementInfo guildTrophy = ComputeGuildTrophy(guildId);
// Compute all guild totals to determine rank
var allGuildIds = Context.Guild.Select(g => g.Id).ToList();
var guildTotals = new List<(long GuildId, int Total)>();
foreach (long id in allGuildIds) {
AchievementInfo info = ComputeGuildTrophy(id);
guildTotals.Add((id, info.Total));
}
guildTotals = guildTotals.OrderByDescending(g => g.Total).ToList();
int rank = guildTotals.FindIndex(g => g.GuildId == guildId) + 1;
if (rank == 0) {
rank = guildTotals.Count + 1;
}
return new GuildTrophyRankInfo(
Rank: rank,
GuildId: guild.Id,
Name: guild.Name,
Emblem: guild.Emblem,
LeaderName: leaderName,
Trophy: guildTrophy);
}
public GuildTrophyRankInfo? GetGuildTrophyRankInfo(string guildName) {
long? guildId = Context.Guild
.Where(g => g.Name == guildName)
.Select(g => (long?) g.Id)
.FirstOrDefault();
if (guildId == null) {
return null;
}
return GetGuildTrophyRankInfo(guildId.Value);
}
public long GetGuildIdByCharacterId(long characterId) {
return Context.GuildMember
.Where(m => m.CharacterId == characterId)
.Select(m => m.GuildId)
.FirstOrDefault();
}
public IList<GuildTrophyRankInfo> GetGuildTrophyRankings() {
// Read guild data with Select projection to avoid EF tracking issues
var guilds = Context.Guild
.Select(g => new { g.Id, g.Name, g.Emblem, g.LeaderId })
.ToList();
// Batch lookup leader names
var leaderIds = guilds.Select(g => g.LeaderId).Distinct().ToList();
var leaderNames = Context.Character
.Where(c => leaderIds.Contains(c.Id))
.Select(c => new { c.Id, c.Name })
.ToDictionary(c => c.Id, c => c.Name);
var rankings = new List<GuildTrophyRankInfo>();
foreach (var guild in guilds) {
AchievementInfo trophy = ComputeGuildTrophy(guild.Id);
if (trophy.Total <= 0) {
continue;
}
string leaderName = leaderNames.GetValueOrDefault(guild.LeaderId, string.Empty);
rankings.Add(new GuildTrophyRankInfo(0, guild.Id, guild.Name, guild.Emblem, leaderName, trophy));
}
return rankings
.OrderByDescending(r => r.Trophy.Total)
.Select((r, index) => new GuildTrophyRankInfo(
Rank: index + 1,
GuildId: r.GuildId,
Name: r.Name,
Emblem: r.Emblem,
LeaderName: r.LeaderName,
Trophy: r.Trophy))
.Take(200)
.ToList();
}
private AchievementInfo ComputeGuildTrophy(long guildId) {
// Get all member character IDs and their account IDs via Select projection
var members = Context.GuildMember
.Where(m => m.GuildId == guildId)
.Select(m => m.CharacterId)
.ToList();
// Batch lookup account IDs
var characterInfo = Context.Character
.Where(c => members.Contains(c.Id))
.Select(c => new { c.Id, c.AccountId })
.ToList();
var total = new AchievementInfo();
foreach (var info in characterInfo) {
total += GetAchievementInfo(info.AccountId, info.Id);
}
return total;
}
#endregion
public IList<long> GetMentorList(long accountId, long characterId) {
// Get only characters that have been modified in the last 30 days (including time)
DateTime thirtyDaysAgo = DateTime.Now.AddDays(-30);
// Get filtered character IDs, excluding the current account and character
// Group by AccountId to ensure we only get one character per account
List<long> filteredCharacterIds = Context.Character
.Where(c => c.LastModified >= thirtyDaysAgo &&
c.AccountId != accountId &&
c.Id != characterId)
.GroupBy(c => c.AccountId) // Group by AccountId
.Select(g => g.OrderByDescending(c => c.LastModified).First().Id) // Take the most recently modified character from each account
.ToList(); // Materialize the query here
// Randomize the order and take up to 50
return filteredCharacterIds
.OrderBy(_ => Random.Shared.Next()) // Randomize order
.Take(50)
.ToList();
}
}
}