aboutsummaryrefslogtreecommitdiff
path: root/components/recentGames.tsx
blob: 92855a14ae4b7524eb37835f652314a33031ad5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { CSSProperties } from 'react';
import friendlyTime from 'friendly-time';

import { gameInfo } from '../api/api';

var LeftAlignedTableColumn: CSSProperties = {
	textAlign: "left",
	paddingLeft: 16
}

var RightAlignedTableColumn: CSSProperties = {
	textAlign: "right",
	paddingRight: 16
}

function GameOutcome(props: { game: gameInfo }) {
	var gameStatus = (() => {
		return {
			"resign": () => "Opgegeven",
			"wait_for_opponent": () => "Aan het wachten op een tegenstander",
			"in_progress": () => "Bezig",
			"finished": () => {
				return {
					"w": "Gewonnen",
					"l": "Verloren",
					"d": "Gelijkspel"
				}[props.game.outcome]
			},
		}[props.game.status]();
	})();
	var outcome = props.game.outcome;
	return <td style={{
		color:
			outcome == "w" ? "var(--disk-b-text)" :
			outcome == "l" ? "var(--disk-a-text)" :
			"var(--text)",
		opacity: !["w", "l"].includes(outcome) ? 0.75 : 1.0
	}}>{gameStatus}</td>
}

export default function RecentGames(props: { games?: Array<gameInfo> }) {
	return <div>
		<h2>Recente partijen</h2>
		<table width="100%" style={{ marginTop: "16px", textAlign: "center" }}>
			<tbody>
				<tr>
					<th style={{ width: "50%" }}>Tegenstander</th>
					<th style={{ width: "15%" }}>Uitkomst</th>
					<th style={{ width: "15%" }}>Zetten</th>
					<th style={{ width: "20%" }}>Datum</th>
				</tr>
				{
					props.games?.map(game => <tr>
						<td style={LeftAlignedTableColumn}>{game.opponent?.username}</td>
						<GameOutcome game={game}/>
						<td>{Math.max(0, game.moves.length -1)}</td>
						<td style={RightAlignedTableColumn}>{(() => {
							var timeCreated = new Date(game.created);
							return friendlyTime(timeCreated);
						})()}</td>
					</tr>)
				}
			</tbody>
		</table>
	</div>
}