Grids can be saved in files and used online

This commit is contained in:
Yousef Khadadeh 2025-09-24 16:18:20 +03:00
parent 0c0d7d8439
commit 52de64907a
14 changed files with 122 additions and 42 deletions

View file

@ -16,7 +16,7 @@ class ClientData {
Vector3f cursorColor;
std::string displayName;
ServerClientID id = {0, 0};
ServerPortionID serverID = 0;
std::queue<ResourceData> pendingResourceData;
std::queue<ServerClientID> pendingResourceID;

View file

@ -18,7 +18,9 @@ enum ServerCommands : MessageCommandType {
SERVER_CHAT_MESSAGE,
SERVER_NEW_BOOKMARK,
SERVER_REMOVE_BOOKMARK,
SERVER_CANVAS_COLOR
SERVER_CANVAS_COLOR,
SERVER_SET_GRID,
SERVER_REMOVE_GRID
};
enum ClientCommands : MessageCommandType {
@ -38,11 +40,14 @@ enum ClientCommands : MessageCommandType {
CLIENT_CHAT_MESSAGE,
CLIENT_NEW_BOOKMARK,
CLIENT_REMOVE_BOOKMARK,
CLIENT_CANVAS_COLOR
CLIENT_CANVAS_COLOR,
CLIENT_SET_GRID,
CLIENT_REMOVE_GRID
};
// NOTE: Do not change savefile header length
#define SAVEFILE_HEADER_LEN 12
// NOTE: Change these values whenever savefile is incompatible with previous version
#define SAVEFILE_HEADER "INFPNT000002"
#define SAVEFILE_HEADER "INFPNT000003"
#define SAVEFILE_HEADER_V2 "INFPNT000002"
#define SAVEFILE_HEADER_V1 "INFPNT000001"

View file

@ -29,7 +29,7 @@ void ServerData::save(cereal::PortableBinaryOutputArchive& a) const {
a((uint64_t)components.size());
for(uint64_t i = 0; i < components.size(); i++)
a(components[i]->get_type(), components[i]->id, components[i]->coords, *components[i]);
a(bookmarks);
a(bookmarks, grids);
}
void ServerData::load(cereal::PortableBinaryInputArchive& a) {
@ -44,20 +44,7 @@ void ServerData::load(cereal::PortableBinaryInputArchive& a) {
components.emplace_back(newComp);
idToComponentMap.emplace(newComp->id, newComp);
}
a(bookmarks);
}
ClientPortionID ServerData::get_max_id(ServerPortionID serverID) const {
ClientPortionID maxClientID = 0;
for(auto& p : resources) {
if(p.first.first == serverID)
maxClientID = std::max(maxClientID, p.first.second);
}
for(auto& p : components) {
if(p->id.first == serverID)
maxClientID = std::max(maxClientID, p->id.second);
}
return maxClientID;
a(bookmarks, grids);
}
MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
@ -69,6 +56,7 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
data.idToComponentMap.emplace(data.components.back()->id, data.components.back());
}
data.bookmarks = world.bMan.bookmark_list();
data.grids = world.gridMan.grids;
data.resources = world.rMan.resource_list();
data.canvasBackColor = convert_vec3<Vector3f>(world.canvasTheme.backColor);
@ -91,23 +79,22 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
if(isDirectConnect)
fileDisplayName = newClient.displayName;
newClient.id.first = Random::get().int_range<ServerPortionID>(1, std::numeric_limits<ServerPortionID>::max());
client->customID = newClient.id.first;
newClient.id.second = data.get_max_id(newClient.id.first);
netServer->send_items_to_all_clients_except(client, RELIABLE_COMMAND_CHANNEL, CLIENT_USER_CONNECT, newClient.id.first, newClient.displayName, newClient.cursorColor);
newClient.serverID = Random::get().int_range<ServerPortionID>(1, std::numeric_limits<ServerPortionID>::max());
client->customID = newClient.serverID;
netServer->send_items_to_all_clients_except(client, RELIABLE_COMMAND_CHANNEL, CLIENT_USER_CONNECT, newClient.serverID, newClient.displayName, newClient.cursorColor);
if(isDirectConnect)
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.id.first, newClient.cursorColor);
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.serverID, newClient.cursorColor);
else {
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.id.first, newClient.cursorColor, newClient.displayName, fileDisplayName, clients, data);
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.serverID, newClient.cursorColor, newClient.displayName, fileDisplayName, clients, data);
for(auto& [id, rData] : data.resources)
netServer->send_items_to_client(client, RESOURCE_COMMAND_CHANNEL, CLIENT_NEW_RESOURCE, id, rData);
}
clients.emplace(newClient.id.first, newClient);
clients.emplace(newClient.serverID, newClient);
});
netServer->add_recv_callback(SERVER_MOVE_MOUSE, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
auto& c = clients[client->customID];
message(c.camCoords, c.windowSize, c.cursorPos);
netServer->send_items_to_all_clients_except(client, UNRELIABLE_COMMAND_CHANNEL, CLIENT_MOVE_MOUSE, c.id.first, c.camCoords, c.windowSize, c.cursorPos);
netServer->send_items_to_all_clients_except(client, UNRELIABLE_COMMAND_CHANNEL, CLIENT_MOVE_MOUSE, c.serverID, c.camCoords, c.windowSize, c.cursorPos);
});
netServer->add_recv_callback(SERVER_PLACE_SINGLE_COMPONENT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
uint64_t placement;
@ -247,6 +234,19 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
data.bookmarks.erase(name);
netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_REMOVE_BOOKMARK, name);
});
netServer->add_recv_callback(SERVER_SET_GRID, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
ServerClientID gID;
message(gID);
WorldGrid& g = data.grids[gID];
message(g);
netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_SET_GRID, gID, g);
});
netServer->add_recv_callback(SERVER_REMOVE_GRID, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
ServerClientID gID;
message(gID);
data.grids.erase(gID);
netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_REMOVE_GRID, gID);
});
netServer->add_recv_callback(SERVER_CANVAS_COLOR, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
message(data.canvasBackColor);
netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_CANVAS_COLOR, data.canvasBackColor);

View file

@ -3,6 +3,7 @@
#include "../SharedTypes.hpp"
#include "../BookmarkManager.hpp"
#include "../ResourceManager.hpp"
#include "../WorldGrid.hpp"
class DrawComponent;
@ -12,9 +13,10 @@ class ServerData {
std::unordered_map<ServerClientID, std::shared_ptr<DrawComponent>> idToComponentMap;
std::unordered_map<ServerClientID, ResourceData> resources;
std::unordered_map<std::string, Bookmark> bookmarks;
std::unordered_map<ServerClientID, WorldGrid> grids;
Vector3f canvasBackColor;
void save(cereal::PortableBinaryOutputArchive& a) const;
void write_to_file(cereal::PortableBinaryOutputArchive& a) const;
void load(cereal::PortableBinaryInputArchive& a);
ClientPortionID get_max_id(ServerPortionID serverID) const;
};