Full orderlist exporting implementation and partial importing implementation.

pull/678/head
lucaFiorini 2 months ago
parent bedc33c46e
commit 366f1b622a

@ -233,6 +233,10 @@ public:
void MakeChangeCounter();
void MakeLabel(OrderLabelSubType subtype);
std::string ToJSONString() const;
static Order FromJSONString(std::string);
/**
* Is this a 'goto' order with a real destination?
* @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION.
@ -780,6 +784,7 @@ public:
inline std::vector<DispatchSlot> &GetScheduledDispatchMutable() { return this->scheduled_dispatch; }
void SetScheduledDispatch(std::vector<DispatchSlot> dispatch_list);
void AddScheduledDispatch(uint32_t offset);
void RemoveScheduledDispatch(uint32_t offset);
void AdjustScheduledDispatch(int32_t adjust);
@ -856,6 +861,19 @@ public:
*/
inline int32_t GetScheduledDispatchDelay() const { return this->scheduled_dispatch_max_delay; }
/**
* Get the scheduled dispatch flags
* @return flags
*/
inline int8_t GetScheduledDispatchFlags() const {return this->scheduled_dispatch_flags; }
/**
* Set the scheduled disaptch flags
* @param flags
*/
inline void SetScheduledDispatchFlags(int8_t flags) { this->scheduled_dispatch_flags = flags; }
inline void BorrowSchedule(DispatchSchedule &other)
{
this->CopyBasicFields(other);
@ -959,6 +977,9 @@ public:
void DeleteOrderAt(int index);
void MoveOrder(int from, int to);
std::string ToJSONString();
static OrderList FromJSONString(std::string);
/**
* Is this a shared order list?
* @return whether this order list is shared among multiple vehicles

@ -40,11 +40,12 @@
#include "tracerestrict.h"
#include "train.h"
#include "date_func.h"
#include "3rdparty/nlohmann/json.hpp"
#include "table/strings.h"
#include "3rdparty/robin_hood/robin_hood.h"
#include <3rdparty/nlohmann/json.hpp>
#include "safeguards.h"
/* DestinationID must be at least as large as every these below, because it can
@ -269,6 +270,41 @@ void Order::MakeLabel(OrderLabelSubType subtype)
this->flags = subtype;
}
std::string Order::ToJSONString() const
{
std::string out;
nlohmann::json json;
json["packed-data"] = this->Pack();
json["destination-id"] = this->GetDestination();
if(this->extra.get() != nullptr){
auto& extraJson = json["extra"];
extraJson["cargo-type-flags"] = this->extra.get()->cargo_type_flags;
extraJson["colour"] = this->extra.get()->colour;
extraJson["dispatch-index"] = this->extra.get()->dispatch_index;
extraJson["xdata"] = this->extra.get()->xdata;
extraJson["xdata2"] = this->extra.get()->xdata2;
extraJson["xflags"] = this->extra.get()->xflags;
}
json["refit-cargo"] = this->GetRefitCargo();
json["wait-time"] = this->GetWaitTime();
json["travel-time"] = this->GetTravelTime();
json["max-speed"] = this->GetMaxSpeed();
out = json.dump();
return out;
}
Order Order::FromJSONString(std::string)
{
Order newOrder;
return newOrder;
}
/**
* Make this depot/station order also a refit order.
* @param cargo the cargo type to change to.
@ -824,6 +860,97 @@ void OrderList::MoveOrder(int from, int to)
this->ReindexOrderList();
}
std::string OrderList::ToJSONString()
{
nlohmann::json json;
auto& SD_data = this->GetScheduledDispatchScheduleSet();
auto& headJson = json["head"];
for (unsigned int i = 0; auto &SD : SD_data) {
auto &SDJson = headJson["scheduled-dispatch"][i++];
for (unsigned int i = 0; auto &SD_slot : SD.GetScheduledDispatch()) {
auto& slotsJson = SDJson["slots"][i++];
slotsJson["offset"] = SD_slot.offset;
slotsJson["flags"] = SD_slot.flags;
}
SDJson["name"] = SD.ScheduleName();
SDJson["start-tick"] = SD.GetScheduledDispatchStartTick().value;
SDJson["duration"] = SD.GetScheduledDispatchDuration();
SDJson["max-delay"] = SD.GetScheduledDispatchDelay();
SDJson["flags"] = SD.GetScheduledDispatchFlags();
}
const Order* o = this->GetFirstOrder();
if (o != nullptr) {
int i = 0;
do {
json["orders"][i++] = nlohmann::json::parse(o->ToJSONString());
} while ((o = this->GetNext(o)) != this->GetFirstOrder());
}
std::cout << json.dump();
return json.dump();
}
OrderList OrderList::FromJSONString(std::string json_str)
{
OrderList list;
nlohmann::json json = nlohmann::json::parse(json_str);
if (json.contains("head")){
auto &headJson = json.at("head");
if (headJson.contains("scheduled-dispatch")) {
auto &SDJson = headJson.at("scheduled-dispatch");
if (SDJson.is_array()) {
for (nlohmann::json::iterator it = SDJson.begin(); it != SDJson.end(); ++it) {
if (it.value().contains("slots")) {
auto &slotsJson = it.value().at("slots");;
DispatchSchedule dispatchSchedule;
if (slotsJson.is_array()) {
for (nlohmann::json::iterator it = slotsJson.begin(); it != slotsJson.end(); ++it) {
DispatchSlot newDispatchSlot;
newDispatchSlot.offset = it.value().at("offset").template get<uint32_t>();
newDispatchSlot.flags = it.value().at("flags").template get<uint16_t>();
dispatchSchedule.GetScheduledDispatchMutable().push_back(newDispatchSlot);
}
}
dispatchSchedule.ScheduleName() = it.value().at("name").template get<std::string>();
dispatchSchedule.SetScheduledDispatchStartTick(it.value().at("start-tick").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchDuration(it.value().at("duration").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchDelay(it.value().at("max-delay").template get<uint32_t>());
dispatchSchedule.SetScheduledDispatchFlags(it.value().at("flags").template get<uint8_t>());
list.dispatch_schedules.push_back(dispatchSchedule);
}
}
}
}
}
if (json.contains("orders")) {
/*Where do orders get allocated?*/
}
return list;
}
/**
* Removes the vehicle from the shared order list.
* @note This is supposed to be called when the vehicle is still in the chain

@ -3491,8 +3491,77 @@ public:
switch (index) {
case 0: this->OrderClick_ReverseOrderList(0); break;
case 1: this->OrderClick_ReverseOrderList(1); break;
case 2: ShowSaveLoadDialog(FT_ORDERLIST, SLO_SAVE); break;
case 3: ShowSaveLoadDialog(FT_ORDERLIST, SLO_LOAD); break;
case 2: this->vehicle->orders->ToJSONString(); break;
case 3: this->vehicle->orders->FromJSONString(R"(
{
"head": {
"scheduled-dispatch": [
{
"duration": 106560,
"flags": 0,
"max-delay": 0,
"name": "alfredo",
"slots": [
{
"flags": 0,
"offset": 4514
},
{
"flags": 0,
"offset": 90280
}
],
"start-tick": 16729920
}
]
},
"orders": [
{
"destination-id": 0,
"max-speed": 65535,
"packed-data": 578,
"refit-cargo": 254,
"travel-time": 0,
"wait-time": 0
},
{
"destination-id": 0,
"max-speed": 65535,
"packed-data": 578,
"refit-cargo": 254,
"travel-time": 0,
"wait-time": 0
},
{
"destination-id": 0,
"max-speed": 65535,
"packed-data": 578,
"refit-cargo": 254,
"travel-time": 0,
"wait-time": 0
},
{
"destination-id": 0,
"max-speed": 65535,
"packed-data": 578,
"refit-cargo": 254,
"travel-time": 0,
"wait-time": 0
},
{
"destination-id": 0,
"max-speed": 65535,
"packed-data": 578,
"refit-cargo": 254,
"travel-time": 0,
"wait-time": 0
}
]
}
)"); break;
//case 2: ShowSaveLoadDialog(FT_ORDERLIST, SLO_SAVE); break;
//case 3: ShowSaveLoadDialog(FT_ORDERLIST, SLO_LOAD); break;
default: NOT_REACHED();
}
break;

Loading…
Cancel
Save