-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMoneyTalks.js
More file actions
200 lines (174 loc) · 5.34 KB
/
MoneyTalks.js
File metadata and controls
200 lines (174 loc) · 5.34 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* # MoneyTalks
* Copyright(c) 2020 Stefano Balietti
* MIT Licensed
*
* Displays a box for formatting earnings ("money") in currency
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('MoneyTalks', MoneyTalks);
// ## Meta-data
MoneyTalks.version = '0.5.0';
MoneyTalks.description = 'Displays the earnings of a player.';
MoneyTalks.title = 'Earnings';
MoneyTalks.className = 'moneytalks';
// ## Dependencies
MoneyTalks.dependencies = {
JSUS: {}
};
/**
* ## MoneyTalks constructor
*
* `MoneyTalks` displays the earnings ("money") of players
*
* @see MoneyTalks.init
*/
function MoneyTalks() {
/**
* ### MoneyTalks.spanCurrency
*
* The SPAN which holds information on the currency
*/
this.spanCurrency = null;
/**
* ### MoneyTalks.spanMoney
*
* The SPAN which holds information about the money earned so far
*/
this.spanMoney = null;
/**
* ### MoneyTalks.currency
*
* String describing the currency
*/
this.currency = 'ECU';
/**
* ### MoneyTalks.money
*
* Currently earned money
*/
this.money = 0;
/**
* ### MoneyTalks.precicison
*
* Precision of floating point number to display
*/
this.precision = 2;
/**
* ### MoneyTalks.showCurrency
*
* If TRUE, the currency is displayed after the money
*/
this.showCurrency = true;
/**
* ### MoneyTalks.currencyClassname
*
* Class name to be attached to the currency span
*/
this.classnameCurrency = 'moneytalkscurrency';
/**
* ### MoneyTalks.currencyClassname
*
* Class name to be attached to the money span
*/
this.classnameMoney = 'moneytalksmoney';
}
// ## MoneyTalks methods
/**
* ### MoneyTalks.init
*
* Initializes the widget
*
* @param {object} options Optional. Configuration options.
*
* The options object can have the following attributes:
*
* - `currency`: The name of currency.
* - `money`: Initial amount of money earned.
* - `precision`: How mamy floating point digits to use.
* - `currencyClassName`: Class name to be set for this.spanCurrency.
* - `moneyClassName`: Class name to be set for this.spanMoney.
* - `showCurrency`: Flag whether the name of currency is to be displayed.
*/
MoneyTalks.prototype.init = function(options) {
options = options || {};
if ('string' === typeof options.currency) {
this.currency = options.currency;
}
if ('undefined' !== typeof options.showCurrency) {
this.showCurrency = !!options.showCurrency;
}
if ('number' === typeof options.money) {
this.money = options.money;
}
if ('number' === typeof options.precision) {
this.precision = options.precision;
}
if ('string' === typeof options.MoneyClassName) {
this.classnameMoney = options.MoneyClassName;
}
if ('string' === typeof options.currencyClassName) {
this.classnameCurrency = options.currencyClassName;
}
};
MoneyTalks.prototype.append = function() {
if (!this.spanMoney) {
this.spanMoney = document.createElement('span');
}
if (!this.spanCurrency) {
this.spanCurrency = document.createElement('span');
}
if (!this.showCurrency) this.spanCurrency.style.display = 'none';
this.spanMoney.className = this.classnameMoney;
this.spanCurrency.className = this.classnameCurrency;
this.spanCurrency.innerHTML = this.currency;
this.spanMoney.innerHTML = this.money;
this.bodyDiv.appendChild(this.spanMoney);
this.bodyDiv.appendChild(this.spanCurrency);
};
MoneyTalks.prototype.listeners = function() {
var that = this;
node.on('MONEYTALKS', function(amount, clear) {
that.update(amount, clear);
});
};
/**
* ### MoneyTalks.update
*
* Updates the display and the count of available "money"
*
* @param {string|number} amount Amount to add to current value of money
* @param {boolean} clear Optional. If TRUE, money will be set to 0
* before adding the new amount
*
* @return {number} The current value after the update
*
* @see MoneyTalks.money
* @see MonetyTalks.spanMoney
*/
MoneyTalks.prototype.update = function(amount, clear) {
var parsedAmount;
parsedAmount = J.isNumber(amount);
if (parsedAmount === false) {
node.err('MoneyTalks.update: invalid amount: ' + amount);
return;
}
if (clear) this.money = 0;
this.money += parsedAmount;
this.spanMoney.innerHTML = this.money.toFixed(this.precision);
return this.money;
};
/**
* ### MoneyTalks.getValues
*
* Returns the current value of "money"
*
* @see MoneyTalks.money
*/
MoneyTalks.prototype.getValues = function() {
return this.money;
};
})(node);