-
Notifications
You must be signed in to change notification settings - Fork 1
MethodSubscriptionsServer
You can subscribe method handlers to different events generated by the Snipe server core or your project on both the cache and slave servers. Subscribing a method to an event is done with a Server.subscribeMethod()
or CacheServer.subscribeMethod()
call that should be in the module constructor.
Example:
public function new(s: ServerTest)
{
super(s);
name = "user";
server.subscribeMethod("core/user.logoutPost",
function (c: ClientGame)
{
// logic
});
}
-
core/serverList.modify
is called to modify server list items before saving them to Server List. Arguments:SlaveClient
- game server instance,Dynamic
- game server info for serialization.
These game server hooks are available in the core:
Quest Module:
-
core/quest.check
is called to handle additional quest completion requirements. Returns 1 if this quest meets this requirement, 0 if he does not, -1 if this hook does not handle this requirement. -
core/quest.requires
is called when the game server checks if the user meets the requirements for receiving a quest. Accepts user instance and full requirement object record as arguments. Returns 1 if the user meets this requirement, 0 if he does not, -1 if this hook does not handle this requirement. -
core/quest.result
is used to handle additional quest completion results. Returns an integer value which is currently unused. -
core/quest.trigger
is a trigger handler. Returns 1 if the trigger was accepted and completed by this handler, 0 if the trigger was accepted by this handler but the trigger parameters were not met, -1 if this handler does not handle this trigger.
User Module:
-
core/user.dailyLoginPost
is called for each user after first successful game server login of the day. Accepts the same arguments and returns the same value as the module subscription hook described at snipe.slave.modules._HookUserDailyLoginPost. -
core/user.freePost
is called on the game server when freeing the client blocks after theClientGame.free()
hook. It accepts the client instance as an argument. -
core/user.loginPost
is called after the user successfully logs in to the game server. Accepts the same arguments and returns the same value as the module subscription hook described at snipe.slave.Module. -
core/user.logoutPost
is called after the user logs out of the game server. Accepts the same arguments and returns the same value as the module subscription hook described at snipe.slave.Module.
The slave server hooks arguments and return types are the same as in the case of module subscriptions.
You can make custom method subscriptions in your project. They provide the needed flexibility, for example, in a case when you have multiple projects with shared code between them that requires some project-specific tidbits. The method Server.getSubscribedMethods()
returns the list of the methods that are subscribed to the specified event.
Let's suppose you have an invitation system that rewards the user who invites another user to your project. Your projects will probably all use the same code for getting the invitations, notifying the server about them, notifying the client, etc. But all projects will have different rewards. It's okay when the reward is just money and you can change the amount using the game variable. But what if the reward contents are more complicated? That's when method subscriptions will do the trick.
Let's take a look at the example. This is a shared invitation reward handling module code. It is set up to check for receiving invitation reward on user login:
class InviteModule extends snipe.slave.Module<ClientGame, ServerGame>
{
public function new(srv: ServerGame)
{
super(srv);
name = "invite";
server.subscribeModule("core/user.loginPost", this);
}
public override function loginPost(c: ClientGame, params: Params, ret: Dynamic, response: Dynamic)
{
// checks for user eligibility
// give reward
for (m in server.getSubscribedMethods("invite.reward"))
m(c);
// client notification logic
}
And this is the project-specific part that sets the reward handler:
class UserModule extends snipe.slave.Module<ClientGame, ServerTest>
{
public function new(srv: ServerTest)
{
super(srv);
name = "user";
// project-specific reward
server.subscribeMethod("invite.reward",
function (c: ClientGame)
{
// custom reward logic
});
}
}
Note the important caveat here. The Server.getSubscribedMethods()
call returns the list of Dynamic
elements. This means that the subsequent call of the methods in that list will not have their arguments checked for proper types. Even the number of arguments can be wrong due to programming mistakes. On the other hand, with that risk you receive the simple and powerful system for extending the code functionality in key places.