refactoring
This commit is contained in:
parent
a86a0e62a5
commit
ddcce45238
@ -17,7 +17,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
startGame() {
|
||||
alert('start game')
|
||||
this.$engine.startGame()
|
||||
},
|
||||
},
|
||||
}
|
||||
|
12
client/src/plugins/engine/callApi.js
Normal file
12
client/src/plugins/engine/callApi.js
Normal 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)
|
||||
}
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
3
client/src/plugins/engine/startGame.js
Normal file
3
client/src/plugins/engine/startGame.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default async function() {
|
||||
alert('start game!')
|
||||
}
|
Loading…
Reference in New Issue
Block a user