Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(LuaEngine): Add RegisterTicketEvent and TicketMethods #225

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
27 changes: 27 additions & 0 deletions src/ElunaLuaEngine_SC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,32 @@ class Eluna_WorldScript : public WorldScript
}
};

class Eluna_TicketScript : public TicketScript
{
public:
Eluna_TicketScript() : TicketScript("Eluna_TicketScript") { }

void OnTicketCreate(Player* player, GmTicket* ticket) override
{
sEluna->OnTicketCreate(player, ticket);
}

void OnTicketUpdate(Player* player, GmTicket* ticket) override
{
sEluna->OnTicketUpdate(player, ticket);
}

void OnTicketClose(Player* player, GmTicket* ticket) override
{
sEluna->OnTicketClose(player, ticket);
}

void OnTicketResolve(Player* player, GmTicket* ticket) override
{
sEluna->OnTicketResolve(player, ticket);
}
};

// Group all custom scripts
void AddSC_ElunaLuaEngine()
{
Expand All @@ -1028,6 +1054,7 @@ void AddSC_ElunaLuaEngine()
new Eluna_PlayerScript();
new Eluna_ServerScript();
new Eluna_SpellSC();
new Eluna_TicketScript();
new Eluna_UnitScript();
new Eluna_VehicleScript();
new Eluna_WorldObjectScript();
Expand Down
11 changes: 11 additions & 0 deletions src/LuaEngine/Hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace Hooks
REGTYPE_BG,
REGTYPE_MAP,
REGTYPE_INSTANCE,
REGTYPE_TICKET,
REGTYPE_SPELL,
REGTYPE_COUNT
};
Expand Down Expand Up @@ -367,6 +368,16 @@ namespace Hooks
INSTANCE_EVENT_COUNT
};

enum TicketEvents
{
TICKET_EVENT_ON_CREATE = 1, // (event, player, ticket)
TICKET_EVENT_ON_UPDATE = 2, // (event, player, ticket, message)
TICKET_EVENT_ON_CLOSE = 3, // (event, player, ticket)
TICKET_EVENT_STATUS_UPDATE = 4, // (event, player, ticket)
TICKET_EVENT_ON_RESOLVE = 5, // (event, player, ticket)
TICKET_EVENT_COUNT
};

