Refactor PlayerGetTokenCsReq to a try-catch syntax
This commit is contained in:
parent
1f2b9f5726
commit
3649681f9f
@ -18,33 +18,47 @@ 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;
|
||||
|
||||
const account = await Account.fromToken(body.token || "");
|
||||
if (!account) retWarn(`Account not found with token ${body.token}`);
|
||||
try {
|
||||
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 || "");
|
||||
if (!player) retWarn(`Player not found with accountToken ${account?.token}`);
|
||||
if (!player || !account) {
|
||||
dataObj.retcode = 6;
|
||||
return;
|
||||
}
|
||||
session.account = account;
|
||||
session.player = player;
|
||||
const player = await Player.fromToken(session, account?.token || "");
|
||||
if (!player) retWarn(`Player not found with accountToken ${account?.token}`);
|
||||
if (!player || !account) {
|
||||
dataObj.retcode = 6;
|
||||
dataObj.msg = "Player not found";
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
session.account = account;
|
||||
session.player = player;
|
||||
dataObj.uid = player.db._id;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user