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

fix: don't provide unicode platform suffix to pre-U32 clients (#3531)

This fixes some regressions in versions that don't (fully) understand cross-play. Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3531 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

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

代码差异

9 个文件 +47 -36
Modified src/controllers/api/addFriendController.ts +9 -8
@@ -1,26 +1,27 @@
1 import type { Types } from "mongoose";
1 2 import { toOid } from "../../helpers/inventoryHelpers.ts";
2 3 import { getJSONfromString } from "../../helpers/stringHelpers.ts";
3 4 import { Friendship } from "../../models/friendModel.ts";
4 5 import { addAccountDataToFriendInfo, addInventoryDataToFriendInfo } from "../../services/friendService.ts";
5 import { getAccountIdForRequest } from "../../services/loginService.ts";
6 import { getAccountForRequest, getAccountIdForRequest } from "../../services/loginService.ts";
6 7 import type { IFriendInfo } from "../../types/friendTypes.ts";
7 8 import type { RequestHandler } from "express";
8 9
9 10 export const addFriendPostController: RequestHandler = async (req, res) => {
10 const accountId = await getAccountIdForRequest(req);
11 const account = await getAccountForRequest(req);
11 12 const payload = getJSONfromString<IAddFriendRequest>(String(req.body));
12 13 const promises: Promise<void>[] = [];
13 14 const newFriends: IFriendInfo[] = [];
14 15 if (payload.friend == "all") {
15 16 const [internalFriendships, externalFriendships] = await Promise.all([
16 Friendship.find({ owner: accountId }, "friend"),
17 Friendship.find({ friend: accountId }, "owner")
17 Friendship.find({ owner: account._id }, "friend"),
18 Friendship.find({ friend: account._id }, "owner")
18 19 ]);
19 20 for (const externalFriendship of externalFriendships) {
20 21 if (!internalFriendships.find(x => x.friend.equals(externalFriendship.owner))) {
21 22 promises.push(
22 23 Friendship.insertOne({
23 owner: accountId,
24 owner: account._id,
24 25 friend: externalFriendship.owner,
25 26 Note: externalFriendship.Note // TOVERIFY: Should the note be copied when accepting a friend request?
26 27 }) as unknown as Promise<void>
@@ -31,10 +32,10 @@ export const addFriendPostController: RequestHandler = async (req, res) => {
31 32 }
32 33 }
33 34 } else {
34 await acceptFriendRequest(accountId, payload.friend, newFriends);
35 await acceptFriendRequest(account._id, payload.friend, newFriends);
35 36 }
36 37 for (const newFriend of newFriends) {
37 promises.push(addAccountDataToFriendInfo(newFriend));
38 promises.push(addAccountDataToFriendInfo(newFriend, account.BuildLabel));
38 39 promises.push(addInventoryDataToFriendInfo(newFriend));
39 40 }
40 41 await Promise.all(promises);
@@ -54,7 +55,7 @@ export const addFriendGetController: RequestHandler = async (req, res) => {
54 55 };
55 56
56 57 const acceptFriendRequest = async (
57 accountId: string,
58 accountId: Types.ObjectId | string,
58 59 otherAccountId: string,
59 60 newFriends: IFriendInfo[] | undefined
60 61 ): Promise<void> => {
Modified src/controllers/api/addPendingFriendController.ts +17 -18
@@ -4,15 +4,15 @@ import { Friendship } from "../../models/friendModel.ts";
4 4 import { Account } from "../../models/loginModel.ts";
5 5 import { addInventoryDataToFriendInfo, areFriendsOfFriends } from "../../services/friendService.ts";
6 6 import { getInventory } from "../../services/inventoryService.ts";
7 import { getAccountIdForRequest } from "../../services/loginService.ts";
7 import { getAccountForRequest, getUnicodeName, type TAccountDocument } from "../../services/loginService.ts";
8 8 import type { IFriendInfo } from "../../types/friendTypes.ts";
9 9 import type { RequestHandler, Response } from "express";
10 10 import { logger } from "../../utils/logger.ts";
11 11
12 12 export const addPendingFriendPostController: RequestHandler = async (req, res) => {
13 const accountId = await getAccountIdForRequest(req);
13 const account = await getAccountForRequest(req);
14 14 const payload = getJSONfromString<IAddPendingFriendRequest>(String(req.body));
15 await sendFriendRequest(accountId, payload.friend, payload.message, res);
15 await sendFriendRequest(account, payload.friend, payload.message, res);
16 16 };
17 17
18 18 interface IAddPendingFriendRequest {
@@ -21,27 +21,27 @@ interface IAddPendingFriendRequest {
21 21 }
22 22
23 23 export const addPendingFriendGetController: RequestHandler = async (req, res) => {
24 const accountId = await getAccountIdForRequest(req);
25 await sendFriendRequest(accountId, req.query.friend as string, undefined, res);
24 const account = await getAccountForRequest(req);
25 await sendFriendRequest(account, req.query.friend as string, undefined, res);
26 26 };
27 27
28 28 const sendFriendRequest = async (
29 accountId: string,
29 requesterAccount: TAccountDocument,
30 30 name: string,
31 31 message: string | undefined,
32 32 res: Response
33 33 ): Promise<void> => {
34 const account = await Account.findOne({ DisplayName: name });
35 if (!account) {
34 const requesteeAccount = await Account.findOne({ DisplayName: name });
35 if (!requesteeAccount) {
36 36 res.status(400).send("Given Username does not exist.");
37 37 return;
38 38 }
39 39
40 const inventory = await getInventory(account._id, "Settings");
40 const requesteeInventory = await getInventory(requesteeAccount._id, "Settings");
41 41 if (
42 inventory.Settings?.FriendInvRestriction == "GIFT_MODE_NONE" ||
43 (inventory.Settings?.FriendInvRestriction == "GIFT_MODE_FRIENDS" &&
44 !(await areFriendsOfFriends(account._id, accountId)))
42 requesteeInventory.Settings?.FriendInvRestriction == "GIFT_MODE_NONE" ||
43 (requesteeInventory.Settings?.FriendInvRestriction == "GIFT_MODE_FRIENDS" &&
44 !(await areFriendsOfFriends(requesteeAccount._id, requesterAccount._id)))
45 45 ) {
46 46 res.status(400).send("Friend Invite Restriction");
47 47 return;
@@ -49,8 +49,8 @@ const sendFriendRequest = async (
49 49
50 50 try {
51 51 await Friendship.insertOne({
52 owner: accountId,
53 friend: account._id,
52 owner: requesterAccount._id,
53 friend: requesteeAccount._id,
54 54 Note: message,
55 55 NewRequest: true
56 56 });
@@ -60,11 +60,10 @@ const sendFriendRequest = async (
60 60 return;
61 61 }
62 62
63 const platformId = 0; // TODO
64 63 const friendInfo: IFriendInfo = {
65 _id: toOid(account._id),
66 DisplayName: account.DisplayName + String.fromCharCode(0xe000 + platformId),
67 LastLogin: toMongoDate(account.LastLogin),
64 _id: toOid(requesteeAccount._id),
65 DisplayName: getUnicodeName(requesteeAccount.DisplayName, requesterAccount.BuildLabel),
66 LastLogin: toMongoDate(requesteeAccount.LastLogin),
68 67 Note: message
69 68 };
70 69 await addInventoryDataToFriendInfo(friendInfo);
Modified src/controllers/api/getFriendsController.ts +1 -1
@@ -40,7 +40,7 @@ export const getFriendsController: RequestHandler = async (req: Request, res: Re
40 40 const promises: Promise<void>[] = [];
41 41 for (const arr of Object.values(response)) {
42 42 for (const friendInfo of arr) {
43 promises.push(addAccountDataToFriendInfo(friendInfo));
43 promises.push(addAccountDataToFriendInfo(friendInfo, account.BuildLabel));
44 44 promises.push(addInventoryDataToFriendInfo(friendInfo));
45 45 }
46 46 }
Modified src/controllers/api/getRecentPlayersController.ts +3 -1
@@ -2,12 +2,14 @@ import type { RequestHandler } from "express";
2 2 import type { IFriendInfo } from "../../types/friendTypes.ts";
3 3 import { getJSONfromString } from "../../helpers/stringHelpers.ts";
4 4 import { addAccountDataToFriendInfo, addInventoryDataToFriendInfo } from "../../services/friendService.ts";
5 import { getAccountForRequest } from "../../services/loginService.ts";
5 6
6 7 export const getRecentPlayersController: RequestHandler = async (req, res): Promise<void> => {
8 const account = await getAccountForRequest(req);
7 9 const payload = getJSONfromString<IRecentPlayersPayload>(String(req.body));
8 10 const promises: Promise<void>[] = [];
9 11 for (const info of payload.RecentPlayers) {
10 promises.push(addAccountDataToFriendInfo(info));
12 promises.push(addAccountDataToFriendInfo(info, account.BuildLabel));
11 13 promises.push(addInventoryDataToFriendInfo(info));
12 14 }
13 15 await Promise.all(promises);
Modified src/controllers/api/removeFriendController.ts +2 -3
@@ -4,7 +4,7 @@ import { getJSONfromString } from "../../helpers/stringHelpers.ts";
4 4 import { Friendship } from "../../models/friendModel.ts";
5 5 import { Account } from "../../models/loginModel.ts";
6 6 import { getInventory } from "../../services/inventoryService.ts";
7 import { getAccountForRequest } from "../../services/loginService.ts";
7 import { getAccountForRequest, getUnicodeName } from "../../services/loginService.ts";
8 8 import type { IOid } from "../../types/commonTypes.ts";
9 9 import { parallelForeach } from "../../utils/async-utils.ts";
10 10 import type { RequestHandler } from "express";
@@ -111,8 +111,7 @@ const toRemoveFriendsResponse = async (
111 111 for (const friend of friends) {
112 112 const acct = await Account.findById(friend.$oid, "DisplayName");
113 113 if (acct) {
114 const platformId = 0; // TODO
115 response.FriendNames.push(acct.DisplayName + String.fromCharCode(0xe000 + platformId));
114 response.FriendNames.push(getUnicodeName(acct.DisplayName, buildLabel));
116 115 }
117 116 }
118 117 return response;
Modified src/controllers/custom/getGuildController.ts +1 -1
@@ -21,7 +21,7 @@ export const getGuildController: RequestHandler = async (req, res) => {
21 21 Note: guildMember.RequestMsg,
22 22 RequestExpiry: guildMember.RequestExpiry ? toMongoDate(guildMember.RequestExpiry) : undefined
23 23 };
24 dataFillInPromises.push(addAccountDataToFriendInfo(member));
24 dataFillInPromises.push(addAccountDataToFriendInfo(member, undefined));
25 25 dataFillInPromises.push(addInventoryDataToFriendInfo(member));
26 26
27 27 members.push(member);
Modified src/services/friendService.ts +3 -3
@@ -4,11 +4,11 @@ import { Account } from "../models/loginModel.ts";
4 4 import type { Types } from "mongoose";
5 5 import { Friendship } from "../models/friendModel.ts";
6 6 import { fromOid, toMongoDate } from "../helpers/inventoryHelpers.ts";
7 import { getUnicodeName } from "./loginService.ts";
7 8
8 export const addAccountDataToFriendInfo = async (info: IFriendInfo): Promise<void> => {
9 export const addAccountDataToFriendInfo = async (info: IFriendInfo, buildLabel: string | undefined): Promise<void> => {
9 10 const account = (await Account.findById(fromOid(info._id), "DisplayName LastLogin"))!;
10 const platformId = 0; // TODO
11 info.DisplayName = account.DisplayName + String.fromCharCode(0xe000 + platformId);
11 info.DisplayName = getUnicodeName(account.DisplayName, buildLabel);
12 12 info.LastLogin = toMongoDate(account.LastLogin);
13 13 };
14 14
Modified src/services/guildService.ts +1 -1
@@ -87,7 +87,7 @@ export const getGuildClient = async (
87 87 if (guildMember.accountId.equals(account._id)) {
88 88 missingEntry = false;
89 89 } else {
90 dataFillInPromises.push(addAccountDataToFriendInfo(member));
90 dataFillInPromises.push(addAccountDataToFriendInfo(member, account.BuildLabel));
91 91 dataFillInPromises.push(addInventoryDataToFriendInfo(member));
92 92 }
93 93 members.push(member);
Modified src/services/loginService.ts +10 -0
@@ -11,6 +11,8 @@ import { createStats } from "./statsService.ts";
11 11 import crc32 from "crc-32";
12 12 import crypto from "node:crypto";
13 13 import { logger } from "../utils/logger.ts";
14 import { version_compare } from "../helpers/inventoryHelpers.ts";
15 import gameToBuildVersion from "../constants/gameToBuildVersion.ts";
14 16
15 17 export const isCorrectPassword = (requestPassword: string, databasePassword: string): boolean => {
16 18 return requestPassword === databasePassword;
@@ -137,3 +139,11 @@ export const getSuffixedName = (account: TAccountDocument): string => {
137 139 export const getAccountFromSuffixedName = (name: string): Promise<TAccountDocument | null> => {
138 140 return Account.findOne({ DisplayName: name.split("#")[0] });
139 141 };
142
143 export const getUnicodeName = (DisplayName: string, buildLabel: string | undefined): string => {
144 if (buildLabel && version_compare(buildLabel, gameToBuildVersion["32.0.0"]) < 0) {
145 return DisplayName;
146 }
147 const platformId = 0; // TODO
148 return DisplayName + String.fromCharCode(0xe000 + platformId);
149 };