2020-07-30 22:07:00 -07:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "ServerClients.h"
|
|
|
|
|
2020-08-13 00:55:18 -07:00
|
|
|
#include "util/Log.h"
|
2020-07-30 22:07:00 -07:00
|
|
|
#include "ServerClient.h"
|
2020-08-13 00:55:18 -07:00
|
|
|
#include "util/net/NetField.h"
|
|
|
|
#include "util/net/Serializer.h"
|
|
|
|
#include "util/net/NetHandler.h"
|
|
|
|
#include "world/player/ServerPlayer.h"
|
2020-07-30 22:07:00 -07:00
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
ServerClients::ServerClients(SubgamePtr game) : game(game) {}
|
2020-07-30 22:07:00 -07:00
|
|
|
|
2020-11-08 22:57:34 -08:00
|
|
|
void ServerClients::init(ServerWorld* world) {
|
|
|
|
this->world = world;
|
2020-07-30 22:07:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerClients::handleConnect(ENetEvent e) {
|
2021-08-29 16:14:23 -07:00
|
|
|
const auto client = make_shared<ServerClient>("TEMP:" + NetHandler::intToIPString(*reinterpret_cast<unsigned int*>(&e.peer->address.host))
|
2021-06-22 18:13:39 -07:00
|
|
|
+ ":" + std::to_string(e.peer->address.port), e.peer->connectID, e.peer);
|
|
|
|
e.peer->data = client.get();
|
|
|
|
clients.emplace(client->id, client);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
std::cout << Log::info << client->username << " connected." << Log::endl;
|
2020-07-30 22:07:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerClients::handleDisconnect(ENetEvent e) {
|
2021-06-22 18:13:39 -07:00
|
|
|
const auto client = static_cast<ServerClient*>(e.peer->data);
|
|
|
|
if (client->player) game.s()->getParser().playerDisconnected(client->player);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
std::cout << Log::info << client->username << " disconnected." << Log::endl;
|
|
|
|
clients.erase(client->id);
|
2020-07-30 22:07:00 -07:00
|
|
|
}
|
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
void ServerClients::createPlayer(sptr<ServerClient> client, DimensionPtr dimension) {
|
|
|
|
client->player = make_shared<ServerPlayer>(*client, dimension->getWorld(), game, dimension);
|
2021-09-26 16:22:07 -07:00
|
|
|
client->player->setPos({ 0, 64, 0 });
|
2020-11-08 22:57:34 -08:00
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
game.s()->getParser().playerConnected(client->player);
|
2021-08-03 16:02:34 -07:00
|
|
|
// client->player->setPos({ 256, -20, 256 }, true);
|
2020-11-08 22:57:34 -08:00
|
|
|
|
|
|
|
Serializer()
|
2021-06-22 18:13:39 -07:00
|
|
|
.append(NetField::ID).append(static_cast<u32>(client->player->getId()))
|
|
|
|
.append(NetField::POS).append(client->player->getPos())
|
|
|
|
.append(NetField::LOOK_PITCH).append(client->player->getPitch())
|
|
|
|
.append(NetField::LOOK_YAW).append(client->player->getYaw())
|
|
|
|
.packet(Packet::Type::THIS_PLAYER_INFO).sendTo(client->peer, Packet::Channel::INTERACT);
|
2020-07-30 22:07:00 -07:00
|
|
|
}
|
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
const sptr<ServerClient> ServerClients::getClient(u32 id) const {
|
|
|
|
if (clients.count(id)) return clients.at(id);
|
2020-11-08 22:57:34 -08:00
|
|
|
return nullptr;
|
2020-07-30 22:07:00 -07:00
|
|
|
}
|
|
|
|
|
2021-06-22 18:13:39 -07:00
|
|
|
const std::unordered_map<u32, sptr<ServerClient>>& ServerClients::getClients() const {
|
|
|
|
return clients;
|
|
|
|
}
|