forked from simrail/simrail.pro
fix(backend): fix redis
This commit is contained in:
parent
e87c9982d8
commit
6b5df79088
@ -13,7 +13,7 @@ export class StationsModule {
|
||||
const stats = await SteamUtil.getPlayerStats(player.steamid);
|
||||
const date = new Date();
|
||||
if (stats) {
|
||||
const time = (date.getTime() - joinedAt) ?? 0;
|
||||
const time = joinedAt ? (date.getTime() - joinedAt) : 0;
|
||||
|
||||
const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4(), steamName: player.personaname });
|
||||
if (!userProfile.dispatcherStats) userProfile.dispatcherStats = {};
|
||||
|
@ -12,7 +12,7 @@ export class TrainsModule {
|
||||
|
||||
client.on(SimrailClientEvents.TrainLeft, async (server: Server, train: Train, player: IPlayer, joinedAt: number, leftAt: number, points: number, distance: number, vehicle: string) => {
|
||||
if (distance) {
|
||||
const time = (leftAt - joinedAt) ?? 0;
|
||||
const time = joinedAt ? (leftAt - joinedAt) : 0;
|
||||
const userProfile = await MProfile.findOne({ steam: player.steamid }) ?? await MProfile.create({ steam: player.steamid, id: v4(), steamName: player.personaname });
|
||||
|
||||
const vehicleName = getVehicle(vehicle) ?? vehicle;
|
||||
|
@ -50,25 +50,41 @@ export class SimrailClient extends EventEmitter {
|
||||
|
||||
public getStation(server: Server['ServerCode'], name: string) {
|
||||
if (!this.stationsOccupied[server] || !this.stationsOccupied[server][name]) return null;
|
||||
const player = PlayerUtil.getPlayer(this.stationsOccupied[server][name].SteamId);
|
||||
const player = PlayerUtil.getPlayer(this.stationsOccupied[server][name]?.SteamId ?? "");
|
||||
return { player, joinedAt: this.stationsOccupied[name].joinedAt };
|
||||
}
|
||||
|
||||
public getTrain(server: Server['ServerCode'], name: string) {
|
||||
if (!this.trainsOccupied[server] || !this.trainsOccupied[server][name]) return null;
|
||||
const player = PlayerUtil.getPlayer(this.trainsOccupied[server][name].SteamId);
|
||||
return { player, joinedAt: this.trainsOccupied[server][name].JoinedAt, startPlayerDistance: this.trainsOccupied[server][name].StartPlayerDistance };
|
||||
const player = PlayerUtil.getPlayer(this.trainsOccupied[server][name]?.SteamId ?? "");
|
||||
return { player, joinedAt: this.trainsOccupied[server][name]?.JoinedAt, startPlayerDistance: this.trainsOccupied[server][name]?.StartPlayerDistance };
|
||||
}
|
||||
|
||||
|
||||
private async setup() {
|
||||
console.log(Date.now() - (Number(await redis.get('last_updated'))));
|
||||
if (!await redis.get('last_updated')) {
|
||||
await redis.json.set('trains_occupied', '$', {});
|
||||
await redis.json.set('trains', '$', []);
|
||||
await redis.json.set('stations', '$', []);
|
||||
await redis.json.set('stations_occupied', '$', {});
|
||||
}
|
||||
|
||||
const lastUpdated = Date.now() - (Number(await redis.get('last_updated')) ?? 0);
|
||||
|
||||
if (lastUpdated > 300_000) {
|
||||
console.log('REDIS: last updated more than > 5 mins');
|
||||
await redis.json.set('trains_occupied', '$', {});
|
||||
await redis.json.set('trains', '$', []);
|
||||
await redis.json.set('stations', '$', []);
|
||||
await redis.json.set('stations_occupied', '$', {});
|
||||
}
|
||||
|
||||
if (!await redis.json.get('stations'))
|
||||
redis.json.set('stations', '$', []);
|
||||
if (!await redis.json.get('trains'))
|
||||
redis.json.set('trains', '$', []);
|
||||
if (!await redis.json.get('trains_occupied'))
|
||||
redis.json.set('trains_occupied', '$', {});
|
||||
|
||||
if (!await redis.json.get('stations_occupied'))
|
||||
redis.json.set('stations_occupied', '$', {});
|
||||
|
||||
@ -76,6 +92,8 @@ export class SimrailClient extends EventEmitter {
|
||||
this.stationsOccupied = (await redis.json.get('stations_occupied') as unknown as SimrailClient['stationsOccupied']);
|
||||
this.trains = (await redis.json.get('trains') as unknown as SimrailClient['trains']);
|
||||
this.trainsOccupied = (await redis.json.get('trains_occupied') as unknown as SimrailClient['trainsOccupied']);
|
||||
|
||||
redis.set('last_updated', Date.now().toString());
|
||||
}
|
||||
|
||||
private async processStation(server: Server, stations: ApiResponse<Station>) {
|
||||
@ -86,7 +104,7 @@ export class SimrailClient extends EventEmitter {
|
||||
if (!this.stations[server.ServerCode].length) {
|
||||
this.stations[server.ServerCode] = stations.data;
|
||||
redis.json.set('stations', '$', this.stations);
|
||||
|
||||
redis.set('last_updated', Date.now().toString());
|
||||
}
|
||||
|
||||
stations.data.forEach(async (x) => {
|
||||
@ -116,9 +134,9 @@ export class SimrailClient extends EventEmitter {
|
||||
}
|
||||
})
|
||||
redis.json.set('stations_occupied', '$', this.stationsOccupied);
|
||||
|
||||
this.stations[server.ServerCode] = stations.data;
|
||||
redis.json.set('stations', '$', this.stations);
|
||||
redis.set('last_updated', Date.now().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,6 +149,7 @@ export class SimrailClient extends EventEmitter {
|
||||
this.trains[server.ServerCode] = trains.data;
|
||||
|
||||
redis.json.set('trains', '$', this.trains);
|
||||
redis.set('last_updated', Date.now().toString());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -194,6 +213,7 @@ export class SimrailClient extends EventEmitter {
|
||||
this.trains[server.ServerCode] = trains.data;
|
||||
redis.json.set('trains', '$', this.trains);
|
||||
redis.json.set('trains_occupied', '$', this.trainsOccupied);
|
||||
redis.set('last_updated', Date.now().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -211,9 +231,6 @@ export class SimrailClient extends EventEmitter {
|
||||
|
||||
this.processStation(server, stations);
|
||||
this.processTrain(server, trains);
|
||||
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user