HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Improved API and Features 0.9.6.1b

x00x00 MVP

This concerns developers looking to integrate KarmaBank.

I've added convenience methods in order to extend KarmaBank, by adding new Meta to check (which now includes the UserMeta key/value table, as well as the User table), even new mapping operations which defines the algorithm, for checking the operation.

One for the key changes it to allow relative operations, where as before the operation would be done pure on the absolute meta value, this is not always desirable

To extend you can hook the KarmaBankMetaMap event.

You have the AddMeta method

You supply with

  • $Name: column or key for the meta
  • $Explanation: which is short description.

If the default operations like Equals, Every, etc are not enough you can create your own, using AddOperation method. Params:

  • $Name: descriptive label
  • $Explanation: explanation
  • $Function: you need to supply a reference to the function, of your algorithm.
    Recommended is to make a static method in you plugin and refer to it like so 'YourPluginClass::YourOperation'. If you need to reference a method of an object instance you could supply array($YourObject,'YourOperation'). The function can be supplied with 4 params:
    • $MetaValue: this is the current meta value detected
    • $Target: this is the target value of the rule condition
    • $Condition: this is the same as the Name of the meta
    • $User: standard User object, with additional UserMeta table value added using the $User->Meta property.
    • $LastTrans: a new object, which enables relative operations, a critical improvement. One important property is $LastTrans->TallyValue which you can compare to the current value. This was the last value since the last transaction using that rule.

Here is an example extend code:

public static function MyAcmeOperation($MetaValue,$Target,$Condition,$User,$LastTrans){
    if([some check])
        return TRUE; 
    else
        return FALSE;
}

public function KarmaBank_KarmaBankMetaMap_Handler($Sender){
    $Sender->AddMeta('MyAcmeMeta','My Acme Meta Explanation');
    $Sender->AddOpperation('MyAcmeOperation','My Acme Operation Explanation','YourPluginClass::MyAcmeOperation');
}

You don't aways need a new operations, the existing one might be sufficient, but you will have to add the meta you want checked

When you create your plugin that you want to extend for use with Karmabank, you need a user meta value. This either means a column in the user table, or by setting key/value pair UserMeta, which is generally recommended method as it is convenient and doesn't pollute the User Table

You can do so like this

UserMetaModel::SetUserMeta($UserID,'MyMetaPrefix.MetaName',$MetaValue);

or

$MyMeta=array('MetaName'=>$MetaValue);
UserModel::SetMeta($UserID,$MyMeta,'MyMetaPrefix.');

register your meta

public function KarmaBank_KarmaBankMetaMap_Handler($Sender){
    $Sender->AddMeta('MyMetaPrefix.MetaName','My Meta Explanation');
}

Note it is definitely a good idea to use a prefix to group related meta.

Now I want to go over relative operation and a new feature which is the 'factor'.

Here is a new relative style operation (that is include default).

public static function OperationDiffEquals($MetaValue,$Target,$Condition,$User,$LastTrans){
    $Difference=$MetaValue-($LastTrans ? $LastTrans->LastTally : 0);
    if(abs($Difference)%$Target!==0)
        return FALSE;
    return abs($Difference)!= $Difference? -1:1;
}

note how $LastTrans->LastTally and the current $MetaValue are subtracted to create a relative difference, this is just one of the way you could make use of it. It checks if the relative target has been hit. The reason for using modulus rather than just equal is the simplification of the logic, for the first first traction were there is no tally, and the meta value is absolute. But essentially you are working out if the change is equal to the target value. However this is the abs difference, but when you return you want to return plus or minus one. This is the factor you return. If you return a signed integer it will be multiplied by the set amount. This mean that not only can karma be rewarded, but it can be deducted under the same rule.

So that method gives you an idea of how develop for KarmaBank.

As always if you need a feature or facility please, sponsor it. Great thing are coming out of this plugin, due to its backers. More to come soon.

grep is your friend.

Comments

Sign In or Register to comment.