返回提交历史
Modified
src/controllers/api/changeGuildRankController.ts
+25
-12
Modified
src/services/guildService.ts
+6
-6
XFEstudio/XFESpaceNinjaServer
chore: handle changeGuildRank request from U10 (#3471)
Closes #3467 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3471 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
af4ec89a
代码差异
2 个文件
+31
-18
@@ -1,28 +1,41 @@
1
import gameToBuildVersion from "../../constants/gameToBuildVersion.ts";
2
import { version_compare } from "../../helpers/inventoryHelpers.ts";
1
3
import { GuildMember } from "../../models/guildModel.ts";
2
import { getGuildForRequest, hasGuildPermissionEx } from "../../services/guildService.ts";
3
import { getAccountIdForRequest } from "../../services/loginService.ts";
4
import { getGuildForRequest, getGuildRankBase, hasGuildPermissionEx } from "../../services/guildService.ts";
5
import { getAccountForRequest } from "../../services/loginService.ts";
4
6
import { GuildPermission } from "../../types/guildTypes.ts";
5
7
import type { RequestHandler } from "express";
8
import { logger } from "../../utils/logger.ts";
6
9
7
10
export const changeGuildRankController: RequestHandler = async (req, res) => {
8
const accountId = await getAccountIdForRequest(req);
11
const account = await getAccountForRequest(req);
9
12
const member = (await GuildMember.findOne({
10
accountId: accountId,
13
accountId: account._id,
11
14
guildId: req.query.guildId as string
12
15
}))!;
13
const newRank: number = parseInt(req.query.rankChange as string);
14
15
16
const guild = await getGuildForRequest(req);
17
const target = (await GuildMember.findOne({
18
guildId: req.query.guildId as string,
19
accountId: req.query.targetId as string
20
}))!;
21
22
// TODO: Figure out the exact version when rankChange stopped being a delta.
23
const rankChange: number = parseInt(req.query.rankChange as string);
24
const rankChangeIsADelta =
25
account.BuildLabel && version_compare(account.BuildLabel, gameToBuildVersion["24.0.0"]) <= 0;
26
const newRank: number = rankChangeIsADelta
27
? target.rank + parseInt(req.query.rankChange as string)
28
: parseInt(req.query.rankChange as string);
29
logger.debug(
30
`treating rankChange of ${rankChange} as ${rankChangeIsADelta ? "a delta" : "an absolute value"}, so newRank = ${newRank}`
31
);
32
16
33
if (newRank < member.rank || !hasGuildPermissionEx(guild, member, GuildPermission.Promoter)) {
17
34
res.status(400).json("Invalid permission");
18
35
return;
19
36
}
20
37
21
const target = (await GuildMember.findOne({
22
guildId: req.query.guildId as string,
23
accountId: req.query.targetId as string
24
}))!;
25
target.rank = parseInt(req.query.rankChange as string);
38
target.rank = newRank;
26
39
await target.save();
27
40
28
41
if (newRank == 0) {
@@ -33,6 +46,6 @@ export const changeGuildRankController: RequestHandler = async (req, res) => {
33
46
34
47
res.json({
35
48
_id: req.query.targetId as string,
36
Rank: newRank
49
Rank: getGuildRankBase(account.BuildLabel) + newRank
37
50
});
38
51
};
@@ -59,22 +59,22 @@ export const getGuildForRequestEx = async (
59
59
return guild;
60
60
};
61
61
62
export const getGuildRankBase = (buildLabel: string | undefined): number => {
63
// Use 1-based indexing for clan ranks for versions before U24. In my testing, 2018.06.14.23.21 and below used 1-based indexing and 2019.04.04.21.31 and above used 0-based indexing. I didn't narrow it down further, but I think U24 is a good spot for them to have changed it.
64
return buildLabel && version_compare(buildLabel, gameToBuildVersion["24.0.0"]) < 0 ? 1 : 0;
65
};
66
62
67
export const getGuildClient = async (
63
68
guild: TGuildDatabaseDocument,
64
69
account: TAccountDocument
65
70
): Promise<IGuildClient> => {
66
71
const guildMembers = await GuildMember.find({ guildId: guild._id });
67
72
73
const rankBase = getGuildRankBase(account.BuildLabel);
68
74
const members: IGuildMemberClient[] = [];
69
75
let missingEntry = true;
70
76
const dataFillInPromises: Promise<void>[] = [];
71
77
for (const guildMember of guildMembers) {
72
// Use 1-based indexing for clan ranks for versions before U24. In my testing, 2018.06.14.23.21 and below used 1-based indexing and 2019.04.04.21.31 and above used 0-based indexing. I didn't narrow it down further, but I think U24 is a good spot for them to have changed it.
73
let rankBase = 0;
74
if (account.BuildLabel && version_compare(account.BuildLabel, gameToBuildVersion["24.0.0"]) < 0) {
75
rankBase += 1;
76
}
77
78
78
const member: IGuildMemberClient = {
79
79
_id: toOid2(guildMember.accountId, account.BuildLabel),
80
80
Rank: guildMember.rank + rankBase,