Appearance
Class: RewardService
Service for distributing rewards to viewers at the end of a game: participants are marked as winners or losers, then the result is sent to the backend, which credits (or not) their currency / ranking.
Remarks
Accessible everywhere via k.rewards. You never have to instantiate this service yourself — it is created by the SDK at the startup of the app/widget.
The typical flow: accumulate winners/losers with `winner` / `loser` during the game, then trigger the batch send with `sendRewards`.
Example
ts
// At the end of the game: reward the winner, mark the others as losers
k.rewards.winner(gagnant);
perdants.forEach((p) => k.rewards.loser(p));
await k.rewards.sendRewards();Implements
Methods
loser()
ts
loser(user): void;Marks a viewer as a loser: they will be counted without a reward on the next `sendRewards`.
Parameters
user
The viewer (User) to add to the list of losers.
Returns
void
See
winner to mark a winner.
sendRewards()
ts
sendRewards(): Promise<void>;Sends the accumulated rewards to the backend: the winners (value 1) and the losers (value 0) marked since the last send, then clears the local lists.
Returns
Promise<void>
A promise resolved once the distribution has been sent. If no winner or loser has been marked, the method makes no network call.
Remarks
The internal winner/loser lists are reset as soon as the method is called, even if the request later fails. Note: a viewer present in both lists will be counted according to the last value written (loser, i.e. 0).
Example
ts
await k.rewards.sendRewards();See
winner and loser to populate the lists before sending.
winner()
ts
winner(user): void;Marks a viewer as a winner: they will be rewarded on the next `sendRewards`.
Parameters
user
The viewer (User) to add to the list of winners.
Returns
void
See
loser to mark a loser.