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: {
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() {
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

View File

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

View File

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

View File

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