Skip to content

Commit

Permalink
Fix networking after serialization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
FergusonAJ committed Dec 13, 2023
1 parent 620e3b6 commit 981d226
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion source/core/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AgentBase;
using agent_map_t = std::map<size_t, std::unique_ptr<AgentBase>>;

/// @brief Common types of properties in network serialization
enum class PropertyType { t_double, t_int, t_char, t_string, t_position, t_other };
enum class PropertyType { t_double, t_int, t_char, t_string, t_position, t_bool, t_other };

/// @brief Enum for World types in network serialization
enum class WorldType { w_maze, w_second, w_generative, w_manual };
Expand Down
4 changes: 4 additions & 0 deletions source/core/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,14 @@ class Entity : public CoreObject {
case t_char: SetProperty(name, DeserializeAs<char>(is)); break;
case t_double: SetProperty(name, DeserializeAs<double>(is)); break;
case t_int: SetProperty(name, DeserializeAs<int>(is)); break;
case t_bool: SetProperty(name, DeserializeAs<bool>(is)); break;
case t_string: SetProperty(name, DeserializeAs<std::string>(is)); break;
case t_position: SetProperty(name, DeserializeAs<GridPosition>(is)); break;
case t_other:
std::cerr << "Warning: Cannot deserialize property'" << name << "'." << std::endl;
std::string tmp_str;
std::getline(is, tmp_str, '\n');
std::cerr << " Data for that property: \"" << tmp_str << "\"." << std::endl;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions source/core/Property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Property : public PropertyBase {
PropertyType GetType() const override {
if constexpr (std::is_same<T, char>()) return PropertyType::t_char;
if constexpr (std::is_same<T, int>()) return PropertyType::t_int;
if constexpr (std::is_same<T, bool>()) return PropertyType::t_bool;
if constexpr (std::is_same<T, double>()) return PropertyType::t_double;
if constexpr (std::is_same<T, std::string>()) return PropertyType::t_string;
if constexpr (std::is_same<T, GridPosition>()) return PropertyType::t_position;
Expand All @@ -45,6 +46,7 @@ struct Property : public PropertyBase {
std::string GetTypeName() const override {
if constexpr (std::is_same<T, char>()) return "char";
if constexpr (std::is_same<T, int>()) return "int";
if constexpr (std::is_same<T, bool>()) return "bool";
if constexpr (std::is_same<T, double>()) return "double";
if constexpr (std::is_same<T, std::string>()) return "string";
if constexpr (std::is_same<T, GridPosition>()) return "GridPosition";
Expand Down
18 changes: 14 additions & 4 deletions source/core/WorldBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,19 +547,29 @@ class WorldBase {
last_entity_id = 0;

// Load the number of agents saved.
size_t size = DeserializeAs<size_t>(is);
size_t server_last_entity_id = DeserializeAs<size_t>(is);

// client id NOT in agent map yet if ID = 0
// append to end of set
if (client_id == 0) client_id = size;
if (client_id == 0) client_id = server_last_entity_id;

// Load back in all agents.
for (size_t i = 0; i < size; i++) {
for (size_t i = 0; i < server_last_entity_id; i++) {
// First, check to see if we've hit the end of the agent_set
// Because we are looking at last entity id (and not total size), we may have
// gaps in our agent_set
auto tmp_pos = is.tellg();
std::getline(is, read, '\n');
if(read == ":::END agent_set"){
if(last_entity_id < client_id) last_entity_id = client_id;
return;
}
else is.seekg(tmp_pos);
auto agent_ptr = std::make_unique<netWorth::ControlledAgent>(0, "temp");
DeserializeValue(is, *agent_ptr);
agent_ptr->SetProperty("manager", manager);

if (agent_ptr->GetID() >= last_entity_id) last_entity_id = agent_ptr->GetID() + 1;
if (agent_ptr->GetID() >= last_entity_id) last_entity_id = agent_ptr->GetID();

// If this agent is the client interface, skip it (we already have it).
if (agent_ptr->GetID() == client_id) { continue; }
Expand Down
12 changes: 8 additions & 4 deletions source/group_5_client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ bool runGenerativeWorldDemo(std::istream &is, const std::string &ipString, unsig
world.Deserialize(is, &manager);
clientKillPort = port;
clientKillIP = ipString;
cse491::Entity & interface = world.AddAgent<netWorth::ClientInterface>(interfaceName, "server_ip", ipString,
"server_port", port, "manager", &manager,
cse491::Entity & interface = world.AddAgent<netWorth::ClientInterface>(interfaceName,
"server_ip", ipString,
"server_port", port,
"manager", &manager,
"socket", socket)
.SetProperty("symbol", '@')
.SetPosition(startX, startY);
Expand Down Expand Up @@ -144,8 +146,10 @@ bool runManualWorldDemo(std::istream &is, const std::string &ipString, unsigned
world.Deserialize(is, &manager);
clientKillPort = port;
clientKillIP = ipString;
cse491::Entity & interface = world.AddAgent<netWorth::ClientInterface>(interface_name, "server_ip", ipString,
"server_port", port, "manager", &manager,
cse491::Entity & interface = world.AddAgent<netWorth::ClientInterface>(interface_name,
"server_ip", ipString,
"server_port", port,
"manager", &manager,
"socket", socket)
.SetProperty("symbol", '@')
.SetPosition(startX, startY);
Expand Down
34 changes: 27 additions & 7 deletions source/group_5_server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void handleConnection(netWorth::ServerManager &serverManager, cse491::WorldBase
cse491::Entity & interface = world.AddAgent<netWorth::ServerInterface>
(serverInterfaceName, "client_ip", sender->toString(), "client_port", port, "server_port",
serverManager.m_max_client_port, "server_manager", &serverManager)
.SetProperty("symbol", '@');
.SetProperty("symbol", '@').SetPosition(startX, startY);

auto & serverInterface = dynamic_cast<netWorth::ServerInterface &>(interface);

Expand Down Expand Up @@ -199,13 +199,33 @@ int runManualWorldDemo() {
cse491_team8::ManualWorld world;
int startX = 80, startY = 63;

// Add agents
world.AddAgent<cse491::PacingAgent>("Pacer 1").SetPosition(3,1);
world.AddAgent<cse491::PacingAgent>("Pacer 2").SetPosition(6,1);

// Add items
world.AddItem("Axe", "Chop", 5, "symbol", 'P').SetPosition(37, 3);
world.AddItem("Boat", "Swim", 7, "symbol", 'U').SetPosition(18, 4);
world.AddItem("Axe", "Uses", 5, "symbol", 'P').SetPosition(80, 120);
world.AddItem("Axe", "Uses", 10, "symbol", 'P').SetPosition(97, 40);
world.AddItem("Boat", "Uses", 7, "symbol", 'U').SetPosition(55, 11);
world.AddItem("Sword", "Strength", 8, "symbol", 't').SetPosition(18, 4);
world.AddItem("Sword", "Strength", 5, "symbol", 't').SetPosition(27, 11);
world.AddItem("Sword", "Strength", 4, "symbol", 't').SetPosition(65, 89);
world.AddItem("Health Potion", "Healing", 25, "symbol", 'j').SetPosition(38, 16);
world.AddItem("Health Potion", "Healing", 30, "symbol", 'j').SetPosition(1, 18);

world.AddAgent<cse491::PacingAgent>("Pacer 1").SetPosition(97, 45);
world.AddAgent<cse491::PacingAgent>("Pacer 5").SetPosition(3,14);
world.AddAgent<cse491::PacingAgent>("Pacer 2").SetPosition(7,30);
world.AddAgent<cse491::PacingAgent>("Pacer 6").SetPosition(27, 10);
world.AddAgent<cse491::PacingAgent>("Pacer 7").SetPosition(38, 10);
world.AddAgent<cse491::PacingAgent>("Pacer 3").SetPosition(18,3);
world.AddAgent<cse491::PacingAgent>("Pacer 4").SetPosition(45,17);

auto & a_star_agent = static_cast<walle::AStarAgent&>(world.AddAgent<walle::AStarAgent>("AStar 1"));
a_star_agent.SetPosition(80, 111);
a_star_agent.SetGoalPosition(80, 63);

world.AddAgent<cse491::PacingAgent>("Shark", "OnlyWater", 1).SetPosition(125, 140);

auto & pacer_1 = world.GetAgent(world.GetAgentID("Pacer 1"));
world.AddItem("Sword", "Strength", 15, "symbol", 't').SetPosition(pacer_1.GetPosition());
world.DoActionAttemptItemPickup(pacer_1, pacer_1.GetPosition());

// Ensure client successfully connects
std::thread connectionThread(handleConnection, std::ref(manager), std::ref(world), startX, startY, cse491::WorldType::w_manual);
Expand Down

0 comments on commit 981d226

Please sign in to comment.