1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Server functionality

This commit is contained in:
Laserlicht 2024-04-30 01:52:49 +02:00
parent f4d1442a86
commit 0565c062e5
9 changed files with 78 additions and 2 deletions

View File

@ -75,7 +75,10 @@ void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName,
lobby->card->chat->addNewMessage(formatted.toString());
if (lobby->card->chatMode != InfoCard::ChatMode::Enabled)
lobby->card->setChat(InfoCard::ChatMode::Enabled);
{
lobby->card->setChat(InfoCard::ChatMode::Disabled);
lobby->toggleChat();
}
}
chatHistory.push_back({senderName, messageText, TextOperations::getCurrentFormattedTimeLocal()});

View File

@ -49,6 +49,7 @@
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/mapping/CMapHeader.h"
#include "../../lib/mapping/CMapInfo.h"
#include "../../lib/networkPacks/PacksForLobby.h"
ISelectionScreenInfo::ISelectionScreenInfo(ESelectionScreen ScreenType)
: screenType(ScreenType)
@ -402,8 +403,26 @@ PvPBox::PvPBox(const Rect & rect)
pos += rect.topLeft();
setRedrawParent(true);
buttonFlipCoin = std::make_shared<CButton>(Point(17, 160), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("flip coin"), [](){ std::cout << "coin flip"; }, EShortcut::NONE);
buttonFlipCoin = std::make_shared<CButton>(Point(17, 160), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("flip coin"), [](){
LobbyPvPAction lpa;
lpa.action = LobbyPvPAction::COIN;
CSH->sendLobbyPack(lpa);
}, EShortcut::NONE);
buttonFlipCoin->setTextOverlay("Flip coin2", EFonts::FONT_SMALL, Colors::WHITE);
buttonRandomTown = std::make_shared<CButton>(Point(17, 184), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("random town"), [](){
LobbyPvPAction lpa;
lpa.action = LobbyPvPAction::RANDOM_TOWN;
CSH->sendLobbyPack(lpa);
}, EShortcut::NONE);
buttonRandomTown->setTextOverlay("random town", EFonts::FONT_SMALL, Colors::WHITE);
buttonRandomTownVs = std::make_shared<CButton>(Point(17, 208), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("random town vs"), [](){
LobbyPvPAction lpa;
lpa.action = LobbyPvPAction::RANDOM_TOWN_VS;
CSH->sendLobbyPack(lpa);
}, EShortcut::NONE);
buttonRandomTownVs->setTextOverlay("random town vs", EFonts::FONT_SMALL, Colors::WHITE);
}
CFlagBox::CFlagBox(const Rect & rect)

View File

@ -147,6 +147,8 @@ class PvPBox : public CIntObject
{
public:
std::shared_ptr<CButton> buttonFlipCoin;
std::shared_ptr<CButton> buttonRandomTown;
std::shared_ptr<CButton> buttonRandomTownVs;
PvPBox(const Rect & rect);
};

View File

@ -172,6 +172,7 @@ public:
virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) {}
virtual void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) {}
virtual void visitLobbyShowMessage(LobbyShowMessage & pack) {}
virtual void visitLobbyPvPAction(LobbyPvPAction & pack) {}
};
VCMI_LIB_NAMESPACE_END

View File

@ -813,6 +813,11 @@ void LobbyShowMessage::visitTyped(ICPackVisitor & visitor)
visitor.visitLobbyShowMessage(*this);
}
void LobbyPvPAction::visitTyped(ICPackVisitor & visitor)
{
visitor.visitLobbyPvPAction(*this);
}
void SetResources::applyGs(CGameState * gs) const
{
assert(player.isValidPlayer());

View File

@ -343,4 +343,19 @@ struct DLL_LINKAGE LobbyShowMessage : public CLobbyPackToPropagate
}
};
struct DLL_LINKAGE LobbyPvPAction : public CLobbyPackToServer
{
enum EAction : ui8 {
NONE, COIN, RANDOM_TOWN, RANDOM_TOWN_VS
} action = NONE;
void visitTyped(ICPackVisitor & visitor) override;
template <typename Handler> void serialize(Handler &h)
{
h & action;
}
};
VCMI_LIB_NAMESPACE_END

View File

@ -59,6 +59,7 @@ void registerTypesLobbyPacks(Serializer &s)
s.template registerType<CLobbyPackToServer, LobbySetDifficulty>();
s.template registerType<CLobbyPackToServer, LobbyForceSetPlayer>();
s.template registerType<CLobbyPackToServer, LobbySetExtraOptions>();
s.template registerType<CLobbyPackToServer, LobbyPvPAction>();
}
VCMI_LIB_NAMESPACE_END

View File

@ -93,4 +93,5 @@ public:
void visitLobbySetSimturns(LobbySetSimturns & pack) override;
void visitLobbySetDifficulty(LobbySetDifficulty & pack) override;
void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) override;
void visitLobbyPvPAction(LobbyPvPAction & pack) override;
};

View File

@ -18,6 +18,9 @@
#include "../lib/campaign/CampaignState.h"
#include "../lib/serializer/Connection.h"
#include "../lib/mapping/CMapInfo.h"
#include "../lib/mapping/CMapHeader.h"
#include "../lib/CTownHandler.h"
void ClientPermissionsCheckerNetPackVisitor::visitForLobby(CPackForLobby & pack)
{
@ -366,3 +369,29 @@ void ApplyOnServerNetPackVisitor::visitLobbyForceSetPlayer(LobbyForceSetPlayer &
srv.si->playerInfos[pack.targetPlayerColor].connectedPlayerIDs.insert(pack.targetConnectedPlayer);
result = true;
}
void ApplyOnServerNetPackVisitor::visitLobbyPvPAction(LobbyPvPAction & pack)
{
std::set<FactionID> allowedFactions;
for(auto & player : srv.mi->mapHeader->players)
{
std::set<FactionID> tmpAllowedFactions = player.allowedFactions;
std::set_union(std::begin(tmpAllowedFactions), std::end(tmpAllowedFactions), std::begin(allowedFactions), std::end(allowedFactions), std::inserter(allowedFactions, std::begin(allowedFactions)));
}
std::vector<FactionID> randomFactions;
std::sample(allowedFactions.begin(), allowedFactions.end(), std::back_inserter(randomFactions), 2, std::mt19937{std::random_device{}()});
switch(pack.action) {
case LobbyPvPAction::COIN:
srv.announceTxt("Coin - " + std::to_string(std::rand()%2));
break;
case LobbyPvPAction::RANDOM_TOWN:
srv.announceTxt("Faction - " + VLC->townh->getById(randomFactions[0])->getNameTranslated());
break;
case LobbyPvPAction::RANDOM_TOWN_VS:
srv.announceTxt("Factions - " + VLC->townh->getById(randomFactions[0])->getNameTranslated() + " vs. " + VLC->townh->getById(randomFactions[randomFactions.size() > 1 ? 1 : 0])->getNameTranslated());
break;
}
result = true;
}