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