Browse Source

Lines are now communicated to uberclient

master
Joshua Moerman 11 years ago
parent
commit
2eddac0349
  1. 9
      src/app.h
  2. 14
      src/main.cpp
  3. 2
      src/websockets.h

9
src/app.h

@ -22,6 +22,7 @@ struct App{
int lineuid{0}; int lineuid{0};
struct OfflineSim { struct OfflineSim {
std::vector<cheap_line_type> lines_to_add;
std::vector<cheap_line_type> lines; std::vector<cheap_line_type> lines;
std::vector<cheap_ball_type> balls; std::vector<cheap_ball_type> balls;
} offline_sim; } offline_sim;
@ -42,13 +43,14 @@ struct App{
void add_line(cheap_line_type const & line){ void add_line(cheap_line_type const & line){
if(uberclient){ if(uberclient){
// write offline_sim.lines_to_add.push_back(line);
} else { } else {
add_line(AbstractLine{ add_line(AbstractLine{
to_FloatVec2(line.starting_point), to_FloatVec2(line.starting_point),
to_FloatVec2(line.end_point), to_FloatVec2(line.end_point),
line.line_kind}); line.line_kind});
} }
lines_changed();
} }
void add_line(AbstractLine const & line){ void add_line(AbstractLine const & line){
@ -56,6 +58,9 @@ struct App{
for(auto & l : line.calculate_lines()){ for(auto & l : line.calculate_lines()){
online_sim.sim.lines.push_back(l); online_sim.sim.lines.push_back(l);
} }
}
void lines_changed() {
for(auto & u : people_online){ for(auto & u : people_online){
u->update_lines = true; u->update_lines = true;
} }
@ -88,6 +93,8 @@ struct App{
online_sim.lines.clear(); online_sim.lines.clear();
online_sim.sim.lines.clear(); online_sim.sim.lines.clear();
online_sim.sim.balls.clear(); online_sim.sim.balls.clear();
lines_changed();
} }
void uberclient_disconnected(basic_websocket_info binfo){ void uberclient_disconnected(basic_websocket_info binfo){

14
src/main.cpp

@ -80,11 +80,14 @@ websockets::TestProtocol<Empty> uberclient_protocol{
}, },
// write (will always come after receive) // write (will always come after receive)
[](Empty& user, basic_websocket_info) -> std::string{ [](Empty& user, basic_websocket_info) -> std::string{
static int i = 0; if(app->offline_sim.lines_to_add.empty()) return "";
return std::to_string(i++); 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 // receive
[](Empty& user, std::string in, basic_websocket_info){ [](Empty& user, std::string in, basic_websocket_info binfo){
auto object = parse_json(in).getObject(); auto object = parse_json(in).getObject();
if(object.count("lines")){ if(object.count("lines")){
app->offline_sim.lines.clear(); app->offline_sim.lines.clear();
@ -92,6 +95,7 @@ websockets::TestProtocol<Empty> uberclient_protocol{
for(auto&& js : lines.getArray()){ for(auto&& js : lines.getArray()){
app->offline_sim.lines.push_back(from_json<cheap_line_type>(js)); app->offline_sim.lines.push_back(from_json<cheap_line_type>(js));
} }
app->lines_changed();
} }
if(object.count("balls")){ if(object.count("balls")){
app->offline_sim.balls.clear(); app->offline_sim.balls.clear();
@ -100,6 +104,10 @@ websockets::TestProtocol<Empty> uberclient_protocol{
app->offline_sim.balls.push_back(from_json<cheap_ball_type>(js)); app->offline_sim.balls.push_back(from_json<cheap_ball_type>(js));
} }
} }
if(!app->offline_sim.lines_to_add.empty()){
request_write(binfo);
}
} }
}; };

2
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){ virtual int call(libwebsocket_context& context, libwebsocket& wsi, libwebsocket_callback_reasons reason, T& user, void *in, size_t len){
return 0; return 0;
}; }
}; };
struct basic_websocket_info{ struct basic_websocket_info{