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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
import React, { useState, useEffect } from "react";
import GlMap, { Source, Marker, Layer } from "react-map-gl/maplibre";
import {
Flex,
Layout,
Col,
Row,
Modal,
Form,
Input,
Button,
Image,
Card,
} from "antd";
const { Header, Content } = Layout;
const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 },
};
const geostyle = {
id: "data",
type: "line",
paint: {
"line-color": "red",
"line-width": 3,
},
};
const App = () => {
const [state, setState] = useState({});
const [selected, setSelected] = useState(null);
const [guess, setGuess] = useState(null);
const [loading, setLoading] = useState(true);
const [form] = Form.useForm();
useEffect(() => {
fetch("/api/state")
.then((x) => x.json())
.then(setState)
.then(() => setLoading(false));
}, []);
useEffect(() => {
if (guess != null) {
setState(guess.state);
}
}, [guess]);
const onFinish = (values) => {
fetch("/api/state", {
method: "POST",
body: JSON.stringify(values),
headers: { "content-type": "application/json" },
})
.then((x) => x.json())
.then(setState);
};
const onReset = () => {
form.resetFields();
};
const onNext = () => {
setSelected(null);
setGuess(null);
setLoading(true);
fetch("/api/next", {
method: "POST",
headers: { "content-type": "application/json" },
})
.then((x) => x.json())
.then(setState)
.then(() => setLoading(false));
};
const onMapClick = (e) => {
if (state.current_guid) {
setSelected(e.lngLat);
}
};
const onGuess = () => {
setLoading(true);
fetch("/api/guess", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(selected),
})
.then((x) => x.json())
.then(setGuess)
.then(() => setLoading(false));
};
return (
<>
<Layout>
<Content>
<Row style={{ height: "100vh" }}>
<Col sm={16}>
<GlMap
initialViewState={{
longitude: 49.106414,
latitude: 55.796127,
zoom: 11,
}}
onClick={onMapClick}
style={{ width: "100%", height: "100%" }}
mapStyle="https://tiles.openfreemap.org/styles/liberty"
>
{selected ? (
<Marker
latitude={selected.lat}
longitude={selected.lng}
/>
) : null}
{guess ? (
<Source
type="geojson"
data={JSON.parse(guess.geojson)}
>
<Layer {...geostyle} />
</Source>
) : null}
</GlMap>
</Col>
<Col sm={8}>
{state.username ? (
<Card>
<h1>{state.username}</h1>
<h2>{state.points} очков</h2>
</Card>
) : null}
<Card>
{!loading && !state.current_guid ? (
<Button
type="primary"
onClick={onNext}
block
size="large"
>
Новое задание
</Button>
) : null}
{!loading && state.current_guid ? (
<Image src={state.image} />
) : null}
{!loading && guess ? (
<>
<h2>{guess.name}</h2>
<Image src={guess.image} />
<h3>
Расстояние: {guess.distance / 1000}
км.
</h3>
</>
) : null}
{state.current_guid && !selected ? (
<p>
Нажмите на карте на точку, где по вашему
мнение находится то, что на фотографии
</p>
) : null}
{state.current_guid && selected ? (
<Button
type="primary"
onClick={onGuess}
block
size="large"
>
Проверить
</Button>
) : null}
</Card>
<p>
Сделал{" "}
<a href="https://neonxp.ru">
Александр Кирюхин
</a>{" "}
в 2024 году
</p>
</Col>
</Row>
</Content>
</Layout>
<Modal
title="Представьтесь"
open={!loading && !state.username}
onOk={form.submit}
onCancel={onReset}
>
<p>Для начала игры необходимо представиться</p>
<Form
{...layout}
form={form}
name="control-hooks"
onFinish={onFinish}
style={{ maxWidth: 600 }}
>
<Form.Item
name="username"
label="Имя"
rules={[{ required: true }]}
>
<Input />
</Form.Item>
</Form>
</Modal>
</>
);
};
export default App;
|