-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfivem.controller.ts
More file actions
92 lines (84 loc) · 3.43 KB
/
fivem.controller.ts
File metadata and controls
92 lines (84 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Controller } from '@nestjs/common';
import { FivemService } from './fivem.service';
import { Get, Inject, Param } from '@nestjs/common/decorators';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Cache } from 'cache-manager';
import ServerTrackedDto from 'src/dto/serverTrackedDto';
import ServerCfxDto from 'src/dto/serverCfxDto';
import { fivemCfxResponse, fivemPlayersResponse, fivemResponse } from './fivem.schema';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { CacheKeys } from 'src/utils/enums';
/*
CODE CacheKey
- FM : FiveM Track server by Address
- FMCFX : FiveM Track server by CFX Code
- FMP : FiveM Track Players
*/
@ApiTags('FiveM / RedM')
@Controller('fivem')
export class FivemController {
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
private readonly service: FivemService
) { }
@Get('/:address')
@ApiOperation({
summary: "Track a FiveM Server",
description: "Return a JSON response with information about the server",
})
@ApiOkResponse({
description: 'Server information',
schema: fivemResponse
})
async trackServer(@Param() address: ServerTrackedDto): Promise<any> {
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveM}:${address.address}`);
let result: any;
if (cache)
return cache;
result = await this.service.trackServer(address);
result["cacheTime"] = Math.floor(Date.now() / 1000);
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
this.cacheManager.set(`${CacheKeys.FiveM}:${address.address}`, result, 5 * 60 * 1000);
return result;
}
@Get('/cfx/:code')
@ApiOperation({
summary: "Track a FiveM Server by his cfx code",
description: "Return a JSON response with information about the server from FiveM's API",
})
@ApiOkResponse({
description: 'Server information',
schema: fivemCfxResponse
})
async trackServerByCfx(@Param() code: ServerCfxDto): Promise<any> {
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveMCfxCode}:${code.code}`);
let result: any;
if (cache)
return cache;
result = await this.service.trackServerByCfx(code);
result["cacheTime"] = Math.floor(Date.now() / 1000);
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
this.cacheManager.set(`${CacheKeys.FiveMCfxCode}:${code.code}`, result, 5 * 60 * 1000);
return result;
}
@Get('/players/:address')
@ApiOperation({
summary: "Track a FiveM Server's players",
description: "Return a JSON response with information about the players currently on the server",
})
@ApiOkResponse({
description: 'Players connected information',
schema: fivemPlayersResponse
})
async trackPlayers(@Param() address: ServerTrackedDto): Promise<any> {
const cache: any = await this.cacheManager.get(`${CacheKeys.FiveMPlayers}:${address.address}`);
let result: any;
if (cache)
return cache;
result = await this.service.trackPlayers(address);
result["cacheTime"] = Math.floor(Date.now() / 1000);
result["cacheExpire"] = Math.floor(Date.now() / 1000) + (5 * 60);
this.cacheManager.set(`${CacheKeys.FiveMPlayers}:${address.address}`, result, 5 * 60 * 1000);
return result;
}
}