Make targetting navigation 100% nullsafe

This commit is contained in:
memetrollsXD 2022-08-05 07:59:55 +02:00
parent 590236cbfa
commit 91d842c1ec
No known key found for this signature in database
GPG Key ID: 105C2F3417AC32CD
2 changed files with 10 additions and 9 deletions

View File

@ -23,7 +23,7 @@ export default class Interface {
output: process.stdout output: process.stdout
}); });
public static target: Session; public static target?: Session;
private constructor() { } private constructor() { }

View File

@ -15,26 +15,27 @@ export default async function handle(command: Command) {
SRServer.getInstance().sessions.forEach(client => { SRServer.getInstance().sessions.forEach(client => {
possibleTargets.push({ possibleTargets.push({
id: `${client.ctx.address}:${client.ctx.port}`, id: `${client.ctx.address}:${client.ctx.port} (UID: ${client.account.uid})`,
uid: client.player.uid, uid: Number(client.account.uid),
session: client session: client
}); });
}); });
if (!target) { if (!target) {
c.log("No target specified"); c.log("No target specified");
if (Interface.target) c.log(`Current target: ${Interface.target.account.name} (UID: ${Interface.target.account.uid})`);
c.log("Possible targets: "); c.log("Possible targets: ");
possibleTargets.forEach(x => c.trail(`${x.id} (UID: ${x.uid})`)); possibleTargets.forEach(x => c.trail(`${x.id} (UID: ${x.uid})`));
if(!possibleTargets[1] && possibleTargets[0]){ if (!possibleTargets[1] && possibleTargets[0]) {
c.log(`Auto target the only session ${possibleTargets[0].uid}`); c.log(`Auto targetting the only session ${possibleTargets[0].uid}`);
Interface.target = possibleTargets[0].session; Interface.target = possibleTargets[0].session;
} }
return; return;
} }
const autoTarget = findBestMatch(target, possibleTargets.map(x => x.id))?.bestMatch.target; const autoTarget = findBestMatch(target, possibleTargets.map(x => x.id))?.bestMatch?.target;
Interface.target = possibleTargets.find(x => x.id === autoTarget)!.session; Interface.target = possibleTargets.find(x => x.id === autoTarget)?.session;
c.log(`Target set to ${autoTarget}`); c.log(`Target set to ${Interface.target ? Interface.target.account.name : "none"} (UID: ${Interface.target ? Interface.target.account.uid : "none"})`);
} }