refactoring

This commit is contained in:
Settel 2021-08-13 00:41:23 +02:00
parent a86a0e62a5
commit ddcce45238
6 changed files with 25 additions and 11 deletions

View File

@ -17,7 +17,7 @@ export default {
}, },
methods: { methods: {
startGame() { startGame() {
alert('start game') this.$engine.startGame()
}, },
}, },
} }

View File

@ -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)
}

View File

@ -1,7 +1,5 @@
import buildUrl from 'build-url'
export default async function() { export default async function() {
const { store, $axios, $config } = this.context const { store } = this.context
if (this.shouldStop || !this.isActive) { if (this.shouldStop || !this.isActive) {
this.isActive = false this.isActive = false
@ -11,14 +9,11 @@ export default async function() {
let delay = 0 let delay = 0
try { try {
const url = buildUrl($config.serverBaseUrl, { const response = await this.callApi('/api/sync', {
path: '/api/sync',
queryParams: {
v: store.state.engine.version + 1, v: store.state.engine.version + 1,
g: store.state.engine.user?.game, g: store.state.engine.user?.game,
},
}) })
const response = await $axios.get(url)
store.commit('engine/setJson', response.data) store.commit('engine/setJson', response.data)
} catch (e) { } catch (e) {
const { status, statusText } = e.response const { status, statusText } = e.response

View File

@ -1,8 +1,8 @@
export default async function() { export default async function() {
const { store, $axios } = this.context const { store } = this.context
try { try {
const response = await $axios.get('/api/userinfo') const response = await this.callApi('/api/userinfo')
store.commit('engine/setUser', response.data) store.commit('engine/setUser', response.data)
} catch(e) { } catch(e) {
store.commit('engine/setUser', undefined) store.commit('engine/setUser', undefined)

View File

@ -1,7 +1,9 @@
import callApi from './callApi'
import start from './start' import start from './start'
import stop from './stop' import stop from './stop'
import fetchUpdate from './fetchUpdate' import fetchUpdate from './fetchUpdate'
import fetchUserInfo from './fetchUserInfo' import fetchUserInfo from './fetchUserInfo'
import startGame from './startGame'
export default (context, inject) => { export default (context, inject) => {
const engine = { const engine = {
@ -10,10 +12,12 @@ export default (context, inject) => {
isActive: false, isActive: false,
shouldStop: false, shouldStop: false,
callApi,
start, start,
stop, stop,
fetchUpdate, fetchUpdate,
fetchUserInfo, fetchUserInfo,
startGame,
} }
inject('engine', engine) inject('engine', engine)

View File

@ -0,0 +1,3 @@
export default async function() {
alert('start game!')
}