forked from realms-mud/core-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.c
227 lines (208 loc) · 8.58 KB
/
events.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// *****************************************************************************
// Class: events
// File Name: events.c
//
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//
// Description: This is an admittedly rudimentary event handling system (for
// those who live in the software development world, it's a
// reactor pattern) that has been designed with only the
// player/NPC object model in mind. Event subscribers (those
// waiting for something to happen) can be any object, though said
// object will only find the event processing useful if it has
// implemented a method named as one of the strings listed in the
// validEventHandlers array. These are the events that the
// player/NPC will broadcast to the registered objects.
//
// *****************************************************************************
// I suspect there will be more. This, however, is a decent starting point.
private nosave string *validEventHandlers = ({ "onDeath", "onHeartBeat",
"onAttack", "onAttacked", "onMove", "onUsedAbility", "onHitPointsChanged",
"onSpellPointsChanged", "onStaminaPointsChanged", "onRunAway", "onEquip",
"onUnequip", "onRegisterItem", "onUnregisterItem", "onAdvancedLevel",
"onAdvancedRank", "onDemotedRank", "onJoinGuild", "onLeaveGuild",
"onResearchPointsAdded", "onResearchStarted", "onResearchCompleted",
"onResearchTreeOpen", "onResearchChoiceAvailable", "onResearchChoiceChosen",
"onResearchPathChosen", "onCombatDelayed", "onHit", "onTraitAdded",
"onTraitRemoved", "onQuestStarted", "onQuestAdvancedState",
"onQuestCompleted", "onQuestSucceeded", "onQuestFailed", "onQuestActivated",
"onQuestDeactivated", "onSkillAdvanced", "onSkillDecreased",
"onSkillPointsIncreased", "onSkillPointsUsed", "onIntoxicationChanged",
"onStuffedChanged", "onDruggedChanged", "onSoakedChanged", "onDrunk",
"onSober", "onDetoxified", "onWastedOnDrugs", "onNoLongerDrugged", "onSoaked",
"onNoLongerSoaked", "onCannotEatMore", "onHungry", "onBeginDetox",
"onSaveSucceeded", "onRestoreSucceeded", "onRestoreFailed", "onStateChanged",
"onConversationResponse", "receiveEvent", "onCraftingStarted",
"onCraftingCompleted", "onCraftingAborted", "onMessageReceived"
});
private nosave mapping eventList = ([ ]);
//-----------------------------------------------------------------------------
// Method: registerEvent
// Description: This method is used to register event handlers that will
// subscribe to a given event. The object can be of any base type.
// If an event handler as defined in validEventHandlers exists
// in this subscriber object, it will be called when the event
// of the same name occurs.
//
// Parameters: subscriber - the object that will subscribe to events
//
// Returns: true if successful.
//-----------------------------------------------------------------------------
public nomask int registerEvent(object subscriber)
{
int ret = 0;
if(subscriber && objectp(subscriber) && !member(eventList, subscriber))
{
string *eventsToAdd = ({ });
foreach(string method in functionlist(subscriber, 0x01))
{
if(method && stringp(method) &&
(member(validEventHandlers, method) > -1))
{
eventsToAdd += ({ method });
ret = 1;
}
}
if(ret)
{
eventList[subscriber] = eventsToAdd;
}
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: unregisterEvent
// Description: This method is used to unregister event handlers.
//
// Parameters: subscriber - the object that will unsubscribe to events
//
// Returns: true if successful.
//-----------------------------------------------------------------------------
public nomask int unregisterEvent(object subscriber)
{
int ret = 0;
if(subscriber && objectp(subscriber) && member(eventList, subscriber))
{
m_delete(eventList, subscriber);
ret = 1;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: registerEventHandler
// Description: This method is used to register custom events that can then be
// subscribed to by event handlers. These events are then placed
// in the validEventHandlers list.
//
// Parameters: eventHandler - new event to manage
//
// Returns: true if successful.
//-----------------------------------------------------------------------------
public nomask int registerEventHandler(string eventHandler)
{
int ret = 0;
if(eventHandler && stringp(eventHandler) &&
(member(validEventHandlers, eventHandler) < 0))
{
validEventHandlers += ({ eventHandler });
ret = 1;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: unregisterEventHandler
// Description: This method is used to unregister custom events.
//
// Parameters: eventHandler - new event to manage
//
// Returns: true if successful.
//-----------------------------------------------------------------------------
public nomask int unregisterEventHandler(string eventHandler)
{
int ret = 0;
if(eventHandler && stringp(eventHandler) &&
(member(validEventHandlers, eventHandler) > -1))
{
validEventHandlers -= ({ eventHandler });
ret = 1;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: notify
// Description: This method is used to send notifications to all registered
// event handlers. Every event handler that has created a method
// of same name as the event that is passed will have that method
// called with this_object() passed as a parameter.
//
// Parameters: event - the event that subscribers will be notified of as having
// occurred. This event must be defined in
// the validEventHandlers array.
//
// Returns: true if successful.
//-----------------------------------------------------------------------------
public varargs nomask int notify(string event, mixed message)
{
int ret = 0;
if(event && stringp(event) && (member(validEventHandlers, event) > -1))
{
// delete all null handlers
m_delete(eventList, 0);
// May want/need to limit calls of this method
object *filteredEvents = filter(m_indices(eventList),
(: (objectp($1) &&
(member(eventList[$1], $2) > -1) &&
function_exists($2, $1)) :), event);
foreach(object handler in filteredEvents)
{
call_out("processEventCallOut", 0, handler, event, message);
ret = 1;
}
filteredEvents = filter(m_indices(eventList),
(: (objectp($1) && function_exists("receiveEvent", $1)) :));
foreach(object handler in filteredEvents)
{
call_out("processEventCallOut", 0, handler, event, message, 1);
ret = 1;
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public varargs nomask int notifySynchronous(string event, mixed message)
{
int ret = 0;
if (event && stringp(event) && (member(validEventHandlers, event) > -1))
{
// delete all null handlers
m_delete(eventList, 0);
// May want/need to limit calls of this method
object *filteredEvents = filter(m_indices(eventList),
(: (objectp($1) &&
(member(eventList[$1], $2) > -1) &&
function_exists($2, $1)) :), event);
call_direct(filteredEvents, event, this_object(), message);
filteredEvents = filter(m_indices(eventList),
(: (objectp($1) && function_exists("receiveEvent", $1)) :));
call_direct(filteredEvents, "receiveEvent", this_object(), event, message);
ret = 1;
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask varargs void processEventCallOut(object handler, string event,
mixed message, int isGenericHandler)
{
if (objectp(handler))
{
if (isGenericHandler)
{
call_other(handler, "receiveEvent", this_object(), event, message);
}
else
{
call_other(handler, event, this_object(), message);
}
}
}