forked from simrail/simrail.pro
switch to @simrail/types
This commit is contained in:
parent
624dc3e0aa
commit
fb6788ae2a
6
package-lock.json
generated
6
package-lock.json
generated
@ -9,6 +9,7 @@
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@simrail/types": "^0.0.4",
|
||||
"dayjs": "^1.11.12",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^4.19.2",
|
||||
@ -85,6 +86,11 @@
|
||||
"@redis/client": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@simrail/types": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@simrail/types/-/types-0.0.4.tgz",
|
||||
"integrity": "sha512-AknM5FP+crERb3m/YtZqCgdFbrwUEwYB4BXfv1z+b7CVSrRsidAnaMIJZw/MhmsGxAmNIqQK7RPhDEx8qNzblA=="
|
||||
},
|
||||
"node_modules/@types/body-parser": {
|
||||
"version": "1.19.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz",
|
||||
|
@ -19,6 +19,7 @@
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@simrail/types": "^0.0.4",
|
||||
"dayjs": "^1.11.12",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^4.19.2",
|
||||
|
@ -1,4 +0,0 @@
|
||||
export type ISimrailPayload = {
|
||||
result: boolean;
|
||||
description: string;
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
/* steam api */
|
||||
|
||||
export type IPlayer = {
|
||||
steamid: string,
|
||||
communityvisibilitystate: number,
|
||||
|
@ -1,23 +0,0 @@
|
||||
import { ISimrailPayload } from './payload.js';
|
||||
|
||||
export type IStation = {
|
||||
Name: string;
|
||||
Prefix: string;
|
||||
DifficultyLevel: number;
|
||||
Latititude: number;
|
||||
Longitude: number;
|
||||
MainImageURL: string;
|
||||
AdditionalImage1URL: string;
|
||||
AdditionalImage2URL: string;
|
||||
DispatchedBy: IStationDispatch[];
|
||||
}
|
||||
|
||||
export type IStationDispatch = {
|
||||
ServerCode: string,
|
||||
SteamId: number
|
||||
}
|
||||
|
||||
export type IStationPayload = {
|
||||
data: IStation[];
|
||||
count: number;
|
||||
} & ISimrailPayload;
|
@ -3,7 +3,7 @@ import { IPlayerPayload } from '../types/player.js';
|
||||
const STEAM_API_KEY = process.env.STEAM_APIKEY;
|
||||
|
||||
export class PlayerUtil {
|
||||
public static async getPlayer(steamId: string) {
|
||||
public static async getPlayer(steamId: number | string) {
|
||||
const data = (await fetch(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=${STEAM_API_KEY}&format=json&steamids=${steamId}`).then(x => x.json())) as IPlayerPayload;
|
||||
if (!data.response.players) return;
|
||||
return data.response.players[0];
|
||||
|
@ -1,24 +1,24 @@
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { IStation, IStationPayload } from '../types/station.js';
|
||||
|
||||
import { IPlayer } from '../types/player.js';
|
||||
import { PlayerUtil } from './PlayerUtil.js';
|
||||
import { Station, ApiResponse } from '@simrail/types';
|
||||
|
||||
export enum SimrailClientEvents {
|
||||
StationJoined = 'stationJoined',
|
||||
StationLeft = 'stationLeft',
|
||||
|
||||
}
|
||||
|
||||
export declare interface SimrailClient {
|
||||
on(event: SimrailClientEvents.StationJoined, listener: (station: IStation, player: IPlayer) => void): this;
|
||||
on(event: SimrailClientEvents.StationLeft, listener: (station: IStation, player: IPlayer, joinedAt: number) => void): this;
|
||||
on(event: SimrailClientEvents.StationJoined, listener: (station: Station, player: IPlayer) => void): this;
|
||||
on(event: SimrailClientEvents.StationLeft, listener: (station: Station, player: IPlayer, joinedAt: number) => void): this;
|
||||
|
||||
on(event: string, listener: Function): this;
|
||||
}
|
||||
|
||||
|
||||
export class SimrailClient extends EventEmitter {
|
||||
public stations: IStation[] = [];
|
||||
public stations: Station[] = [];
|
||||
public stationsOccupied: Record<string, { steamId: string; joinedAt: number } | null> = {};
|
||||
|
||||
public constructor() {
|
||||
@ -41,13 +41,13 @@ export class SimrailClient extends EventEmitter {
|
||||
if (!await redis.json.get('stations_occupied'))
|
||||
redis.json.set('stations_occupied', '$', {});
|
||||
|
||||
this.stations = (await redis.json.get('stations') as unknown as IStation[]);
|
||||
this.stations = (await redis.json.get('stations') as unknown as Station[]);
|
||||
this.stationsOccupied = (await redis.json.get('stations_occupied') as unknown as Record<string, { steamId: string; joinedAt: number } | null>);
|
||||
}
|
||||
|
||||
|
||||
private async update() {
|
||||
const servers = (await fetch('https://panel.simrail.eu:8084/stations-open?serverCode=pl2').then(x => x.json())) as IStationPayload;
|
||||
const servers = (await fetch('https://panel.simrail.eu:8084/stations-open?serverCode=pl2').then(x => x.json())) as ApiResponse<Station>;
|
||||
if (!servers.result) return
|
||||
|
||||
|
||||
@ -68,11 +68,11 @@ export class SimrailClient extends EventEmitter {
|
||||
if (!data.DispatchedBy[0]?.SteamId) {
|
||||
// join
|
||||
const date = new Date();
|
||||
const player = await PlayerUtil.getPlayer(x.DispatchedBy[0]?.SteamId.toString());
|
||||
const player = await PlayerUtil.getPlayer(x.DispatchedBy[0]?.SteamId);
|
||||
|
||||
this.emit(SimrailClientEvents.StationJoined, x, player);
|
||||
this.stationsOccupied[data.Prefix] = {
|
||||
steamId: x.DispatchedBy[0]?.SteamId.toString(),
|
||||
steamId: x.DispatchedBy[0]?.SteamId,
|
||||
joinedAt: date.getTime()
|
||||
}
|
||||
redis.json.set('stations_occupied', '$', this.stationsOccupied);
|
||||
@ -80,7 +80,7 @@ export class SimrailClient extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
const player = await PlayerUtil.getPlayer(data.DispatchedBy[0]?.SteamId.toString())
|
||||
const player = await PlayerUtil.getPlayer(data.DispatchedBy[0]?.SteamId)
|
||||
|
||||
this.emit(SimrailClientEvents.StationLeft, x, player, this.stationsOccupied[data.Prefix]?.joinedAt);
|
||||
delete this.stationsOccupied[data.Prefix];
|
||||
|
Loading…
x
Reference in New Issue
Block a user