Skip to content

Commit

Permalink
🐛 [feat/config] #83 correct lib to give config game
Browse files Browse the repository at this point in the history
Signed-off-by: JAGFx <contact@jagfx.fr>
  • Loading branch information
JAGFx committed Dec 16, 2021
1 parent a5df28a commit 530bce6
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 89 deletions.
1 change: 1 addition & 0 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "MIT",
"type": "module",
"dependencies": {
"cors": "^2.8.5",
"data-store": "^4.0.3",
"socket.io": "^4.4.0"
}
Expand Down
28 changes: 18 additions & 10 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import bodyParser from 'body-parser';
import fs from 'fs';
import path from 'path';
import { saveAppConfig, saveGameConfig } from './config.js';
import cors from 'cors';
import { gameConfig, saveAppConfig, saveGameConfig } from './config.js';
import store, { app, isDevelopment, server, socket } from './store.js';
import { telemetryInterval } from './telemetry.js';

Expand All @@ -22,27 +23,23 @@ const postConfigRequest = ( req, res, basePath = null ) => {
app: req.body.app ,
game: req.body.game
} )
res.header("Access-Control-Allow-Origin", "*");
res.send( req.body );
}

const getConfigRequest = ( req, res ) => {
//console.log( store.get('config') );
res.header("Access-Control-Allow-Origin", "*");
res.send( store.get('config') );
}

export const initApp = () => {
app.use( bodyParser.json() );
app.use(cors())
}

export const initConfig = ( basePath = null) => {
app.post( '/config/:target', ( req, res) => {
postConfigRequest( req, res, basePath )
} );
app.get( '/config', (req, res) => {
getConfigRequest(req, res);
} );
app.get( '/config', getConfigRequest);
const config = store.get('config.app');
const port = (config.hasOwnProperty('general_port'))
? parseInt(config.general_port)
Expand All @@ -56,10 +53,21 @@ export const initSocket = () => {
console.log( 'Connection: ' + socket.id );

if( isDevelopment() ){
const dataFileName = path.resolve( process.cwd(), '../src/data/scs_sdk_plugin_parsed_data.json' );
const data = fs.readFileSync( dataFileName );
const dataFileName = path.resolve( process.cwd(), '../../src/data/scs_sdk_plugin_parsed_data.json' );
const data = JSON.parse(fs.readFileSync( dataFileName ).toString());
let lastGameName = null;

setInterval( () => {
socket.emit( 'update', JSON.parse( data.toString() ))
socket.emit( 'update', data)
const currentGameName = data.game.game.name;

if( currentGameName !== lastGameName ){
const config = JSON.parse( gameConfig( currentGameName ) );
store.set( 'config.game', config);
}

lastGameName = currentGameName;
//}, 1000 );
}, telemetryInterval() );
}
} );
Expand Down
11 changes: 3 additions & 8 deletions lib/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@
* Time: 21:45
*/

import { gameConfig } from './config.js';
import store from './store.js';

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

export const telemetryWatch = (io, telemetry) => {
const config = store.get('config')
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 );
} );
}
3 changes: 2 additions & 1 deletion servers/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@


import { initApp, initConfig, initServer, initSocket } from 'ets2-dashboard-lib/server.js';
import path from 'path';
import path from 'path';

const dirname = path.resolve();

process.env.NODE_ENV = 'development';
initApp();
initConfig( dirname );
initSocket();
Expand Down
1 change: 0 additions & 1 deletion servers/dev/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion servers/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"http": "0.0.1-security",
"socket.io": "^4.1.3"
Expand Down
4 changes: 4 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default {
.then( setLocale )
.then( startTelemetryConnection )
.then( loadGameConfig )
.then( () => {
this.$store.commit('app/setLaunched', true)
this.$store.dispatch('app/endProcessing');
} )
.catch( e => {
this.$store.dispatch( 'app/setError', {
message: {
Expand Down
31 changes: 3 additions & 28 deletions src/plugins/app.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export default {
}
}
};
const updateTelemetry = payload => {

Vue.prototype.$updateTelemetry = payload => {
const data = { ...payload };
Object.freeze( data );
const gameConnected = data.game !== null &&
Expand All @@ -47,32 +47,7 @@ export default {

if ( gameConnected )
mutations.setReceivedData( true );
}

Vue.prototype.$updateTelemetry = updateTelemetry;

//if ( !app.useFakeData ) {
// const telemetrySocket = io( 'http://' + window.location.hostname + ':3000' );
// telemetrySocket.on( 'connect', () => {
// store.dispatch( 'app/showMessage', {
// icon: '<i class="fas fa-truck"></i>',
// title: 'Connected to telemetry server',
// message: 'Ready to delivering'
// } );
//
// setTimeout( () => {
// store.dispatch( 'app/showMessage', {
// icon: '<i class="fas fa-truck"></i>',
// title: 'Waiting game connection',
// message: 'Run the game to start your job !'
// } );
// }, 5000 );
// } );
// telemetrySocket.on( 'update', updateTelemetry);
// telemetrySocket.on( 'log', data => {
// Vue.prototype.$updateEvent( data );
// } );
//}
};

// ---

Expand Down
16 changes: 10 additions & 6 deletions src/utils/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export const emptyData = (withGame = false, gameId) => {
let gameConfig = null;

if( withGame ){
gameConfig = ( gameId === 'ets2' ) ? defaultEts2Config : defaultAtsConfig;
gameConfig = ( gameId === 'ets2' )
? defaultEts2Config
: defaultAtsConfig;
}

return {
Expand Down Expand Up @@ -80,7 +82,7 @@ export const save = async (data, target, gameId) => {
code: 'CONFIG_SAVE_FAILED'
}
} );
return error;
throw error;
} );
};

Expand Down Expand Up @@ -127,19 +129,21 @@ export const load = (target, gameId) => {
code: 'CONFIG_LOAD_FAILED'
}
} );
return emptyData()[ target ];
throw error;
} );
};