enum SpellEvents
{
SPELL_EVENT_ON_PREPARE = 1, // (event, caster, spell)
Expand Down
11 changes: 11 additions & 0 deletions src/LuaEngine/LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ ItemGossipBindings(NULL),
PlayerGossipBindings(NULL),
MapEventBindings(NULL),
InstanceEventBindings(NULL),
TicketEventBindings(NULL),
SpellEventBindings(NULL),

CreatureUniqueBindings(NULL)
Expand Down Expand Up @@ -270,6 +271,7 @@ void Eluna::CreateBindStores()
GroupEventBindings = new BindingMap< EventKey<Hooks::GroupEvents> >(L);
VehicleEventBindings = new BindingMap< EventKey<Hooks::VehicleEvents> >(L);
BGEventBindings = new BindingMap< EventKey<Hooks::BGEvents> >(L);
TicketEventBindings = new BindingMap< EventKey<Hooks::TicketEvents> >(L);

PacketEventBindings = new BindingMap< EntryKey<Hooks::PacketEvents> >(L);
CreatureEventBindings = new BindingMap< EntryKey<Hooks::CreatureEvents> >(L);
Expand Down Expand Up @@ -1209,6 +1211,15 @@ int Eluna::Register(lua_State* L, uint8 regtype, uint32 entry, ObjectGuid guid,
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_TICKET:
if (event_id < Hooks::TICKET_EVENT_COUNT)
{
auto key = EventKey<Hooks::TicketEvents>((Hooks::TicketEvents)event_id);
bindingID = TicketEventBindings->Insert(key, functionRef, shots);
createCancelCallback(L, bindingID, TicketEventBindings);
return 1; // Stack: callback
}
break;
case Hooks::REGTYPE_SPELL:
if (event_id < Hooks::SPELL_EVENT_COUNT)
{
Expand Down
9 changes: 9 additions & 0 deletions src/LuaEngine/LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ElunaUtility.h"
#include "HttpManager.h"
#include "EventEmitter.h"
#include "TicketMgr.h"
#include <mutex>
#include <memory>

Expand Down Expand Up @@ -220,6 +221,7 @@ class ELUNA_GAME_API Eluna
BindingMap< EntryKey<Hooks::GossipEvents> >* PlayerGossipBindings;
BindingMap< EntryKey<Hooks::InstanceEvents> >* MapEventBindings;
BindingMap< EntryKey<Hooks::InstanceEvents> >* InstanceEventBindings;
BindingMap< EventKey<Hooks::TicketEvents> >* TicketEventBindings;
BindingMap< EntryKey<Hooks::SpellEvents> >* SpellEventBindings;

BindingMap< UniqueObjectKey<Hooks::CreatureEvents> >* CreatureUniqueBindings;
Expand Down Expand Up @@ -530,6 +532,13 @@ class ELUNA_GAME_API Eluna
void OnBGCreate(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId);
void OnBGDestroy(BattleGround* bg, BattleGroundTypeId bgId, uint32 instanceId);

/* Ticket */
void OnTicketCreate(Player* player, GmTicket* ticket);
void OnTicketUpdate(Player* player, GmTicket* ticket);
void OnTicketClose(Player* player, GmTicket* ticket);
void OnTicketStatusUpdate(Player* player, GmTicket* ticket);
void OnTicketResolve(Player* player, GmTicket* ticket);

/* Spell */
void OnSpellPrepare(Unit* caster, Spell* spell, SpellInfo const* spellInfo);
void OnSpellCast(Unit* caster, Spell* spell, SpellInfo const* spellInfo, bool skipCheck);
Expand Down
37 changes: 37 additions & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C"
#include "AchievementMethods.h"
#include "ItemTemplateMethods.h"
#include "RollMethods.h"
#include "TicketMethods.h"

// DBCStores includes
#include "GemPropertiesEntryMethods.h"
Expand All @@ -64,6 +65,7 @@ luaL_Reg GlobalMethods[] =
{ "RegisterBGEvent", &LuaGlobalFunctions::RegisterBGEvent },
{ "RegisterMapEvent", &LuaGlobalFunctions::RegisterMapEvent },
{ "RegisterInstanceEvent", &LuaGlobalFunctions::RegisterInstanceEvent },
{ "RegisterTicketEvent", &LuaGlobalFunctions::RegisterTicketEvent },
{ "RegisterSpellEvent", &LuaGlobalFunctions::RegisterSpellEvent },

{ "ClearBattleGroundEvents", &LuaGlobalFunctions::ClearBattleGroundEvents },
Expand All @@ -82,6 +84,7 @@ luaL_Reg GlobalMethods[] =
{ "ClearServerEvents", &LuaGlobalFunctions::ClearServerEvents },
{ "ClearMapEvents", &LuaGlobalFunctions::ClearMapEvents },
{ "ClearInstanceEvents", &LuaGlobalFunctions::ClearInstanceEvents },
{ "ClearTicketEvents", &LuaGlobalFunctions::ClearTicketEvents },
{ "ClearSpellEvents", &LuaGlobalFunctions::ClearSpellEvents },

// Getters
Expand Down Expand Up @@ -1297,6 +1300,37 @@ ElunaRegister<Roll> RollMethods[] =
{ NULL, NULL }
};

ElunaRegister<GmTicket> TicketMethods[] =
{
{ "IsClosed", &LuaTicket::IsClosed },
{ "IsCompleted", &LuaTicket::IsCompleted },
{ "IsFromPlayer", &LuaTicket::IsFromPlayer },
{ "IsAssigned", &LuaTicket::IsAssigned },
{ "IsAssignedTo", &LuaTicket::IsAssignedTo },
{ "IsAssignedNotTo", &LuaTicket::IsAssignedNotTo },

{ "GetId", &LuaTicket::GetId },
{ "GetPlayer", &LuaTicket::GetPlayer },
{ "GetPlayerName", &LuaTicket::GetPlayerName },
{ "GetMessage", &LuaTicket::GetMessage },
{ "GetAssignedPlayer", &LuaTicket::GetAssignedPlayer },
{ "GetAssignedToGUID", &LuaTicket::GetAssignedToGUID },
{ "GetLastModifiedTime", &LuaTicket::GetLastModifiedTime },
{ "GetResponse", &LuaTicket::GetResponse },
{ "GetChatLog", &LuaTicket::GetChatLog },

{ "SetAssignedTo", &LuaTicket::SetAssignedTo },
{ "SetResolvedBy", &LuaTicket::SetResolvedBy },
{ "SetCompleted", &LuaTicket::SetCompleted },
{ "SetMessage", &LuaTicket::SetMessage },
{ "SetComment", &LuaTicket::SetComment },
{ "SetViewed", &LuaTicket::SetViewed },
{ "SetUnassigned", &LuaTicket::SetUnassigned },
{ "SetPosition", &LuaTicket::SetPosition },
{ "AppendResponse", &LuaTicket::AppendResponse },
{ "DeleteResponse", &LuaTicket::DeleteResponse },
};

ElunaRegister<GemPropertiesEntry> GemPropertiesEntryMethods[] =
{
// Getters
Expand Down Expand Up @@ -1550,6 +1584,9 @@ void RegisterFunctions(Eluna* E)
ElunaTemplate<Roll>::Register(E, "Roll");
ElunaTemplate<Roll>::SetMethods(E, RollMethods);

ElunaTemplate<GmTicket>::Register(E, "Ticket");
ElunaTemplate<GmTicket>::SetMethods(E, TicketMethods);

ElunaTemplate<GemPropertiesEntry>::Register(E, "GemPropertiesEntry");
ElunaTemplate<GemPropertiesEntry>::SetMethods(E, GemPropertiesEntryMethods);

Expand Down
71 changes: 71 additions & 0 deletions src/LuaEngine/hooks/TicketHooks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#include "Hooks.h"
#include "HookHelpers.h"
#include "LuaEngine.h"
#include "BindingMap.h"
#include "ElunaIncludes.h"
#include "ElunaTemplate.h"

using namespace Hooks;

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<TicketEvents>(EVENT);\
if (!TicketEventBindings->HasBindingsFor(key))\
return;\
LOCK_ELUNA

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<TicketEvents>(EVENT);\
if (!TicketEventBindings->HasBindingsFor(key))\
return;\
LOCK_ELUNA

void Eluna::OnTicketCreate(Player* player, GmTicket* ticket)
{
START_HOOK(TICKET_EVENT_ON_CREATE);
Push(player);
Push(ticket);
CallAllFunctions(TicketEventBindings, key);
}

void Eluna::OnTicketUpdate(Player* player, GmTicket* ticket)
{
START_HOOK(TICKET_EVENT_ON_UPDATE);
Push(player);
Push(ticket);
CallAllFunctions(TicketEventBindings, key);
}

void Eluna::OnTicketClose(Player* player, GmTicket* ticket)
{
START_HOOK(TICKET_EVENT_ON_CLOSE);
Push(player);
Push(ticket);
CallAllFunctions(TicketEventBindings, key);
}

void Eluna::OnTicketStatusUpdate(Player* player, GmTicket* ticket)
{
START_HOOK(TICKET_EVENT_STATUS_UPDATE);
Push(player);
Push(ticket);
CallAllFunctions(TicketEventBindings, key);
}

void Eluna::OnTicketResolve(Player* player, GmTicket* ticket)
{
START_HOOK(TICKET_EVENT_ON_RESOLVE);
Push(player);
Push(ticket);
CallAllFunctions(TicketEventBindings, key);
}

51 changes: 51 additions & 0 deletions src/LuaEngine/methods/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,30 @@ namespace LuaGlobalFunctions
return RegisterEntryHelper(L, Hooks::REGTYPE_GAMEOBJECT);
}

/**
* Registers a [Ticket] event handler.
*
* <pre>
* enum TicketEvents
* {
* TICKET_EVENT_ON_CREATE = 1, // (event, player, ticket)
* TICKET_EVENT_ON_UPDATE = 2, // (event, player, ticket, message)
* TICKET_EVENT_ON_CLOSE = 3, // (event, player, ticket)
* TICKET_EVENT_STATUS_UPDATE = 4, // (event, player, ticket)
* TICKET_EVENT_ON_RESOLVE = 5, // (event, player, ticket)
* TICKET_EVENT_COUNT
* };
* </pre>
*
* @param uint32 event : event ID, refer to UnitEvents above
* @param function function : function to register
* @param uint32 shots = 0 : the number of times the function will be called, 0 means "always call this function"
*/
int RegisterTicketEvent(lua_State* L)
{
return RegisterEventHelper(L, Hooks::REGTYPE_TICKET);
}

/**
* Registers a [Spell] event handler.
*
Expand Down Expand Up @@ -3145,6 +3169,33 @@ namespace LuaGlobalFunctions
return 0;
}

/**
* Unbinds event handlers for either all [Ticket] events, or one type of [Ticket] event.
*
* If `event_type` is `nil`, all [Ticket] event handlers are cleared.
*
* Otherwise, only event handlers for `event_type` are cleared.
*
* @proto ()
* @proto (event_type)
* @param uint32 event_type : the event whose handlers will be cleared, see [Global:RegisterTicketEvent]
*/
int ClearTicketEvents(lua_State* L)
{
typedef EventKey<Hooks::TicketEvents> Key;

if (lua_isnoneornil(L, 1))
{
Eluna::GetEluna(L)->TicketEventBindings->Clear();
}
else
{
uint32 event_type = Eluna::CHECKVAL<uint32>(L, 1);
Eluna::GetEluna(L)->TicketEventBindings->Clear(Key((Hooks::TicketEvents)event_type));
}
return 0;
}

/**
* Unbinds event handlers for either all of a [Spell]'s events, or one type of event.
*
Expand Down
Loading
Loading