1
0

Loading, but then server locks

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-01-26 13:46:39 -08:00
parent 5544485e30
commit 650462004c
2 changed files with 64 additions and 59 deletions

View File

@ -25,7 +25,7 @@ function App() {
return ( return (
<Router> <Router>
<Routes> <Routes>
<Route element={<Board/>} path={`${base}/games/:id`}/> <Route exact element={<Board/>} path={`${base}/games/:id`}/>
<Route exact element={<Board/>} path={`${base}`}/> <Route exact element={<Board/>} path={`${base}`}/>
</Routes> </Routes>
</Router> </Router>

View File

@ -1,6 +1,6 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import "./Board.css"; import "./Board.css";
//import history from "./history.js"; import history from "./history.js";
import Paper from '@material-ui/core/Paper'; import Paper from '@material-ui/core/Paper';
import TextField from '@material-ui/core/TextField'; import TextField from '@material-ui/core/TextField';
import List from '@material-ui/core/List'; import List from '@material-ui/core/List';
@ -423,65 +423,17 @@ class Board extends React.Component {
settlement: null settlement: null
}; };
const params = {}; this.id = (props.router && props.router.params.id) ? props.router.params.id : 0;
if (props.router && props.router.params.id) {
console.log(`Loading game: ${props.router.params.id}`);
params.url = `${base}/api/v1/games/${props.router.params.id}`;
params.method = "GET"
} else {
console.log("Requesting new game.");
params.url = `${base}/api/v1/games`;
params.method = "POST";
}
window.fetch(params.url, {
method: params.method,
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
// body: JSON.stringify(data) // body data type must match "Content-Type" header
}).then((res) => {
if (res.status > 400) {
console.log(`Unable to find game ${props.router.params.id}`);
throw new Error(`Unable to find requested game ${props.router.params.id}. Starting new one.`);
}
return res.json();
}).then((game) => {
console.log (`Game ${game.id} loaded.`);
if (!props.router.params.id) {
// history.push(`${gamesPath}/${game.id}`);
}
this.game = game;
//this.setState({ game: game });
this.pips = Pips(this);
this.tiles = Tiles(this);
this.table = Table(this);
this.borders = this.game.borders.map((file) => {
console.log(file);
return Border(this, file);
});
//this.loadTimer = window.setTimeout(this.loadGame, 1000);
}).catch((error) => {
console.error(error);
alert(error);
})
} }
loadGame() { loadGame() {
if (this.loadTimer) { if (this.loadTimer) {
window.clearTimeout(this.loadTimer); window.clearTimeout(this.loadTimer);
this.loadTimer = null; this.loadTimer = null;
} }
if (this.state.game) { if (this.state.game) {
return; // return;
} }
window.fetch(`${base}/api/v1/games/${this.state.game.id}`, { window.fetch(`${base}/api/v1/games/${this.state.game.id}`, {
@ -539,8 +491,8 @@ class Board extends React.Component {
}).then(() => { }).then(() => {
this.loadTimer = window.setTimeout(this.loadGame, 1000); this.loadTimer = window.setTimeout(this.loadGame, 1000);
}); });
} }
randomize() { randomize() {
//this.borders = shuffle(this.borders); //this.borders = shuffle(this.borders);
this.tiles = shuffle(this.tiles); this.tiles = shuffle(this.tiles);
@ -684,17 +636,16 @@ class Board extends React.Component {
if (this.updateSizeTimer) { if (this.updateSizeTimer) {
clearTimeout(this.updateSizeTimer); clearTimeout(this.updateSizeTimer);
} }
console.log("Fix updateDimensions");
this.updateSizeTimer = setTimeout(() => { this.updateSizeTimer = setTimeout(() => {
const container = document.getElementById("root"), const container = document.getElementById("root"),
offset = container.firstChild.offsetHeight, offset = container.firstChild.offsetHeight,
height = window.innerHeight - offset; height = window.innerHeight - offset;
return;
this.offsetY = offset; this.offsetY = offset;
this.width = window.innerWidth; this.width = window.innerWidth;
this.height = height; this.height = height;
this.canvas.width = this.width; this.canvas.width = this.width;
this.canvas.height = this.height; this.canvas.height = this.height;
this.canvas.style.top = `${offset}px`; this.canvas.style.top = `${offset}px`;
@ -1071,6 +1022,60 @@ class Board extends React.Component {
window.addEventListener("mousemove", this.mouseMove); window.addEventListener("mousemove", this.mouseMove);
window.addEventListener("resize", this.updateDimensions); window.addEventListener("resize", this.updateDimensions);
const params = {};
if (this.id) {
console.log(`Loading game: ${this.id}`);
params.url = `${base}/api/v1/games/${this.id}`;
params.method = "GET"
} else {
console.log("Requesting new game.");
params.url = `${base}/api/v1/games`;
params.method = "POST";
}
window.fetch(params.url, {
method: params.method,
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
// body: JSON.stringify(data) // body data type must match "Content-Type" header
}).then((res) => {
if (res.status > 400) {
let message;
if (this.id) {
message = `Unable to find game ${this.id}. Starting new game.`;
} else {
message = `Starting new game.`;
}
console.log(message);
throw new Error(message);
}
return res.json();
}).then((game) => {
console.log (`Game ${game.id} loaded.`);
if (!this.id) {
history.push(`${gamesPath}/${game.id}`);
}
this.game = game;
this.setState({ game: game });
this.pips = Pips(this);
this.tiles = Tiles(this);
this.table = Table(this);
this.borders = this.game.borders.map((file) => {
return Border(this, file);
});
this.loadTimer = window.setTimeout(this.loadGame, 1000);
}).catch((error) => {
console.error(error);
alert(error);
})
setTimeout(this.updateDimensions, 1000); setTimeout(this.updateDimensions, 1000);
} }