DEV: zmiana w pobieraniu danych z API

This commit is contained in:
2021-12-30 00:57:22 +01:00
parent cb306d4be5
commit 967356a02a
8 changed files with 114 additions and 45 deletions
+3 -2
View File
@@ -102,6 +102,7 @@ import { StoreData } from './scripts/interfaces/StoreData';
import { useStore } from './store';
import packageInfo from '.././package.json';
import axios from 'axios';
export default defineComponent({
components: {
@@ -149,7 +150,7 @@ export default defineComponent({
this.loadLang();
},
mounted() {
async mounted() {
if (StorageManager.getStringValue('version') != this.VERSION) {
StorageManager.setStringValue('version', this.VERSION);
@@ -158,7 +159,7 @@ export default defineComponent({
this.updateModalVisible = this.hasReleaseNotes && !StorageManager.getBooleanValue('version_notes_read');
this.updateToNewestVersion();
this.updateToNewestVersion();
},
methods: {
+1 -1
View File
@@ -99,7 +99,7 @@
/>
<img
v-if="Number(stationInfo.reqLevel) < 0"
v-if="stationInfo.reqLevel < 0"
:src="unknownIcon"
alt="icon-unknown"
:title="$t('desc.unknown')"
+4 -6
View File
@@ -52,11 +52,9 @@
</td>
<td class="station_level">
<span v-if="station.reqLevel" :style="calculateExpStyle(station.reqLevel, station.supportersOnly)">
{{ Number(station.reqLevel) > -1 ? (Number(station.reqLevel) >= 2 ? station.reqLevel : 'L') : '?' }}
<span :style="calculateExpStyle(station.reqLevel, station.supportersOnly)">
{{ station.reqLevel > -1 ? (station.reqLevel >= 2 ? station.reqLevel : 'L') : '?' }}
</span>
<span v-else>?</span>
</td>
<td class="station_status">
@@ -140,7 +138,7 @@
/>
<img
v-if="station.nonPublic && Number(station.reqLevel) > -1"
v-if="station.nonPublic && station.reqLevel > -1"
:src="lockIcon"
alt="non-public"
:title="$t('desc.non-public')"
@@ -154,7 +152,7 @@
/>
<img
v-if="Number(station.reqLevel) < 0"
v-if="station.reqLevel < 0"
:src="unknownIcon"
alt="icon-unknown"
:title="$t('desc.unknown')"
+1 -1
View File
@@ -2,7 +2,7 @@ import { defineComponent } from 'vue';
export default defineComponent({
methods: {
calculateExpStyle(exp: string | number, isSupporter = false): string {
calculateExpStyle(exp: number, isSupporter = false): string {
const bgColor = exp > -1 ? (exp < 2 ? '#26B0D9' : `hsl(${-exp * 5 + 100}, 85%, 50%)`) : '#666';
const fontColor = exp > 15 || exp == -1 ? 'white' : 'black';
+1 -1
View File
@@ -19,7 +19,7 @@ export default interface Station {
stationLines: string;
stationProject: string;
reqLevel: string;
reqLevel: number;
supportersOnly: boolean;
SBL: string;
+5 -8
View File
@@ -5,11 +5,8 @@ import StorageManager from './storageManager';
const sortStations = (a: Station, b: Station, sorter: { index: number; dir: number }) => {
switch (sorter.index) {
case 1:
const aLevel = a.reqLevel == "" ? -1 : parseInt(a.reqLevel);
const bLevel = b.reqLevel == "" ? -1 : parseInt(b.reqLevel);
if (aLevel > bLevel) return sorter.dir;
if (aLevel < bLevel) return -sorter.dir;
if (a.reqLevel > b.reqLevel) return sorter.dir;
if (a.reqLevel < b.reqLevel) return -sorter.dir;
break;
case 2:
@@ -85,9 +82,9 @@ const filterStations = (station: Station, filters: Filter) => {
if (filters['real'] && station.stationLines != '') return returnMode;
if (filters['fictional'] && station.stationLines == '') return returnMode;
if (station.reqLevel == '-1') return true;
if (parseInt(station.reqLevel) < filters['minLevel']) return returnMode;
if (parseInt(station.reqLevel) > filters['maxLevel']) return returnMode;
if (station.reqLevel == -1) return true;
if (station.reqLevel < filters['minLevel']) return returnMode;
if (station.reqLevel > filters['maxLevel']) return returnMode;
if (filters['no-1track'] && (station.routes.oneWay.catenary != 0 || station.routes.oneWay.noCatenary != 0)) return returnMode;
if (filters['no-2track'] && (station.routes.twoWay.catenary != 0 || station.routes.twoWay.noCatenary != 0)) return returnMode;
+1
View File
@@ -1,5 +1,6 @@
export const URLs = {
sceneryData: "https://spythere.github.io/api/stationData.json",
sceneryDataDev: "http://127.0.0.1:8000/data",
stations: "https://api.td2.info.pl:9640/?method=getStationsOnline",
trains: "https://api.td2.info.pl:9640/?method=getTrainsOnline",
dispatchers: "https://api.td2.info.pl:9640/?method=readFromSWDR&value=getDispatcherStatusList%3B1",
+98 -26
View File
@@ -42,6 +42,28 @@ export interface State {
listenerLaunched: boolean;
}
interface SceneryData {
id: number;
name: string;
url: string;
project_lines: string;
project_name: string;
req_level?: number;
supporters_only: boolean;
signal_type: string;
control_type: string;
sbl_routes: string;
twb_routes: string;
track_oneway_e: number;
track_oneway_ne: number;
track_twoway_e: number;
track_twoway_ne: number;
checkpoints?: string;
is_default: boolean;
is_nonpublic: boolean;
is_unavailable: boolean;
}
export const key: InjectionKey<Store<State>> = Symbol()
export const store = createStore<State>({
@@ -107,8 +129,14 @@ export const store = createStore<State>({
}
const sceneryData = await (await axios.get(sceneryDataQuery)).data;
// const sceneryData = await (await axios.get(sceneryDataQuery)).data;
commit(MUTATIONS.SET_SCENERY_DATA, sceneryData);
const sceneryData2 = await (await axios.get('http://127.0.0.1:8000/data')).data;
console.log(sceneryData2);
commit(MUTATIONS.SET_SCENERY_DATA, sceneryData2);
commit(MUTATIONS.SET_SCENERY_DATA_STATUS, DataStatus.Loaded);
dispatch(ACTIONS.fetchOnlineData);
@@ -122,8 +150,6 @@ export const store = createStore<State>({
async fetchOnlineData({ commit, dispatch }) {
commit(MUTATIONS.SET_DATA_CONNECTION_STATUS, DataStatus.Loading);
Promise.all([axios.get(URLs.stations), axios.get(URLs.trains), axios.get(URLs.dispatchers)])
.then(async response => {
const onlineStationsData: StationAPIData[] = response[0].data.message;
@@ -291,35 +317,37 @@ export const store = createStore<State>({
},
mutations: {
SET_SCENERY_DATA(state, data: any[][]) {
state.sceneryData = [...data];
SET_SCENERY_DATA(state, data: SceneryData[]) {
// state.sceneryData = [...data];
console.log('Data:', data);
state.stationList = data.map(station => ({
stationName: station[0] as string,
stationURL: station[1] as string,
stationLines: station[2] as string,
stationProject: station[3] as string,
reqLevel: station[4] as string,
supportersOnly: station[5] == "TAK",
signalType: station[6] as string,
controlType: station[7] as string,
SBL: station[8] as string,
TWB: station[9] as string,
state.stationList = data.map(scenery => ({
stationName: scenery.name,
stationURL: scenery.url,
stationLines: scenery.project_lines,
stationProject: scenery.project_name,
reqLevel: scenery.req_level === undefined ? -1 : scenery.req_level,
supportersOnly: scenery.supporters_only,
signalType: scenery.signal_type,
controlType: scenery.control_type,
SBL: scenery.sbl_routes,
TWB: scenery.twb_routes,
routes: {
oneWay: {
catenary: station[10] as number,
noCatenary: station[11] as number
catenary: scenery.track_oneway_e,
noCatenary: scenery.track_oneway_ne
},
twoWay: {
catenary: station[12] as number,
noCatenary: station[13] as number
catenary: scenery.track_twoway_e,
noCatenary: scenery.track_twoway_ne
}
},
checkpoints: station[14] ? (station[14] as string).split(";").map(sub => ({ checkpointName: sub, scheduledTrains: [] })) : [],
checkpoints: scenery.checkpoints ? scenery.checkpoints.split(";").map(sub => ({ checkpointName: sub, scheduledTrains: [] })) : [],
default: station[15] as boolean,
nonPublic: station[16] as boolean,
unavailable: station[17] as boolean,
default: scenery.is_default,
nonPublic: scenery.is_nonpublic,
unavailable: scenery.is_unavailable,
stationHash: "",
maxUsers: 0,
@@ -338,6 +366,50 @@ export const store = createStore<State>({
spawns: []
}));
// state.stationList = data.map(station => ({
// stationName: station[0] as string,
// stationURL: station[1] as string,
// stationLines: station[2] as string,
// stationProject: station[3] as string,
// reqLevel: station[4] as string,
// supportersOnly: station[5] == "TAK",
// signalType: station[6] as string,
// controlType: station[7] as string,
// SBL: station[8] as string,
// TWB: station[9] as string,
// routes: {
// oneWay: {
// catenary: station[10] as number,
// noCatenary: station[11] as number
// },
// twoWay: {
// catenary: station[12] as number,
// noCatenary: station[13] as number
// }
// },
// checkpoints: station[14] ? (station[14] as string).split(";").map(sub => ({ checkpointName: sub, scheduledTrains: [] })) : [],
// default: station[15] as boolean,
// nonPublic: station[16] as boolean,
// unavailable: station[17] as boolean,
// stationHash: "",
// maxUsers: 0,
// currentUsers: 0,
// dispatcherName: "",
// dispatcherRate: 0,
// dispatcherExp: -1,
// dispatcherId: 0,
// dispatcherIsSupporter: false,
// online: false,
// statusTimestamp: -3,
// statusID: "free",
// statusTimeString: "",
// stationTrains: [],
// scheduledTrains: [],
// spawns: []
// }));
},
@@ -361,7 +433,7 @@ export const store = createStore<State>({
state.stationList = state.stationList.reduce((acc: Station[], station) => {
const onlineStationData = updatedStationList.find(updatedStation => updatedStation.stationName === station.stationName);
const listedStationData = state.sceneryData.find(data => data[0] === station.stationName);
const listedStationData = state.stationList.find(data => data.stationName === station.stationName);
if (onlineStationData)
acc.push({
@@ -400,7 +472,7 @@ export const store = createStore<State>({
stationTrains: uStation.stationTrains || [],
subStations: [],
online: true,
reqLevel: "-1",
reqLevel: -1,
nonPublic: true
});
});