Improve guessing

This commit is contained in:
Tim
2021-07-13 17:57:06 +02:00
parent 698849c49d
commit 926ade42a7
4 changed files with 77 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
import axios, {AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios'
import {JoinCreateGameObject, IsStartedObject, ApiObject, GameStateObject, MyTurnObject} from "@/objects/objects";
import axios, {AxiosPromise, AxiosResponse} from 'axios'
import {JoinCreateGameObject, IsStartedObject, ApiObject, GameStateObject, MyTurnObject, GuessAction} from "@/objects/objects";
export default class PerudoApi {
private static baseUrl = 'http://localhost:8000/'
@@ -14,42 +14,49 @@ export default class PerudoApi {
}
public createGame(name: string): Promise<JoinCreateGameObject> {
return this.get<JoinCreateGameObject>(PerudoApi.baseUrl + 'game/create?name=' + name)
return this.get<JoinCreateGameObject>('game/create?name=' + name)
.then((response: AxiosResponse<JoinCreateGameObject>) => {
return response.data;
})
}
public joinGame(name: string, code: string): Promise<JoinCreateGameObject> {
return this.get<JoinCreateGameObject>(PerudoApi.baseUrl + 'game/join/' + code + '?name=' + name)
return this.get<JoinCreateGameObject>('game/join/' + code + '?name=' + name)
.then((response: AxiosResponse<JoinCreateGameObject>) => {
return response.data;
})
}
public gameStarted(playerId: string): Promise<IsStartedObject> {
return this.get<IsStartedObject>(PerudoApi.baseUrl + 'game/started/' + playerId)
return this.get<IsStartedObject>('game/started/' + playerId)
.then((response: AxiosResponse<IsStartedObject>) => {
return response.data;
})
}
public startGame(playerId: string): Promise<ApiObject> {
return this.get<ApiObject>(PerudoApi.baseUrl + 'game/start/' + playerId)
return this.get<ApiObject>('game/start/' + playerId)
.then((response: AxiosResponse<ApiObject>) => {
return response.data;
});
}
public myTurn(playerId: string): Promise<MyTurnObject> {
return this.get<MyTurnObject>(PerudoApi.baseUrl + 'player/turn/' + playerId)
return this.get<MyTurnObject>('player/turn/' + playerId)
.then((response: AxiosResponse<MyTurnObject>) => {
return response.data;
})
}
public makeGuess(playerId: string, guess: GuessAction): Promise<MyTurnObject> {
return this.post<MyTurnObject>('player/guess/' + playerId, guess)
.then((response: AxiosResponse<MyTurnObject>) => {
return response.data;
});
}
private get<T extends ApiObject>(url: string, getParameters?: Record<string, any>): AxiosPromise<T> {
return axios.get(url).then((response: AxiosResponse<T>) => {
return axios.get(PerudoApi.baseUrl + url).then((response: AxiosResponse<T>) => {
// @ts-ignore
if (response?.data?.errors?.length > 0) {
throw response.data.errors?.join();
@@ -57,4 +64,15 @@ export default class PerudoApi {
return response;
})
}
private post<T extends ApiObject>(url: string, data?: any,): AxiosPromise<T> {
console.log(data);
return axios.post(PerudoApi.baseUrl + url, data).then((response: AxiosResponse) => {
// @ts-ignore
if (response?.data?.errors?.length > 0) {
throw response.data.errors?.join();
}
return response;
});
}
}