Skip to content
Max S edited this page Mar 22, 2017 · 1 revision

Useful Macros

The Snipe server core contains some useful macros that might need explanation and examples.

Build macros

The build macros are contained in the "snipe.lib.MacroBuild" class. Using them you can retrieve the current Snipe core version, git branch and last commit date. If you use git for your project as well, there are macros available to get the project branch name and last commit date, too.

Usage example:

import snipe.cache.UniServer;

class UniServerTest extends UniServer
{
  function new()
    {
      super();
    }


// main function
  static var u: UniServerTest;
  static function main()
    {
      u = new UniServerTest();
      u.print("UniServerTest " +
        snipe.lib.MacroBuild.getBuildBranch() + "-" +
        snipe.lib.MacroBuild.getBuildDate());
      u.print("Snipe Core " +
        snipe.lib.MacroBuild.getCoreVersion('../..') + "-" +
        snipe.lib.MacroBuild.getCoreBranch('../..') + "-" +
        snipe.lib.MacroBuild.getCoreDate('../..'));
      u.cacheClass = CacheServerTest;
      u.slaveInfos = [
        {
          type: 'game',
          client: TestClient,
          server: ServerTest,
        },
        ];
      u.init();
      u.start();
    }
}

Running the server will print something like this:

UniServerTest master-20170322.1642
Snipe Core 3.4-main-20170320.2110

Client macros

To reduce the hassle of accessing user attributes and variables through the client instance variable, you can use macros defined in the "snipe.lib.MacroClient" class.

For example:

@:build(snipe.lib.MacroClient.addUserAttr('money'))
@:build(snipe.lib.MacroClient.addUserAttr('level'))
@:build(snipe.lib.MacroClient.addUserAttr('exp'))
@:build(snipe.lib.MacroClient.addUserAttrBool('tutorialCompleted'))
@:build(snipe.lib.MacroClient.addUserVar('battlesTotal'))
@:build(snipe.lib.MacroClient.addUserVar('battlesWon'))

class Client extends ClientGame
{
  public function new(s: Socket, srv: Server)
    {
      super(s, srv);
    }
}

Using this client class declaration will allow you to access the defined user attributes and variables as the client instance fields. For example:

  client.money = 30; // sets "money" user attribute
  client.level = 10; // sets "level" user attribute
  client.exp += 200; // modifies "exp" user attribute

  // checks for "tutorialCompleted" user attribute
  if (!client.tutorialCompleted)
    return { errorCode: 'tutorialNotCompleted' };

  client.battlesTotal++; // modifies "battlesTotal" user variable

During the compilation all type checks will remain in place, making life easier. And the compiled code will have a low-level "Client.user.attrs.get()/set()" instead.

Clone this wiki locally