Zmiany w wyglądzie list graczy online i spawnów

This commit is contained in:
2020-12-02 20:59:35 +01:00
parent 6ae89c9953
commit 93f2d7e526
7 changed files with 146 additions and 499 deletions
-264
View File
@@ -1,264 +0,0 @@
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';
import axios from 'axios';
import data from '@/data/stations.json';
import Station from '@/scripts/interfaces/Station';
const stationsOnlineURL = 'https://api.td2.info.pl:9640/?method=getStationsOnline';
const trainsOnlineURL = 'https://api.td2.info.pl:9640/?method=getTrainsOnline';
const dispatchersOnlineURL = 'https://api.td2.info.pl:9640/?method=readFromSWDR&value=getDispatcherStatusList%3B1';
enum ConnState {
Loading = 0,
Error = 1,
Connected = 2,
}
interface TimetableResponseData {
stopPoints: {
arrivalTime: string;
arrivalDelay: number;
departureTime: string;
departureDelay: number;
pointNameRAW: string;
}[];
trainInfo: {
timetableId: number;
trainCategoryCode: string;
};
}
interface OnlineStationsResponseData {
stationName: string;
stationHash: string;
maxUsers: number;
currentUsers: number;
spawnString: string;
dispatcherRate: number;
dispatcherName: string;
dispatcherExp: number;
dispatcherId: number;
region: string;
isOnline: number;
}
let onlineDispatchersData: [string, string, number, number][];
let onlineStationsData: OnlineStationsResponseData[];
let onlineTrainsData: {
isOnline: number;
region: string;
trainNo: number;
station: {
stationName: string;
};
}[];
const getStationLabel = (stationStatus: any) => {
if (!stationStatus) return 'NIEZALOGOWANY';
const statusCode = stationStatus[2];
const statusTimestamp = stationStatus[3];
switch (statusCode) {
case 0:
if (statusTimestamp - Date.now() > 21000000) return 'BEZ LIMITU';
return `DO ${new Date(statusTimestamp).toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
})}`;
case 1:
return 'Z/W';
case 2:
if (statusTimestamp == 0) return 'KOŃCZY';
break;
case 3:
return 'BRAK MIEJSCA';
default:
break;
}
return 'NIEDOSTĘPNY';
};
const getOpenSpawns = (spawnString: string) => {
if (!spawnString) return '';
return spawnString.split(';').map(v => (v.split(',')[6] ? v.split(',')[6] : v.split(',')[0]));
};
@Module
export default class StationsModule extends VuexModule {
private trainCount: number = 0;
private stationCount: number = 0;
private stationsConnectionState: ConnState = ConnState.Loading;
private stations: Station[] = [];
get getConnectionState() {
return this.stationsConnectionState;
}
get getOnlineInfo() {
return {
trainCount: this.trainCount,
stationCount: this.stationCount,
};
}
get getStationList() {
return this.stations;
}
@Mutation
private setConnectionState(state: ConnState) {
this.stationsConnectionState = state;
}
@Mutation
private updateStations(updatedStations) {
this.stations = this.stations.reduce((acc, station) => {
const onlineStationData = updatedStations.find(uStation => uStation.stationName === station.stationName);
if (!onlineStationData) {
acc.push({
...station,
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,
dispatcherName: '',
dispatcherRate: 0,
dispatcherExp: -1,
dispatcherId: 0,
occupiedTo: 'WOLNA',
statusTimestamp: 0,
online: false,
});
return acc;
}
acc.push({
...station,
...onlineStationData,
online: true,
});
// updatedStations = updatedStations.filter(
// (updated: any) => updated.stationName !== station.stationName
// );
return acc;
}, [] as Station[]);
// Dodawanie do listy online potencjalnych scenerii niewpisanych do bazy
updatedStations.forEach((updated: any) => {
const alreadyInList: any = this.stations.find(station => station.stationName === updated.stationName);
if (!alreadyInList) {
this.stations.push({
...updated,
online: true,
reqLevel: '-1',
});
}
});
this.stationCount = this.stations.filter(station => station.online).length;
this.trainCount = onlineTrainsData.filter(train => train.isOnline && train.region === 'eu').length;
this.stationsConnectionState = ConnState.Connected;
}
@Mutation
private mutateStations(stations) {
this.stations = stations;
}
@Action({
commit: 'mutateStations',
})
async loadStations() {
return await data.map(stationData => ({
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,
dispatcherName: '',
dispatcherRate: 0,
dispatcherExp: -1,
dispatcherId: 0,
online: false,
occupiedTo: 'WOLNA',
statusTimestamp: 0,
scheduledTrains: [],
...stationData,
}));
}
@Action
async initStations() {
this.context.dispatch('loadStations');
this.context.dispatch('fetchOnlineStations');
}
@Action({
commit: 'updateStations',
})
async fetchOnlineStations() {
return await Promise.all([axios.get(stationsOnlineURL), axios.get(trainsOnlineURL), axios.get(dispatchersOnlineURL)])
.then(async response => {
onlineStationsData = response[0].data.message;
onlineTrainsData = await response[1].data.message;
onlineDispatchersData = await response[2].data.message;
const updatedStations = await Promise.all(
onlineStationsData
.filter(station => station.region === 'eu' && station.isOnline)
.map(async station => {
const stationStatus = onlineDispatchersData.find(status => status[0] == station.stationHash && status[1] == 'eu');
const statusLabel = getStationLabel(stationStatus);
const statusTimestamp = stationStatus ? stationStatus[3] : -1;
const trains = onlineTrainsData.filter(train => train.region === 'eu' && train.isOnline && train.station.stationName === station.stationName);
const stationData = data.find(s => s.stationName === station.stationName) || {
stationName: station.stationName,
stationURL: '',
};
return {
...stationData,
stationHash: station.stationHash,
maxUsers: station.maxUsers,
currentUsers: station.currentUsers,
spawnString: getOpenSpawns(station.spawnString),
dispatcherName: station.dispatcherName,
dispatcherRate: station.dispatcherRate,
dispatcherId: station.dispatcherId,
dispatcherExp: station.dispatcherExp,
occupiedTo: statusLabel,
statusTimestamp,
trains,
};
})
);
return updatedStations;
})
.catch(err => {
this.context.commit('setConnectionState', ConnState.Error);
console.log(err);
});
}
}
-198
View File
@@ -1,198 +0,0 @@
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';
import Train from '@/scripts/interfaces/Train';
import axios from 'axios';
const API_URL = 'https://api.td2.info.pl:9640/?method=getTrainsOnline';
enum ConnState {
Loading = 0,
Error = 1,
Connected = 2,
}
interface TrainData {
driverId: number;
driverName: string;
trainNo: number;
station: { stationName: string };
dataMass: number;
dataLength: number;
dataSpeed: number;
dataDistance: number;
dataSignal: string;
dataCon: string;
dataSceneryConnection: string;
isOnline: boolean;
}
interface TimetableResponseData {
stopPoints: {
pointDistance: number;
pointNameRAW: string;
pointName: string;
pointStopType: string;
arrivalLine: string;
departureLine: string;
arrivalTime: string;
arrivalDelay: number;
departureTime: string;
departureDelay: number;
confirmed: boolean;
stopped: boolean;
pointStopTime: number;
}[];
trainInfo: {
timetableId: number;
trainCategoryCode: string;
route: string;
twr: boolean;
skr: boolean;
sceneries: string[];
};
}
interface TimetableData {
timetableId: number;
trainCategoryCode: string;
route: string;
twr: boolean;
skr: boolean;
sceneries: string[];
routeDistance: number;
stopPoints?: {}[];
}
const getTimestamp = (date: string) => (date ? new Date(date).getTime() : 0);
const getTimetableURL = (trainNo: number) => `https://api.td2.info.pl:9640/?method=readFromSWDR&value=getTimetable%3B${trainNo}%3Beu`;
const getLocoURL = (locoType: string) => `https://rj.td2.info.pl/dist/img/thumbnails/${locoType.includes('EN') ? locoType + 'rb' : locoType}.png`;
@Module
export default class TrainsModule extends VuexModule {
onlineTrainsData: Train[] = [];
onlineTrainsState: ConnState = ConnState.Loading;
@Action({ commit: 'loadTrainsData' })
async fetchTrainsData() {
let trainDataResponse;
try {
trainDataResponse = await axios.get(API_URL);
} catch (error) {
this.context.commit('setConnectionState', ConnState.Error);
return null;
}
let onlineTrainsData: TrainData[] = trainDataResponse.data.message;
return await Promise.all(
onlineTrainsData.map(async train => {
const timetableResponseData: TimetableResponseData | null = (await axios.get(getTimetableURL(train.trainNo))).data.message;
let timetableData: TimetableData | null = null;
if (timetableResponseData && timetableResponseData.trainInfo) {
const routeDistance: number = timetableResponseData.stopPoints[timetableResponseData.stopPoints.length - 1].pointDistance;
timetableData = {
...timetableResponseData.trainInfo,
routeDistance,
stopPoints: timetableResponseData.stopPoints,
};
}
const locoType = train.dataCon.split(';') ? train.dataCon.split(';')[0] : train.dataCon;
const followingStops = timetableResponseData?.stopPoints.reduce(
(acc, point) => {
const stopObj: any = {};
if (!point.pointName.includes('Południowy') && (point.pointName.includes('strong') || point.pointName.includes('podg.'))) {
if (point.pointName.includes('strong')) {
stopObj.stopName = point.pointNameRAW;
stopObj.stopType = point.pointStopType;
} else {
stopObj.stopName = point.pointNameRAW.split(',')[0];
stopObj.stopType = 'podg.';
}
stopObj.arrivalTime = getTimestamp(point.arrivalTime);
stopObj.departureTime = getTimestamp(point.departureTime);
stopObj.arrivalDelay = point.arrivalDelay;
stopObj.departureDelay = point.departureDelay;
stopObj.beginsHere = getTimestamp(point.arrivalTime) == 0 ? true : false;
stopObj.terminatesHere = getTimestamp(point.departureTime) == 0 ? true : false;
stopObj.confirmed = point.confirmed;
stopObj.stopped = point.stopped;
stopObj.currentStationName = train.station.stationName;
acc.push(stopObj);
}
return acc;
},
[] as {
stopName: string;
stopType: string;
arrivalTime: number;
arrivalDelay: number;
departureTime: number;
departureDelay: number;
confirmed: boolean;
stopped: boolean;
stopTime: number;
beginsHere: boolean;
terminatesHere: boolean;
}[]
);
return {
online: train.isOnline,
driverId: train.driverId,
driverName: train.driverName,
trainNo: train.trainNo,
currentStationName: train.station.stationName,
mass: train.dataMass,
length: train.dataLength,
speed: train.dataSpeed,
distance: train.dataDistance,
signal: train.dataSignal,
connectedTrack: train.dataSceneryConnection,
locoType,
locoURL: getLocoURL(locoType),
noTimetable: timetableData == null,
route: timetableData?.route,
timetableId: timetableData?.timetableId,
category: timetableData?.trainCategoryCode,
routeDistance: timetableData?.routeDistance || 0,
followingStops,
TWR: timetableData?.twr,
SKR: timetableData?.skr,
};
})
);
}
@Mutation
private loadTrainsData(data: Train[] | null) {
if (data) {
this.onlineTrainsData = data;
this.onlineTrainsState = ConnState.Connected;
}
}
@Mutation
private setConnectionState(state: ConnState) {
this.onlineTrainsState = state;
}
get trainsDataList() {
return this.onlineTrainsData;
}
get trainsDataState() {
return this.onlineTrainsState;
}
}
+17 -6
View File
@@ -30,6 +30,8 @@ interface TimetableData {
followingSceneries: string[];
}
// const devEnv = true;
const URLs = {
stations: 'https://api.td2.info.pl:9640/?method=getStationsOnline',
trains: 'https://api.td2.info.pl:9640/?method=getTrainsOnline',
@@ -95,7 +97,19 @@ const getStatusTimestamp = (stationStatus: any) => {
return -1;
};
const getOpenSpawns = (spawnString: string) => (spawnString ? spawnString.split(';').map(v => (v.split(',')[6] ? v.split(',')[6] : v.split(',')[0])) : '');
const parseSpawns = (spawnString: string) => {
if (!spawnString) return [];
if (spawnString === 'NO_SPAWN') return [];
return spawnString.split(';').map(spawn => {
const spawnArray = spawn.split(',');
const spawnName = spawnArray[6] ? spawnArray[6] : spawnArray[0];
const spawnLength = parseInt(spawnArray[2]);
return { spawnName, spawnLength };
});
};
const getTimestamp = (date: string) => (date ? new Date(date).getTime() : 0);
const timestampToTime = (timestamp: number) =>
new Date(timestamp).toLocaleTimeString('pl-PL', {
@@ -113,7 +127,6 @@ export default class Store extends VuexModule {
private stationList: Station[] = [];
private trainList: Train[] = [];
private spawnList: { spawnName: string; spawnLength: number }[] = [];
//GETTERS
get getAllData() {
@@ -232,7 +245,6 @@ export default class Store extends VuexModule {
const stationStatus = onlineDispatchersData.find(status => status[0] == station.stationHash && status[1] == 'eu');
const statusLabel = getStatusLabel(stationStatus);
// let statusTimestamp = stationStatus ? stationStatus[3] : -1;
const statusTimestamp = getStatusTimestamp(stationStatus);
const stationTrains = onlineTrainsData.filter(train => train.region === 'eu' && train.isOnline && train.station.stationName === station.stationName);
@@ -242,7 +254,7 @@ export default class Store extends VuexModule {
stationHash: station.stationHash,
maxUsers: station.maxUsers,
currentUsers: station.currentUsers,
spawnString: getOpenSpawns(station.spawnString),
spawns: parseSpawns(station.spawnString),
dispatcherName: station.dispatcherName,
dispatcherRate: station.dispatcherRate,
dispatcherId: station.dispatcherId,
@@ -301,7 +313,6 @@ export default class Store extends VuexModule {
this.stationList = JSONStationData.map(stationData => ({
...stationData,
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,
@@ -315,6 +326,7 @@ export default class Store extends VuexModule {
statusTimestamp: -3,
stationTrains: [],
scheduledTrains: [],
spawns: [],
checkpoints: stationData.subStations ? stationData.subStations.map(sub => ({ checkpointName: sub, scheduledTrains: [] })) : null,
}));
}
@@ -335,7 +347,6 @@ export default class Store extends VuexModule {
acc.push({
...station,
stationProject: '',
spawnString: '',
stationHash: '',
maxUsers: 0,
currentUsers: 0,