Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server/player): handle player loading errors and cleanup #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions server/player/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ const connectingPlayers: Dict<OxPlayer> = {};

/** Loads existing data for the player, or inserts new data into the database. */
async function loadPlayer(playerId: number) {
let player: OxPlayer | undefined;

try {
if (serverLockdown) return serverLockdown;

const player = new OxPlayer(playerId);
player = new OxPlayer(playerId);
const license = SV_LAN ? 'fayoum' : GetPlayerLicense(playerId);

if (!license) return `could not validate player license.`;
Expand All @@ -23,22 +25,31 @@ async function loadPlayer(playerId: number) {

if (userId && OxPlayer.getFromUserId(userId)) {
const kickReason = `userId '${userId}' is already active.`;

if (!DEBUG) return kickReason;

userId = (await GetUserIdFromIdentifier(identifier, 1)) ?? 0;

if (userId && OxPlayer.getFromUserId(userId)) return kickReason;
}

// Safely set player properties within try block
player.username = GetPlayerName(player.source as string);
player.userId = userId ? userId : await CreateUser(player.username, GetIdentifiers(playerId));
player.identifier = identifier;

DEV: console.info(`Loaded player data for OxPlayer<${player.userId}>`);

return player;

} catch (err) {
console.error('Error loading player:', err);
// Ensure we clean up if there was an error during setup
if (player?.userId) {
try {
OxPlayer.remove(player.source);
} catch (cleanupErr) {
console.error('Error during cleanup:', cleanupErr);
}
}
return err.message;
}
}
Expand Down