export const loadGameConfig = () => {
const gameName = telemetryStore.telemetry.game.game.name;

if ( process.env.VUE_APP_USE_FAKE_DATA === 'true' )
return new Promise( resolve => {
setTimeout( () => {
resolve( emptyData(true, telemetryStore.telemetry.game.game.name).game );
resolve( emptyData(true, gameName).game );
}, 1000 );
} );

return axios.get( basePathHost() + '/config' )
return axios.get( basePathHost() + `/config` )
.then( response => {
return response.data.game;
}, error => {
Expand All @@ -154,7 +158,7 @@ export const loadGameConfig = () => {
code: 'CONFIG_LOAD_FAILED'
}
} );
return emptyData(true).game;
throw error;
} );
}

Expand Down
70 changes: 36 additions & 34 deletions src/utils/_splashScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import testData from '@/data/scs_sdk_plugin_parsed_data.json';
import store from '@/store';
import { pushLog } from '@/utils/_app';
import { basePathHost, pushLog } from '@/utils/_app';
import { changeLocale, fallbackLocale } from '@/utils/_i18n';
import { app, history, config } from '@/utils/utils';
import { io } from 'socket.io-client';
Expand Down Expand Up @@ -90,44 +90,46 @@ export const loadGameConfig = () => {
.then( data => {
pushLog( 'Game config loaded', history.HTY_ZONE.MAIN );
store.commit( 'config/setGame', data )
store.dispatch('app/endProcessing');
store.commit('app/setLaunched', true)
} );
};

export const startTelemetryConnection = () => {
store.dispatch('app/showMessage', {
icon: null,
title: 'Launching',
message: 'Start '
} )

if ( !app.useFakeData ) {
const telemetrySocket = io( 'http://' + window.location.hostname + ':3000' );
telemetrySocket.on( 'connect', () => {
store.dispatch( 'app/showMessage', {
icon: '<i class="fas fa-truck"></i>',
title: 'Connected to telemetry server',
message: 'Ready to delivering'
} );

setTimeout( () => {
return new Promise((resolve) => {
store.dispatch('app/showMessage', {
icon: null,
title: 'Launching',
message: 'Start '
} )

if ( !app.useFakeData ) {
const telemetrySocket = io( basePathHost() );
telemetrySocket.on( 'connect', () => {
store.dispatch( 'app/showMessage', {
icon: '<i class="fas fa-truck"></i>',
title: 'Waiting game connection',
message: 'Run the game to start your job !'
title: 'Connected to telemetry server',
message: 'Ready to delivering'
} );
}, 5000 );
} );
telemetrySocket.on( 'update', data => {
Vue.prototype.$updateEvent( data );
});
telemetrySocket.on( 'log', data => {
Vue.prototype.$updateEvent( data );
} );
} else {
setTimeout( () => {
Vue.prototype.$updateTelemetry( testData )
}, 1000 );
}

setTimeout( () => {
store.dispatch( 'app/showMessage', {
icon: '<i class="fas fa-truck"></i>',
title: 'Waiting game connection',
message: 'Run the game to start your job !'
} );
resolve()
}, 500 );
} );
telemetrySocket.on( 'update', data => {
Vue.prototype.$updateTelemetry( data );
});
telemetrySocket.on( 'log', data => {
Vue.prototype.$updateEvent( data );
} );
} else {
setTimeout( () => {
Vue.prototype.$updateTelemetry( testData )
resolve()
}, 1000 );
}
})
}

0 comments on commit 530bce6

Please sign in to comment.