Refactor PlayerGetTokenCsReq to a try-catch syntax
This commit is contained in:
parent
1f2b9f5726
commit
3649681f9f
@ -18,12 +18,12 @@ 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;
|
||||||
|
|
||||||
|
try {
|
||||||
const account = await Account.fromToken(body.token || "");
|
const account = await Account.fromToken(body.token || "");
|
||||||
if (!account) retWarn(`Account not found with token ${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) retWarn(`Player not found with accountToken ${account?.token}`);
|
||||||
if (!player || !account) {
|
if (!player || !account) {
|
||||||
dataObj.retcode = 6;
|
dataObj.retcode = 6;
|
||||||
|
dataObj.msg = "Player not found";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
session.account = account;
|
session.account = account;
|
||||||
session.player = player;
|
session.player = player;
|
||||||
|
dataObj.uid = player.db._id;
|
||||||
|
|
||||||
const isTokenValid = player.db.token === body.token;
|
if (player.db.banned) {
|
||||||
const isBanned = player.db.banned;
|
dataObj.retcode = 1013;
|
||||||
if (isBanned) dataObj.retcode = 1013;
|
dataObj.blackInfo = {
|
||||||
if (!isTokenValid) {
|
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})`);
|
retWarn(`Token invalid (${session.ctx.address}:${session.ctx.port})`);
|
||||||
dataObj.retcode = 1005;
|
dataObj.retcode = 1005;
|
||||||
return;
|
dataObj.msg = "Token invalid";
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
dataObj.uid = player.db._id;
|
dataObj.retcode = 2;
|
||||||
|
c.error(e as Error);
|
||||||
|
} finally {
|
||||||
session.send(PlayerGetTokenScRsp, dataObj);
|
session.send(PlayerGetTokenScRsp, dataObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user