|
1 | 1 | (function () {
|
2 | 2 | "use strict";
|
3 | 3 |
|
4 |
| -var converters = { |
5 |
| - //temperatures |
6 |
| - // 1C = 32.8F = 274.15K |
7 |
| - C : function ( c ) { |
8 |
| - return { |
9 |
| - F : c * 1.8 + 32, // 9/5 = 1.8 |
10 |
| - K : c + 273.15 }; |
11 |
| - }, |
12 |
| - F : function ( f ) { |
13 |
| - return { |
14 |
| - C : (f - 32) / 1.8, |
15 |
| - K : (f + 459.67) * 5 / 9 }; |
16 |
| - }, |
17 |
| - K : function ( k ) { |
18 |
| - if ( k < 0 ) { |
19 |
| - return null; |
20 |
| - } |
21 |
| - |
22 |
| - return { |
23 |
| - C : k - 273.15, |
24 |
| - F : k * 1.8 - 459.67 }; |
25 |
| - }, |
26 |
| - |
27 |
| - //lengths |
28 |
| - //1m = 3.2808(...)f |
29 |
| - m : function ( m ) { |
30 |
| - return { |
31 |
| - f : m * 3.280839895 }; |
32 |
| - }, |
33 |
| - f : function ( f ) { |
34 |
| - return { |
35 |
| - m : f / 3.28083989 }; |
36 |
| - }, |
37 |
| - |
38 |
| - //km: 1m = 1km * 1000 |
39 |
| - km : function ( km ) { |
40 |
| - return converters.m( km * 1000 ); |
41 |
| - }, |
42 |
| - //centimeter: 1m = 100cm |
43 |
| - cm : function ( cm ) { |
44 |
| - return converters.m( cm / 100 ); |
45 |
| - }, |
46 |
| - //millimeters: 1m = 1mm / 1000 |
47 |
| - mm : function ( mm ) { |
48 |
| - return converters.m( mm / 1000 ); |
49 |
| - }, |
50 |
| - //inches: 1f = 1i / 12 |
51 |
| - i : function ( i ) { |
52 |
| - return converters.f( i / 12 ); |
53 |
| - }, |
54 |
| - |
55 |
| - //angles |
56 |
| - d : function ( d ) { |
57 |
| - return { |
58 |
| - r : d * Math.PI / 180 }; |
59 |
| - }, |
60 |
| - r : function ( r ) { |
61 |
| - return { |
62 |
| - d : r * 180 / Math.PI }; |
63 |
| - }, |
64 |
| - |
65 |
| - //weights |
66 |
| - g : function ( g ) { |
67 |
| - return { |
68 |
| - lb : g * 0.0022, |
69 |
| - //the following will be horribly inaccurate |
70 |
| - st : g * 0.000157473 }; |
71 |
| - }, |
72 |
| - lb : function ( lb ) { |
73 |
| - return { |
74 |
| - g : lb * 453.592, |
75 |
| - st : lb * 0.0714286 }; |
76 |
| - }, |
77 |
| - //stones: 1st = 6350g = 14lb |
78 |
| - st : function ( st ) { |
79 |
| - return { |
80 |
| - g : st * 6350.29, |
81 |
| - lb : st * 14 }; |
82 |
| - }, |
83 |
| - |
84 |
| - //kg: 1g = 1kg * 1000 |
85 |
| - kg : function ( kg ) { |
86 |
| - return converters.g( kg * 1000 ); |
87 |
| - } |
88 |
| -}; |
89 |
| - |
90 |
| -var longNames = { |
91 |
| - lbs : 'lb', |
92 |
| - ft : 'f', |
93 |
| - foot : 'f', |
94 |
| - metres : 'm', |
95 |
| - millimetres : 'mm', |
96 |
| - killometres : 'km', |
97 |
| - degrees : 'd', |
98 |
| - radians : 'r', |
99 |
| - grams : 'g', |
100 |
| - kilograms : 'kg', |
101 |
| - inches : 'i', |
102 |
| - stones : 'st', |
103 |
| -}; |
104 |
| - |
105 |
| -var currencies, symbols; //to be filled in next line by build |
106 |
| -//#build ../static/currencies.js |
107 |
| - |
108 |
| -function unalias ( unit ) { |
109 |
| - var up = unit.toUpperCase(); |
110 |
| - if ( symbols.hasOwnProperty(up) ) { |
111 |
| - return symbols[ up ]; |
112 |
| - } |
113 |
| - if ( longNames.hasOwnProperty(unit) ) { |
114 |
| - return longNames[ unit ]; |
115 |
| - } |
116 |
| - |
117 |
| - return unit; |
118 |
| -} |
119 |
| - |
120 |
| -/* |
121 |
| - ( #start number matching |
122 |
| - -? #optional negative |
123 |
| - \d+ #the integer part of the number |
124 |
| - \.? #optional dot for decimal portion |
125 |
| - \d* #optional decimal portion |
126 |
| - ) |
127 |
| - \s* #optional whitespace, just 'cus |
128 |
| - ( #start unit matching |
129 |
| - \S+ #the unit. we don't know anyhing about it, besides having no ws |
130 |
| - ) |
131 |
| - ( #begin matching optional target unit (required for currencies) |
132 |
| - \s+ |
133 |
| - (?: |
134 |
| - (?: |
135 |
| - to|in #10 X to Y, 10 X in Y |
136 |
| - ) |
137 |
| - \s+ |
138 |
| - )? |
139 |
| - (\S+) #the unit itself |
140 |
| - )? |
141 |
| - */ |
142 |
| -var rUnits = /(-?\d+\.?\d*)\s*(\S+)(\s+(?:(?:to|in)\s+)?(\S+))?$/; |
143 |
| - |
144 |
| -//string is in the form of: |
145 |
| -// <number><unit> |
146 |
| -// <number><unit> to|in <unit> |
147 |
| -//note that units are case-sensitive: F is the temperature, f is the length |
148 |
| -var convert = function ( inp, cb ) { |
149 |
| - if ( inp.toLowerCase() === 'list' ) { |
150 |
| - finish( listUnits().join(', ') ); |
151 |
| - return; |
152 |
| - } |
153 |
| - |
154 |
| - var parts = rUnits.exec( inp ); |
155 |
| - |
156 |
| - if ( !parts ) { |
157 |
| - finish( {error : 'Unidentified format; please see `/help convert`'} ); |
158 |
| - return; |
159 |
| - } |
160 |
| - |
161 |
| - var num = Number( parts[1] ), |
162 |
| - unit = parts[ 2 ], |
163 |
| - target = parts[ 4 ] || '', |
164 |
| - moneh = false; |
165 |
| - bot.log( num, unit, target, '/convert input' ); |
166 |
| - |
167 |
| - unit = unalias( unit ); |
168 |
| - target = unalias( target ); |
169 |
| - if ( currencies[unit.toUpperCase()] ) { |
170 |
| - moneh = true; |
171 |
| - } |
172 |
| - |
173 |
| - if ( moneh ) { |
174 |
| - moneyConverter.convert( num, unit, target, finish ); |
175 |
| - } |
176 |
| - else { |
177 |
| - convertUnit( num, unit, finish ); |
178 |
| - } |
179 |
| - |
180 |
| - function finish ( res ) { |
181 |
| - bot.log( res, '/convert answer' ); |
182 |
| - |
183 |
| - var reply; |
184 |
| - // list was passed |
185 |
| - if ( res.substr ) { |
186 |
| - reply = res; |
187 |
| - } |
188 |
| - //an error occured |
189 |
| - else if ( res.error ) { |
190 |
| - reply = res.error; |
191 |
| - } |
192 |
| - //just a normal result |
193 |
| - else { |
194 |
| - reply = format( res ); |
195 |
| - } |
196 |
| - |
197 |
| - if ( cb && cb.call ) { |
198 |
| - cb( reply ); |
199 |
| - } |
200 |
| - else { |
201 |
| - inp.reply( reply ); |
202 |
| - } |
203 |
| - } |
204 |
| - |
205 |
| - function format ( res ) { |
206 |
| - var keys = Object.keys( res ); |
207 |
| - |
208 |
| - if ( !keys.length ) { |
209 |
| - return 'Could not convert {0} to {1}'.supplant( unit, target ); |
210 |
| - } |
211 |
| - return keys.filter( nameGoesHere ).map( formatKey ).join( ', ' ); |
212 |
| - |
213 |
| - function nameGoesHere ( key ) { |
214 |
| - return !target || target === key; |
215 |
| - } |
216 |
| - function formatKey ( key ) { |
217 |
| - return res[ key ].maxDecimal( 4 ) + key; |
218 |
| - } |
219 |
| - } |
220 |
| -}; |
221 |
| - |
222 |
| -function convertUnit ( number, unit, cb ) { |
223 |
| - bot.log( number, unit, '/convert unit broken' ); |
224 |
| - |
225 |
| - if ( !converters[unit] ) { |
226 |
| - cb({ |
227 |
| - error:'Confuse converter with ' + unit + ', receive error message' |
228 |
| - }); |
229 |
| - } |
230 |
| - else { |
231 |
| - cb( converters[unit](number) ); |
232 |
| - } |
233 |
| -} |
234 |
| - |
235 |
| -var moneyConverter = { |
236 |
| - ratesCache : {}, |
237 |
| - |
238 |
| - convert : function ( number, from, to, cb ) { |
239 |
| - this.from = from; |
240 |
| - this.to = to; |
241 |
| - |
242 |
| - this.upFrom = from.toUpperCase(); |
243 |
| - this.upTo = to.toUpperCase(); |
244 |
| - |
245 |
| - var err = this.errorMessage(); |
246 |
| - if ( err ) { |
247 |
| - cb( { error : err } ); |
248 |
| - return; |
249 |
| - } |
250 |
| - bot.log( number, from, to, '/convert money broken' ); |
251 |
| - |
252 |
| - this.getRate(function ( rate ) { |
253 |
| - var res = {}; //once again, the lack of dynamic key names sucks. |
254 |
| - res[ to ] = number * rate; |
255 |
| - |
256 |
| - cb( res ); |
257 |
| - }); |
258 |
| - }, |
259 |
| - |
260 |
| - getRate : function ( cb ) { |
261 |
| - var self = this, |
262 |
| - rate; |
263 |
| - |
264 |
| - if ( rate = this.checkCache() ) { |
265 |
| - cb( rate ); |
266 |
| - return; |
267 |
| - } |
268 |
| - |
269 |
| - IO.jsonp({ |
270 |
| - url : 'http://rate-exchange.appspot.com/currency', |
271 |
| - jsonpName : 'callback', |
272 |
| - data : { |
273 |
| - from : self.from, |
274 |
| - to : self.to |
275 |
| - }, |
276 |
| - fun : finish |
277 |
| - }); |
278 |
| - |
279 |
| - function finish ( resp ) { |
280 |
| - rate = resp.rate; |
281 |
| - |
282 |
| - self.updateCache( rate ); |
283 |
| - cb( rate ); |
284 |
| - } |
285 |
| - }, |
286 |
| - |
287 |
| - updateCache : function ( rate ) { |
288 |
| - this.ratesCache[ this.upFrom ] = this.ratesCache[ this.upFrom ] || {}; |
289 |
| - this.ratesCache[ this.upFrom ][ this.upTo ] = { |
290 |
| - rate : rate, |
291 |
| - time : Date.now() |
292 |
| - }; |
293 |
| - }, |
294 |
| - |
295 |
| - checkCache : function () { |
296 |
| - var now = Date.now(), obj; |
297 |
| - |
298 |
| - var exists = ( |
299 |
| - this.ratesCache[ this.upFrom ] && |
300 |
| - ( obj = this.ratesCache[this.upFrom][this.upTo] ) && |
301 |
| - //so we won't request again, keep it in memory for 5 hours |
302 |
| - // 5(hours) = 1000(ms) * 60(seconds) |
303 |
| - // * 60(minutes) * 5 = 18000000 |
304 |
| - obj.time - now <= 18e6 ); |
305 |
| - |
306 |
| - console.log( this.ratesCache, exists ); |
307 |
| - |
308 |
| - return exists ? obj.rate : false; |
309 |
| - }, |
310 |
| - |
311 |
| - errorMessage : function () { |
312 |
| - if ( !this.to ) { |
313 |
| - return 'What do you want to convert ' + this.from + ' to?'; |
314 |
| - } |
315 |
| - if ( !currencies[this.upTo] ) { |
316 |
| - return this.to + ' aint no currency I ever heard of'; |
317 |
| - } |
318 |
| - } |
319 |
| -}; |
320 |
| - |
321 |
| -function listUnits () { |
322 |
| - return Object.keys( converters ); |
323 |
| -} |
324 |
| - |
325 | 4 | bot.addCommand({
|
326 | 5 | name : 'convert',
|
327 |
| - fun : convert, |
| 6 | + fun : function () { |
| 7 | + return 'Command deprecated. If you want it to stay, ping Zirak.'; |
| 8 | + }, |
328 | 9 | permissions : {
|
329 | 10 | del : 'NONE'
|
330 | 11 | },
|
|
0 commit comments