change guess-player-levels to use existing functions to turn name to guid

this prevents it from unnecessary server calls to convert all player GUIDs into names, when we only care about already resolved names
also gives a single point for optimisation of name to guid lookup if required
This commit is contained in:
Jon Atkins 2013-05-19 03:36:13 +01:00
parent 36dad3d58f
commit 6f6cf8efe9

View File

@ -27,30 +27,26 @@ window.plugin.guessPlayerLevels.setupCallback = function() {
addHook('portalAdded', window.plugin.guessPlayerLevels.extractPortalData);
}
// This function is intended to be called by other plugins
window.plugin.guessPlayerLevels.fetchLevelByPlayer = function(guid) {
return(window.localStorage['level-' + guid]);
}
window.plugin.guessPlayerLevels._nameToGuidCache = {};
window.plugin.guessPlayerLevels.setLevelTitle = function(dom) {
// expects dom node with nick in its child text node
var playersNamed = {};
for (var i = 0; i < localStorage.length; i++) {
var ident = localStorage.key(i);
if(ident.startsWith('level-')) {
var guid = ident.slice(6);
var level = localStorage[ident];
playersNamed[getPlayerName(guid)] = level;
}
}
var el = $(dom);
var nick = el.text();
var guid = window.playerNameToGuid(nick);
var level = guid ? localStorage['level-'+guid] : null;
var text;
if (nick in playersNamed) {
text = 'Min player level: ' + playersNamed[nick];
if(playersNamed[nick] < window.MAX_XM_PER_LEVEL.length - 1) text += ' (guessed)';
if (level) {
text = 'Min player level: ' + level;
if(level < window.MAX_XM_PER_LEVEL.length - 1) text += ' (guessed)';
} else {
text = 'Min player level unknown';
}