[Server] Use queue for MessageBox packets

sol2-server-rewrite
Koncord 7 years ago
parent 9dae748a76
commit 2bfd4627ed

@ -30,129 +30,100 @@ GUI::GUI(Player *player): BaseMgr(player)
void GUI::processUpdate() void GUI::processUpdate()
{ {
player->guiMessageBox = guiQueue.front().first;
auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GUI_MESSAGEBOX); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GUI_MESSAGEBOX);
packet->setPlayer(player); packet->setPlayer(player);
packet->Send(false); packet->Send(false);
} }
int GUI::generateGuiId() uint64_t GUI::generateGuiId()
{ {
int id = 0; return RakNet::RakPeerInterface::Get64BitUniqueRandomNumber();
for (const auto &item: callbacks)
{
if (item.second == nullptr)
{
id = item.first;
break;
}
}
if (id == 0)
id = lastGuiId++;
return id;
} }
void GUI::messageBox(sol::function fn, const char *label, sol::this_environment te) void GUI::messageBox(sol::function fn, const char *label, sol::this_environment te)
{ {
if(fn.valid()) mwmp::BasePlayer::GUIMessageBox mbox;
{
int id = generateGuiId();
callbacks[id] = std::make_shared<sol::function>(fn);
player->guiMessageBox.id = id;
}
else
player->guiMessageBox.id = -1;
player->guiMessageBox.label = label; mbox.id = generateGuiId();
player->guiMessageBox.type = Player::GUIMessageBox::Type::MessageBox; mbox.label = label;
mbox.type = Player::GUIMessageBox::Type::MessageBox;
guiQueue.emplace(std::move(mbox), std::move(fn));
setChanged(); setChanged();
} }
void GUI::customMessageBox(sol::function fn, const char *label, const char *buttons, sol::this_environment te) void GUI::customMessageBox(sol::function fn, const char *label, const char *buttons, sol::this_environment te)
{ {
if(fn.valid()) mwmp::BasePlayer::GUIMessageBox mbox;
{ mbox.id = generateGuiId();
int id = generateGuiId(); mbox.label = label;
callbacks[id] = std::make_shared<sol::function>(fn); mbox.buttons = buttons;
player->guiMessageBox.id = id; mbox.type = Player::GUIMessageBox::Type::CustomMessageBox;
}
else
player->guiMessageBox.id = -1;
player->guiMessageBox.label = label; guiQueue.emplace(std::move(mbox), std::move(fn));
player->guiMessageBox.buttons = buttons;
player->guiMessageBox.type = Player::GUIMessageBox::Type::CustomMessageBox;
setChanged(); setChanged();
} }
void GUI::inputDialog(sol::function fn, const char *label, sol::this_environment te) void GUI::inputDialog(sol::function fn, const char *label, sol::this_environment te)
{ {
if(fn.valid()) mwmp::BasePlayer::GUIMessageBox mbox;
{
int id = generateGuiId();
callbacks[id] = std::make_shared<sol::function>(fn);
player->guiMessageBox.id = id;
}
else
player->guiMessageBox.id = -1;
player->guiMessageBox.label = label; mbox.id = generateGuiId();
player->guiMessageBox.type = Player::GUIMessageBox::Type::InputDialog; mbox.label = label;
mbox.type = Player::GUIMessageBox::Type::InputDialog;
guiQueue.emplace(std::move(mbox), std::move(fn));
setChanged(); setChanged();
} }
void GUI::passwordDialog(sol::function fn, const char *label, const char *note, sol::this_environment te) void GUI::passwordDialog(sol::function fn, const char *label, const char *note, sol::this_environment te)
{ {
if(fn.valid()) mwmp::BasePlayer::GUIMessageBox mbox;
{
int id = generateGuiId(); mbox.id = generateGuiId();
callbacks[id] = std::make_shared<sol::function>(fn); mbox.label = label;
player->guiMessageBox.id = id; mbox.note = note;
} mbox.type = Player::GUIMessageBox::Type::PasswordDialog;
else
player->guiMessageBox.id = -1;
player->guiMessageBox.label = label; guiQueue.emplace(std::move(mbox), std::move(fn));
player->guiMessageBox.note = note;
player->guiMessageBox.type = Player::GUIMessageBox::Type::PasswordDialog;
setChanged(); setChanged();
} }
void GUI::listBox(sol::function fn, const char *label, const char *items, sol::this_environment te) void GUI::listBox(sol::function fn, const char *label, const char *items, sol::this_environment te)
{ {
if(fn.valid()) mwmp::BasePlayer::GUIMessageBox mbox;
{
int id = generateGuiId();
callbacks[id] = std::make_shared<sol::function>(fn);
player->guiMessageBox.id = id;
}
else
player->guiMessageBox.id = -1;
player->guiMessageBox.label = label; mbox.id = generateGuiId();
player->guiMessageBox.data = items;
player->guiMessageBox.type = Player::GUIMessageBox::Type::ListBox; mbox.label = label;
mbox.data = items;
mbox.type = Player::GUIMessageBox::Type::ListBox;
guiQueue.emplace(std::move(mbox), std::move(fn));
setChanged(); setChanged();
} }
void GUI::onGUIAction() void GUI::onGUIAction()
{ {
if(player->guiMessageBox.id == -1) auto mbox = std::move(guiQueue.front().first);
return; auto callback = std::move(guiQueue.front().second);
guiQueue.pop();
auto it = callbacks.find(player->guiMessageBox.id); if(!guiQueue.empty())
if (it != callbacks.end() && it->second != nullptr) setChanged();
if(player->guiMessageBox.id != mbox.id)
{ {
it->second->call(player, player->guiMessageBox.data); LOG_MESSAGE_SIMPLE(Log::LOG_ERROR, "Wrong MessageBox id from %s(%d).", player->npc.mName, player->getId());
it->second = nullptr; return;
} }
if (callback.valid())
callback.call(player, player->guiMessageBox.data);
} }
void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state) void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state)

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <queue>
#include "Window.hpp" #include "Window.hpp"
#include "BaseMgr.hpp" #include "BaseMgr.hpp"
@ -40,12 +41,11 @@ public:
unsigned int getChanges() const; unsigned int getChanges() const;
private: private:
int generateGuiId(); uint64_t generateGuiId();
void processUpdate() final; void processUpdate() final;
std::unordered_map<int, std::shared_ptr<Window>> windows; std::unordered_map<int, std::shared_ptr<Window>> windows;
std::unordered_map<int, std::shared_ptr<sol::function>> callbacks; std::queue<std::pair<mwmp::BasePlayer::GUIMessageBox, sol::function>> guiQueue;
int lastWindowId; int lastWindowId;
int lastGuiId; // for message boxes
}; };
class QuickKey class QuickKey

Loading…
Cancel
Save