aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/managers/stats.js
blob: c3b6f9f49584062ec674ec1fa5f9596388c217ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
  * Simple generic stats collection script for events occurances (etc)
  *
  * Version: v2.0.0
  * Developer: Marzavec ( https://github.com/marzavec )
  * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
  *
  */

'use strict';

class Stats {
  /**
    * Create a stats instance.
    *
    */
  constructor () {
    this._stats = {};
  }

  /**
    * Retrieve value of arbitrary `key` reference
    *
    * @param {String} key Reference to the arbitrary store name
    */
  get (key) {
    return this._stats[key];
  }

  /**
    * Set value of arbitrary `key` reference
    *
    * @param {String} key Reference to the arbitrary store name
    * @param {Number} value New value for `key`
    */
  set (key, value) {
    this._stats[key] = value;
  }

  /**
    * Increase value of arbitrary `key` reference, by 1 or `amount`
    *
    * @param {String} key Reference to the arbitrary store name
    * @param {Number} amount Value to increase `key` by, or 1 if omitted
    */
  increment (key, amount) {
    this.set(key, (this.get(key) || 0) + (amount || 1));
  }

  /**
    * Reduce value of arbitrary `key` reference, by 1 or `amount`
    *
    * @param {String} key Reference to the arbitrary store name
    * @param {Number} amount Value to decrease `key` by, or 1 if omitted
    */
  decrement (key, amount) {
    this.set(key, (this.get(key) || 0) - (amount || 1));
  }
}

module.exports = Stats;