diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-23 17:06:02 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-05-23 17:06:02 +0200 |
| commit | ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21 (patch) | |
| tree | 3946a99938c10b61f7822c715771c3529400cebc /web/adapter.cpp | |
| parent | c6a77f30f64be73a5e55e06336975f2ecfbb2324 (diff) | |
| download | nissy-core-ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21.tar.gz nissy-core-ecb04c6b7fbf5d06c2fdce5128e83575cac2bd21.zip | |
Web version (work in progress)
Diffstat (limited to 'web/adapter.cpp')
| -rw-r--r-- | web/adapter.cpp | 91 |
1 files changed, 80 insertions, 11 deletions
diff --git a/web/adapter.cpp b/web/adapter.cpp index 1774187..3613730 100644 --- a/web/adapter.cpp +++ b/web/adapter.cpp | |||
| @@ -1,11 +1,15 @@ | |||
| 1 | #include "../cpp/nissy.h" | 1 | #include "../cpp/nissy.h" |
| 2 | #include "storage.h" | ||
| 2 | 3 | ||
| 4 | #include <emscripten.h> | ||
| 3 | #include <emscripten/bind.h> | 5 | #include <emscripten/bind.h> |
| 4 | #include <map> | 6 | #include <map> |
| 5 | #include <set> | 7 | #include <set> |
| 6 | #include <string> | 8 | #include <string> |
| 7 | #include <vector> | 9 | #include <vector> |
| 8 | 10 | ||
| 11 | EM_ASYNC_JS(void, fake_async, (), {}); | ||
| 12 | |||
| 9 | extern "C" { | 13 | extern "C" { |
| 10 | extern int addCallbackFunction(/* args intentionally unspecified */); | 14 | extern int addCallbackFunction(/* args intentionally unspecified */); |
| 11 | extern void callFunction(int, const char *); | 15 | extern void callFunction(int, const char *); |
| @@ -47,26 +51,43 @@ const std::set<std::string> available_solvers | |||
| 47 | std::map<std::string, nissy::solver> loaded_solvers; | 51 | std::map<std::string, nissy::solver> loaded_solvers; |
| 48 | 52 | ||
| 49 | // TODO: this should ask the user if they want to download or generate. | 53 | // TODO: this should ask the user if they want to download or generate. |
| 50 | // TODO: this should also save the data to a file (IDBFS / NODEFS) | ||
| 51 | bool init_solver(const std::string& name) | 54 | bool init_solver(const std::string& name) |
| 52 | { | 55 | { |
| 53 | auto se = nissy::solver::get(name); | 56 | auto se = nissy::solver::get(name); |
| 54 | nissy::solver solver = std::get<nissy::solver>(se); | 57 | nissy::solver solver = std::get<nissy::solver>(se); |
| 55 | log("Generating data for solver " + solver.name + "\n"); | 58 | |
| 56 | auto err = solver.generate_data(); | 59 | solver.data.resize(solver.size); |
| 57 | if (!err.ok()) { | 60 | if (storage::read(solver.id, solver.size, |
| 58 | log("Error generating the data!\n"); | 61 | reinterpret_cast<char *>(solver.data.data()))) { |
| 59 | return false; | 62 | log("Data for solver " + solver.name + " read from storage\n"); |
| 63 | } else { | ||
| 64 | log("Could not read data for solver " + solver.name + | ||
| 65 | " from storage, generating it\n"); | ||
| 66 | auto err = solver.generate_data(); | ||
| 67 | |||
| 68 | if (!err.ok()) { | ||
| 69 | log("Error generating the data!\n"); | ||
| 70 | return false; | ||
| 71 | } | ||
| 60 | } | 72 | } |
| 73 | |||
| 61 | log("Checking data integrity " | 74 | log("Checking data integrity " |
| 62 | "(this is done only once per session per solver)...\n"); | 75 | "(this is done only once per session per solver)...\n"); |
| 63 | if (!solver.check_data().ok()) { | 76 | if (!solver.check_data().ok()) { |
| 64 | log("Error generating the data!\n"); | 77 | log("Error! Data is corrupted!\n"); |
| 65 | return false; | 78 | return false; |
| 66 | } | 79 | } |
| 67 | loaded_solvers.insert({name, solver}); | 80 | loaded_solvers.insert({name, solver}); |
| 68 | log("Data generated successfully, but not saved " | 81 | |
| 69 | "(feature not yet available)\n"); | 82 | if (storage::write(solver.id, solver.size, |
| 83 | reinterpret_cast<const char *>(solver.data.data()))) { | ||
| 84 | log("Data for solver " + solver.name + " stored\n"); | ||
| 85 | } else { | ||
| 86 | log("Error storing the data (the solver is usable, " | ||
| 87 | "but the data will have to be re-generated next " | ||
| 88 | "time you want to use it)"); | ||
| 89 | } | ||
| 90 | |||
| 70 | return true; | 91 | return true; |
| 71 | } | 92 | } |
| 72 | 93 | ||
| @@ -87,17 +108,31 @@ bool solver_valid(const std::string& name) | |||
| 87 | 108 | ||
| 88 | int poll_status(void *arg) | 109 | int poll_status(void *arg) |
| 89 | { | 110 | { |
| 111 | return nissy::status::RUN.value; | ||
| 112 | /* | ||
| 113 | TODO: reintroduce poll status | ||
| 90 | int id = *(int *)arg; | 114 | int id = *(int *)arg; |
| 91 | if (id == -1) | 115 | if (id == -1) |
| 92 | return nissy::status::RUN.value; | 116 | return nissy::status::RUN.value; |
| 93 | return callFunctionInt(id); | 117 | return callFunctionInt(id); |
| 118 | */ | ||
| 94 | } | 119 | } |
| 95 | 120 | ||
| 121 | #if 0 | ||
| 122 | |||
| 96 | nissy::solver::solve_result solve(std::string name, | 123 | nissy::solver::solve_result solve(std::string name, |
| 97 | nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, | 124 | nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, |
| 98 | unsigned maxmoves, unsigned maxsols, unsigned optimal, unsigned threads, | 125 | unsigned maxmoves, unsigned maxsols, unsigned optimal, unsigned threads, |
| 99 | int poll_status_id) | 126 | int poll_status_id) |
| 100 | { | 127 | { |
| 128 | // Here we use a dirty trick to make this function always return the | ||
| 129 | // same kind of JavaScript object. If we did not do this, the returned | ||
| 130 | // object would be a Promise on the first run of the solver for each | ||
| 131 | // session (because when loading the table some async JS code is | ||
| 132 | // called), and a regular object otherwise. | ||
| 133 | // TODO figure out if there is a better way to do this. | ||
| 134 | fake_async(); | ||
| 135 | |||
| 101 | if (!solver_valid(name)) | 136 | if (!solver_valid(name)) |
| 102 | return nissy::solver::solve_result | 137 | return nissy::solver::solve_result |
| 103 | {.err = nissy::error::INVALID_SOLVER}; | 138 | {.err = nissy::error::INVALID_SOLVER}; |
| @@ -106,6 +141,35 @@ nissy::solver::solve_result solve(std::string name, | |||
| 106 | maxmoves, maxsols, optimal, threads, NULL, &poll_status_id); | 141 | maxmoves, maxsols, optimal, threads, NULL, &poll_status_id); |
| 107 | } | 142 | } |
| 108 | 143 | ||
| 144 | #else | ||
| 145 | |||
| 146 | std::string solve(std::string name, | ||
| 147 | nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, | ||
| 148 | unsigned maxmoves, unsigned maxsols, unsigned optimal, unsigned threads, | ||
| 149 | int poll_status_id) | ||
| 150 | { | ||
| 151 | // Here we use a dirty trick to make this function always return the | ||
| 152 | // same kind of JavaScript object. If we did not do this, the returned | ||
| 153 | // object would be a Promise on the first run of the solver for each | ||
| 154 | // session (because when loading the table some async JS code is | ||
| 155 | // called), and a regular object otherwise. | ||
| 156 | // TODO figure out if there is a better way to do this. | ||
| 157 | fake_async(); | ||
| 158 | |||
| 159 | if (!solver_valid(name)) | ||
| 160 | return ""; | ||
| 161 | /* | ||
| 162 | return nissy::solver::solve_result | ||
| 163 | {.err = nissy::error::INVALID_SOLVER}; | ||
| 164 | */ | ||
| 165 | |||
| 166 | return loaded_solvers.at(name).solve(cube, nissflag, minmoves, | ||
| 167 | maxmoves, maxsols, optimal, threads, NULL, &poll_status_id) | ||
| 168 | .solutions[0]; | ||
| 169 | } | ||
| 170 | |||
| 171 | #endif | ||
| 172 | |||
| 109 | EMSCRIPTEN_BINDINGS(Nissy) | 173 | EMSCRIPTEN_BINDINGS(Nissy) |
| 110 | { | 174 | { |
| 111 | emscripten::class_<nissy::nissflag>("NissFlag") | 175 | emscripten::class_<nissy::nissflag>("NissFlag") |
| @@ -134,13 +198,11 @@ EMSCRIPTEN_BINDINGS(Nissy) | |||
| 134 | emscripten::constant("statusRUN", nissy::status::RUN.value); | 198 | emscripten::constant("statusRUN", nissy::status::RUN.value); |
| 135 | emscripten::constant("statusSTOP", nissy::status::STOP.value); | 199 | emscripten::constant("statusSTOP", nissy::status::STOP.value); |
| 136 | emscripten::constant("statusPAUSE", nissy::status::PAUSE.value); | 200 | emscripten::constant("statusPAUSE", nissy::status::PAUSE.value); |
| 137 | /* | ||
| 138 | emscripten::class_<nissy::status>("Status") | 201 | emscripten::class_<nissy::status>("Status") |
| 139 | .class_property("run", &nissy::status::RUN) | 202 | .class_property("run", &nissy::status::RUN) |
| 140 | .class_property("stop", &nissy::status::STOP) | 203 | .class_property("stop", &nissy::status::STOP) |
| 141 | .class_property("pause", &nissy::status::PAUSE) | 204 | .class_property("pause", &nissy::status::PAUSE) |
| 142 | ; | 205 | ; |
| 143 | */ | ||
| 144 | 206 | ||
| 145 | emscripten::class_<nissy::cube>("Cube") | 207 | emscripten::class_<nissy::cube>("Cube") |
| 146 | .constructor<>() | 208 | .constructor<>() |
| @@ -150,10 +212,17 @@ EMSCRIPTEN_BINDINGS(Nissy) | |||
| 150 | .function("toString", &nissy::cube::to_string) | 212 | .function("toString", &nissy::cube::to_string) |
| 151 | ; | 213 | ; |
| 152 | 214 | ||
| 215 | emscripten::register_vector<std::string>("StringVector"); | ||
| 216 | emscripten::value_array<nissy::solver::solve_result>("SolveResult") | ||
| 217 | .element(&nissy::solver::solve_result::err) | ||
| 218 | .element(&nissy::solver::solve_result::solutions) | ||
| 219 | ; | ||
| 220 | /* | ||
| 153 | emscripten::class_<nissy::solver::solve_result>("SolveResult") | 221 | emscripten::class_<nissy::solver::solve_result>("SolveResult") |
| 154 | .property("err", &nissy::solver::solve_result::err) | 222 | .property("err", &nissy::solver::solve_result::err) |
| 155 | .property("solutions", &nissy::solver::solve_result::solutions) | 223 | .property("solutions", &nissy::solver::solve_result::solutions) |
| 156 | ; | 224 | ; |
| 225 | */ | ||
| 157 | 226 | ||
| 158 | emscripten::function("countMoves", &nissy::count_moves); | 227 | emscripten::function("countMoves", &nissy::count_moves); |
| 159 | emscripten::function("solve", &solve, | 228 | emscripten::function("solve", &solve, |
