|
@@ -1,36 +1,9 @@
|
|
import React from 'react';
|
|
import React from 'react';
|
|
-import { getStatus, createGame, gameInfo, joinGame, getGuesses, sendGuess } from "./services/ggsh.service";
|
|
|
|
|
|
+import { createGame, gameInfo, joinGame, getGuesses, sendGuess } from "./services/ggsh.service";
|
|
import GamePanel from "./components/game-panel.component";
|
|
import GamePanel from "./components/game-panel.component";
|
|
|
|
+import ApiInfo from "./components/api-info.component";
|
|
import './App.css';
|
|
import './App.css';
|
|
|
|
|
|
-class InfoComponent extends React.Component {
|
|
|
|
- constructor(props) {
|
|
|
|
- super(props);
|
|
|
|
- this.state = {
|
|
|
|
- loading: true,
|
|
|
|
- version: null,
|
|
|
|
- status: null,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- async componentDidMount() {
|
|
|
|
- const { version, status } = await getStatus();
|
|
|
|
- this.setState({loading: false, version, status});
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- render() {
|
|
|
|
- if (this.state.loading) {
|
|
|
|
- return <p>Connecting to back-end...</p>
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (this.state.status !== "healthy") {
|
|
|
|
- return <p>Unable to communicate with API server! Error: {this.state.status}</p>
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return <p>API Version: {this.state.version}</p>
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
const GameInfo = ({game}) => {
|
|
const GameInfo = ({game}) => {
|
|
const {
|
|
const {
|
|
gameId,
|
|
gameId,
|
|
@@ -87,13 +60,6 @@ class GameMakerComp extends React.Component {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- async handleGameCreate() {
|
|
|
|
- this.setState({loading: true});
|
|
|
|
- const gameId = await createGame("testing", 300);
|
|
|
|
- const game = await gameInfo(gameId);
|
|
|
|
- this.setState({loading: false, game});
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
async handleGameJoin() {
|
|
async handleGameJoin() {
|
|
this.setState({loading: true});
|
|
this.setState({loading: true});
|
|
const { gameId } = this.state.game
|
|
const { gameId } = this.state.game
|
|
@@ -139,19 +105,57 @@ class Game extends React.Component {
|
|
constructor(props) {
|
|
constructor(props) {
|
|
super(props);
|
|
super(props);
|
|
this.state = {
|
|
this.state = {
|
|
- targetPoint: {lat: 42.57343, lng: 1.58651},
|
|
|
|
|
|
+ playerName: "testing",
|
|
|
|
+ loading: false,
|
|
|
|
+ gameId: null,
|
|
|
|
+ currentRound: null,
|
|
|
|
+ targetPoint: null,
|
|
selectedPoint: null,
|
|
selectedPoint: null,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- handleSubmitGuess() {
|
|
|
|
- const { selectedPoint } = this.state;
|
|
|
|
- console.log(JSON.stringify(selectedPoint));
|
|
|
|
- // TODO actually submit
|
|
|
|
|
|
+ async handleGameCreate() {
|
|
|
|
+ this.setState({loading: true});
|
|
|
|
+ const { playerName } = this.state;
|
|
|
|
+ const gameId = await createGame(playerName, 300);
|
|
|
|
+ this.setState({ gameId });
|
|
|
|
+ await this.updateRoundState();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async updateRoundState() {
|
|
|
|
+ const { gameId, playerName } = this.state;
|
|
|
|
+ const { currentRound } = await getGuesses(gameId, playerName);
|
|
|
|
+ if (currentRound) {
|
|
|
|
+ const { coords } = await gameInfo(gameId);
|
|
|
|
+ const targetPoint = coords[currentRound];
|
|
|
|
+ this.setState({ loading: false, currentRound, targetPoint });
|
|
|
|
+ } else {
|
|
|
|
+ this.setState({ loading: false, gameId: null, currentRound: null, targetPoint: null });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async handleSubmitGuess() {
|
|
|
|
+ const { gameId, playerName, currentRound, selectedPoint } = this.state;
|
|
|
|
+ this.setState({ loading: true, selectedPoint: null });
|
|
|
|
+ const score = await sendGuess(gameId, playerName, currentRound, selectedPoint);
|
|
|
|
+ console.log(`Score: ${score}`);
|
|
|
|
+ await this.updateRoundState();
|
|
}
|
|
}
|
|
|
|
|
|
render() {
|
|
render() {
|
|
- const { selectedPoint, targetPoint } = this.state;
|
|
|
|
|
|
+ const { loading, selectedPoint, targetPoint } = this.state;
|
|
|
|
+
|
|
|
|
+ if (loading) {
|
|
|
|
+ return <p>Loading...</p>
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!targetPoint) {
|
|
|
|
+ return (<div>
|
|
|
|
+ <p>No game in progress!</p>
|
|
|
|
+ <button onClick={() => this.handleGameCreate()}>Create New Game</button>
|
|
|
|
+ </div>);
|
|
|
|
+ }
|
|
|
|
+
|
|
return <GamePanel
|
|
return <GamePanel
|
|
onSelectPoint={latLng => this.setState({selectedPoint: latLng})}
|
|
onSelectPoint={latLng => this.setState({selectedPoint: latLng})}
|
|
onSubmitGuess={() => this.handleSubmitGuess()}
|
|
onSubmitGuess={() => this.handleSubmitGuess()}
|
|
@@ -169,7 +173,7 @@ const App = () => {
|
|
<hr/>
|
|
<hr/>
|
|
<Game/>
|
|
<Game/>
|
|
<hr/>
|
|
<hr/>
|
|
- <InfoComponent/>
|
|
|
|
|
|
+ <ApiInfo/>
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|
|
}
|
|
}
|