2019-03-17 17:22:53 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: ServerCommandHandler.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 17/03/2019 17:12:52
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "Registry.hpp"
|
2019-04-07 18:20:15 +02:00
|
|
|
#include "ScriptEngine.hpp"
|
2019-03-17 17:22:53 +01:00
|
|
|
#include "Server.hpp"
|
|
|
|
#include "ServerBlock.hpp"
|
|
|
|
#include "ServerPlayer.hpp"
|
|
|
|
#include "ServerWorld.hpp"
|
|
|
|
#include "ServerCommandHandler.hpp"
|
|
|
|
|
|
|
|
void ServerCommandHandler::setupCallbacks() {
|
|
|
|
m_server.setConnectionCallback([this](Client &client) {
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::RegistryData;
|
|
|
|
m_registry.serialize(packet);
|
|
|
|
client.tcpSocket->send(packet);
|
|
|
|
|
2019-12-30 20:19:16 +09:00
|
|
|
// FIXME: Duplicated below, why?
|
2020-01-10 16:08:57 +09:00
|
|
|
// Here the new client is not part of m_players though
|
2019-04-08 15:29:19 +02:00
|
|
|
for (auto &it : m_players) {
|
|
|
|
sf::Packet spawnPacket;
|
|
|
|
spawnPacket << Network::Command::PlayerSpawn << it.first;
|
|
|
|
spawnPacket << it.second.x() << it.second.y() << it.second.z();
|
|
|
|
client.tcpSocket->send(spawnPacket);
|
|
|
|
}
|
2019-04-07 18:20:15 +02:00
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
m_players.emplace(client.id, client);
|
2019-04-08 15:29:19 +02:00
|
|
|
|
|
|
|
auto &player = m_players.at(client.id);
|
2020-01-17 14:39:29 +09:00
|
|
|
player.setPosition(m_spawnPosition.x, m_spawnPosition.y, m_spawnPosition.z);
|
2019-04-08 15:29:19 +02:00
|
|
|
|
|
|
|
m_scriptEngine.lua()["init"](player);
|
2019-04-07 18:20:15 +02:00
|
|
|
|
2019-03-17 17:22:53 +01:00
|
|
|
sf::Packet invPacket;
|
|
|
|
invPacket << Network::Command::PlayerInvUpdate << client.id;
|
2019-04-07 18:20:15 +02:00
|
|
|
invPacket << m_players.at(client.id).inventory();
|
2019-03-17 17:22:53 +01:00
|
|
|
client.tcpSocket->send(invPacket);
|
|
|
|
|
2019-12-30 20:19:16 +09:00
|
|
|
// FIXME: Duplicated above, why?
|
2019-03-17 17:22:53 +01:00
|
|
|
sf::Packet spawnPacket;
|
|
|
|
spawnPacket << Network::Command::PlayerSpawn << client.id;
|
|
|
|
spawnPacket << m_spawnPosition.x << m_spawnPosition.y << m_spawnPosition.z;
|
|
|
|
m_server.sendToAllClients(spawnPacket);
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
// m_world.sendSpawnData(client, player);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::PlayerInvUpdate, [this](Client &client, sf::Packet &packet) {
|
|
|
|
u16 clientId;
|
|
|
|
packet >> clientId;
|
|
|
|
if (clientId == client.id) {
|
2019-04-07 18:20:15 +02:00
|
|
|
packet >> m_players.at(client.id).inventory();
|
2019-03-17 17:22:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-07 17:20:35 +02:00
|
|
|
m_server.setCommandCallback(Network::Command::PlayerPosUpdate, [this](Client &client, sf::Packet &packet) {
|
|
|
|
s32 x, y, z;
|
|
|
|
u16 clientId;
|
|
|
|
packet >> clientId;
|
|
|
|
packet >> x >> y >> z;
|
2019-03-17 17:22:53 +01:00
|
|
|
|
2019-04-07 17:20:35 +02:00
|
|
|
if (clientId == client.id)
|
2019-04-07 18:20:15 +02:00
|
|
|
m_players.at(client.id).setPosition(x, y, z);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::PlayerPlaceBlock, [this](Client &, sf::Packet &packet) {
|
|
|
|
s32 x, y, z;
|
|
|
|
u32 block;
|
|
|
|
packet >> x >> y >> z >> block;
|
|
|
|
m_world.setBlock(x, y, z, block);
|
|
|
|
|
|
|
|
sf::Packet answer;
|
|
|
|
answer << Network::Command::BlockUpdate << x << y << z << block;
|
|
|
|
m_server.sendToAllClients(answer);
|
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::PlayerDigBlock, [this](Client &, sf::Packet &packet) {
|
|
|
|
s32 x, y, z;
|
|
|
|
packet >> x >> y >> z;
|
|
|
|
m_world.setBlock(x, y, z, 0);
|
|
|
|
|
|
|
|
sf::Packet answer;
|
|
|
|
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
|
|
|
|
m_server.sendToAllClients(answer);
|
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::BlockActivated, [this](Client &client, sf::Packet &packet) {
|
|
|
|
s32 x, y, z;
|
|
|
|
packet >> x >> y >> z;
|
|
|
|
|
|
|
|
u16 id = m_world.getBlock(x, y, z);
|
2019-04-07 18:20:15 +02:00
|
|
|
((ServerBlock &)(m_registry.getBlock(id))).onBlockActivated({x, y, z}, m_players.at(client.id), m_world, client);
|
2019-03-17 17:22:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::BlockInvUpdate, [this](Client &, sf::Packet &packet) {
|
|
|
|
gk::Vector3<s32> pos;
|
|
|
|
packet >> pos.x >> pos.y >> pos.z;
|
|
|
|
|
|
|
|
BlockData *data = m_world.getBlockData(pos.x, pos.y, pos.z);
|
|
|
|
if (data)
|
|
|
|
packet >> data->inventory;
|
|
|
|
else
|
|
|
|
DEBUG("BlockInvUpdate: No block data found at", pos.x, pos.y, pos.z);
|
|
|
|
});
|
|
|
|
|
|
|
|
m_server.setCommandCallback(Network::Command::BlockDataUpdate, [this](Client &, sf::Packet &packet) {
|
|
|
|
gk::Vector3<s32> pos;
|
|
|
|
packet >> pos.x >> pos.y >> pos.z;
|
|
|
|
|
|
|
|
BlockData *data = m_world.getBlockData(pos.x, pos.y, pos.z);
|
|
|
|
if (data) {
|
|
|
|
packet >> data->data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|