-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c017f0
commit 7873a78
Showing
29 changed files
with
555 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,49 @@ | ||
import { z } from 'zod' | ||
import { standardAdminPage } from '../plugins/standard-admin-page' | ||
import fp from 'fastify-plugin' | ||
import { GameServersPage } from './views/html/game-servers.page' | ||
import type { ZodTypeProvider } from 'fastify-type-provider-zod' | ||
import { PlayerRole } from '../../database/models/player.model' | ||
import { z } from 'zod' | ||
import { configuration } from '../../configuration' | ||
import { RegionList } from './views/html/serveme-tf-preferred-region' | ||
|
||
export default standardAdminPage({ | ||
path: '/admin/game-servers', | ||
page: async user => await GameServersPage({ user }), | ||
bodySchema: z.object({}), | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
save: async () => {}, | ||
}) | ||
export default fp( | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async app => { | ||
app | ||
.withTypeProvider<ZodTypeProvider>() | ||
.get( | ||
'/admin/game-servers', | ||
{ | ||
config: { | ||
authorize: [PlayerRole.admin], | ||
}, | ||
}, | ||
async (request, reply) => { | ||
return reply.status(200).html(GameServersPage({ user: request.user! })) | ||
}, | ||
) | ||
.put( | ||
'/admin/game-servers/serveme-tf/preferred-region', | ||
{ | ||
config: { | ||
authorize: [PlayerRole.admin], | ||
}, | ||
schema: { | ||
body: z.object({ | ||
servemeTfPreferredRegion: z.string().transform(val => (val === 'none' ? null : val)), | ||
}), | ||
}, | ||
}, | ||
async (request, reply) => { | ||
await configuration.set( | ||
'serveme_tf.preferred_region', | ||
request.body.servemeTfPreferredRegion, | ||
) | ||
return reply.status(200).html(RegionList({ saveResult: { success: true } })) | ||
}, | ||
) | ||
}, | ||
{ | ||
name: `admin - game servers`, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/admin/game-servers/views/html/serveme-tf-preferred-region.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { memoize } from 'lodash-es' | ||
import { servemeTf } from '../../../../serveme-tf' | ||
import getUnicodeFlagIcon from 'country-flag-icons/unicode' | ||
import { configuration } from '../../../../configuration' | ||
import { IconLoader3 } from '../../../../html/components/icons' | ||
|
||
export function ServemeTfPreferredRegion() { | ||
return ( | ||
<p> | ||
<dl> | ||
<dt> | ||
<label for="serveme-tf-preferred-region-select"> | ||
Preferred region for reserved servers | ||
</label> | ||
</dt> | ||
<dd> | ||
<div> | ||
<RegionList /> | ||
</div> | ||
<span class="text-sm text-abru-light-75"> | ||
If a game server from the preferred region is not available, another one will be picked | ||
up instead. | ||
</span> | ||
</dd> | ||
</dl> | ||
</p> | ||
) | ||
} | ||
|
||
const getRegions = memoize(async () => await servemeTf.listRegions()) | ||
|
||
type SaveResult = { success: true } | ||
|
||
export async function RegionList(props?: { saveResult?: SaveResult }) { | ||
const regions = await getRegions() | ||
const selected = await configuration.get('serveme_tf.preferred_region') | ||
|
||
return ( | ||
<div id="serveme-tf-preferred-region" class="flex flex-row items-center gap-2"> | ||
<select | ||
name="servemeTfPreferredRegion" | ||
id="serveme-tf-preferred-region-select" | ||
hx-put="/admin/game-servers/serveme-tf/preferred-region" | ||
hx-trigger="change" | ||
hx-target="#serveme-tf-preferred-region" | ||
hx-swap="outerHTML" | ||
hx-disabled-elt="this" | ||
class="peer" | ||
> | ||
<option value="none" selected={selected === null}> | ||
none | ||
</option> | ||
{regions.map(region => ( | ||
<option value={region} selected={region === selected} safe> | ||
{getUnicodeFlagIcon(region)} {region} | ||
</option> | ||
))} | ||
</select> | ||
<IconLoader3 class="hidden animate-spin peer-[.htmx-request]:block" /> | ||
{props?.saveResult?.success === true && ( | ||
<span remove-me="3s" class="text-green-600"> | ||
saved! | ||
</span> | ||
)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IconCheck, IconX } from '../../../../html/components/icons' | ||
import { servemeTf } from '../../../../serveme-tf' | ||
|
||
export function ServemeTfStatus() { | ||
return servemeTf.isEnabled ? ( | ||
<span class="flex flex-row text-green-600"> | ||
<IconCheck /> | ||
enabled | ||
</span> | ||
) : ( | ||
<span class="flex flex-row text-red-600"> | ||
<IconX /> | ||
disabled | ||
</span> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
import { GameServerProvider, type GameModel } from '../database/models/game.model' | ||
import { type GameModel, type GameServer } from '../database/models/game.model' | ||
import { games } from '../games' | ||
import { staticGameServers } from '../static-game-servers' | ||
import { events } from '../events' | ||
import { GameEventType } from '../database/models/game-event.model' | ||
import { logger } from '../logger' | ||
import { Mutex } from 'async-mutex' | ||
import { servemeTf } from '../serveme-tf' | ||
|
||
const mutex = new Mutex() | ||
|
||
export async function assign(game: GameModel) { | ||
await mutex.runExclusive(async () => { | ||
const staticGameServer = await staticGameServers.assign(game) | ||
if (!staticGameServer) { | ||
throw new Error(`no free servers available for game ${game.number}`) | ||
} | ||
const gameServer = await assignFirstFree(game) | ||
|
||
game = await games.update(game.number, { | ||
$set: { | ||
gameServer: { | ||
id: staticGameServer.id, | ||
name: staticGameServer.name, | ||
address: staticGameServer.address, | ||
port: staticGameServer.port, | ||
provider: GameServerProvider.static, | ||
}, | ||
gameServer, | ||
}, | ||
$push: { | ||
events: { | ||
event: GameEventType.gameServerAssigned, | ||
at: new Date(), | ||
gameServerName: staticGameServer.name, | ||
gameServerName: gameServer.name, | ||
}, | ||
}, | ||
}) | ||
logger.info({ game }, `game ${game.number} assigned to game server ${staticGameServer.name}`) | ||
logger.info({ game }, `game ${game.number} assigned to game server ${gameServer.name}`) | ||
events.emit('game:gameServerAssigned', { game }) | ||
}) | ||
} | ||
|
||
function assignFirstFree(game: GameModel): Promise<GameServer> { | ||
return staticGameServers | ||
.assign(game) | ||
.catch(() => servemeTf.assign(game)) | ||
.catch(() => { | ||
throw new Error(`no free servers available for game ${game.number}`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.