-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathemmodule.cpp
More file actions
357 lines (340 loc) · 13.1 KB
/
emmodule.cpp
File metadata and controls
357 lines (340 loc) · 13.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <string>
#include <memory>
#include "game.h"
#include "pgnparser.h"
using namespace emscripten;
// Safe wrapper that returns JavaScript object with success/error
val create_game_from_pgn(const std::string& pgn)
{
val result = val::object();
try {
auto game_ptr = std::make_shared<game>(game::from_pgn(pgn));
result.set("success", true);
result.set("game", val(game_ptr));
return result;
}
catch(const parse_error &e) {
result.set("success", false);
result.set("error", "ParseError");
result.set("message", e.what());
result.set("type", "parse_error");
return result;
}
catch(const std::invalid_argument &e) {
result.set("success", false);
result.set("error", "InvalidArgumentError");
result.set("message", e.what());
result.set("type", "invalid_argument");
return result;
}
catch(const std::runtime_error &e) {
result.set("success", false);
result.set("error", "RuntimeError");
result.set("message", e.what());
result.set("type", "runtime_error");
return result;
}
catch(const std::exception &e) {
result.set("success", false);
result.set("error", "Exception");
result.set("message", e.what());
result.set("type", "generic_exception");
return result;
}
catch(...) {
result.set("success", false);
result.set("error", "UnknownError");
result.set("message", "Unknown exception occurred");
result.set("type", "unknown");
return result;
}
}
///////////////////////////////
// cpp objects ~> js objects //
///////////////////////////////
inline val convert_vector_int_to_js(const std::vector<int>& v)
{
val arr = val::array();
for (size_t i = 0; i < v.size(); ++i)
{
arr.set(i, v[i]);
}
return arr;
}
inline val convert_vec4_to_js(const vec4& v)
{
val js_vec4 = val::object();
js_vec4.set("l", v.l());
js_vec4.set("t", v.t());
js_vec4.set("y", v.y());
js_vec4.set("x", v.x());
return js_vec4;
}
inline val convert_vector_vec4_to_js(const std::vector<vec4>& vec)
{
val js_array = val::array();
for (size_t i = 0; i < vec.size(); ++i)
{
js_array.set(i, convert_vec4_to_js(vec[i]));
}
return js_array;
}
inline val convert_action_to_js(const action &act)
{
val js_act = val::array();
const std::vector<ext_move> mvs = act.get_moves();
for (size_t i = 0; i < mvs.size(); ++i)
{
val js_mv = val::object();
js_mv.set("from", convert_vec4_to_js(mvs[i].get_from()));
js_mv.set("to", convert_vec4_to_js(mvs[i].get_to()));
js_mv.set("promote", static_cast<int>(mvs[i].get_promote()));
js_act.set(i, js_mv);
}
return js_act;
}
///////////////////////////////
// js objects ~> cpp objects //
///////////////////////////////
inline vec4 convert_js_to_vec4(const val& js_vec4)
{
int l = js_vec4["l"].as<int>();
int t = js_vec4["t"].as<int>();
int y = js_vec4["y"].as<int>();
int x = js_vec4["x"].as<int>();
return vec4(x, y, t, l);
}
inline action convert_js_to_action(const val& js_action, const state& s)
{
std::vector<ext_move> mvs;
unsigned length = js_action["length"].as<unsigned>();
for (unsigned i = 0; i < length; ++i)
{
val js_mv = js_action[i];
vec4 from = convert_js_to_vec4(js_mv["from"]);
vec4 to = convert_js_to_vec4(js_mv["to"]);
piece_t promote_to = js_mv.hasOwnProperty("promote") ? static_cast<piece_t>(js_mv["promote"].as<int>()) : QUEEN_W;
mvs.emplace_back(from, to, promote_to);
}
return action::from_vector(mvs, s);
}
/////////////////////////
// emscripten bindings //
/////////////////////////
EMSCRIPTEN_BINDINGS(engine) {
// Factory function for creating games
function("from_pgn", &create_game_from_pgn);
// Class: game
class_<game>("game")
.smart_ptr<std::shared_ptr<game>>("game")
.property("metadata", &game::metadata)
.function("get_current_present", optional_override([](const game& self) {
const auto [t, c] = self.get_current_present();
val obj = val::object();
obj.set("t", t);
obj.set("c", c);
return obj;
}))
.function("get_cached_moves", optional_override([](const game &self) {
val result = val::array();
auto cached_moves = self.get_cached_moves();
for (size_t i = 0; i < cached_moves.size(); ++i)
{
const ext_move &m = cached_moves[i];
val move_info = val::object();
move_info.set("from", convert_vec4_to_js(m.get_from()));
move_info.set("to", convert_vec4_to_js(m.get_to()));
move_info.set("promote_to", static_cast<int>(m.get_promote()));
result.set(i, move_info);
}
return result;
}))
.function("get_current_boards", optional_override([](const game &self) {
val result = val::array();
auto boards = self.get_current_boards();
for (size_t i = 0; i < boards.size(); ++i)
{
const auto &[l, t, c, fen] = boards[i];
val board_info = val::object();
board_info.set("l", l);
board_info.set("t", t);
board_info.set("c", c);
board_info.set("fen", fen);
result.set(i, board_info);
}
return result;
}))
.function("get_phantom_boards_and_checks", optional_override([](const game &self) {
val result = val::object();
auto [boards, checks] = self.get_phantom_boards_and_checks();
val boards_array = val::array();
for (size_t i = 0; i < boards.size(); ++i)
{
const auto &[l, t, c, fen] = boards[i];
val board_info = val::object();
board_info.set("l", l);
board_info.set("t", t);
board_info.set("c", c);
board_info.set("fen", fen);
boards_array.set(i, board_info);
}
val checks_array = val::array();
for (size_t i = 0; i < checks.size(); ++i)
{
const auto &fm = checks[i];
val check_info = val::object();
check_info.set("from", convert_vec4_to_js(fm.from));
check_info.set("to", convert_vec4_to_js(fm.to));
checks_array.set(i, check_info);
}
result.set("boards", boards_array);
result.set("checks", checks_array);
return result;
}))
.function("get_current_timeline_status", optional_override([](const game& self) {
const auto& [mandatory, optional, unplayable] =
self.get_current_timeline_status();
val obj = val::object();
obj.set("mandatory_timelines", convert_vector_int_to_js(mandatory));
obj.set("optional_timelines", convert_vector_int_to_js(optional));
obj.set("unplayable_timelines", convert_vector_int_to_js(unplayable));
return obj;
}))
.function("gen_move_if_playable", optional_override([](const game& self, val obj) {
auto vec = self.gen_move_if_playable(convert_js_to_vec4(obj));
return convert_vector_vec4_to_js(vec);
}))
.function("get_match_status", optional_override([](const game& self) {
match_status_t status = self.get_match_status();
switch(status)
{
case match_status_t::PLAYING:
if(self.get_current_present().second)
return std::string("Black's Move");
else
return std::string("White's Move");
case match_status_t::WHITE_WINS:
return std::string("White Wins");
case match_status_t::BLACK_WINS:
return std::string("Black Wins");
case match_status_t::STALEMATE:
return std::string("Stalemate");
default:
return std::string("Unknown Status");
}
}))
.function("get_movable_pieces", optional_override([](const game& self) {
auto vec = self.get_movable_pieces();
return convert_vector_vec4_to_js(vec);
}))
.function("is_playable", optional_override([](const game& self, val obj) {
return self.is_playable(convert_js_to_vec4(obj));
}))
.function("can_undo", &game::can_undo)
.function("can_redo", &game::can_redo)
.function("can_submit", &game::can_submit)
.function("undo", &game::undo)
.function("redo", &game::redo)
.function("apply_move", optional_override([](game &g, val obj) {
piece_t pt = obj.hasOwnProperty("promote_to") ? static_cast<piece_t>(obj["promote_to"].as<int>()) : QUEEN_W;
ext_move m(
convert_js_to_vec4(obj["from"]),
convert_js_to_vec4(obj["to"]),
pt
);
return g.apply_move(m);
}))
.function("submit", &game::submit)
.function("currently_check", &game::currently_check)
.function("get_current_checks", optional_override([](const game &self) {
val result = val::array();
auto checks = self.get_current_checks();
for (size_t i = 0; i < checks.size(); ++i)
{
const auto& [from, to] = checks[i];
val check_info = val::object();
check_info.set("from", convert_vec4_to_js(from));
check_info.set("to", convert_vec4_to_js(to));
result.set(i, check_info);
}
return result;
}))
.function("get_board_size", optional_override([](const game &self) {
const auto& [x, y] = self.get_board_size();
val obj = val::object();
obj.set("x", x);
obj.set("y", y);
return obj;
}))
.function("suggest_action", &game::suggest_action)
.function("get_comments", optional_override([](const game &self) {
val result = val::array();
auto comments = self.get_comments();
for (size_t i = 0; i < comments.size(); ++i)
{
result.set(i, comments[i]);
}
return result;
}))
.function("set_comments", optional_override([](game &self, val js_comments) {
std::vector<std::string> comments;
unsigned length = js_comments["length"].as<unsigned>();
for (unsigned i = 0; i < length; ++i)
{
comments.push_back(js_comments[i].as<std::string>());
}
self.set_comments(comments);
}))
.function("has_parent", &game::has_parent)
.function("visit_parent", &game::visit_parent)
.function("get_child_actions", optional_override([](const game &self) {
val result = val::array();
auto child_actions = self.get_child_actions();
for (size_t i = 0; i < child_actions.size(); ++i)
{
const auto& [act, pgn] = child_actions[i];
val move_info = val::object();
move_info.set("action", convert_action_to_js(act));
move_info.set("pgn", pgn);
result.set(i, move_info);
}
return result;
}))
.function("get_historical_actions", optional_override([](const game &self) {
val result = val::array();
auto hist = self.get_historical_actions();
for (size_t i = 0; i < hist.size(); ++i)
{
const auto& [act, pgn] = hist[i];
val move_info = val::object();
move_info.set("action", convert_action_to_js(act));
move_info.set("pgn", pgn);
result.set(i, move_info);
}
return result;
}))
.function("visit_child", optional_override([](game &g, val js_action) {
action act = convert_js_to_action(js_action, g.get_unmoved_state());
return g.visit_child(act);
}))
.function("show_pgn", &game::show_pgn);
constant("SHOW_NOTHING", state::SHOW_NOTHING);
constant("SHOW_RELATIVE", state::SHOW_RELATIVE);
constant("SHOW_PAWN", state::SHOW_PAWN);
constant("SHOW_CAPTURE", state::SHOW_CAPTURE);
constant("SHOW_PROMOTION", state::SHOW_PROMOTION);
constant("SHOW_MATE", state::SHOW_MATE);
constant("SHOW_LCOMMENT", state::SHOW_LCOMMENT);
constant("SHOW_ALL", state::SHOW_ALL);
constant("SHOW_SHORT", state::SHOW_SHORT);
// Export version information
function("get_version", optional_override([]() {
#ifdef PROJECT_VERSION_STRING
return std::string(PROJECT_VERSION_STRING);
#else
return std::string("unknown");
#endif
}));
}