shooter/Server.cpp

171 lines
6.8 KiB
C++
Raw Normal View History

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;
for (auto& player : _players) {
2021-09-13 15:53:43 +03:00
updatePacket << player.first << player.second->position().x() << player.second->position().y() << player.second->position().z() << player.second->health() << player.second->angle().y() << player.second->headAngle();
}
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>() });
for (const auto& player : _players)
{
sendPacket1 << player.first << player.second->position().x() << player.second->position().y() << player.second->position().z() << player.second->health();
if (player.first != targetId)
_socket.sendRely(extraPacket, player.first);
}
_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];
packet >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4];
_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]);
}
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)
_socket.send(sendPacket, player.first);
}
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)
_socket.send(sendPacket, player.first);
}
break;
2021-10-17 10:21:10 +03:00
case MsgType::Error:
break;
default:
throw std::logic_error{"Server::processCustomPacket: unknown MsgType"};
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-17 10:21:10 +03:00
_bonuses.insert({"Bonus_gun_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(-10, -2, -15), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_gun_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(10, -2, 15), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_shotgun_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(-10, 13, -24), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_shotgun_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(10, 13, 24), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_ak47_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(-25, 30, 50), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_ak47_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(25, 30, -50), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_gold_ak47_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(-35, 80, 25), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_gold_ak47_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(35, 80, -25), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_rifle_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(40, -2, 45), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_rifle_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(-40, -2, -45), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_hill_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(-40, -2, 45), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_hill_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(40, -2, -45), -2*_bonusRechargeTime, true})});
2021-09-13 15:53:43 +03:00
2021-10-17 10:21:10 +03:00
_bonuses.insert({"Bonus_ability_1", std::make_shared<BonusInfo>(BonusInfo{Point4D(25, 18, -33), -2*_bonusRechargeTime, true})});
_bonuses.insert({"Bonus_ability_2", std::make_shared<BonusInfo>(BonusInfo{Point4D(-25, 18, 33), -2*_bonusRechargeTime, 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) {
if(!bonusInfo->onTheMap && std::abs(Time::time() - bonusInfo->lastTake) > _bonusRechargeTime) {
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();
}