Skip to content

Commit

Permalink
🚧 #77 [feat/config] auto change config for a game
Browse files Browse the repository at this point in the history
Signed-off-by: JAGFx <contact@jagfx.fr>
  • Loading branch information
JAGFx committed Oct 20, 2021
1 parent 35b8644 commit 969225c
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 100 deletions.
1 change: 1 addition & 0 deletions server.dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
Expand Down
27 changes: 10 additions & 17 deletions server.dev/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@ const path = require( 'path' );
const fs = require( 'fs' );
const socketio = require( 'socket.io' );

const express = require( 'express' );
const http = require( 'http' );
const bodyParser = require( 'body-parser' );
const dateFilename = path.resolve( process.cwd(), '../src/data/scs_sdk_plugin_parsed_data.json' );
const port = 3000;
const configFilePath = path.resolve( process.cwd(), '../src/data/ets2-dashboard-skin.config.json' );
const interval = () => {
const config = JSON.parse( fs.readFileSync( configFilePath, 'UTF-8' ) );
const rateFound = config.hasOwnProperty( 'general_refresh_rate' );

return (rateFound)
? Math.min( parseInt( config.general_refresh_rate ), 100 )
: 100;
}; // Milisecond

const express = require( 'express' );
const http = require( 'http' );
const bodyParser = require( 'body-parser' );
const { telemetryInterval } = require( './services/telemetry.service' );
const { initConfig } = require( './services/config.service' );
const dateFilename = path.resolve( process.cwd(), '../src/data/scs_sdk_plugin_parsed_data.json' );
const port = 3000;
// ---

let app = express();
Expand All @@ -40,13 +32,14 @@ app.use( bodyParser.json() );
app.use( '/maps', express.static( path.resolve( __dirname, '../../maps' ) ) );

// ---
initConfig( app, __dirname );

io.on( 'connection', socket => {
const data = fs.readFileSync( dateFilename );
console.log( 'Connection: ' + socket.id );
setInterval( () => {
socket.emit( 'update', JSON.parse( data.toString() ))
}, interval() );
}, telemetryInterval() );
} );

