2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 25.05.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Server.h"
|
2021-10-09 13:41:12 +03:00
|
|
|
#include "engine/utils/Log.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
void Server::broadcast() {
|
|
|
|
sf::Packet updatePacket;
|
|
|
|
updatePacket << MsgType::Update;
|
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
for (auto& [playerId, player] : _players) {
|
|
|
|
updatePacket << playerId << player->position().x() << player->position().y() << player->position().z() << player->health() << player->angle().y() << player->headAngle() << player->playerName();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
for (auto& player : _players) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_socket.send(updatePacket, player.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Server::processConnect(sf::Uint16 targetId) {
|
|
|
|
sf::Packet sendPacket1, sendPacket2;
|
|
|
|
sf::Packet extraPacket;
|
|
|
|
|
|
|
|
// players init
|
|
|
|
extraPacket << MsgType::NewClient << targetId;
|
|
|
|
sendPacket1 << MsgType::Init << targetId;
|
|
|
|
_players.insert({ targetId, std::make_shared<Player>() });
|
2021-10-26 10:08:41 +03:00
|
|
|
for (const auto& [playerId, player] : _players)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
2021-10-26 10:08:41 +03:00
|
|
|
sendPacket1 << playerId << player->position().x() << player->position().y() << player->position().z() << player->health() << player->kills() << player->deaths();
|
|
|
|
if (playerId != targetId)
|
|
|
|
_socket.sendRely(extraPacket, playerId);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
_socket.sendRely(sendPacket1, targetId);
|
|
|
|
|
|
|
|
// bonuses init
|
|
|
|
sendPacket2 << MsgType::InitBonuses;
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto& [bonusName, bonusInfo] : _bonuses) {
|
|
|
|
if(bonusInfo->onTheMap)
|
|
|
|
sendPacket2 << bonusName << bonusInfo->position.x() << bonusInfo->position.y() << bonusInfo->position.z();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
_socket.sendRely(sendPacket2, targetId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) {
|
|
|
|
double buf[5];
|
2021-10-26 09:40:35 +03:00
|
|
|
std::string playerName;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
packet >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4] >> playerName;
|
2021-10-12 17:12:47 +03:00
|
|
|
_players.at(senderId)->translateToPoint(Vec3D{ buf[0], buf[1], buf[2] });
|
|
|
|
_players.at(senderId)->rotateToAngle(Vec3D{0, buf[3], 0});
|
2021-09-13 15:53:43 +03:00
|
|
|
_players.at(senderId)->setHeadAngle(buf[4]);
|
2021-10-26 09:40:35 +03:00
|
|
|
_players.at(senderId)->setPlayerName(playerName);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Server::processDisconnect(sf::Uint16 senderId) {
|
|
|
|
sf::Packet sendPacket;
|
|
|
|
|
|
|
|
sendPacket << MsgType::Disconnect << senderId;
|
|
|
|
_players.erase(senderId);
|
|
|
|
for (const auto& player : _players)
|
|
|
|
_socket.sendRely(sendPacket, player.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Server::processCustomPacket(MsgType type, sf::Packet& packet, sf::Uint16 senderId) {
|
|
|
|
sf::Packet sendPacket;
|
|
|
|
double dbuff[10];
|
|
|
|
sf::Uint16 targetId;
|
|
|
|
double damage;
|
|
|
|
std::string tmp;
|
|
|
|
double newHealth;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MsgType::Damage:
|
|
|
|
packet >> targetId >> damage;
|
|
|
|
newHealth = _players[targetId]->health() - damage;
|
|
|
|
if(newHealth > 0) {
|
|
|
|
_players[targetId]->setHealth(newHealth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_players[targetId]->setFullHealth();
|
|
|
|
_players[targetId]->setFullAbility();
|
|
|
|
_players[targetId]->addDeath();
|
|
|
|
_players[senderId]->addKill();
|
|
|
|
|
|
|
|
sendPacket << MsgType::Kill << targetId << senderId;
|
|
|
|
for (auto& player : _players)
|
2021-10-18 18:30:02 +03:00
|
|
|
_socket.sendRely(sendPacket, player.first);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MsgType::FireTrace:
|
|
|
|
packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5];
|
|
|
|
sendPacket << MsgType::FireTrace << dbuff[0] << dbuff[1] << dbuff[2] << dbuff[3] << dbuff[4] << dbuff[5];
|
|
|
|
for (auto& player : _players) {
|
|
|
|
if(player.first != senderId)
|
|
|
|
_socket.send(sendPacket, player.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case MsgType::RemoveBonus:
|
|
|
|
packet >> tmp;
|
|
|
|
|
|
|
|
if(tmp.find("Bonus_hill") != std::string::npos) {
|
|
|
|
_players[senderId]->setFullHealth();
|
|
|
|
}
|
|
|
|
if(tmp.find("Bonus_ability") != std::string::npos) {
|
|
|
|
_players[senderId]->setFullAbility();
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
_bonuses[tmp] = std::make_shared<BonusInfo>(BonusInfo{_bonuses[tmp]->position, Time::time(), false});
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
sendPacket << MsgType::RemoveBonus << tmp;
|
|
|
|
for (auto& player : _players) {
|
|
|
|
if(player.first != senderId)
|
2021-10-18 18:30:02 +03:00
|
|
|
_socket.sendRely(sendPacket, player.first);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-18 18:30:02 +03:00
|
|
|
break;
|
|
|
|
case MsgType::ChangeWeapon:
|
|
|
|
packet >> tmp;
|
|
|
|
sendPacket << MsgType::ChangeWeapon << senderId << tmp;
|
|
|
|
|
|
|
|
for (auto& player : _players) {
|
|
|
|
if(player.first != senderId)
|
|
|
|
_socket.sendRely(sendPacket, player.first);
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
2021-10-17 10:44:17 +03:00
|
|
|
return;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::processStop() {
|
2021-10-16 20:22:55 +03:00
|
|
|
_players.clear();
|
|
|
|
_bonuses.clear();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Server::generateBonuses() {
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_gun_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-10, -2, -15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_gun_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(10, -2, 15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_shotgun_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-10, 13, -24), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_shotgun_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(10, 13, 24), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_ak47_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-25, 30, 50), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_ak47_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(25, 30, -50), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_gold_ak47_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-35, 80, 25), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_gold_ak47_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(35, 80, -25), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_rifle_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(40, -2, 45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_rifle_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-40, -2, -45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_hill_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-40, -2, 45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_hill_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(40, -2, -45), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
_bonuses.insert({"Bonus_ability_1", std::make_shared<BonusInfo>(BonusInfo{Vec3D(25, 18, -33), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
|
|
|
_bonuses.insert({"Bonus_ability_2", std::make_shared<BonusInfo>(BonusInfo{Vec3D(-25, 18, 33), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Server::updateInfo() {
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto& [bonusName, bonusInfo] : _bonuses) {
|
2021-10-26 09:40:35 +03:00
|
|
|
if(!bonusInfo->onTheMap && std::abs(Time::time() - bonusInfo->lastTake) > ShooterConsts::BONUS_RECHARGE_TIME) {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Packet sendPacket;
|
2021-10-17 10:21:10 +03:00
|
|
|
sendPacket << MsgType::AddBonus << bonusName << bonusInfo->position.x() << bonusInfo->position.y() << bonusInfo->position.z();
|
2021-09-13 15:53:43 +03:00
|
|
|
for (const auto& player : _players)
|
|
|
|
_socket.sendRely(sendPacket, player.first);
|
2021-10-17 10:21:10 +03:00
|
|
|
bonusInfo = std::make_shared<BonusInfo>(BonusInfo{bonusInfo->position, bonusInfo->lastTake, true});
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 20:22:55 +03:00
|
|
|
|
|
|
|
Server::~Server() {
|
|
|
|
processStop();
|
|
|
|
}
|