Compare commits

..

No commits in common. "b34335cefc96ad9427b3f5c6e35f9aff2e42296a" and "72daeddb9db91100b83ff2496ec909c369471116" have entirely different histories.

5 changed files with 69 additions and 94 deletions

View File

@ -2,7 +2,7 @@ import './App.css';
import React, { useState, useEffect, useCallback } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import { import {
BrowserRouter, BrowserRouter,
Routes, Routes, //replaces "Switch" used till v5
Route, Route,
} from "react-router-dom"; } from "react-router-dom";
@ -15,13 +15,18 @@ function App() {
const [polling, setPolling] = useState(false); const [polling, setPolling] = useState(false);
const pollGames = useCallback(() => { const pollGames = useCallback(() => {
if (polling) console.log('start polling..');
if (polling) {
console.log(' ..already in progress');
return; return;
}
setPolling(true); setPolling(true);
fetch('/api/gamestate') fetch('/api/gamestate')
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
console.log('poooled');
setGames(data); setGames(data);
setPolling(false); setPolling(false);
}) })
@ -29,11 +34,12 @@ function App() {
}, [polling]); }, [polling]);
useEffect(() => { useEffect(() => {
const timer = setInterval(pollGames(), 35 * 1000); // <<-- poll new gamestates every 35 sec const timer = setInterval(pollGames(), 35 * 1000);
return clearInterval(timer); return clearInterval(timer);
}, [pollGames]) }, [pollGames])
return <div className="App"> return (
<div className="App">
<BrowserRouter> <BrowserRouter>
<Header/> <Header/>
<div className="Container"> <div className="Container">
@ -42,8 +48,10 @@ function App() {
<Route path="/gameproposal" element={<GameProposal games={games}/>} /> <Route path="/gameproposal" element={<GameProposal games={games}/>} />
</Routes> </Routes>
</div> </div>
</BrowserRouter> </BrowserRouter>
</div> </div>
);
} }
export default App; export default App;

View File

@ -15,18 +15,6 @@
margin: 5px; margin: 5px;
} }
.GameProposal .li p q {
color: gray;
}
.GameProposal .li button.action {
display: none;
}
.GameProposal .li:hover button.action {
display: initial;
}
.separator { .separator {
width: 20%; width: 20%;
/* height: 20px; */ /* height: 20px; */

View File

@ -1,5 +1,4 @@
import React from 'react'; import React from 'react';
import {Accept, Reject, Cancel} from './GameProposalAction';
import './GameProposal.css'; import './GameProposal.css';
const State = { const State = {
@ -17,34 +16,28 @@ const GameProposal = ({games}) => {
const waitForYou = games const waitForYou = games
.filter(game => game.status === State.WaitForYou) .filter(game => game.status === State.WaitForYou)
.map(game => { .map(game => { return (
return <div class="li" key={game.uuid}> <div class="li" key={game.uuid}>
<p> <p>
from {game.opponentName}, opponentColor {game.opponentColor} from {game.opponentName}, opponentColor {game.opponentColor}<br/>
<br/> <q color="grey">{game.message}</q>
<q>{game.message}</q>
<br/>
<Accept uuid={game.uuid}/>
<Reject uuid={game.uuid}/>
</p> </p>
</div> </div>
}); )});
const WaitForOpponent = games const WaitForOpponent = games
.filter(game => game.status === State.WaitForOpponent) .filter(game => game.status === State.WaitForOpponent)
.map(game => { .map(game => { return (
return <div class="li" key={game.uuid}> <div class="li" key={game.uuid}>
<p> <p>
to {game.opponentName}, opponentColor {game.opponentColor} to {game.opponentName}, opponentColor {game.opponentColor}<br/>
<br/> <q color="grey">{game.message}</q>
<q>{game.message}</q>
<br/>
<Cancel uuid={game.uuid}/>
</p> </p>
</div> </div>
}); )});
return <div className="GameProposal"> return (
<p className="GameProposal">
{waitForYou} {waitForYou}
{WaitForOpponent.length > 0 && {WaitForOpponent.length > 0 &&
<div class="separator"> <div class="separator">
@ -52,7 +45,8 @@ const GameProposal = ({games}) => {
</div> </div>
} }
{WaitForOpponent} {WaitForOpponent}
</div> </p>
);
}; };
export default GameProposal; export default GameProposal;

View File

@ -1,19 +0,0 @@
import React from 'react';
export function Accept({uuid}) {
return <button className="action" id={uuid} type="submit">
Accept
</button>
}
export function Reject({uuid}) {
return <button className="action" id={uuid} type="submit">
Reject
</button>
}
export function Cancel({uuid}) {
return <button className="action" id={uuid} type="submit">
Cancel
</button>
}

View File

@ -31,15 +31,18 @@ const Leaderboard = () => {
const tableRows = Object.keys(data).map(playerName => { const tableRows = Object.keys(data).map(playerName => {
var rank = data[playerName]; var rank = data[playerName];
return <tr key={playerName}> return (
<tr key={playerName}>
<td>{playerName}</td> <td>{playerName}</td>
<td>{rank.gamesPlayed}</td> <td>{rank.gamesPlayed}</td>
<td>{rank.gamesWon}</td> <td>{rank.gamesWon}</td>
<td>{rank.gamesDraw}</td> <td>{rank.gamesDraw}</td>
</tr> </tr>
);
}); });
return <div className="Leaderboard"> return (
<div className="Leaderboard">
<table> <table>
<thead> <thead>
<tr> <tr>
@ -54,6 +57,7 @@ const Leaderboard = () => {
</tbody> </tbody>
</table> </table>
</div> </div>
);
}; };
export default Leaderboard; export default Leaderboard;