You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.3 KiB
103 lines
2.3 KiB
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
|
|
#include <lib/libwebsockets.h>
|
|
#include "websockets.h"
|
|
|
|
#include "app_common.h"
|
|
|
|
using namespace websockets;
|
|
|
|
struct App{
|
|
std::vector<User*> people_online;
|
|
libwebsocket * uberclient{nullptr};
|
|
int peopleuid{0};
|
|
int balluid{0};
|
|
int lineuid{0};
|
|
|
|
struct OfflineSim {
|
|
std::vector<cheap_line_type> lines;
|
|
std::vector<cheap_ball_type> balls;
|
|
} offline_sim;
|
|
|
|
struct OnlineSim {
|
|
const float iframerate = 1.0/60.0;
|
|
std::vector<AbstractLine> lines;
|
|
simu_type sim;
|
|
beat_type beat;
|
|
} online_sim;
|
|
|
|
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);
|
|
add_line(line);
|
|
}
|
|
|
|
void add_line(cheap_line_type const & line){
|
|
if(uberclient){
|
|
// write
|
|
} else {
|
|
add_line(AbstractLine{
|
|
to_FloatVec2(line.starting_point),
|
|
to_FloatVec2(line.end_point),
|
|
line.line_kind});
|
|
}
|
|
}
|
|
|
|
void add_line(AbstractLine const & line){
|
|
online_sim.lines.push_back(line);
|
|
for(auto & l : line.calculate_lines()){
|
|
online_sim.sim.lines.push_back(l);
|
|
}
|
|
for(auto & u : people_online){
|
|
u->update_lines = true;
|
|
}
|
|
}
|
|
|
|
void login(User& user){
|
|
user.index = peopleuid++;
|
|
people_online.push_back(&user);
|
|
}
|
|
|
|
void logout(User& user){
|
|
people_online.erase(std::remove(people_online.begin(), people_online.end(), &user), people_online.end());
|
|
}
|
|
|
|
void update(){
|
|
if(!uberclient){
|
|
float dt = online_sim.iframerate;
|
|
online_sim.sim.update(dt);
|
|
|
|
for(auto n : online_sim.beat.update(dt)){
|
|
online_sim.sim.balls.emplace_back(n.x, n.y, 0, 0, balluid++);
|
|
}
|
|
}
|
|
}
|
|
|
|
void uberclient_connected(basic_websocket_info binfo){
|
|
if(uberclient) throw websockets::runtime_error("Warning: another uberclient connection\n");
|
|
uberclient = binfo.wsi;
|
|
|
|
online_sim.lines.clear();
|
|
online_sim.sim.lines.clear();
|
|
online_sim.sim.balls.clear();
|
|
}
|
|
|
|
void uberclient_disconnected(basic_websocket_info binfo){
|
|
if(uberclient != binfo.wsi) throw websockets::runtime_error("Warning: non-uberclient disconnected");
|
|
uberclient = nullptr;
|
|
|
|
for(auto l : offline_sim.lines){
|
|
add_line(l);
|
|
}
|
|
offline_sim.lines.clear();
|
|
offline_sim.balls.clear();
|
|
}
|
|
};
|