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

Improve addEvent when sharing events over multiple resources #2333

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions Client/mods/deathmatch/logic/CEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ bool CEvents::AddEvent(const char* szName, const char* szArguments, CLuaMain* pL
assert(szName);
assert(szArguments);

// If it already exists, return
if (Get(szName))
return false;
// Get the event if it already exists
SEvent* pEvent = Get(szName);

// Create and add the event
SEvent* pEvent = new SEvent;
pEvent->strName = szName;
pEvent->strArguments = szArguments;
pEvent->pLuaMain = pLuaMain;
pEvent->bAllowRemoteTrigger = bAllowRemoteTrigger;
if (pEvent)
{
// If bAllowRemoteTrigger has been altered, return
if (pEvent->bAllowRemoteTrigger != bAllowRemoteTrigger)
return false;
Pieter-Dewachter marked this conversation as resolved.
Show resolved Hide resolved

// Add pLuaMain to the set, std::set guarantees unique elements
pEvent->pLuaMainSet.insert(pLuaMain);
}
else
{
// Create and add the event
pEvent = new SEvent;
pEvent->strName = szName;
pEvent->strArguments = szArguments;
pEvent->pLuaMainSet.insert(pLuaMain);
pEvent->bAllowRemoteTrigger = bAllowRemoteTrigger;
}

m_EventHashMap[szName] = pEvent;

Expand Down Expand Up @@ -80,17 +91,24 @@ void CEvents::RemoveAllEvents(class CLuaMain* pMain)
while (iter != m_EventHashMap.end())
{
SEvent* pEvent = (*iter).second;
// If they match, delete it null it and set the bool
if (pEvent != NULL && pEvent->pLuaMain == pMain)

// If they match, remove pMain from the set and check for deletion
if (pEvent->pLuaMainSet.find(pMain) != pEvent->pLuaMainSet.end())
{
// Delete the object
delete pEvent;
pEvent->pLuaMainSet.erase(pMain);

// Remove from list
m_EventHashMap.erase(iter++);
// If no pMain is left, delete it null it and set the bool
if (pEvent->pLuaMainSet.size() == 0)
Pieter-Dewachter marked this conversation as resolved.
Show resolved Hide resolved
{
// Delete the object
delete pEvent;

// Remove from list
m_EventHashMap.erase(iter);
}
}
else
++iter;

++iter;
}
}

Expand Down
8 changes: 4 additions & 4 deletions Client/mods/deathmatch/logic/CEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

struct SEvent
{
class CLuaMain* pLuaMain;
std::string strName;
std::string strArguments;
bool bAllowRemoteTrigger;
std::set<class CLuaMain*> pLuaMainSet;
Pieter-Dewachter marked this conversation as resolved.
Show resolved Hide resolved
std::string strName;
std::string strArguments;
bool bAllowRemoteTrigger;
};

class CEvents
Expand Down
52 changes: 35 additions & 17 deletions Server/mods/deathmatch/logic/CEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ bool CEvents::AddEvent(const char* szName, const char* szArguments, CLuaMain* pL
assert(szName);
assert(szArguments);

// If it already exists, return
if (Get(szName))
return false;
// Get the event if it already exists
SEvent* pEvent = Get(szName);

// Create and add the event
SEvent* pEvent = new SEvent;
pEvent->strName = szName;
pEvent->strArguments = szArguments;
pEvent->pLuaMain = pLuaMain;
pEvent->bAllowRemoteTrigger = bAllowRemoteTrigger;
if (pEvent)
{
// If bAllowRemoteTrigger has been altered, return
if (pEvent->bAllowRemoteTrigger != bAllowRemoteTrigger)
return false;

// Add pLuaMain to the set, std::set guarantees unique elements
pEvent->pLuaMainSet.insert(pLuaMain);
}
else
{
// Create and add the event
pEvent = new SEvent;
pEvent->strName = szName;
pEvent->strArguments = szArguments;
pEvent->pLuaMainSet.insert(pLuaMain);
pEvent->bAllowRemoteTrigger = bAllowRemoteTrigger;
}

m_EventHashMap[szName] = pEvent;

Expand Down Expand Up @@ -68,17 +79,24 @@ void CEvents::RemoveAllEvents(class CLuaMain* pMain)
while (iter != m_EventHashMap.end())
{
SEvent* pEvent = (*iter).second;
// If they match, delete it null it and set the bool
if (pEvent->pLuaMain == pMain)

// If they match, remove pMain from the set and check for deletion
if (pEvent->pLuaMainSet.find(pMain) != pEvent->pLuaMainSet.end())
{
// Delete the object
delete pEvent;
pEvent->pLuaMainSet.erase(pMain);

// Remove from list
m_EventHashMap.erase(iter++);
// If no pMain is left, delete it null it and set the bool
if (pEvent->pLuaMainSet.size() == 0)
Pieter-Dewachter marked this conversation as resolved.
Show resolved Hide resolved
{
// Delete the object
delete pEvent;

// Remove from list
m_EventHashMap.erase(iter);
}
}
else
++iter;

++iter;
}
}

Expand Down
8 changes: 4 additions & 4 deletions Server/mods/deathmatch/logic/CEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

struct SEvent
{
class CLuaMain* pLuaMain;
std::string strName;
std::string strArguments;
bool bAllowRemoteTrigger;
std::set<class CLuaMain*> pLuaMainSet;
std::string strName;
std::string strArguments;
bool bAllowRemoteTrigger;
};

class CEvents
Expand Down