feat: add NpcDialogueForm

This commit is contained in:
KobeBryant114514
2024-10-10 11:59:48 +08:00
Unverified
parent efacbeb89b
commit 5cc610a830
2 changed files with 130 additions and 7 deletions
+55 -2
View File
@@ -1,6 +1,10 @@
const getNextCallbackId = ll.import("GMLIB_FormAPI", "getNextFormCallbackId"); const getNextCallbackId = ll.import("GMLIB_FormAPI", "getNextFormCallbackId");
class ServerSettingForm { class ServerSettingForm {
constructor() {
throw new Error("Static class cannot be instantiated");
}
static getDefaultPriority() { static getDefaultPriority() {
return ll.import("GMLIB_ServerSettingForm", "getDefaultPriority")(); return ll.import("GMLIB_ServerSettingForm", "getDefaultPriority")();
} }
@@ -123,7 +127,56 @@ class ServerSettingForm {
static removeElement(id) { static removeElement(id) {
return ll.import("GMLIB_ServerSettingForm", "removeElement")(id); return ll.import("GMLIB_ServerSettingForm", "removeElement")(id);
} }
} }
module.exports = { ServerSettingForm }; class NpcDialogueForm {
constructor(npcName, sceneName, dialogue) {
this.mFormId = ll.import("GMLIB_NpcDialogueForm", "createForm")(npcName, sceneName, dialogue);
}
addButton(button) {
return ll.import("GMLIB_NpcDialogueForm", "addButton")(this.mFormId, button);
}
sendTo(pl, callback = (pl, index, type) => { }, free = true) {
let callbackId = getNextCallbackId();
ll.export(callback, "GMLIB_FORM_CALLBACK", callbackId);
ll.import("GMLIB_NpcDialogueForm", "sendTo")(this.mFormId, pl, callbackId);
if (free) {
this.destroy();
}
}
destroy() {
ll.import("GMLIB_NpcDialogueForm", "destroyForm")(this.mFormId);
}
}
class ChestForm {
constructor(npcName, sceneName, dialogue) {
this.mFormId = ll.import("GMLIB_NpcDialogueForm", "createForm")(npcName, sceneName, dialogue);
}
addButton(button) {
return ll.import("GMLIB_NpcDialogueForm", "addButton")(this.mFormId, button);
}
sendTo(pl, free = true) {
//let callbackId = getNextCallbackId();
//ll.export(callback, "GMLIB_FORM_CALLBACK", callbackId);
//ll.import("GMLIB_NpcDialogueForm", "sendTo")(this.mFormId, pl, callbackId);
//if (free) {
// this.destroy();
//}
}
destroy() {
//ll.import("GMLIB_NpcDialogueForm", "destroyForm")(this.mFormId);
}
}
module.exports = {
ServerSettingForm,
NpcDialogueForm,
ChestForm
};
+74 -4
View File
@@ -1,12 +1,16 @@
#include "Global.h" #include "Global.h"
#include <GMLIB/Server/FormAPI/ChestForm.h>
using namespace GMLIB::Server::Form; using namespace GMLIB::Server::Form;
using namespace ll::hash_utils; using namespace ll::hash_utils;
class LegacyScriptFormManager { class LegacyScriptFormManager {
private: private:
int64 mNextFormCallbackId = 0; int64 mNextFormCallbackId = 0;
// std::unordered_map<int64, ll::event::ListenerPtr> mEventListeners; int64 mNextFormId = 0;
std::unordered_map<int64, std::unique_ptr<NpcDialogueForm>> mNpcDialogueForms;
std::unordered_map<int64, std::unique_ptr<ChestForm>> mChestForms;
public: public:
std::string getNextFormCallbackId() { std::string getNextFormCallbackId() {
@@ -14,6 +18,33 @@ public:
return "GMLIB_EVENT_" + std::to_string(mNextFormCallbackId); return "GMLIB_EVENT_" + std::to_string(mNextFormCallbackId);
} }
int64 getNextFormId() {
mNextFormId++;
return mNextFormId;
}
int64 createNpcDialogueForm(std::string const& npcName, std::string const& sceneName, std::string const& dialogue) {
auto formId = LegacyScriptFormManager::getInstance().getNextFormId();
auto formPtr = std::make_unique<NpcDialogueForm>(npcName, sceneName, dialogue);
mNpcDialogueForms[formId] = std::move(formPtr);
return formId;
}
bool destroyNpcDialogueForm(int64 formId) {
if (mNpcDialogueForms.contains(formId)) {
mNpcDialogueForms.erase(formId);
return true;
}
return false;
}
optional_ref<NpcDialogueForm> getNpcDialogueForm(int64 formId) {
if (mNpcDialogueForms.contains(formId)) {
return mNpcDialogueForms[formId].get();
}
return {};
}
public: public:
static LegacyScriptFormManager& getInstance() { static LegacyScriptFormManager& getInstance() {
static std::unique_ptr<LegacyScriptFormManager> instance; static std::unique_ptr<LegacyScriptFormManager> instance;
@@ -89,12 +120,14 @@ public:
void Export_Form_API() { void Export_Form_API() {
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getDefaultPriority", []() -> int { //////////////////////////////// Form Manager /////////////////////////////////
return ServerSettingForm::getDefaultPriority();
});
RemoteCall::exportAs("GMLIB_FormAPI", "getNextFormCallbackId", []() -> std::string { RemoteCall::exportAs("GMLIB_FormAPI", "getNextFormCallbackId", []() -> std::string {
return LegacyScriptFormManager::getInstance().getNextFormCallbackId(); return LegacyScriptFormManager::getInstance().getNextFormCallbackId();
}); });
////////////////////////////// ServerSettingForm //////////////////////////////
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getDefaultPriority", []() -> int {
return ServerSettingForm::getDefaultPriority();
});
RemoteCall::exportAs("GMLIB_ServerSettingForm", "hasTitle", []() -> bool { return ServerSettingForm::hasTitle(); }); RemoteCall::exportAs("GMLIB_ServerSettingForm", "hasTitle", []() -> bool { return ServerSettingForm::hasTitle(); });
RemoteCall::exportAs("GMLIB_ServerSettingForm", "getTitle", []() -> std::string { RemoteCall::exportAs("GMLIB_ServerSettingForm", "getTitle", []() -> std::string {
return ServerSettingForm::getTitle(); return ServerSettingForm::getTitle();
@@ -229,4 +262,41 @@ void Export_Form_API() {
RemoteCall::exportAs("GMLIB_ServerSettingForm", "removeElement", [](uint id) -> bool { RemoteCall::exportAs("GMLIB_ServerSettingForm", "removeElement", [](uint id) -> bool {
return ServerSettingForm::removeElement(id); return ServerSettingForm::removeElement(id);
}); });
////////////////////////////// NpcDialogueForm //////////////////////////////
RemoteCall::exportAs(
"GMLIB_NpcDialogueForm",
"createForm",
[](std::string const& npcName, std::string const& sceneName, std::string const& dialogue) -> int64 {
return LegacyScriptFormManager::getInstance().createNpcDialogueForm(npcName, sceneName, dialogue);
}
);
RemoteCall::exportAs("GMLIB_NpcDialogueForm", "destroyForm", [](int64 formId) -> bool {
return LegacyScriptFormManager::getInstance().destroyNpcDialogueForm(formId);
});
RemoteCall::exportAs("GMLIB_NpcDialogueForm", "addButton", [](int64 formId, std::string const& button) -> int {
if (auto formPtr = LegacyScriptFormManager::getInstance().getNpcDialogueForm(formId)) {
return formPtr->addButton(button);
}
return -1;
});
RemoteCall::exportAs(
"GMLIB_NpcDialogueForm",
"sendTo",
[](int64 formId, Player* pl, std::string const& callbackId) -> void {
if (auto formPtr = LegacyScriptFormManager::getInstance().getNpcDialogueForm(formId)) {
formPtr->sendTo(*pl, [callbackId](Player& pl, int index, NpcRequestPacket::RequestType type) -> void {
try {
if (RemoteCall::hasFunc("GMLIB_FORM_CALLBACK", callbackId)) {
auto const& callback = RemoteCall::importAs<void(Player*, int index, int type)>(
"GMLIB_FORM_CALLBACK",
callbackId
);
callback(&pl, index, (int)type);
}
} catch (...) {}
});
}
}
);
////////////////////////////// ChestForm //////////////////////////////
} }