Skip to content
pyrosshade edited this page May 11, 2019 · 5 revisions

write a user transaction

We want to implement a "welcome bonus" when the user reach a place.

server side code

function __welcomebonus(params, customData, mod) {
	"use strict;"
	// don't edit above this line // must be on line 3
	var self = this;
	var user_id = params.user_id;
	var domain = this.game.getPrivateDomain();
    return self.tx.transaction(domain, user_id, {"bonus" : mod.common.square(10) }, "welcome bonus").spread (function (newbalance, triggered) {
    	mod.debug(JSON.stringify(newbalance));
    	return { "totalbonus" : newbalance.bonus };
    });
}

Here, bonus is calculate to show a sample use of mod.common. mod.common act as a library : you can define reusable functions within a dedicated batch called 'common'. the content returned by this function will be available in all others batches and hooks.

Let's try to define a square function :

function common(mod) {
	"use strict;"
	// don't edit above this line // must be on line 3
  return { 
       square: function (x) { return x*x; }
  };
}

Therefor you can call mod.common.square in any of your batches. Obviously, you can define function which access to any cloudbuilder API.

client invocation

CHJSON config;
config.Put("name", "welcomebonus");
CHJSON params;
CUserManager::Instance()->Batch(MakeResultHandler(this, &Handle), &config, &params);

results

{
"totalbonus" :  100,
...
}

Clone this wiki locally