XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

chore: delete guild when founding warlord leaves (#1371)

Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1371 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

f7ada5a7
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

3 个文件 +64 -41
Modified src/controllers/api/removeFromGuildController.ts +44 -40
@@ -1,7 +1,7 @@
1 1 import { GuildMember } from "@/src/models/guildModel";
2 2 import { Inbox } from "@/src/models/inboxModel";
3 3 import { Account } from "@/src/models/loginModel";
4 import { getGuildForRequest, hasGuildPermission } from "@/src/services/guildService";
4 import { deleteGuild, getGuildForRequest, hasGuildPermission } from "@/src/services/guildService";
5 5 import { getInventory } from "@/src/services/inventoryService";
6 6 import { getAccountForRequest, getSuffixedName } from "@/src/services/loginService";
7 7 import { GuildPermission } from "@/src/types/guildTypes";
@@ -18,50 +18,54 @@ export const removeFromGuildController: RequestHandler = async (req, res) => {
18 18 }
19 19
20 20 const guildMember = (await GuildMember.findOne({ accountId: payload.userId, guildId: guild._id }))!;
21 if (guildMember.status == 0) {
22 const inventory = await getInventory(payload.userId);
23 inventory.GuildId = undefined;
21 if (guildMember.rank == 0) {
22 await deleteGuild(guild._id);
23 } else {
24 if (guildMember.status == 0) {
25 const inventory = await getInventory(payload.userId);
26 inventory.GuildId = undefined;
24 27
25 // Remove clan key or blueprint from kicked member
26 const itemIndex = inventory.LevelKeys.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKey");
27 if (itemIndex != -1) {
28 inventory.LevelKeys.splice(itemIndex, 1);
29 } else {
30 const recipeIndex = inventory.Recipes.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKeyBlueprint");
31 if (recipeIndex != -1) {
32 inventory.Recipes.splice(recipeIndex, 1);
28 // Remove clan key or blueprint from kicked member
29 const itemIndex = inventory.LevelKeys.findIndex(x => x.ItemType == "/Lotus/Types/Keys/DojoKey");
30 if (itemIndex != -1) {
31 inventory.LevelKeys.splice(itemIndex, 1);
32 } else {
33 const recipeIndex = inventory.Recipes.findIndex(
34 x => x.ItemType == "/Lotus/Types/Keys/DojoKeyBlueprint"
35 );
36 if (recipeIndex != -1) {
37 inventory.Recipes.splice(recipeIndex, 1);
38 }
33 39 }
34 }
35 40
36 await inventory.save();
37
38 // TODO: Handle clan leader kicking themselves (guild should be deleted in this case, I think)
39 } else if (guildMember.status == 2) {
40 // Delete the inbox message for the invite
41 await Inbox.deleteOne({
42 ownerId: guildMember.accountId,
43 contextInfo: guild._id.toString(),
44 acceptAction: "GUILD_INVITE"
45 });
46 }
47 await GuildMember.deleteOne({ _id: guildMember._id });
41 await inventory.save();
42 } else if (guildMember.status == 2) {
43 // Delete the inbox message for the invite
44 await Inbox.deleteOne({
45 ownerId: guildMember.accountId,
46 contextInfo: guild._id.toString(),
47 acceptAction: "GUILD_INVITE"
48 });
49 }
50 await GuildMember.deleteOne({ _id: guildMember._id });
48 51
49 guild.RosterActivity ??= [];
50 if (isKick) {
51 const kickee = (await Account.findById(payload.userId))!;
52 guild.RosterActivity.push({
53 dateTime: new Date(),
54 entryType: 12,
55 details: getSuffixedName(kickee) + "," + getSuffixedName(account)
56 });
57 } else {
58 guild.RosterActivity.push({
59 dateTime: new Date(),
60 entryType: 7,
61 details: getSuffixedName(account)
62 });
52 guild.RosterActivity ??= [];
53 if (isKick) {
54 const kickee = (await Account.findById(payload.userId))!;
55 guild.RosterActivity.push({
56 dateTime: new Date(),
57 entryType: 12,
58 details: getSuffixedName(kickee) + "," + getSuffixedName(account)
59 });
60 } else {
61 guild.RosterActivity.push({
62 dateTime: new Date(),
63 entryType: 7,
64 details: getSuffixedName(account)
65 });
66 }
67 await guild.save();
63 68 }
64 await guild.save();
65 69
66 70 res.json({
67 71 _id: payload.userId,
Modified src/controllers/custom/deleteAccountController.ts +8 -1
@@ -9,10 +9,17 @@ import { Ship } from "@/src/models/shipModel";
9 9 import { Stats } from "@/src/models/statsModel";
10 10 import { GuildMember } from "@/src/models/guildModel";
11 11 import { Leaderboard } from "@/src/models/leaderboardModel";
12 import { deleteGuild } from "@/src/services/guildService";
12 13
13 14 export const deleteAccountController: RequestHandler = async (req, res) => {
14 15 const accountId = await getAccountIdForRequest(req);
15 // TODO: Handle the account being the creator of a guild
16
17 // If account is the founding warlord of a guild, delete that guild as well.
18 const guildMember = await GuildMember.findOne({ accountId, rank: 0, status: 0 });
19 if (guildMember) {
20 await deleteGuild(guildMember.guildId);
21 }
22
16 23 await Promise.all([
17 24 Account.deleteOne({ _id: accountId }),
18 25 GuildMember.deleteMany({ accountId: accountId }),
Modified src/services/guildService.ts +12 -0
@@ -22,6 +22,7 @@ import { logger } from "../utils/logger";
22 22 import { config } from "./configService";
23 23 import { Account } from "../models/loginModel";
24 24 import { getRandomInt } from "./rngService";
25 import { Inbox } from "../models/inboxModel";
25 26
26 27 export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
27 28 const accountId = await getAccountIdForRequest(req);
@@ -360,3 +361,14 @@ export const removePigmentsFromGuildMembers = async (guildId: string | Types.Obj
360 361 }
361 362 }
362 363 };
364
365 export const deleteGuild = async (guildId: Types.ObjectId): Promise<void> => {
366 await Guild.deleteOne({ _id: guildId });
367 await GuildMember.deleteMany({ guildId });
368
369 // If guild sent any invites, delete those inbox messages as well.
370 await Inbox.deleteMany({
371 contextInfo: guildId.toString(),
372 acceptAction: "GUILD_INVITE"
373 });
374 };