Refactor PlayerGetTokenCsReq to a try-catch syntax

This commit is contained in:
memetrollsXD 2022-08-05 07:50:42 +02:00
parent 1f2b9f5726
commit 3649681f9f
No known key found for this signature in database
GPG Key ID: 105C2F3417AC32CD

View File

@ -18,12 +18,12 @@ const retWarn = (msg: string) => c.warn(msg);
export default async function handle(session: Session, packet: Packet) {
const body = packet.body as PlayerGetTokenCsReq;
const dataObj = {
retcode: 0,
secretKeySeed: 0,
} as PlayerGetTokenScRsp;
try {
const account = await Account.fromToken(body.token || "");
if (!account) retWarn(`Account not found with token ${body.token}`);
@ -31,20 +31,34 @@ export default async function handle(session: Session, packet: Packet) {
if (!player) retWarn(`Player not found with accountToken ${account?.token}`);
if (!player || !account) {
dataObj.retcode = 6;
dataObj.msg = "Player not found";
return;
}
session.account = account;
session.player = player;
dataObj.uid = player.db._id;
const isTokenValid = player.db.token === body.token;
const isBanned = player.db.banned;
if (isBanned) dataObj.retcode = 1013;
if (!isTokenValid) {
retWarn(`Token invalid (${session.ctx.address}:${session.ctx.port})`);
dataObj.retcode = 1005;
return;
if (player.db.banned) {
dataObj.retcode = 1013;
dataObj.blackInfo = {
banType: 2,
beginTime: Math.floor(Date.now() / 1000),
endTime: Math.floor(Date.now() / 1000) + 86400,
limitLevel: 0,
}
}
dataObj.uid = player.db._id;
if (player.db.token !== body.token) {
retWarn(`Token invalid (${session.ctx.address}:${session.ctx.port})`);
dataObj.retcode = 1005;
dataObj.msg = "Token invalid";
}
} catch (e) {
dataObj.retcode = 2;
c.error(e as Error);
} finally {
session.send(PlayerGetTokenScRsp, dataObj);
}
}