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,9 +1,9 @@
<template> <template>
<div id="app"> <div id="app">
<div id="nav"> <!-- <div id="nav">-->
<router-link to="/">Home</router-link> | <!-- <router-link to="/">Home</router-link> |-->
<router-link to="/about">About</router-link> <!-- <router-link to="/about">About</router-link>-->
</div> <!-- </div>-->
<div class="page-container"> <div class="page-container">
<md-app> <md-app>
<md-app-content> <md-app-content>
@ -31,6 +31,6 @@
.page-container { .page-container {
width: 1024px; width: 1024px;
margin: auto; margin: 100px auto auto auto;
} }
</style> </style>

View File

@ -44,6 +44,7 @@ export interface RoundObject {
turns: TurnObject[], turns: TurnObject[],
loser?: PlayerObject, loser?: PlayerObject,
myRolls: number[], myRolls: number[],
rolls: number[][],
} }
export interface TurnObject { export interface TurnObject {
@ -57,3 +58,8 @@ export interface PlayerObject {
id: string, id: string,
name: string, name: string,
} }
export interface GuessAction {
diceCount: number,
dieValue: number,
}

View File

@ -1,5 +1,5 @@
import axios, {AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios' import axios, {AxiosPromise, AxiosResponse} from 'axios'
import {JoinCreateGameObject, IsStartedObject, ApiObject, GameStateObject, MyTurnObject} from "@/objects/objects"; import {JoinCreateGameObject, IsStartedObject, ApiObject, GameStateObject, MyTurnObject, GuessAction} from "@/objects/objects";
export default class PerudoApi { export default class PerudoApi {
private static baseUrl = 'http://localhost:8000/' private static baseUrl = 'http://localhost:8000/'
@ -14,42 +14,49 @@ export default class PerudoApi {
} }
public createGame(name: string): Promise<JoinCreateGameObject> { 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>) => { .then((response: AxiosResponse<JoinCreateGameObject>) => {
return response.data; return response.data;
}) })
} }
public joinGame(name: string, code: string): Promise<JoinCreateGameObject> { 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>) => { .then((response: AxiosResponse<JoinCreateGameObject>) => {
return response.data; return response.data;
}) })
} }
public gameStarted(playerId: string): Promise<IsStartedObject> { 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>) => { .then((response: AxiosResponse<IsStartedObject>) => {
return response.data; return response.data;
}) })
} }
public startGame(playerId: string): Promise<ApiObject> { 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>) => { .then((response: AxiosResponse<ApiObject>) => {
return response.data; return response.data;
}); });
} }
public myTurn(playerId: string): Promise<MyTurnObject> { 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>) => { .then((response: AxiosResponse<MyTurnObject>) => {
return response.data; 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> { 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 // @ts-ignore
if (response?.data?.errors?.length > 0) { if (response?.data?.errors?.length > 0) {
throw response.data.errors?.join(); throw response.data.errors?.join();
@ -57,4 +64,15 @@ export default class PerudoApi {
return response; 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;
});
}
} }

View File

@ -34,7 +34,23 @@
It is your turn! <br> It is your turn! <br>
Round: {{ currentRound.number + 1 }} <br> Round: {{ currentRound.number + 1 }} <br>
Turn: {{ currentTurn ? currentTurn.number : 1 }} <br> Turn: {{ currentTurn ? currentTurn.number : 1 }} <br>
Throws: {{ currentRound.myRolls }} Throws: {{ currentRound.myRolls }} <br>
Players: <br>
<template v-for="player in gameStateObject.players">
- {{ player.name }} <br>
</template>
<br>
<md-field>
<label>Dice count</label>
<md-input type="number" v-model="diceCount"></md-input>
</md-field>
<md-field>
<label>Die value</label>
<md-input type="number" v-model="dieValue"></md-input>
</md-field>
<md-button @click="makeGuess" class="md-raised md-primary">Guess</md-button>
</template> </template>
</div> </div>
</template> </template>
@ -42,14 +58,7 @@
<script lang="ts"> <script lang="ts">
import {Component, Vue} from 'vue-property-decorator' import {Component, Vue} from 'vue-property-decorator'
import PerudoApi from "@/services/PerudoApi"; import PerudoApi from "@/services/PerudoApi";
import { import {ApiObject, GameState, GameStateObject, IsStartedObject, JoinCreateGameObject, MyTurnObject, RoundObject, TurnObject} from "@/objects/objects"; // @ is an alias to /src
ApiObject,
GameState,
GameStateObject,
IsStartedObject,
JoinCreateGameObject,
MyTurnObject, RoundObject, TurnObject
} from "@/objects/objects"; // @ is an alias to /src
@Component({ @Component({
components: {} components: {}
@ -66,6 +75,9 @@ export default class Home extends Vue {
private playerId: string | null = null; private playerId: string | null = null;
private gameTimer: number | null = null; private gameTimer: number | null = null;
private diceCount: number = 0;
private dieValue: number = 1;
private get gameJoined(): boolean { private get gameJoined(): boolean {
return this.gameState >= GameState.Joined; return this.gameState >= GameState.Joined;
} }
@ -130,7 +142,24 @@ export default class Home extends Vue {
} }
} }
private makeGuess(): void {
if (this.myTurn && this.playerId) {
PerudoApi.instance.makeGuess(this.playerId, {
diceCount: parseInt(this.diceCount),
dieValue: parseInt(this.dieValue),
}).then((response: MyTurnObject) => {
this.gameState = GameState.Started;
this.gameTimer = setInterval(() => {
this.checkTurn();
}, 1000);
}).catch((reason => {
this.error = reason;
}));
}
}
private checkStarted(): void { private checkStarted(): void {
console.log("Check started");
if (this.gameJoined && this.playerId) { if (this.gameJoined && this.playerId) {
PerudoApi.instance.gameStarted(this.playerId).then((response: IsStartedObject) => { PerudoApi.instance.gameStarted(this.playerId).then((response: IsStartedObject) => {
if (response.started) { if (response.started) {
@ -147,11 +176,12 @@ export default class Home extends Vue {
} }
private checkTurn(): void { private checkTurn(): void {
console.log("Check turn");
if (this.gameStarted && this.playerId) { if (this.gameStarted && this.playerId) {
PerudoApi.instance.myTurn(this.playerId).then((response: MyTurnObject) => { PerudoApi.instance.myTurn(this.playerId).then((response: MyTurnObject) => {
if (response.turn) { if (response.turn) {
console.log(response.gameState);
this.gameStateObject = response.gameState; this.gameStateObject = response.gameState;
console.log(this.gameStateObject);
this.gameState = GameState.MyTurn; this.gameState = GameState.MyTurn;
this.clearGameTimer(); this.clearGameTimer();
} }