comparison Core.lua @ 122:6724bc8eface

Reduced usage of global functions by defining them locally.
author Zerotorescue
date Sat, 15 Jan 2011 18:52:01 +0100
parents 00cf4fc1697f
children 1729a41324d5
comparison
equal deleted inserted replaced
121:ca6280dc2f5b 122:6724bc8eface
1 -- You can access this addon's object through: LibStub("AceAddon-3.0"):GetAddon("Inventorium") 1 -- You can access this addon's object through: LibStub("AceAddon-3.0"):GetAddon("Inventorium")
2 local addon = select(2, ...); 2 local addon = select(2, ...);
3 addon = LibStub("AceAddon-3.0"):NewAddon(addon, "Inventorium", "AceEvent-3.0"); 3 addon = LibStub("AceAddon-3.0"):NewAddon(addon, "Inventorium", "AceEvent-3.0");
4
5 local _G = _G;
6 local sformat, ssplit, slower, strim, smatch = _G.string.format, _G.string.split, _G.string.lower, _G.string.trim, _G.string.match;
7 local mfloor, print, pairs, tonumber = _G.math.floor, _G.print, _G.pairs, _G.tonumber;
4 8
5 --@debug@ 9 --@debug@
6 local addonRevision = 1; 10 local addonRevision = 1;
7 --@end-debug@ 11 --@end-debug@
8 --[===[@non-debug@ 12 --[===[@non-debug@
9 local addonRevision = @project-revision@; 13 local addonRevision = @project-revision@;
10 --@end-non-debug@]===] 14 --@end-non-debug@]===]
11
12 local _G = _G;
13 local sformat, print, pairs, tonumber = _G.string.format, _G.print, _G.pairs, _G.tonumber;
14 15
15 -- All modules must be able to retrieve our supported addons database, thus keep it a part of the addon object rather than local 16 -- All modules must be able to retrieve our supported addons database, thus keep it a part of the addon object rather than local
16 addon.supportedAddons = {}; 17 addon.supportedAddons = {};
17 addon.supportedAddons.auctionPricing = {}; 18 addon.supportedAddons.auctionPricing = {};
18 addon.supportedAddons.itemCount = {}; 19 addon.supportedAddons.itemCount = {};
307 308
308 local slashArgs = {}; 309 local slashArgs = {};
309 local slashError = "Wrong argument, the following arguments are available:"; 310 local slashError = "Wrong argument, the following arguments are available:";
310 311
311 function addon:CommandHandler(message) 312 function addon:CommandHandler(message)
312 local cmd, arg = string.split(" ", (message or ""), 2); 313 local cmd, arg = ssplit(" ", (message or ""), 2);
313 cmd = string.lower(cmd); 314 cmd = slower(cmd);
314 315
315 if slashArgs[cmd] then 316 if slashArgs[cmd] then
316 -- Pass a reference to the addon (to be used as "self") and the provided arg 317 -- Pass a reference to the addon (to be used as "self") and the provided arg
317 slashArgs[cmd](addon, arg); 318 slashArgs[cmd](addon, arg);
318 else 319 else
367 local copperText = "%s%d|cffeda55fc|r"; 368 local copperText = "%s%d|cffeda55fc|r";
368 369
369 function addon:ReadableMoney(copper, clean) 370 function addon:ReadableMoney(copper, clean)
370 local text = ""; 371 local text = "";
371 372
372 local gold = floor( copper / COPPER_PER_GOLD ); 373 local gold = mfloor( copper / COPPER_PER_GOLD );
373 if gold > 0 then 374 if gold > 0 then
374 text = sformat(goldText, text, gold); 375 text = sformat(goldText, text, gold);
375 end 376 end
376 377
377 if not clean or (not gold or gold < 10) then 378 if not clean or (not gold or gold < 10) then
378 local silver = floor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER ); 379 local silver = mfloor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER );
379 if silver > 0 then 380 if silver > 0 then
380 text = sformat(silverText, text, silver); 381 text = sformat(silverText, text, silver);
381 end 382 end
382 383
383 if not clean or (not gold or gold < 1) then 384 if not clean or (not gold or gold < 1) then
384 local copper = floor( copper % COPPER_PER_SILVER ); 385 local copper = mfloor( copper % COPPER_PER_SILVER );
385 if copper > 0 or text == "" then 386 if copper > 0 or text == "" then
386 text = sformat(copperText, text, copper); 387 text = sformat(copperText, text, copper);
387 end 388 end
388 end 389 end
389 end 390 end
390 391
391 392
392 return string.trim(text); 393 return strim(text);
393 end 394 end
394 395
395 function addon:ReadableMoneyToCopper(value) 396 function addon:ReadableMoneyToCopper(value)
396 -- If a player enters a value it will be filled without color codes 397 -- If a player enters a value it will be filled without color codes
397 -- If it is retrieved from the database, it will be colored coded 398 -- If it is retrieved from the database, it will be colored coded
398 -- Thus we look for both 399 -- Thus we look for both
399 local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g")); 400 local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
400 local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s")); 401 local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
401 local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c")); 402 local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
402 403
403 return ( (gold or 0) * COPPER_PER_GOLD ) + ( (silver or 0) * COPPER_PER_SILVER ) + (copper or 0); 404 return ( (gold or 0) * COPPER_PER_GOLD ) + ( (silver or 0) * COPPER_PER_SILVER ) + (copper or 0);
404 end 405 end
405 406
406 function addon:ValidateReadableMoney(info, value) 407 function addon:ValidateReadableMoney(info, value)
407 -- If a player enters a value it will be filled without color codes 408 -- If a player enters a value it will be filled without color codes
408 -- If it is retrieved from the database, it will be colored coded 409 -- If it is retrieved from the database, it will be colored coded
409 -- Thus we look for both 410 -- Thus we look for both
410 local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g")); 411 local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
411 local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s")); 412 local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
412 local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c")); 413 local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
413 414
414 if not gold and not silver and not copper then 415 if not gold and not silver and not copper then
415 return "The provided amount of money is invalid. Please provide the amount of money as #g#s#c, e.g. 591617g24s43c."; 416 return "The provided amount of money is invalid. Please provide the amount of money as #g#s#c, e.g. 591617g24s43c.";
416 else 417 else
417 return true; 418 return true;