aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/serverLib/StatsManager.js
blob: e472504879a38a0e54f79daedc53f1417d91bfc1 (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/ )
  *
  */

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

  /**
    * Retrieve value of arbitrary `key` reference
    *
    * @param {String} key Reference to the arbitrary store name
    *
    * @return {*} Data referenced by `key`
    */
  get (key) {
    return this.data[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.data[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 = StatsManager;