From ddcce45238a764076f910baaa82d9a4ccb79eb93 Mon Sep 17 00:00:00 2001 From: Settel Date: Fri, 13 Aug 2021 00:41:23 +0200 Subject: [PATCH] refactoring --- client/src/components/GameControls.vue | 2 +- client/src/plugins/engine/callApi.js | 12 ++++++++++++ client/src/plugins/engine/fetchUpdate.js | 11 +++-------- client/src/plugins/engine/fetchUserInfo.js | 4 ++-- client/src/plugins/engine/index.js | 4 ++++ client/src/plugins/engine/startGame.js | 3 +++ 6 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 client/src/plugins/engine/callApi.js create mode 100644 client/src/plugins/engine/startGame.js diff --git a/client/src/components/GameControls.vue b/client/src/components/GameControls.vue index 90c90d8..bafc736 100644 --- a/client/src/components/GameControls.vue +++ b/client/src/components/GameControls.vue @@ -17,7 +17,7 @@ export default { }, methods: { startGame() { - alert('start game') + this.$engine.startGame() }, }, } diff --git a/client/src/plugins/engine/callApi.js b/client/src/plugins/engine/callApi.js new file mode 100644 index 0000000..11aef6e --- /dev/null +++ b/client/src/plugins/engine/callApi.js @@ -0,0 +1,12 @@ +import buildUrl from 'build-url' + +export default async function(path, queryParams) { + const { $axios, $config } = this.context + + const url = buildUrl($config.serverBaseUrl, { + path, + queryParams, + }) + + return await $axios.get(url) +} diff --git a/client/src/plugins/engine/fetchUpdate.js b/client/src/plugins/engine/fetchUpdate.js index 7a14aad..9c8b2e1 100644 --- a/client/src/plugins/engine/fetchUpdate.js +++ b/client/src/plugins/engine/fetchUpdate.js @@ -1,7 +1,5 @@ -import buildUrl from 'build-url' - export default async function() { - const { store, $axios, $config } = this.context + const { store } = this.context if (this.shouldStop || !this.isActive) { this.isActive = false @@ -11,14 +9,11 @@ export default async function() { let delay = 0 try { - const url = buildUrl($config.serverBaseUrl, { - path: '/api/sync', - queryParams: { + const response = await this.callApi('/api/sync', { v: store.state.engine.version + 1, g: store.state.engine.user?.game, - }, }) - const response = await $axios.get(url) + store.commit('engine/setJson', response.data) } catch (e) { const { status, statusText } = e.response diff --git a/client/src/plugins/engine/fetchUserInfo.js b/client/src/plugins/engine/fetchUserInfo.js index c086c4e..a2dc292 100644 --- a/client/src/plugins/engine/fetchUserInfo.js +++ b/client/src/plugins/engine/fetchUserInfo.js @@ -1,8 +1,8 @@ export default async function() { - const { store, $axios } = this.context + const { store } = this.context try { - const response = await $axios.get('/api/userinfo') + const response = await this.callApi('/api/userinfo') store.commit('engine/setUser', response.data) } catch(e) { store.commit('engine/setUser', undefined) diff --git a/client/src/plugins/engine/index.js b/client/src/plugins/engine/index.js index c404f94..9657a47 100644 --- a/client/src/plugins/engine/index.js +++ b/client/src/plugins/engine/index.js @@ -1,7 +1,9 @@ +import callApi from './callApi' import start from './start' import stop from './stop' import fetchUpdate from './fetchUpdate' import fetchUserInfo from './fetchUserInfo' +import startGame from './startGame' export default (context, inject) => { const engine = { @@ -10,10 +12,12 @@ export default (context, inject) => { isActive: false, shouldStop: false, + callApi, start, stop, fetchUpdate, fetchUserInfo, + startGame, } inject('engine', engine) diff --git a/client/src/plugins/engine/startGame.js b/client/src/plugins/engine/startGame.js new file mode 100644 index 0000000..93f1f72 --- /dev/null +++ b/client/src/plugins/engine/startGame.js @@ -0,0 +1,3 @@ +export default async function() { + alert('start game!') +}