mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Reorganized client source code
Removed many useless and old classes Started transition to new CSS Added URIs for specific REST requests
This commit is contained in:
@@ -16,7 +16,7 @@ public:
|
||||
|
||||
void updateExportData(json::value json);
|
||||
void updateMissionData(json::value json);
|
||||
json::value json();
|
||||
json::value json(bool fullRefresh);
|
||||
|
||||
virtual void setState(int newState) { state = newState; };
|
||||
void resetTask();
|
||||
|
||||
@@ -18,7 +18,6 @@ public:
|
||||
|
||||
private:
|
||||
std::thread* serverThread;
|
||||
json::value answer;
|
||||
|
||||
void handle_options(http_request request);
|
||||
void handle_get(http_request request);
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
Unit* getUnit(int ID);
|
||||
void updateExportData(lua_State* L);
|
||||
void updateMissionData(json::value missionData);
|
||||
void updateAnswer(json::value& answer);
|
||||
void updateAnswer(json::value& answer, bool fullRefresh);
|
||||
void deleteUnit(int ID);
|
||||
|
||||
private:
|
||||
|
||||
@@ -34,7 +34,6 @@ void Unit::updateExportData(json::value json)
|
||||
}
|
||||
oldPosition = Coords(latitude, longitude, altitude);
|
||||
|
||||
/* Update all the internal fields from the input json file */
|
||||
if (json.has_string_field(L"Name"))
|
||||
name = json[L"Name"].as_string();
|
||||
if (json.has_string_field(L"UnitName"))
|
||||
@@ -80,17 +79,71 @@ void Unit::updateMissionData(json::value json)
|
||||
hasTask = json[L"hasTask"].as_bool();
|
||||
}
|
||||
|
||||
json::value Unit::json()
|
||||
json::value Unit::json(bool fullRefresh)
|
||||
{
|
||||
auto json = json::value::object();
|
||||
|
||||
/********** Base data **********/
|
||||
json[L"AI"] = AI;
|
||||
json[L"name"] = json::value::string(name);
|
||||
json[L"unitName"] = json::value::string(unitName);
|
||||
json[L"groupName"] = json::value::string(groupName);
|
||||
json[L"alive"] = alive;
|
||||
json[L"category"] = json::value::string(getCategory());
|
||||
if (fullRefresh)
|
||||
{
|
||||
/********** Base data **********/
|
||||
json[L"AI"] = AI;
|
||||
json[L"name"] = json::value::string(name);
|
||||
json[L"unitName"] = json::value::string(unitName);
|
||||
json[L"groupName"] = json::value::string(groupName);
|
||||
json[L"alive"] = alive;
|
||||
json[L"category"] = json::value::string(getCategory());
|
||||
|
||||
/********** Mission data **********/
|
||||
json[L"missionData"] = json::value::object();
|
||||
json[L"missionData"][L"fuel"] = fuel;
|
||||
json[L"missionData"][L"ammo"] = ammo;
|
||||
json[L"missionData"][L"targets"] = targets;
|
||||
json[L"missionData"][L"hasTask"] = hasTask;
|
||||
if (coalitionID == 0)
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"neutral");
|
||||
else if (coalitionID == 1)
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"red");
|
||||
else
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"blue");
|
||||
json[L"missionData"][L"flags"] = flags;
|
||||
|
||||
/********** Formation data **********/
|
||||
json[L"formationData"] = json::value::object();
|
||||
json[L"formationData"][L"isLeader"] = isLeader;
|
||||
json[L"formationData"][L"isWingman"] = isWingman;
|
||||
json[L"formationData"][L"formation"] = json::value::string(formation);
|
||||
int i = 0;
|
||||
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)
|
||||
json[L"formationData"][L"wingmenIDs"][i++] = (*itr)->getID();
|
||||
|
||||
if (leader != nullptr)
|
||||
json[L"formationData"][L"leaderID"] = leader->getID();
|
||||
|
||||
/********** Task data **********/
|
||||
json[L"taskData"] = json::value::object();
|
||||
json[L"taskData"][L"currentTask"] = json::value::string(getCurrentTask());
|
||||
json[L"taskData"][L"targetSpeed"] = getTargetSpeed();
|
||||
json[L"taskData"][L"targetAltitude"] = getTargetAltitude();
|
||||
/* Send the active path as a json object */
|
||||
auto path = json::value::object();
|
||||
if (activePath.size() > 0) {
|
||||
int count = 1;
|
||||
for (auto& destination : activePath)
|
||||
{
|
||||
auto json = json::value::object();
|
||||
json[L"lat"] = destination.lat;
|
||||
json[L"lng"] = destination.lng;
|
||||
json[L"alt"] = destination.alt;
|
||||
path[to_wstring(count++)] = json;
|
||||
}
|
||||
}
|
||||
json[L"taskData"][L"activePath"] = path;
|
||||
|
||||
/********** Options data **********/
|
||||
json[L"optionsData"] = json::value::object();
|
||||
json[L"optionsData"][L"ROE"] = json::value::string(ROE);
|
||||
json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat);
|
||||
}
|
||||
|
||||
/********** Flight data **********/
|
||||
json[L"flightData"] = json::value::object();
|
||||
@@ -100,57 +153,6 @@ json::value Unit::json()
|
||||
json[L"flightData"][L"speed"] = speed;
|
||||
json[L"flightData"][L"heading"] = heading;
|
||||
|
||||
/********** Mission data **********/
|
||||
json[L"missionData"] = json::value::object();
|
||||
json[L"missionData"][L"fuel"] = fuel;
|
||||
json[L"missionData"][L"ammo"] = ammo;
|
||||
json[L"missionData"][L"targets"] = targets;
|
||||
json[L"missionData"][L"hasTask"] = hasTask;
|
||||
if (coalitionID == 0)
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"neutral");
|
||||
else if (coalitionID == 1)
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"red");
|
||||
else
|
||||
json[L"missionData"][L"coalition"] = json::value::string(L"blue");
|
||||
json[L"missionData"][L"flags"] = flags;
|
||||
|
||||
/********** Formation data **********/
|
||||
json[L"formationData"] = json::value::object();
|
||||
json[L"formationData"][L"isLeader"] = isLeader;
|
||||
json[L"formationData"][L"isWingman"] = isWingman;
|
||||
json[L"formationData"][L"formation"] = json::value::string(formation);
|
||||
int i = 0;
|
||||
for (auto itr = wingmen.begin(); itr != wingmen.end(); itr++)
|
||||
json[L"formationData"][L"wingmenIDs"][i++] = (*itr)->getID();
|
||||
|
||||
if (leader != nullptr)
|
||||
json[L"formationData"][L"leaderID"] = leader->getID();
|
||||
|
||||
/********** Task data **********/
|
||||
json[L"taskData"] = json::value::object();
|
||||
json[L"taskData"][L"currentTask"] = json::value::string(getCurrentTask());
|
||||
json[L"taskData"][L"targetSpeed"] = getTargetSpeed();
|
||||
json[L"taskData"][L"targetAltitude"] = getTargetAltitude();
|
||||
/* Send the active path as a json object */
|
||||
auto path = json::value::object();
|
||||
if (activePath.size() > 0) {
|
||||
int count = 1;
|
||||
for (auto& destination : activePath)
|
||||
{
|
||||
auto json = json::value::object();
|
||||
json[L"lat"] = destination.lat;
|
||||
json[L"lng"] = destination.lng;
|
||||
json[L"alt"] = destination.alt;
|
||||
path[to_wstring(count++)] = json;
|
||||
}
|
||||
}
|
||||
json[L"taskData"][L"activePath"] = path;
|
||||
|
||||
/********** Options data **********/
|
||||
json[L"optionsData"] = json::value::object();
|
||||
json[L"optionsData"][L"ROE"] = json::value::string(ROE);
|
||||
json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ Server::Server(lua_State* L):
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Server::start(lua_State* L)
|
||||
{
|
||||
log("Starting RESTServer");
|
||||
@@ -71,15 +70,28 @@ void Server::handle_get(http_request request)
|
||||
|
||||
std::exception_ptr eptr;
|
||||
try {
|
||||
unitsManager->updateAnswer(answer);
|
||||
auto answer = json::value::object();
|
||||
wstring requestUri = request.request_uri().to_string();
|
||||
log(requestUri);
|
||||
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(PARTIAL_REFRESH_URI)) == 0)
|
||||
unitsManager->updateAnswer(answer, false);
|
||||
|
||||
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(FULL_REFRESH_URI)) == 0)
|
||||
unitsManager->updateAnswer(answer, true);
|
||||
|
||||
/* Get the logs from the logger */
|
||||
auto logs = json::value::object();
|
||||
getLogsJSON(logs); // By reference, for thread safety
|
||||
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(LOGS_URI)) == 0)
|
||||
{
|
||||
auto logs = json::value::object();
|
||||
getLogsJSON(logs); // By reference, for thread safety
|
||||
answer[L"logs"] = logs;
|
||||
}
|
||||
|
||||
answer[L"airbases"] = airbasesData;
|
||||
answer[L"bullseye"] = bullseyeData;
|
||||
answer[L"logs"] = logs;
|
||||
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(AIRBASES_URI)) == 0)
|
||||
answer[L"airbases"] = airbasesData;
|
||||
|
||||
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(BULLSEYE_URI)) == 0)
|
||||
answer[L"bullseye"] = bullseyeData;
|
||||
|
||||
response.set_body(answer);
|
||||
}
|
||||
@@ -94,7 +106,6 @@ void Server::handle_get(http_request request)
|
||||
void Server::handle_request(http_request request, function<void(json::value const&, json::value&)> action)
|
||||
{
|
||||
auto answer = json::value::object();
|
||||
|
||||
request.extract_json().then([&answer, &action](pplx::task<json::value> task)
|
||||
{
|
||||
try
|
||||
@@ -148,7 +159,7 @@ void Server::handle_put(http_request request)
|
||||
|
||||
void Server::task()
|
||||
{
|
||||
http_listener listener(REST_ADDRESS);
|
||||
http_listener listener(wstring(REST_ADDRESS) + L"/" + wstring(REST_URI));
|
||||
|
||||
std::function<void(http_request)> handle_options = std::bind(&Server::handle_options, this, std::placeholders::_1);
|
||||
std::function<void(http_request)> handle_get = std::bind(&Server::handle_get, this, std::placeholders::_1);
|
||||
|
||||
@@ -48,32 +48,20 @@ void UnitsManager::updateExportData(lua_State* L)
|
||||
if (type[L"level1"].as_number().to_int32() == 1)
|
||||
{
|
||||
if (type[L"level2"].as_number().to_int32() == 1)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new Aircraft(p.second, ID));
|
||||
}
|
||||
else if (type[L"level2"].as_number().to_int32() == 2)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new Helicopter(p.second, ID));
|
||||
}
|
||||
}
|
||||
else if (type[L"level1"].as_number().to_int32() == 2)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new GroundUnit(p.second, ID));
|
||||
}
|
||||
else if (type[L"level1"].as_number().to_int32() == 3)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new NavyUnit(p.second, ID));
|
||||
}
|
||||
else if (type[L"level1"].as_number().to_int32() == 4)
|
||||
{
|
||||
if (type[L"level2"].as_number().to_int32() == 4)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new Missile(p.second, ID));
|
||||
}
|
||||
else if (type[L"level2"].as_number().to_int32() == 5)
|
||||
{
|
||||
units[ID] = dynamic_cast<Unit*>(new Bomb(p.second, ID));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,10 +75,7 @@ void UnitsManager::updateExportData(lua_State* L)
|
||||
/* Set the units that are not present in the JSON as dead (probably have been destroyed) */
|
||||
for (auto const& unit : units)
|
||||
{
|
||||
if (unitJSONs.find(unit.first) == unitJSONs.end())
|
||||
{
|
||||
unit.second->setAlive(false);
|
||||
}
|
||||
unit.second->setAlive(unitJSONs.find(unit.first) != unitJSONs.end());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,16 +92,13 @@ void UnitsManager::updateMissionData(json::value missionData)
|
||||
}
|
||||
}
|
||||
|
||||
void UnitsManager::updateAnswer(json::value& answer)
|
||||
void UnitsManager::updateAnswer(json::value& answer, bool fullRefresh)
|
||||
{
|
||||
// TODO THREAT SAFEY!
|
||||
auto unitsJson = json::value::object();
|
||||
|
||||
for (auto const& p : units)
|
||||
{
|
||||
unitsJson[to_wstring(p.first)] = p.second->json();
|
||||
unitsJson[to_wstring(p.first)] = p.second->json(fullRefresh);
|
||||
}
|
||||
|
||||
answer[L"units"] = unitsJson;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user