1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-30 08:57:00 +02:00
vcmi/server/NetPacksLobbyServer.cpp

411 lines
11 KiB
C++
Raw Normal View History

/*
* NetPacksLobbyServer.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "LobbyNetPackVisitors.h"
#include "CVCMIServer.h"
#include "CGameHandler.h"
#include "../lib/StartInfo.h"
#include "../lib/CRandomGenerator.h"
2023-06-25 21:28:24 +02:00
#include "../lib/campaign/CampaignState.h"
#include "../lib/serializer/Connection.h"
2024-04-30 01:52:49 +02:00
#include "../lib/mapping/CMapInfo.h"
#include "../lib/mapping/CMapHeader.h"
#include "../lib/CTownHandler.h"
void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
{
if(pack.isForServer())
{
result = srv.isClientHost(pack.c->connectionID);
}
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitForLobby(CPackForLobby & pack)
{
// Propogate options after every CLobbyPackToServer
if(pack.isForServer())
{
srv.updateAndPropagateLobbyState();
}
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
{
result = srv.getState() == EServerState::LOBBY;
}
void ApplyOnServerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
{
srv.clientConnected(pack.c, pack.names, pack.uuid, pack.mode);
// Server need to pass some data to newly connected client
pack.clientId = pack.c->connectionID;
pack.mode = srv.si->mode;
pack.hostClientId = srv.hostClientId;
result = true;
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
{
// FIXME: we need to avoid senting something to client that not yet get answer for LobbyClientConnected
// Until UUID set we only pass LobbyClientConnected to this client
pack.c->uuid = pack.uuid;
srv.updateAndPropagateLobbyState();
2023-12-26 18:29:13 +02:00
// FIXME: what is this??? We do NOT support reconnection into ongoing game - at the very least queries and battles are NOT serialized
// if(srv.getState() == EServerState::GAMEPLAY)
// {
// //immediately start game
// std::unique_ptr<LobbyStartGame> startGameForReconnectedPlayer(new LobbyStartGame);
// startGameForReconnectedPlayer->initializedStartInfo = srv.si;
// startGameForReconnectedPlayer->initializedGameState = srv.gh->gameState();
// startGameForReconnectedPlayer->clientId = pack.c->connectionID;
// srv.announcePack(std::move(startGameForReconnectedPlayer));
// }
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
{
if(pack.clientId != pack.c->connectionID)
{
result = false;
return;
}
if(pack.shutdownServer)
{
if(!srv.wasStartedByClient())
{
result = false;
return;
}
2024-01-21 00:53:22 +02:00
if(pack.c->connectionID != srv.hostClientId)
{
result = false;
return;
}
}
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
{
srv.clientDisconnected(pack.c);
result = true;
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
{
if(pack.shutdownServer)
{
logNetwork->info("Client requested shutdown, server will close itself...");
srv.setState(EServerState::SHUTDOWN);
return;
}
else if(srv.activeConnections.empty())
{
logNetwork->error("Last connection lost, server will close itself...");
srv.setState(EServerState::SHUTDOWN);
}
else if(pack.c->connectionID == srv.hostClientId)
{
2022-12-07 23:36:20 +02:00
auto ph = std::make_unique<LobbyChangeHost>();
auto newHost = srv.activeConnections.front();
ph->newHostConnectionId = newHost->connectionID;
srv.announcePack(std::move(ph));
}
srv.updateAndPropagateLobbyState();
2023-09-29 19:49:18 +02:00
// if(srv.getState() != EServerState::SHUTDOWN && srv.remoteConnections.count(pack.c))
// {
// srv.remoteConnections -= pack.c;
// srv.connectToRemote();
// }
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
{
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetMap(LobbySetMap & pack)
{
if(srv.getState() != EServerState::LOBBY)
{
result = false;
return;
}
srv.updateStartInfoOnMapChange(pack.mapInfo, pack.mapGenOpts);
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetCampaign(LobbySetCampaign & pack)
{
srv.si->mapname = pack.ourCampaign->getFilename();
srv.si->mode = EStartMode::CAMPAIGN;
srv.si->campState = pack.ourCampaign;
2023-08-14 00:16:25 +02:00
srv.si->turnTimerInfo = TurnTimerInfo{};
bool isCurrentMapConquerable = pack.ourCampaign->currentScenario() && pack.ourCampaign->isAvailable(*pack.ourCampaign->currentScenario());
for(auto scenarioID : pack.ourCampaign->allScenarios())
{
if(pack.ourCampaign->isAvailable(scenarioID))
{
if(!isCurrentMapConquerable || (isCurrentMapConquerable && scenarioID == *pack.ourCampaign->currentScenario()))
{
srv.setCampaignMap(scenarioID);
}
}
}
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetCampaignMap(LobbySetCampaignMap & pack)
{
srv.setCampaignMap(pack.mapId);
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetCampaignBonus(LobbySetCampaignBonus & pack)
{
srv.setCampaignBonus(pack.bonusId);
result = true;
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
{
srv.prepareToRestart();
result = true;
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
{
for(const auto & connection : srv.activeConnections)
connection->enterLobbyConnectionMode();
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ApplyOnServerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
{
try
{
srv.verifyStateBeforeStart(true);
}
catch(...)
{
result = false;
return;
}
2023-08-21 03:06:58 +02:00
// Server will prepare gamestate and we announce StartInfo to clients
if(!srv.prepareToStartGame())
{
result = false;
return;
}
pack.initializedStartInfo = std::make_shared<StartInfo>(*srv.gh->getStartInfo(true));
pack.initializedGameState = srv.gh->gameState();
result = true;
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
{
if(pack.clientId == -1) //do not restart game for single client only
srv.startGameImmediately();
2022-10-01 16:28:45 +02:00
else
{
for(const auto & connection : srv.activeConnections)
2022-10-01 16:28:45 +02:00
{
if(connection->connectionID == pack.clientId)
2022-10-01 16:28:45 +02:00
{
connection->enterGameplayConnectionMode(srv.gh->gameState());
srv.reconnectPlayer(pack.clientId);
2022-10-01 16:28:45 +02:00
}
}
}
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ApplyOnServerNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
{
result = true;
}
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyChangeHost(LobbyChangeHost & pack)
{
auto result = srv.passHost(pack.newHostConnectionId);
if(!result)
{
logGlobal->error("passHost returned false. What does it mean?");
}
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
{
if(srv.isClientHost(pack.c->connectionID))
{
result = true;
return;
}
if(vstd::contains(srv.getAllClientPlayers(pack.c->connectionID), pack.color))
{
result = true;
return;
}
result = false;
}
void ApplyOnServerNetPackVisitor::visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack)
{
switch(pack.what)
{
case LobbyChangePlayerOption::TOWN_ID:
srv.optionSetCastle(pack.color, FactionID(pack.value));
break;
case LobbyChangePlayerOption::TOWN:
srv.optionNextCastle(pack.color, pack.value);
break;
case LobbyChangePlayerOption::HERO_ID:
srv.optionSetHero(pack.color, HeroTypeID(pack.value));
break;
case LobbyChangePlayerOption::HERO:
srv.optionNextHero(pack.color, pack.value);
break;
case LobbyChangePlayerOption::BONUS_ID:
srv.optionSetBonus(pack.color, PlayerStartingBonus(pack.value));
break;
case LobbyChangePlayerOption::BONUS:
srv.optionNextBonus(pack.color, pack.value);
break;
}
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetPlayer(LobbySetPlayer & pack)
{
srv.setPlayer(pack.clickedColor);
result = true;
}
2023-10-13 23:04:35 +02:00
void ApplyOnServerNetPackVisitor::visitLobbySetPlayerName(LobbySetPlayerName & pack)
{
srv.setPlayerName(pack.color, pack.name);
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetSimturns(LobbySetSimturns & pack)
{
srv.si->simturnsInfo = pack.simturnsInfo;
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack)
{
2023-08-14 00:16:25 +02:00
srv.si->turnTimerInfo = pack.turnTimerInfo;
result = true;
}
2023-12-28 21:48:19 +02:00
void ApplyOnServerNetPackVisitor::visitLobbySetExtraOptions(LobbySetExtraOptions & pack)
2023-12-27 15:39:35 +02:00
{
2023-12-28 21:48:19 +02:00
srv.si->extraOptionsInfo = pack.extraOptionsInfo;
2023-12-27 15:39:35 +02:00
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack)
{
2023-04-11 00:00:46 +02:00
srv.si->difficulty = std::clamp<uint8_t>(pack.difficulty, 0, 4);
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack)
{
srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
result = true;
}
2024-04-30 01:52:49 +02:00
2024-05-01 15:58:22 +02:00
void ClientPermissionsCheckerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
{
result = true;
}
2024-04-30 01:52:49 +02:00
void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
{
2024-05-01 01:26:51 +02:00
std::vector<FactionID> allowedTowns;
2024-05-01 00:49:33 +02:00
2024-05-01 01:26:51 +02:00
VLC->townh->forEach([pack, &allowedTowns](const Faction *entity, bool &stop){
2024-05-01 00:49:33 +02:00
if(!entity->hasTown())
return;
if(std::find(pack.bannedTowns.begin(), pack.bannedTowns.end(), entity->getId()) == pack.bannedTowns.end()) {
2024-05-01 01:26:51 +02:00
allowedTowns.push_back(entity->getId());
2024-05-01 00:49:33 +02:00
}
});
2024-04-30 01:52:49 +02:00
2024-05-01 00:49:33 +02:00
std::vector<FactionID> randomFaction1;
2024-05-01 01:26:51 +02:00
std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction1), 1, std::mt19937{std::random_device{}()});
2024-05-01 00:49:33 +02:00
std::vector<FactionID> randomFaction2;
2024-05-01 01:26:51 +02:00
std::sample(allowedTowns.begin(), allowedTowns.end(), std::back_inserter(randomFaction2), 1, std::mt19937{std::random_device{}()});
2024-04-30 01:52:49 +02:00
switch(pack.action) {
case LobbyPvPAction::COIN:
2024-05-01 15:00:36 +02:00
srv.announceTxt("~~vcmi.lobby.pvp.coin.hover~~ - " + std::to_string(std::rand()%2));
2024-04-30 01:52:49 +02:00
break;
case LobbyPvPAction::RANDOM_TOWN:
2024-05-01 01:26:51 +02:00
if(allowedTowns.size())
2024-05-01 15:00:36 +02:00
srv.announceTxt("~~core.overview.3~~ - ~~" + VLC->townh->getById(randomFaction1[0])->getNameTextID() + "~~");
2024-04-30 01:52:49 +02:00
break;
case LobbyPvPAction::RANDOM_TOWN_VS:
2024-05-01 01:26:51 +02:00
if(allowedTowns.size())
2024-05-01 15:30:35 +02:00
srv.announceTxt("~~core.overview.3~~ - ~~" + VLC->townh->getById(randomFaction1[0])->getNameTextID() + "~~ ~~vcmi.lobby.pvp.versus~~ ~~" + VLC->townh->getById(randomFaction2[0])->getNameTextID() + "~~");
2024-04-30 01:52:49 +02:00
break;
}
result = true;
}