From 2eddac0349191b7269ec2f438396ed3fd078a757 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Wed, 17 Apr 2013 10:28:01 +0200 Subject: [PATCH] Lines are now communicated to uberclient --- src/app.h | 9 ++++++++- src/main.cpp | 14 +++++++++++--- src/websockets.h | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/app.h b/src/app.h index ea52b14..fa84d87 100644 --- a/src/app.h +++ b/src/app.h @@ -22,6 +22,7 @@ struct App{ int lineuid{0}; struct OfflineSim { + std::vector lines_to_add; std::vector lines; std::vector balls; } offline_sim; @@ -42,13 +43,14 @@ struct App{ void add_line(cheap_line_type const & line){ if(uberclient){ - // write + 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}); } + lines_changed(); } void add_line(AbstractLine const & line){ @@ -56,6 +58,9 @@ struct App{ for(auto & l : line.calculate_lines()){ online_sim.sim.lines.push_back(l); } + } + + void lines_changed() { for(auto & u : people_online){ u->update_lines = true; } @@ -88,6 +93,8 @@ struct App{ online_sim.lines.clear(); online_sim.sim.lines.clear(); online_sim.sim.balls.clear(); + + lines_changed(); } void uberclient_disconnected(basic_websocket_info binfo){ diff --git a/src/main.cpp b/src/main.cpp index 8c73c51..8cb4934 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,11 +80,14 @@ websockets::TestProtocol uberclient_protocol{ }, // write (will always come after receive) [](Empty& user, basic_websocket_info) -> std::string{ - static int i = 0; - return std::to_string(i++); + 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; }, // receive - [](Empty& user, std::string in, basic_websocket_info){ + [](Empty& user, std::string in, basic_websocket_info binfo){ auto object = parse_json(in).getObject(); if(object.count("lines")){ app->offline_sim.lines.clear(); @@ -92,6 +95,7 @@ websockets::TestProtocol uberclient_protocol{ for(auto&& js : lines.getArray()){ app->offline_sim.lines.push_back(from_json(js)); } + app->lines_changed(); } if(object.count("balls")){ app->offline_sim.balls.clear(); @@ -100,6 +104,10 @@ websockets::TestProtocol uberclient_protocol{ app->offline_sim.balls.push_back(from_json(js)); } } + + if(!app->offline_sim.lines_to_add.empty()){ + request_write(binfo); + } } }; diff --git a/src/websockets.h b/src/websockets.h index 3cd81de..ad39ca8 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -87,7 +87,7 @@ namespace websockets { } virtual int call(libwebsocket_context& context, libwebsocket& wsi, libwebsocket_callback_reasons reason, T& user, void *in, size_t len){ return 0; - }; + } }; struct basic_websocket_info{