diff --git a/simulation b/simulation index 61c889b..092045a 160000 --- a/simulation +++ b/simulation @@ -1 +1 @@ -Subproject commit 61c889b0c697f522a66a7bfaf002cd143d018060 +Subproject commit 092045a2c26552b9494ac0e2a708a56a775904e8 diff --git a/src/app.h b/src/app.h index fa84d87..883edc0 100644 --- a/src/app.h +++ b/src/app.h @@ -22,6 +22,7 @@ struct App{ int lineuid{0}; struct OfflineSim { + std::vector lines_to_remove; std::vector lines_to_add; std::vector lines; std::vector balls; @@ -37,22 +38,37 @@ struct App{ App(){ online_sim.beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f}); - AbstractLine line(Vec2{50.0f, 100.0f}, Vec2{150.0f, 200.0f}, simulation::kOneWay); + AbstractLine line(Vec2{50.0f, 100.0f}, Vec2{150.0f, 200.0f}, simulation::kOneWay, lineuid++); add_line(line); } - void add_line(cheap_line_type const & line){ + void add_line(cheap_line_type line){ + line.ID = lineuid++; + std::cout << "adding line: " << line.ID << std::endl; if(uberclient){ offline_sim.lines_to_add.push_back(line); } else { add_line(AbstractLine{ to_FloatVec2(line.starting_point), to_FloatVec2(line.end_point), - line.line_kind}); + line.line_kind, + line.ID}); } lines_changed(); } + void remove_line(int ID){ + if(uberclient){ + offline_sim.lines_to_remove.push_back(ID); + } else { + auto& v = online_sim.sim.lines; + v.erase(std::remove_if(v.begin(), v.end(), [ID](line_type const &l){ return l.information == ID; }), v.end()); + + auto& v2 = online_sim.lines; + v2.erase(std::remove_if(v2.begin(), v2.end(), [ID](AbstractLine const &l){ return l.ID == ID; }), v2.end()); + } + } + void add_line(AbstractLine const & line){ online_sim.lines.push_back(line); for(auto & l : line.calculate_lines()){ diff --git a/src/app_common.h b/src/app_common.h index 2ee9c73..194c814 100644 --- a/src/app_common.h +++ b/src/app_common.h @@ -10,7 +10,7 @@ using Vec2 = math::Vec2; using LineKind = simulation::LineKind; using ball_info = int; -using line_info = void; +using line_info = int; using ball_type = simulation::Ball; using line_type = simulation::Line; using simu_type = simulation::Simulation; @@ -19,18 +19,24 @@ using note_info = Vec2; using note_type = Note; using beat_type = Beat; +// Pretty unuseful in server struct AbstractLine { math::Vec2 starting_point; math::Vec2 end_point; + + int ID; int line_kind; - const float hsize = 5.0f; + static constexpr float hsize = 5.0f; AbstractLine() = default; + AbstractLine(AbstractLine const&) = default; + AbstractLine& operator=(AbstractLine const&) = default; - AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, int line_kind) + AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, int line_kind, int ID) : starting_point(starting_point) , end_point(end_point) + , ID(ID) , line_kind(line_kind) {} @@ -42,16 +48,16 @@ struct AbstractLine { if(line_kind == simulation::kOneWay){ std::vector ret; - ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk); - ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk); - ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk); - ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk); - ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk); - ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk); + ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk, ID); + ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk, ID); + ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk, ID); + ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk, ID); + ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk, ID); + ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk, ID); return ret; } else { std::vector ret; - ret.emplace_back(starting_point, end_point, lk); + ret.emplace_back(starting_point, end_point, lk, ID); return ret; } } @@ -61,6 +67,7 @@ BOOST_FUSION_ADAPT_STRUCT( AbstractLine, (::math::Vec2, starting_point) (::math::Vec2, end_point) + (int, ID) (int, line_kind) ) @@ -94,12 +101,14 @@ BOOST_FUSION_ADAPT_STRUCT( struct cheap_line_type { IntVec2 starting_point; IntVec2 end_point; + int ID; int line_kind; cheap_line_type() = default; cheap_line_type(AbstractLine const & l) : starting_point(l.starting_point) , end_point(l.end_point) + , ID(l.ID) , line_kind(l.line_kind) {} }; @@ -108,5 +117,6 @@ BOOST_FUSION_ADAPT_STRUCT( cheap_line_type, (IntVec2, starting_point) (IntVec2, end_point) + (int, ID) (int, line_kind) ) diff --git a/src/main.cpp b/src/main.cpp index 8cb4934..964bfc2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,9 @@ websockets::TestProtocol default_protocol{ app->add_line(from_json(object["data"])); std::cout << "Line from: " << &user << ": " << user.name << std::endl; request_write(binfo); + } else if (command == "remove line"){ + app->remove_line(from_json(object["data"])); + request_write(binfo); } else if (command == "update user") { user = from_json(object["data"]); std::cout << "Updated user: " << &user << ": " << user.name << std::endl; @@ -80,11 +83,21 @@ websockets::TestProtocol uberclient_protocol{ }, // write (will always come after receive) [](Empty& user, basic_websocket_info) -> std::string{ - if(app->offline_sim.lines_to_add.empty()) return ""; - auto it = app->offline_sim.lines_to_add.begin(); - auto str = write_json(to_json(*it)); - app->offline_sim.lines_to_add.erase(it); - return str; + if(!app->offline_sim.lines_to_remove.empty()){ + js::Object ret; + ret["command"] = "remove lines"; + ret["data"] = vector_to_json(app->offline_sim.lines_to_remove); + app->offline_sim.lines_to_remove.clear(); + return write_json(ret); + } + if(!app->offline_sim.lines_to_add.empty()){ + js::Object ret; + ret["command"] = "add lines"; + ret["data"] = vector_to_json(app->offline_sim.lines_to_add); + app->offline_sim.lines_to_add.clear(); + return write_json(ret); + } + return ""; }, // receive [](Empty& user, std::string in, basic_websocket_info binfo){ @@ -105,7 +118,7 @@ websockets::TestProtocol uberclient_protocol{ } } - if(!app->offline_sim.lines_to_add.empty()){ + if(!app->offline_sim.lines_to_add.empty() || !app->offline_sim.lines_to_remove.empty()){ request_write(binfo); } }