Skip to content

Commit 6318ca7

Browse files
committed
Implement Dialog packet and add MP.ConfirmationDialog
1 parent 731599f commit 6318ca7

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

include/LuaAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace MP {
3636
std::pair<bool, std::string> DropPlayer(int ID, std::optional<std::string> MaybeReason);
3737
std::pair<bool, std::string> SendChatMessage(int ID, const std::string& Message);
3838
std::pair<bool, std::string> SendNotification(int ID, const std::string& Message, const std::string& Icon, const std::string& Category);
39+
std::pair<bool, std::string> ConfirmationDialog(int ID, const std::string& Title, const std::string& Body, const sol::table& buttons, const std::string& InteractionID, const bool& warning = false);
3940
std::pair<bool, std::string> RemoveVehicle(int PlayerID, int VehicleID);
4041
void Set(int ConfigID, sol::object NewValue);
4142
TLuaValue Get(int ConfigID);

src/LuaAPI.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,46 @@ std::pair<bool, std::string> LuaAPI::MP::SendNotification(int ID, const std::str
241241
return Result;
242242
}
243243

244+
std::pair<bool, std::string> LuaAPI::MP::ConfirmationDialog(int ID, const std::string& Title, const std::string& Body, const sol::table& buttons, const std::string& InteractionID, const bool& warning) {
245+
std::pair<bool, std::string> Result;
246+
247+
const nlohmann::json PacketBody = {
248+
{ "title", Title },
249+
{ "body", Body },
250+
{ "buttons", nlohmann::json::parse(JsonEncode(buttons), nullptr, false) },
251+
{ "interactionID", InteractionID },
252+
{ "class", warning ? "experimental" : "" }
253+
};
254+
255+
std::string Packet = "D" + PacketBody.dump();
256+
if (ID == -1) {
257+
Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
258+
Result.first = true;
259+
} else {
260+
auto MaybeClient = GetClient(Engine->Server(), ID);
261+
if (MaybeClient) {
262+
auto c = MaybeClient.value().lock();
263+
if (!c->IsSynced()) {
264+
Result.first = false;
265+
Result.second = "Player is not synced yet";
266+
return Result;
267+
}
268+
if (!Engine->Network().Respond(*c, StringToVector(Packet), true)) {
269+
beammp_errorf("Failed to send confirmation dialog to player (id {}) - did the player disconnect?", ID);
270+
Result.first = false;
271+
Result.second = "Failed to send packet";
272+
}
273+
Result.first = true;
274+
} else {
275+
beammp_lua_error("ConfirmationDialog invalid argument [1] invalid ID");
276+
Result.first = false;
277+
Result.second = "Invalid Player ID";
278+
}
279+
return Result;
280+
}
281+
return Result;
282+
}
283+
244284
std::pair<bool, std::string> LuaAPI::MP::RemoveVehicle(int PID, int VID) {
245285
std::pair<bool, std::string> Result;
246286
auto MaybeClient = GetClient(Engine->Server(), PID);

src/TLuaEngine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,12 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
876876
beammp_lua_error("SendNotification expects 2, 3 or 4 arguments.");
877877
}
878878
});
879+
MPTable.set_function("ConfirmationDialog", sol::overload(
880+
&LuaAPI::MP::ConfirmationDialog,
881+
[&](const int& ID, const std::string& Title, const std::string& Body, const sol::table& Buttons, const std::string& InteractionID) {
882+
LuaAPI::MP::ConfirmationDialog(ID, Title, Body, Buttons, InteractionID);
883+
}
884+
));
879885
MPTable.set_function("GetPlayers", [&]() -> sol::table {
880886
return Lua_GetPlayers();
881887
});

0 commit comments

Comments
 (0)