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

Return the server a tier below if it does not exist #158

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
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
54 changes: 42 additions & 12 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const discordConnectionSchema = joi.object({
id: joi.string()
});

const accessModeOrder: Record<string, string[]> = {
prod: ['prod'],
test: ['test', 'prod'],
dev: ['dev', 'test', 'prod']
};

let _connection: mongoose.Connection;

export async function connect(): Promise<void> {
Expand Down Expand Up @@ -206,24 +212,48 @@ export async function getPNIDProfileJSONByPID(pid: number): Promise<PNIDProfile
}

export async function getServerByGameServerID(gameServerID: string, accessMode: string): Promise<HydratedServerDocument | null> {
return await Server.findOne({
game_server_id: gameServerID,
access_mode: accessMode
});
const searchModes = accessModeOrder[accessMode] || ['prod'];

for (const mode of searchModes) {
const server = await Server.findOne({
game_server_id: gameServerID,
access_mode: mode
});

if (server) return server;
}

return null;
}

export async function getServerByTitleID(titleID: string, accessMode: string): Promise<HydratedServerDocument | null> {
return await Server.findOne({
title_ids: titleID,
access_mode: accessMode
});
const searchModes = accessModeOrder[accessMode] || ['prod'];

for (const mode of searchModes) {
const server = await Server.findOne({
title_ids: titleID,
access_mode: mode
});

if (server) return server;
}

return null;
}

export async function getServerByClientID(clientID: string, accessMode: string): Promise<HydratedServerDocument | null> {
return await Server.findOne({
client_id: clientID,
access_mode: accessMode
});
const searchModes = accessModeOrder[accessMode] || ['prod'];

for (const mode of searchModes) {
const server = await Server.findOne({
client_id: clientID,
access_mode: mode
});

if (server) return server;
}

return null;
}


Expand Down