unify commands

This commit is contained in:
Julian Nießner
2019-06-02 22:17:03 +02:00
parent 6258ee9193
commit e6d262cfc0
5 changed files with 24 additions and 33 deletions

View File

@@ -9,11 +9,11 @@ Command *Command::create(int clientIndex, yojimbo::Message *message)
switch (message->GetType()) switch (message->GetType())
{ {
case (int)GameMessageType::TEST: case (int)GameMessageType::TEST:
return new TestCommand(clientIndex, message); return new TestCommand(clientIndex, ((TestMessage *)message)->m_data);
case (int)GameMessageType::SETNAME: case (int)GameMessageType::SETNAME:
return new SetNameCommand(clientIndex, message); return new SetNameCommand(clientIndex, std::string(((SetNameMessage *)message)->buffer));
default: default:
std::cout << "Unknown message type received!" << std::endl; std::cout << "Unknown message type received over network!" << std::endl;
return nullptr; return nullptr;
} }
} }
@@ -26,12 +26,12 @@ Command *Command::create(std::string command)
token; token;
if (token == "stop") if (token == "stop")
{ {
return new StopCommandInternal(); return new StopCommand();
} }
else if (token == "test") else if (token == "test")
{ {
return new TestCommandInternal(); return new TestCommand(-1, 100);
} }
std::cout << "Internal commando don't exists: " << command << std::endl; std::cout << "Local command don't exists: " << command << std::endl;
return nullptr; return nullptr;
} }

View File

@@ -24,9 +24,9 @@ public:
class TestCommand : public Command class TestCommand : public Command
{ {
public: public:
TestCommand(int clientIndex, yojimbo::Message *message) TestCommand(int clientIndex, int data)
: m_clientIndex(clientIndex), : m_clientIndex(clientIndex),
m_data(((TestMessage *)message)->m_data) m_data(data)
{} {}
virtual void execute(GameServer *server) override; virtual void execute(GameServer *server) override;
@@ -39,9 +39,9 @@ private:
class SetNameCommand : public Command class SetNameCommand : public Command
{ {
public: public:
SetNameCommand(int clientIndex, yojimbo::Message *message) SetNameCommand(int clientIndex, std::string name)
: m_clientIndex(clientIndex), : m_clientIndex(clientIndex),
m_name(std::string(((SetNameMessage *)message)->buffer)) m_name(name)
{} {}
virtual void execute(GameServer *server) override; virtual void execute(GameServer *server) override;
@@ -51,15 +51,9 @@ private:
std::string m_name; std::string m_name;
}; };
// --- Classes Internal: // --- Classes local:
class TestCommandInternal : public Command class StopCommand : public Command
{
public:
virtual void execute(GameServer *server) override;
};
class StopCommandInternal : public Command
{ {
public: public:
virtual void execute(GameServer *server) override; virtual void execute(GameServer *server) override;

View File

@@ -1,6 +1,6 @@
#include "Command.h" #include "Command.h"
void StopCommandInternal::execute(GameServer *server) void StopCommand::execute(GameServer *server)
{ {
server->m_running = false; server->m_running = false;
} }

View File

@@ -4,9 +4,14 @@
void TestCommand::execute(GameServer *server) void TestCommand::execute(GameServer *server)
{ {
// ... process test message ... if (m_clientIndex == -1)
{
std::cout << "Test message received from server cmd :" << m_clientIndex << " with data: " << m_data << std::endl;
}
else
{
entityx::Entity entity = server->serverToEntityX.at(m_clientIndex); entityx::Entity entity = server->serverToEntityX.at(m_clientIndex);
entityx::ComponentHandle<NetworkPlayerProperties> playerproperties = entity.component<NetworkPlayerProperties>(); entityx::ComponentHandle<NetworkPlayerProperties> playerproperties = entity.component<NetworkPlayerProperties>();
std::cout << "Test message received from client " << playerproperties->name << ":" << m_clientIndex << " with data: " << m_data << std::endl; std::cout << "Test message received from client " << playerproperties->name << ":" << m_clientIndex << " with data: " << m_data << std::endl;
} }
}

View File

@@ -1,8 +0,0 @@
#include "Command.h"
#include <iostream>
void TestCommandInternal::execute(GameServer *server)
{
std::cout << "Internal testcommand launched!" << std::endl;
}