-
-
Notifications
You must be signed in to change notification settings - Fork 274
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
03fce5e
commit 99aba7c
Showing
8 changed files
with
86 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ok } from 'assert'; | ||
import { RouterContext } from 'fivem-router'; | ||
import { z } from 'zod'; | ||
import { handleError } from '../utils/errors'; | ||
import PlayerService from '../services/PlayerService'; | ||
|
||
const selectDeviceSchema = z.object({ | ||
deviceIdentifier: z.string(), | ||
}); | ||
|
||
export const selectDeviceMiddleware = async (ctx: RouterContext, next: () => Promise<void>) => { | ||
if (ctx.url !== '/select-device') { | ||
return next(); | ||
} | ||
|
||
try { | ||
const { deviceIdentifier } = selectDeviceSchema.parse(ctx.request.body); | ||
await PlayerService.selectDevice(ctx.source, deviceIdentifier); | ||
ctx.status = 200; | ||
ctx.body = { | ||
ok: true, | ||
message: `Device selected: ${deviceIdentifier}`, | ||
}; | ||
} catch (error) { | ||
handleError(error, ctx); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class PlayerService { | ||
deviceMap: Map<number, string> = new Map(); | ||
constructor() {} | ||
|
||
public authorizeDevice(src: number, deviceIdentifier: string) { | ||
return true; | ||
} | ||
|
||
public async selectDevice(src: number, deviceIdentifier: string) { | ||
if (!this.authorizeDevice(src, deviceIdentifier)) { | ||
throw new Error('Unauthorized'); | ||
} | ||
|
||
this.deviceMap.set(src, deviceIdentifier); | ||
} | ||
|
||
public getDevice(src: number) { | ||
return this.deviceMap.get(src); | ||
} | ||
|
||
public async getDevices() { | ||
return Array.from(this.deviceMap.values()); | ||
} | ||
} | ||
|
||
export default new PlayerService(); |
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