vectozavr-shooter/network/ShooterServer.cpp

224 lines
8.7 KiB
C++
Raw Permalink Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 25.05.2021.
//
2023-08-03 01:48:04 +03:00
#include <utils/Log.h>
2021-10-28 17:06:10 +03:00
#include "ShooterServer.h"
2021-10-28 16:58:02 +03:00
#include "ShooterMsgType.h"
2021-09-13 15:53:43 +03:00
2021-10-28 17:06:10 +03:00
void ShooterServer::broadcast() {
2021-09-13 15:53:43 +03:00
sf::Packet updatePacket;
2021-10-28 16:58:02 +03:00
updatePacket << MsgType::ServerUpdate;
2021-09-13 15:53:43 +03:00
2021-10-31 11:39:08 +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->playerNickName();
2021-09-13 15:53:43 +03:00
}
2021-10-31 11:39:08 +03:00
for (auto &player : _players) {
2021-09-13 15:53:43 +03:00
_socket.send(updatePacket, player.first);
}
}
2021-10-28 17:06:10 +03:00
void ShooterServer::processConnect(sf::Uint16 targetId) {
2021-09-13 15:53:43 +03:00
sf::Packet sendPacket1, sendPacket2;
sf::Packet extraPacket;
// players init
extraPacket << MsgType::NewClient << targetId;
sendPacket1 << MsgType::Init << targetId;
2021-10-31 11:39:08 +03:00
_players.insert({targetId, std::make_shared<Player>(ObjectNameTag("Player_" + std::to_string(targetId)))});
for (const auto&[playerId, player] : _players) {
sendPacket1 << playerId << player->position().x() << player->position().y() << player->position().z()
<< player->health() << player->kills() << player->deaths();
2021-10-26 10:08:41 +03:00
if (playerId != targetId)
_socket.sendRely(extraPacket, playerId);
2021-09-13 15:53:43 +03:00
}
_socket.sendRely(sendPacket1, targetId);
// bonuses init
2021-10-28 16:58:02 +03:00
sendPacket2 << MsgType::Custom << ShooterMsgType::InitBonuses;
2021-10-31 11:39:08 +03:00
for (auto&[bonusName, bonusInfo] : _bonuses) {
if (bonusInfo->onTheMap) {
2021-10-17 10:21:10 +03:00
sendPacket2 << bonusName << bonusInfo->position.x() << bonusInfo->position.y() << bonusInfo->position.z();
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
}
_socket.sendRely(sendPacket2, targetId);
}
2021-10-31 11:39:08 +03:00
void ShooterServer::processClientUpdate(sf::Uint16 senderId, sf::Packet &packet) {
2022-02-23 17:29:42 +03:00
double x, y, z, angleBody, headAngle;
2021-10-26 09:40:35 +03:00
std::string playerName;
2021-09-13 15:53:43 +03:00
2022-02-23 17:29:42 +03:00
packet >> x >> y >> z >> angleBody >> headAngle >> playerName;
_players.at(senderId)->translateToPoint(Vec3D{x, y, z});
_players.at(senderId)->rotateToAngle(Vec3D{0, angleBody, 0});
_players.at(senderId)->setHeadAngle(headAngle);
2021-10-28 16:58:02 +03:00
_players.at(senderId)->setPlayerNickName(playerName);
2021-09-13 15:53:43 +03:00
}
2021-10-28 17:06:10 +03:00
void ShooterServer::processDisconnect(sf::Uint16 senderId) {
2021-09-13 15:53:43 +03:00
sf::Packet sendPacket;
sendPacket << MsgType::Disconnect << senderId;
_players.erase(senderId);
2021-10-31 11:39:08 +03:00
for (const auto &player : _players) {
2021-09-13 15:53:43 +03:00
_socket.sendRely(sendPacket, player.first);
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
}
2021-10-31 11:39:08 +03:00
void ShooterServer::processCustomPacket(sf::Packet &packet, sf::Uint16 senderId) {
2021-09-13 15:53:43 +03:00
sf::Packet sendPacket;
double dbuff[10];
sf::Uint16 targetId;
double damage;
std::string tmp;
double newHealth;
2022-07-22 22:52:54 +03:00
std::string message;
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +03:00
ShooterMsgType type;
packet >> type;
2021-09-13 15:53:43 +03:00
switch (type) {
2021-10-28 16:58:02 +03:00
case ShooterMsgType::Damage:
2021-09-13 15:53:43 +03:00
packet >> targetId >> damage;
newHealth = _players[targetId]->health() - damage;
2021-10-31 11:39:08 +03:00
if (newHealth > 0) {
2021-09-13 15:53:43 +03:00
_players[targetId]->setHealth(newHealth);
2021-10-28 16:58:02 +03:00
} else {
2021-09-13 15:53:43 +03:00
_players[targetId]->setFullHealth();
_players[targetId]->setFullAbility();
_players[targetId]->addDeath();
_players[senderId]->addKill();
2021-10-28 16:58:02 +03:00
sendPacket << MsgType::Custom << ShooterMsgType::Kill << targetId << senderId;
2021-10-31 11:39:08 +03:00
for (auto &player : _players)
_socket.sendRely(sendPacket, player.first);
2021-09-13 15:53:43 +03:00
}
break;
2021-10-28 16:58:02 +03:00
case ShooterMsgType::FireTrace:
2021-09-13 15:53:43 +03:00
packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5];
2021-10-31 11:39:08 +03:00
sendPacket << MsgType::Custom << ShooterMsgType::FireTrace << dbuff[0] << dbuff[1] << dbuff[2] << dbuff[3]
<< dbuff[4] << dbuff[5];
for (auto &player : _players) {
if (player.first != senderId) {
2021-09-13 15:53:43 +03:00
_socket.send(sendPacket, player.first);
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
}
break;
2021-10-28 16:58:02 +03:00
case ShooterMsgType::RemoveBonus:
2021-09-13 15:53:43 +03:00
packet >> tmp;
2021-10-31 11:39:08 +03:00
if (tmp.find("Bonus_hill") != std::string::npos) {
2021-09-13 15:53:43 +03:00
_players[senderId]->setFullHealth();
}
2021-10-31 11:39:08 +03:00
if (tmp.find("Bonus_ability") != std::string::npos) {
2021-09-13 15:53:43 +03:00
_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-10-28 16:58:02 +03:00
sendPacket << MsgType::Custom << ShooterMsgType::RemoveBonus << tmp;
2021-10-31 11:39:08 +03:00
for (auto &player : _players) {
if (player.first != senderId) {
_socket.sendRely(sendPacket, player.first);
2021-10-28 16:58:02 +03:00
}
2021-09-13 15:53:43 +03:00
}
break;
2021-10-28 16:58:02 +03:00
case ShooterMsgType::ChangeWeapon:
packet >> tmp;
2021-10-28 16:58:02 +03:00
sendPacket << MsgType::Custom << ShooterMsgType::ChangeWeapon << senderId << tmp;
2021-10-31 11:39:08 +03:00
for (auto &player : _players) {
if (player.first != senderId) {
_socket.sendRely(sendPacket, player.first);
2021-10-28 16:58:02 +03:00
}
}
2022-07-22 22:52:54 +03:00
break;
case ShooterMsgType::newMessage:
packet >> message;
sendPacket << MsgType::Custom << ShooterMsgType::newMessage << _players[senderId]->playerNickName() << message;
if (message.length() == 0)
break;
2022-07-22 22:52:54 +03:00
for (auto& player : _players) {
if (player.first != senderId) {
_socket.send(sendPacket, player.first);
}
}
2021-10-17 10:21:10 +03:00
break;
default:
2021-10-31 11:39:08 +03:00
Log::log("ShooterServer::processCustomPacket: unknown message type " +
std::to_string(static_cast<int>(type)));
2021-10-17 10:44:17 +03:00
return;
2021-09-13 15:53:43 +03:00
}
}
2021-10-28 17:06:10 +03:00
void ShooterServer::processStop() {
2021-10-16 20:22:55 +03:00
_players.clear();
_bonuses.clear();
2021-09-13 15:53:43 +03:00
}
2021-10-28 17:06:10 +03:00
void ShooterServer::generateBonuses() {
2021-10-31 11:39:08 +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})});
_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})});
_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})});
_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})});
_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})});
_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})});
_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
}
2021-10-28 17:06:10 +03:00
void ShooterServer::updateInfo() {
2021-10-31 11:39:08 +03:00
for (auto&[bonusName, bonusInfo] : _bonuses) {
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-31 11:39:08 +03:00
sendPacket << MsgType::Custom << ShooterMsgType::AddBonus << bonusName << bonusInfo->position.x()
<< bonusInfo->position.y() << bonusInfo->position.z();
for (const auto &player : _players) {
2021-09-13 15:53:43 +03:00
_socket.sendRely(sendPacket, player.first);
2021-10-28 16:58:02 +03:00
}
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
2021-10-28 17:06:10 +03:00
ShooterServer::~ShooterServer() {
2021-10-16 20:22:55 +03:00
processStop();
}