forked from Borf/NetworkEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_route.cpp
167 lines (141 loc) · 4.23 KB
/
api_route.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "api.h"
#include "Route.h"
#include <algorithm>
Api route_add("route/add", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
Route* r = new Route();
if (data.find("id") != data.end())
{
printf("Got forced route id!\n");
r->id = data["id"].get<std::string>();
}
for (size_t i = 0; i < data["nodes"].size(); i++)
{
r->addNode(glm::vec3(data["nodes"][i]["pos"][0], data["nodes"][i]["pos"][1], data["nodes"][i]["pos"][2]),
glm::vec3(data["nodes"][i]["dir"][0], data["nodes"][i]["dir"][1], data["nodes"][i]["dir"][2]));
}
r->finish();
engine->routes.push_back(r);
data["id"] = r->id;
json packet;
packet["id"] = "route/add";
packet["data"]["status"] = "ok";
packet["data"]["uuid"] = r->id;
tunnel->send(packet);
});
Api route_update("route/update", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
});
Api route_delete("route/delete", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
for (size_t i = 0; i < engine->routeFollowers.size(); i++)
{
if (engine->routeFollowers[i].route->id == data["id"])
{
json packet;
packet["id"] = "route/delete";
packet["data"]["status"] = "error";
packet["data"]["error"] = "Route is still in use";
tunnel->send(packet);
return;
}
}
for (size_t i = 0; i <engine->routes.size(); i++)
{
if (engine->routes[i]->id == data["id"])
{
engine->routes.erase(engine->routes.begin() + i);
json packet;
packet["id"] = "route/delete";
packet["data"]["status"] = "ok";
tunnel->send(packet);
return;
}
}
json packet;
packet["id"] = "route/delete";
packet["data"]["status"] = "error";
packet["data"]["error"] = "Route not found";
tunnel->send(packet);
});
Api route_follow("route/follow", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, json &data)
{
json packet;
packet["id"] = "route/follow";
packet["data"]["status"] = "error";
bool found = false;
for (size_t i = 0; i <engine->routes.size(); i++)
{
if (engine->routes[i]->id == data["route"])
{
found = true;
vrlib::tien::Node* n = engine->tien.scene.findNodeWithGuid(data["node"]);
if (!n)
{
packet["data"]["error"] = "node not found";
break;
}
for (size_t ii = 0; ii < engine->routeFollowers.size(); ii++)
{
auto & f = engine->routeFollowers[ii];
if (f.node == n)
{
engine->routeFollowers.erase(engine->routeFollowers.begin() + ii);
packet["data"]["status"] = "ok";
packet["data"]["msg"] = "removed";
tunnel->send(packet);
return;
}
}
RouteFollower f;
f.node = n;
f.route = engine->routes[i];
f.speed = data["speed"];
f.offset = data.find("offset") != data.end() ? data["offset"] : 0.0f;
f.followHeight = false;
if(data.find("followHeight") != data.end())
f.followHeight = data["followHeight"];
f.rotate = RouteFollower::Rotate::NONE;
if (data.find("rotate") != data.end())
{
if (data["rotate"] == "XZ")
f.rotate = RouteFollower::Rotate::XZ;
else if (data["rotate"] == "XYZ")
f.rotate = RouteFollower::Rotate::XYZ;
}
if (data.find("rotateOffset") != data.end())
f.rotateOffset = glm::quat(glm::vec3(data["rotateOffset"][0], data["rotateOffset"][1], data["rotateOffset"][2]));
if (data.find("positionOffset") != data.end())
f.positionOffset = glm::vec3(data["positionOffset"][0], data["positionOffset"][1], data["positionOffset"][2]);
engine->routeFollowers.push_back(f);
packet["data"]["status"] = "ok";
}
}
if(!found && packet["data"].find("error") == packet["data"].end())
packet["data"]["error"] = "Route not found";
tunnel->send(packet);
});
Api route_follow_speed("route/follow/speed", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, const json &data)
{
for (auto &f : engine->routeFollowers)
{
if (f.node->guid == data["node"])
{
f.speed = data["speed"];
sendOk(tunnel, "route/follow/speed");
return;
}
}
sendError(tunnel, "route/follow/speed", "Node ID not found");
});
Api route_show("route/show", [](NetworkEngine* engine, vrlib::Tunnel* tunnel, const json &data)
{
if (data.find("show") == data.end())
engine->showRoutes = !engine->showRoutes;
else
engine->showRoutes = data["show"];
json ret;
ret["id"] = "route/show";
ret["data"]["status"] = "ok";
ret["data"]["show"] = engine->showRoutes;
});