Multi-user gravity beats for Sound of Science 2013 (server)
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 

129 lines
3.0 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;
bool peeps{true};
libwebsocket * uberclient{nullptr};
int peopleuid{0};
int balluid{0};
int lineuid{0};
struct OfflineSim {
std::vector<int> lines_to_remove;
std::vector<cheap_line_type> lines_to_add;
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, lineuid++);
add_line(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.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()){
online_sim.sim.lines.push_back(l);
}
}
void lines_changed() {
for(auto & u : people_online){
u->update_lines = true;
}
}
void login(User& user){
user.index = peopleuid++;
people_online.push_back(&user);
peeps = true;
}
void logout(User& user){
people_online.erase(std::remove(people_online.begin(), people_online.end(), &user), people_online.end());
peeps = true;
}
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();
lines_changed();
}
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();
}
};