server.listen( port, () => {
Expand All @@ -56,5 +49,5 @@ server.listen( port, () => {
port: port
};

console.log( 'server.listen', data, `Euro Truck Simulator 2 dashboard is running at http://${ url }/` );
console.log( 'server.listen', data, `Euro Truck Simulator 2 dashboard is running at http://${ url }:${port}/` );
} );
1 change: 1 addition & 0 deletions server.dev/src/services
1 change: 1 addition & 0 deletions server.dev/src/utils
50 changes: 17 additions & 33 deletions server/src/helpers/server.helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
* Time: 15:56
*/

import bodyParser from 'body-parser';
import express from 'express';
import fs from 'fs';
import http from 'http';
import path from 'path';
import socketio from 'socket.io';
import truckSimTelemetry from 'trucksim-telemetry';
import { logIt } from './utils.helpers';
import bodyParser from 'body-parser';
import express from 'express';
import fs from 'fs';
import http from 'http';
import path from 'path';
import socketio from 'socket.io';
import truckSimTelemetry from 'trucksim-telemetry';
import { initConfig } from '../services/config.service.js';
import { telemetryInterval, telemetryWatch } from '../services/telemetry.service.js';
import { config, gameConfig } from '../utils/config.util.js';
import { logIt } from './utils.helpers';

let app, server, io, telemetry, port, config, interval, pathDist, pathMap;
const configFilePath = path.resolve( process.cwd(), './config.ets2-dashboard-skin.json' );
let app, server, io, telemetry, port, pathDist, pathMap;
//const configFilePath = path.resolve( process.cwd(), './config.ets2-dashboard-skin.json' );

const init = () => {
app = express();
Expand All @@ -29,14 +32,7 @@ const init = () => {
} );
telemetry = truckSimTelemetry();
port = 3000;
interval = () => {
const config = JSON.parse( fs.readFileSync( configFilePath, 'UTF-8' ) );
const rateFound = config.hasOwnProperty( 'general_refresh_rate' );

return (rateFound)
? Math.min( parseInt( config.general_refresh_rate ), 100 )
: 100;
};

pathDist = path.resolve( __dirname, '../../../dist' );
pathMap = path.resolve( process.cwd(), './maps' );

Expand All @@ -46,21 +42,9 @@ const init = () => {
if ( fs.existsSync( pathMap ) )
app.use( '/maps', express.static( pathMap ) );

app.post( '/config', ( req, res ) => {
fs.writeFileSync( configFilePath, JSON.stringify( req.body, null, 2 ) );

res.send( req.body );
} );

app.get( '/config', ( req, res ) => {
const file = fs.readFileSync( configFilePath, 'UTF-8' );

res.send( file );
} );
initConfig(app);

telemetry.watch( { interval: interval() }, data => {
io.emit( 'update', data );
} );
telemetryWatch( io, telemetry );

io.on( 'connection', socket => {
socket.emit( 'update', telemetry.data );
Expand All @@ -73,7 +57,7 @@ const init = () => {
port: port
};

logIt( 'server.listen', data, `Euro Truck Simulator 2 dashboard is running at http://${ url }/` );
logIt( 'server.listen', data, `Euro Truck Simulator 2 dashboard is running at http://${ url }:${port}/` );
} );
};

Expand Down
31 changes: 31 additions & 0 deletions server/src/services/config.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author: Emmanuel SMITH <hey@emmanuel-smith.me>
* project: ets2-dashboard-skin
* file: config.service.js
* Date: 18/10/2021
* Time: 19:48
*/

import { saveAppConfig, saveGameConfig, config } from '../utils/config.util';

const postConfigRequest = ( req, res, basePath = null ) => {
saveAppConfig( req.body.app, basePath );
saveGameConfig( req.params.target, req.body.game, basePath );

config = {
app: req.body.app,
game: req.body.game
};
res.send( req.body );
}

const getConfigRequest = ( req, res ) => {
res.send( config );
}

export const initConfig = (app, basePath = null) => {
app.post( '/config/:target', ( req, res) => {
postConfigRequest( req, res, basePath )
} );
app.get( '/config', getConfigRequest );
}
24 changes: 24 additions & 0 deletions server/src/services/telemetry.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @author: Emmanuel SMITH <hey@emmanuel-smith.me>
* project: ets2-dashboard-skin
* file: telemetry.service.js
* Date: 18/10/2021
* Time: 20:09
*/

import { config, gameConfig } from '../utils/config.util';

export const telemetryInterval = () => {
return (config.app.hasOwnProperty( 'general_refresh_rate' ))
? Math.min( parseInt( config.app.general_refresh_rate ), 100 )
: 100;
};

export const telemetryWatch = (io, telemetry) => {
telemetry.watch( { interval: telemetryInterval }, data => {
if ( config.game === null && data.game.game.name !== undefined )
config.game = gameConfig( data.game.game.name );

io.emit( 'update', data );
} );
}
45 changes: 45 additions & 0 deletions server/src/utils/config.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @author: Emmanuel SMITH <hey@emmanuel-smith.me>
* project: ets2-dashboard-skin
* file: _config.js
* Date: 14/10/2021
* Time: 19:24
*/

import { existsSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';

const oldConfigPath = (basePath = null) => path.resolve( basePath ?? process.cwd(), './config.ets2-dashboard-skin.json' );

export const appConfigPath = (basePath = null) => {
const newAppConfigPath = path.resolve( basePath ?? process.cwd(), `./config.json` );

return ( existsSync( newAppConfigPath ) )
? newAppConfigPath
: oldConfigPath(basePath)
}

export const gameConfigPath = (target, basePath = null) => {
const newGameConfigPath = path.resolve( basePath ?? process.cwd(), `./config.${ target }.json` );

return ( existsSync( newGameConfigPath ) )
? newGameConfigPath
: oldConfigPath(basePath)
}

export const appConfig = (basePath = null) => readFileSync( appConfigPath(basePath), 'UTF-8' );

export const gameConfig = (target, basePath = null) => readFileSync( gameConfigPath(target, basePath), 'UTF-8' );

export const saveAppConfig = (data, basePath = null) => {
writeFileSync( appConfigPath(basePath), JSON.stringify( data, null, 2 ) );
}

export const saveGameConfig = (target, data, basePath = null) => {
writeFileSync( gameConfigPath(target, basePath), JSON.stringify( data, null, 2 ) );
}

export let config = {
app: appConfig(),
game: null
}
3 changes: 2 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export default {
...mapGetters( {
menuIsDisplayed: 'menu/isDisplayed',
currentSkin: 'skins/current',
getConfig: 'config/get'
getConfig: 'config/get',
configExist: 'config/exist'
} )
},
mounted() {
Expand Down
8 changes: 4 additions & 4 deletions src/data/config-field-values.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@
}
],
"maps_map_tilesRemotePath" : [
{
"label" : "ets2.jagfx.fr",
"value" : "https://ets2.jagfx.fr"
},
{
"label" : "TwinDragon/SCS_Map_Tiles",
"value" : "https://raw.githubusercontent.com/TwinDragon/SCS_Map_Tiles/master/"
},
{
"label" : "ets2.jagfx.fr",
"value" : "https://ets2.jagfx.fr"
}
],
"maps_map_navigationRemaining" : [
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
{
"general_region" : "eu",
"general_refresh_rate" : 100,
"general_skin_on_load" : "JAGFx",
"general_unit_auto" : false,
"general_h-shift-layout" : "h-shifter",
"unit_weight" : "kg",
"unit_length" : "m",
"unit_degrees" : "C",
Expand All @@ -12,7 +7,6 @@
"unit_pressure" : "psi",
"unit_speed" : "kph",
"unit_currency" : "EUR",
"general_job_remaining" : "due_date",
"jagfx_elements_right_oilTemperature" : "true",
"jagfx_elements_right_brakesTemperature" : "true",
"jagfx_elements_right_brakesAirPressure" : "true",
Expand Down Expand Up @@ -42,32 +36,31 @@
"events_truck.cruise-control-increase" : true,
"events_truck.cruise-control" : true,
"events_trailers.damage" : true,
"events_trailers.coupling" : true,
"events_navigation.speed-limit" : true,
"events_job.started" : true,
"events_job.finished" : true,
"events_job.delivered" : true,
"events_job.cancelled" : true,
"events_game.refuel-payed" : true,
"events_game.train" : true,
"events_game.ferry" : true,
"events_game.tollgate" : true,
"events_game.fine" : true,
"events_game.pause" : true,
"maps_elements_eta" : true,
"maps_elements_speedAndGear" : true,
"maps_elements_mapControls" : true,
"maps_elements_speedLimit" : true,
"maps_elements_rotateWithPlayer" : true,
"maps_map_type" : "vanilla",
"maps_map_tilesRemotePath" : "https://ets2.jagfx.fr",
"maps_map_activeMap" : null,
"maps_map_navigationRemaining" : "remaining_date",
"maps_map_tilesVersion" : "latest",
"general_damage_accurate" : "damage-diagnostic",
"jagfx_elements_right_map" : false,
"maps_map_tilesRemoteUseCustom": false,
"maps_map_tilesRemoteCustomPath": null,
"maps_general_debug": false,
"general_skin_locale": "en-EN"
"events_trailers.coupling" : true,
"events_navigation.speed-limit" : true,
"events_job.started" : true,
"events_job.finished" : true,
"events_job.delivered" : true,
"events_job.cancelled" : true,
"events_game.refuel-payed" : true,
"events_game.train" : true,
"events_game.ferry" : true,
"events_game.tollgate" : true,
"events_game.fine" : true,
"events_game.pause" : true,
"maps_elements_eta" : true,
"maps_elements_speedAndGear" : true,
"maps_elements_mapControls" : true,
"maps_elements_speedLimit" : true,
"maps_elements_rotateWithPlayer" : true,
"maps_map_type" : "vanilla",
"maps_map_tilesRemotePath" : "https://raw.githubusercontent.com/TwinDragon/SCS_Map_Tiles/master/",
"maps_map_activeMap" : null,
"maps_map_navigationRemaining" : "remaining_date",
"maps_map_tilesVersion" : "latest",
"general_damage_accurate" : "damage-diagnostic",
"jagfx_elements_right_map" : false,
"maps_map_tilesRemoteUseCustom" : false,
"maps_map_tilesRemoteCustomPath" : null,
"maps_general_debug" : false
}
9 changes: 9 additions & 0 deletions src/data/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"general_region" : "eu",
"general_refresh_rate" : 100,
"general_skin_on_load" : "JAGFx",
"general_unit_auto" : false,
"general_h-shift-layout" : "h-shifter",
"general_skin_locale" : "en-EN",
"general_job_remaining" : "due_date"
}
7 changes: 4 additions & 3 deletions src/mixins/TelemetryMixin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export default {
},
computed: {
telemetry: () => store.telemetry,
gameConnected: () => app.useFakeData || (store.receivedData
&& store.telemetry.game.sdkActive
&& (store.telemetry.truck.brand.id.length !== 0)),
gameConnected: () => (app.useFakeData
|| (store.receivedData && store.telemetry.game.sdkActive
&& (store.telemetry.truck.brand.id.length !== 0)))
&& this.config('unit_weight'),
receivedData: () => store.receivedData,
jobDeliveryTime() {
return (this.telemetry.job.market.id === 'external_contracts')
Expand Down
8 changes: 4 additions & 4 deletions src/store/modules/_config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defaultData from '@/data/ets2-dashboard-skin.config.json';
import { config } from '@/utils/utils';
import Vue from 'vue';
import { emptyData } from '@/utils/_config';
import { config } from '@/utils/utils';
import Vue from 'vue';

// initial state
const state = () => (defaultData);
const state = () => (emptyData());

// getters
const getters = {
Expand Down
Loading

0 comments on commit 969225c

Please sign in to comment.