-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
196 lines (173 loc) · 4.54 KB
/
Copy pathApp.tsx
File metadata and controls
196 lines (173 loc) · 4.54 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { reset } from "viem/actions";
import { useMUD } from "./MUDContext";
import { useState } from "react";
// Styles
const dugAlready = {
backgroundColor: "white",
color: "black",
fontSize: "30px",
width: "1.5em",
}
const digHere = {
backgroundColor: "black",
color: "white",
fontSize: "30px",
width: "1.5em",
}
const digHereHover = {
backgroundColor: "yellow",
color: "black",
fontSize: "30px",
width: "1.5em",
}
const controlButton = {
fontSize: "30px",
display: "inline-block",
cursor: "pointer",
}
function useHover(styleOnHover: CSSProperties, styleOnNotHover: CSSProperties = {})
{
const [style, setStyle] = useState(styleOnNotHover);
const onMouseEnter = () => setStyle(styleOnHover)
const onMouseLeave = () => setStyle(styleOnNotHover)
return {style, onMouseEnter, onMouseLeave}
}
const MapCell = attrs => {
const hover = useHover(digHereHover, digHere)
return (
<button disabled={attrs.GameMap!==undefined || !attrs.active}
onMouseEnter={hover.onMouseEnter}
onMouseLeave={hover.onMouseLeave}
style={attrs.GameMap===undefined ? hover.style : dugAlready}
onClick={() => {
attrs.dig(attrs.x, attrs.y)
}}
>
{
attrs.GameMap===undefined ? "?" :
attrs.GameMap===254 ? "💣" :
attrs.GameMap.toString()
}
</button>
)
}
const Map = attrs => {
return (
<table>
{attrs.lines.map((line, y) => (
<tr key={`tr_${y}`}>
{attrs.columns.map((column, x) => (
<td key={`td_${x}_${y}`}>
<MapCell
active={attrs.active}
x={x}
y={y}
GameMap={attrs.gameMap[`${x},${y}`]}
dig={attrs.dig}
/>
</td>
)
)}
</tr>
)
)}
</table>
)
}
const NewGame = (attrs) => {
return (
<>
<h4>Click to start a new game</h4>
<button
style={controlButton}
onClick={() => attrs.newGame()}
>
New Game
</button>
</>
)
}
const EndGame = (attrs) => {
return (
<>
{
attrs.win ?
(<h4>You won!</h4>)
: (<h4>You lost</h4>)
}
<Map
lines={attrs.lines}
columns={attrs.columns}
gameMap={attrs.gameMap}
active={false}
/>
<br />
<button
style={controlButton}
onClick={() => attrs.reset()}
>
Reset
</button>
</>
)
}
export const App = () => {
const {
network: { tables, useStore, walletClient },
systemCalls: { dig, reset, newGame },
} = useMUD();
const { width, height, numberOfBombs } = useStore(state => {
const configuration = Object.values(state.getRecords(tables.Configuration))
if (configuration.length > 0)
return Object.values(state.getRecords(tables.Configuration))[0].value
else // If we don't have the values yet
return {}
})
const gameRecord = useStore(state => {
const address = walletClient.account.address
const records = Object.values(state.getRecords(tables.PlayerGame))
const relevantRecord = records.filter(record => record.key.player == address)[0]
return relevantRecord?.value
})
const gameMap = useStore(state => {
const filteredMap = Object.values(state.getRecords(tables.Map))
.filter(mapItem => mapItem.key.gameId == gameRecord?.gameId)
const keyValueMap = filteredMap.reduce((result, item) =>
{
result[`${item.key.x},${item.key.y}`] = item.value.bombsAroundPlusOne-1
return result
},
{})
return keyValueMap;
})
const lines = new Array(height)
lines.fill("", 0, height)
const columns = new Array(width)
columns.fill("", 0, width)
const digsLeft = height*width - (numberOfBombs+gameRecord?.digNumber)
return (
<>
<h2>Minesweeper Game</h2>
Configuration: {width} x {height}, {numberOfBombs} <br />
Digs left: {digsLeft} <br />
{ gameRecord && gameRecord?.gameId != 0 ?
gameRecord.win || gameRecord.lose ?
( <EndGame
reset={reset}
win={gameRecord.win}
lines={lines}
columns={columns}
gameMap={gameMap}
/> )
: ( <Map
lines={lines}
columns={columns}
gameMap={gameMap}
dig={dig}
active={true}
/> )
: ( <NewGame newGame={newGame} /> )
}
</>
)
};