diff --git a/code/boot.js b/code/boot.js index f27ec694..7b1956a9 100644 --- a/code/boot.js +++ b/code/boot.js @@ -153,11 +153,11 @@ window.setupPlayerStat = function() { var cls = PLAYER.team === 'ALIENS' ? 'enl' : 'res'; - var t = 'Level:\t\t' + level + '\n' - + 'XM:\t\t\t' + PLAYER.energy + ' / ' + xmMax + '\n' - + 'AP:\t\t\t' + digits(ap) + '\n' + var t = 'Level:\t' + level + '\n' + + 'XM:\t' + PLAYER.energy + ' / ' + xmMax + '\n' + + 'AP:\t' + digits(ap) + '\n' + (level < 8 ? 'level up in:\t' + lvlUpAp + ' AP' : 'Congrats! (neeeeerd)') - + '\n\Invites:\t\t'+PLAYER.available_invites; + + '\n\Invites:\t'+PLAYER.available_invites; + '\n\nNote: your player stats can only be updated by a full reload (F5)'; $('#playerstat').html('' @@ -189,10 +189,57 @@ window.setupSidebarToggle = function() { }); } +window.setupTooltips = function() { + $(document).tooltip({ + // disable show/hide animation + show: { effect: "hide", duration: 0 } , + hide: false, + open: function(event, ui) { + ui.tooltip.delay(300).fadeIn(0); + }, + content: function() { + var title = $(this).attr('title'); + + // check if it should be converted to a table + if(!title.match(/\t/)) { + return title.replace(/\n/g, '
'); + } + + var data = []; + var columnCount = 0; + + // parse data + var rows = title.split('\n'); + $.each(rows, function(i, row) { + data[i] = row.split('\t'); + if(data[i].length > columnCount) columnCount = data[i].length; + }); + + // build the table + var tooltip = ''; + $.each(data, function(i, row) { + tooltip += ''; + $.each(data[i], function(k, cell) { + var attributes = ''; + if(k === 0 && data[i].length < columnCount) { + attributes = ' colspan="'+(columnCount - data[i].length + 1)+'"'; + } + tooltip += ''+cell+''; + }); + tooltip += ''; + }); + tooltip += '
'; + return tooltip; + } + }); +} + // BOOTING /////////////////////////////////////////////////////////// function boot() { + window.debug.console.overwriteNativeIfRequired(); + console.log('loading done, booting'); window.setupStyles(); window.setupMap(); @@ -202,6 +249,7 @@ function boot() { window.setupSidebarToggle(); window.updateGameScore(); window.setupPlayerStat(); + window.setupTooltips(); window.chat.setup(); // read here ONCE, so the URL is only evaluated one time after the // necessary data has been loaded. @@ -230,10 +278,11 @@ function asyncLoadScript(a){return function(b,c){var d=document.createElement("s // contains the default Ingress map style. var LLGMAPS = 'http://breunigs.github.com/ingress-intel-total-conversion/dist/leaflet_google.js'; var JQUERY = 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'; +var JQUERYUI = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js'; var LEAFLET = 'http://cdn.leafletjs.com/leaflet-0.5/leaflet.js'; var AUTOLINK = 'http://breunigs.github.com/ingress-intel-total-conversion/dist/autolink.js'; // after all scripts have loaded, boot the actual app -load(JQUERY, LEAFLET, AUTOLINK).then(LLGMAPS).onError(function (err) { +load(JQUERY, LEAFLET, AUTOLINK).then(LLGMAPS, JQUERYUI).onError(function (err) { alert('Could not all resources, the script likely won’t work.\n\nIf this happend the first time for you, it’s probably a temporary issue. Just wait a bit and try again.\n\nIf you installed the script for the first time and this happens:\n– try disabling NoScript if you have it installed\n– press CTRL+SHIFT+K in Firefox or CTRL+SHIFT+I in Chrome/Opera and reload the page. Additional info may be available in the console.\n– Open an issue at https://github.com/breunigs/ingress-intel-total-conversion/issues'); }).thenRun(boot); diff --git a/code/chat.js b/code/chat.js index 77969845..4e7b7893 100644 --- a/code/chat.js +++ b/code/chat.js @@ -1,22 +1,14 @@ window.chat = function() {}; -window.chat._lastNicksForAutocomplete = [[], []]; -window.chat.addNickForAutocomplete = function(nick, isFaction) { - var r = chat._lastNicksForAutocomplete[isFaction ? 0 : 1]; - if(r.indexOf(nick) !== -1) return; - r.push(nick); - if(r.length >= 15) - r.shift(); -} - window.chat.handleTabCompletion = function() { var el = $('#chatinput input'); var curPos = el.get(0).selectionStart; var text = el.val(); var word = text.slice(0, curPos).replace(/.*\b([a-z0-9-_])/, '$1').toLowerCase(); - var list = window.chat._lastNicksForAutocomplete; - list = list[1].concat(list[0]); + var list = $('#chat > div:visible mark'); + list = list.map(function(ind, mark) { return $(mark).text(); } ); + list = uniqueArray(list); var nick = null; for(var i = 0; i < list.length; i++) { @@ -43,27 +35,24 @@ window.chat.handleTabCompletion = function() { // timestamp and clear management // -window.chat._oldFactionTimestamp = -1; -window.chat._newFactionTimestamp = -1; -window.chat._oldPublicTimestamp = -1; -window.chat._newPublicTimestamp = -1; - -window.chat.getOldestTimestamp = function(public) { - return chat['_old'+(public ? 'Public' : 'Faction')+'Timestamp']; +window.chat.getTimestamps = function(isFaction) { + var storage = isFaction ? chat._factionData : chat._publicData; + return $.map(storage, function(v, k) { return [v[0]]; }); } -window.chat.getNewestTimestamp = function(public) { - return chat['_new'+(public ? 'Public' : 'Faction')+'Timestamp']; +window.chat.getOldestTimestamp = function(isFaction) { + var t = Math.min.apply(null, chat.getTimestamps(isFaction)); + return t === Infinity ? -1 : t; } -window.chat.clearIfRequired = function(elm) { - if(!elm.data('needsClearing')) return; - elm.data('ignoreNextScroll', true).data('needsClearing', false).html(''); +window.chat.getNewestTimestamp = function(isFaction) { + var t = Math.max.apply(null, chat.getTimestamps(isFaction)); + return t === -1*Infinity ? -1 : t; } window.chat._oldBBox = null; -window.chat.genPostData = function(public, getOlderMsgs) { - if(typeof public !== 'boolean') throw('Need to know if public or faction chat.'); +window.chat.genPostData = function(isFaction, getOlderMsgs) { + if(typeof isFaction !== 'boolean') throw('Need to know if public or faction chat.'); chat._localRangeCircle.setLatLng(map.getCenter()); var b = map.getBounds().extend(chat._localRangeCircle.getBounds()); @@ -78,35 +67,30 @@ window.chat.genPostData = function(public, getOlderMsgs) { // need to reset these flags now because clearing will only occur // after the request is finished – i.e. there would be one almost // useless request. - chat._displayedFactionGuids = []; - chat._displayedPublicGuids = []; - chat._displayedPlayerActionTime = {}; - chat._oldFactionTimestamp = -1; - chat._newFactionTimestamp = -1; - chat._oldPublicTimestamp = -1; - chat._newPublicTimestamp = -1; + chat._factionData = {}; + chat._publicData = {}; } chat._oldBBox = bbs; var ne = b.getNorthEast(); var sw = b.getSouthWest(); var data = { - desiredNumItems: public ? CHAT_PUBLIC_ITEMS : CHAT_FACTION_ITEMS, + desiredNumItems: isFaction ? CHAT_FACTION_ITEMS : CHAT_PUBLIC_ITEMS , minLatE6: Math.round(sw.lat*1E6), minLngE6: Math.round(sw.lng*1E6), maxLatE6: Math.round(ne.lat*1E6), maxLngE6: Math.round(ne.lng*1E6), minTimestampMs: -1, maxTimestampMs: -1, - factionOnly: !public + factionOnly: isFaction } if(getOlderMsgs) { // ask for older chat when scrolling up - data = $.extend(data, {maxTimestampMs: chat.getOldestTimestamp(public)}); + data = $.extend(data, {maxTimestampMs: chat.getOldestTimestamp(isFaction)}); } else { // ask for newer chat - var min = chat.getNewestTimestamp(public); + var min = chat.getNewestTimestamp(isFaction); // the inital request will have both timestamp values set to -1, // thus we receive the newest desiredNumItems. After that, we will // only receive messages with a timestamp greater or equal to min @@ -130,322 +114,217 @@ window.chat.genPostData = function(public, getOlderMsgs) { // -// requesting faction +// faction // -window.chat._requestOldFactionRunning = false; -window.chat.requestOldFaction = function(isRetry) { - if(chat._requestOldFactionRunning) return; +window.chat._requestFactionRunning = false; +window.chat.requestFaction = function(getOlderMsgs, isRetry) { + if(chat._requestFactionRunning && !isRetry) return; if(isIdle()) return renderUpdateStatus(); - chat._requestOldFactionRunning = true; + chat._requestFactionRunning = true; - var d = chat.genPostData(false, true); + var d = chat.genPostData(true, getOlderMsgs); var r = window.postAjax( 'getPaginatedPlextsV2', d, - chat.handleOldFaction, + chat.handleFaction, isRetry - ? function() { window.chat._requestOldFactionRunning = false; } - : function() { window.chat.requestOldFaction(true) } - ); - - requests.add(r); -} - -window.chat._requestNewFactionRunning = false; -window.chat.requestNewFaction = function(isRetry) { - if(chat._requestNewFactionRunning) return; - if(window.isIdle()) return renderUpdateStatus(); - chat._requestNewFactionRunning = true; - - var d = chat.genPostData(false, false); - var r = window.postAjax( - 'getPaginatedPlextsV2', - d, - chat.handleNewFaction, - isRetry - ? function() { window.chat._requestNewFactionRunning = false; } - : function() { window.chat.requestNewFaction(true) } + ? function() { window.chat._requestFactionRunning = false; } + : function() { window.chat.requestFaction(getOlderMsgs, true) } ); requests.add(r); } -// -// handle faction -// +window.chat._factionData = {}; +window.chat.handleFaction = function(data, textStatus, jqXHR) { + chat._requestFactionRunning = false; -window.chat.handleOldFaction = function(data, textStatus, jqXHR) { - chat._requestOldFactionRunning = false; - chat.handleFaction(data, textStatus, jqXHR, true); -} - -window.chat.handleNewFaction = function(data, textStatus, jqXHR) { - chat._requestNewFactionRunning = false; - chat.handleFaction(data, textStatus, jqXHR, false); -} - - - -window.chat._displayedFactionGuids = []; -window.chat.handleFaction = function(data, textStatus, jqXHR, isOldMsgs) { if(!data || !data.result) { window.failedRequestCount++; return console.warn('faction chat error. Waiting for next auto-refresh.'); } - var c = $('#chatfaction'); - chat.clearIfRequired(c); - if(data.result.length === 0) return; - chat._newFactionTimestamp = data.result[0][1]; - chat._oldFactionTimestamp = data.result[data.result.length-1][1]; + var old = chat.getOldestTimestamp(true); + chat.writeDataToHash(data, chat._factionData, false); + var oldMsgsWereAdded = old !== chat.getOldestTimestamp(true); - var scrollBefore = scrollBottom(c); - chat.renderPlayerMsgsTo(true, data, isOldMsgs, chat._displayedFactionGuids); - chat.keepScrollPosition(c, scrollBefore, isOldMsgs); + window.chat.renderFaction(oldMsgsWereAdded); if(data.result.length >= CHAT_FACTION_ITEMS) chat.needMoreMessages(); } - +window.chat.renderFaction = function(oldMsgsWereAdded) { + chat.renderData(chat._factionData, 'chatfaction', oldMsgsWereAdded); +} // -// requesting public +// public // -window.chat._requestOldPublicRunning = false; -window.chat.requestOldPublic = function(isRetry) { - if(chat._requestOldPublicRunning) return; +window.chat._requestPublicRunning = false; +window.chat.requestPublic = function(getOlderMsgs, isRetry) { + if(chat._requestPublicRunning && !isRetry) return; if(isIdle()) return renderUpdateStatus(); - chat._requestOldPublicRunning = true; + chat._requestPublicRunning = true; - var d = chat.genPostData(true, true); + var d = chat.genPostData(false, getOlderMsgs); var r = window.postAjax( 'getPaginatedPlextsV2', d, - chat.handleOldPublic, + chat.handlePublic, isRetry - ? function() { window.chat._requestOldPublicRunning = false; } - : function() { window.chat.requestOldPublic(true) } + ? function() { window.chat._requestPublicRunning = false; } + : function() { window.chat.requestPublic(getOlderMsgs, true) } ); requests.add(r); } -window.chat._requestNewPublicRunning = false; -window.chat.requestNewPublic = function(isRetry) { - if(chat._requestNewPublicRunning) return; - if(window.isIdle()) return renderUpdateStatus(); - chat._requestNewPublicRunning = true; +window.chat._publicData = {}; +window.chat.handlePublic = function(data, textStatus, jqXHR) { + chat._requestPublicRunning = false; - var d = chat.genPostData(true, false); - var r = window.postAjax( - 'getPaginatedPlextsV2', - d, - chat.handleNewPublic, - isRetry - ? function() { window.chat._requestNewPublicRunning = false; } - : function() { window.chat.requestNewPublic(true) } - ); - - requests.add(r); -} - - -// -// handle public -// - - -window.chat.handleOldPublic = function(data, textStatus, jqXHR) { - chat._requestOldPublicRunning = false; - chat.handlePublic(data, textStatus, jqXHR, true); -} - -window.chat.handleNewPublic = function(data, textStatus, jqXHR) { - chat._requestNewPublicRunning = false; - chat.handlePublic(data, textStatus, jqXHR, false); -} - -window.chat._displayedPublicGuids = []; -window.chat._displayedPlayerActionTime = {}; -window.chat.handlePublic = function(data, textStatus, jqXHR, isOldMsgs) { if(!data || !data.result) { window.failedRequestCount++; return console.warn('public chat error. Waiting for next auto-refresh.'); } - var ca = $('#chatautomated'); - var cp = $('#chatpublic'); - chat.clearIfRequired(ca); - chat.clearIfRequired(cp); - if(data.result.length === 0) return; - chat._newPublicTimestamp = data.result[0][1]; - chat._oldPublicTimestamp = data.result[data.result.length-1][1]; + var old = chat.getOldestTimestamp(true); + chat.writeDataToHash(data, chat._publicData, true); + var oldMsgsWereAdded = old !== chat.getOldestTimestamp(true); - - var scrollBefore = scrollBottom(ca); - chat.handlePublicAutomated(data); - chat.keepScrollPosition(ca, scrollBefore, isOldMsgs); - - - var scrollBefore = scrollBottom(cp); - chat.renderPlayerMsgsTo(false, data, isOldMsgs, chat._displayedPublicGuids); - chat.keepScrollPosition(cp, scrollBefore, isOldMsgs); + switch(chat.getActive()) { + case 'public': window.chat.renderPublic(oldMsgsWereAdded); break; + case 'compact': window.chat.renderCompact(oldMsgsWereAdded); break; + case 'full': window.chat.renderFull(oldMsgsWereAdded); break; + } if(data.result.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages(); } - -window.chat.handlePublicAutomated = function(data) { - $.each(data.result, function(ind, json) { // newest first! - var time = json[1]; - - // ignore player messages - var t = json[2].plext.plextType; - if(t !== 'SYSTEM_BROADCAST' && t !== 'SYSTEM_NARROWCAST') return true; - - var tmpmsg = '', nick = null, pguid, team; - - // each automated message is composed of many text chunks. loop - // over them to gather all necessary data. - $.each(json[2].plext.markup, function(ind, part) { - switch(part[0]) { - case 'PLAYER': - pguid = part[1].guid; - var lastAction = window.chat._displayedPlayerActionTime[pguid]; - // ignore older messages about player - if(lastAction && lastAction[0] > time) return false; - - nick = part[1].plain; - team = part[1].team === 'ALIENS' ? TEAM_ENL : TEAM_RES; - window.setPlayerName(pguid, nick); // free nick name resolves - if(ind > 0) tmpmsg += nick; // don’t repeat nick directly - break; - - case 'TEXT': - tmpmsg += part[1].plain; - break; - - case 'PORTAL': - var latlng = [part[1].latE6/1E6, part[1].lngE6/1E6]; - var js = 'window.zoomToAndShowPortal(\''+part[1].guid+'\', ['+latlng[0]+', '+latlng[1]+'])'; - tmpmsg += ''+part[1].name+''; - break; - } - }); - - // nick will only be set if we don’t have any info about that - // player yet. - if(nick) { - tmpmsg = chat.renderMsg(tmpmsg, nick, time, team); - window.chat._displayedPlayerActionTime[pguid] = [time, tmpmsg]; - }; - }); - - if(chat.getActive() === 'automated') - window.chat.renderAutomatedMsgsTo(); +window.chat.renderPublic = function(oldMsgsWereAdded) { + // only keep player data + var data = $.map(chat._publicData, function(entry) { + if(!entry[1]) return [entry]; + }); + chat.renderData(data, 'chatpublic', oldMsgsWereAdded); } -window.chat.renderAutomatedMsgsTo = function() { - var x = window.chat._displayedPlayerActionTime; - // we don’t care about the GUIDs anymore - var vals = $.map(x, function(v, k) { return [v]; }); - // sort them old to new - vals = vals.sort(function(a, b) { return a[0]-b[0]; }); - - var prevTime = null; - var msgs = $.map(vals, function(v) { - var nowTime = new Date(v[0]).toLocaleDateString(); - if(prevTime && prevTime !== nowTime) - var val = chat.renderDivider(nowTime) + v[1]; - else - var val = v[1]; - - prevTime = nowTime; - return val; - }).join('\n'); - - $('#chatautomated').html(msgs); +window.chat.renderCompact = function(oldMsgsWereAdded) { + var data = {}; + $.each(chat._publicData, function(guid, entry) { + // skip player msgs + if(!entry[1]) return true; + var pguid = entry[3]; + // ignore if player has newer data + if(data[pguid] && data[pguid][0] > entry[0]) return true; + data[pguid] = entry; + }); + // data keys are now player guids instead of message guids. However, + // it is all the same to renderData. + chat.renderData(data, 'chatcompact', oldMsgsWereAdded); } - +window.chat.renderFull = function(oldMsgsWereAdded) { + // only keep automatically generated data + var data = $.map(chat._publicData, function(entry) { + if(entry[1]) return [entry]; + }); + chat.renderData(data, 'chatfull', oldMsgsWereAdded); +} // // common // - -window.chat.renderPlayerMsgsTo = function(isFaction, data, isOldMsgs, dupCheckArr) { - var msgs = ''; - var prevTime = null; - - $.each(data.result.reverse(), function(ind, json) { // oldest first! - if(json[2].plext.plextType !== 'PLAYER_GENERATED') return true; - +window.chat.writeDataToHash = function(newData, storageHash, skipSecureMsgs) { + $.each(newData.result, function(ind, json) { // avoid duplicates - if(dupCheckArr.indexOf(json[0]) !== -1) return true; - dupCheckArr.push(json[0]); + if(json[0] in storageHash) return true; var time = json[1]; var team = json[2].plext.team === 'ALIENS' ? TEAM_ENL : TEAM_RES; - var msg, nick, pguid; + var auto = json[2].plext.plextType !== 'PLAYER_GENERATED'; + var msg = '', nick, pguid; $.each(json[2].plext.markup, function(ind, markup) { - if(markup[0] === 'SENDER') { + switch(markup[0]) { + case 'SENDER': // user generated messages nick = markup[1].plain.slice(0, -2); // cut “: ” at end pguid = markup[1].guid; - window.setPlayerName(pguid, nick); // free nick name resolves - if(!isOldMsgs) window.chat.addNickForAutocomplete(nick, isFaction); - } + break; - if(markup[0] === 'TEXT') { - msg = markup[1].plain.autoLink(); - msg = msg.replace(window.PLAYER['nickMatcher'], '$1'); - } + case 'PLAYER': // automatically generated messages + pguid = markup[1].guid; + nick = markup[1].plain; + team = markup[1].team === 'ALIENS' ? TEAM_ENL : TEAM_RES; + if(ind > 0) msg += nick; // don’t repeat nick directly + break; - if(!isFaction && markup[0] === 'SECURE') { - nick = null; - return false; // aka break + case 'TEXT': + var tmp = markup[1].plain.autoLink(); + msg += tmp.replace(window.PLAYER['nickMatcher'], '$1'); + break; + + case 'PORTAL': + var latlng = [markup[1].latE6/1E6, markup[1].lngE6/1E6]; + var js = 'window.zoomToAndShowPortal(\''+markup[1].guid+'\', ['+latlng[0]+', '+latlng[1]+'])'; + msg += ''+markup[1].name+''; + break; + + case 'SECURE': + if(skipSecureMsgs) { + nick = null; + return false; // breaks $.each + } } }); - if(!nick) return true; // aka next - var nowTime = new Date(time).toLocaleDateString(); - if(prevTime && prevTime !== nowTime) - msgs += chat.renderDivider(nowTime); + // format: timestamp, autogenerated, HTML message, player guid + storageHash[json[0]] = [json[1], auto, chat.renderMsg(msg, nick, time, team), pguid]; - msgs += chat.renderMsg(msg, nick, time, team); - prevTime = nowTime; + window.setPlayerName(pguid, nick); // free nick name resolves + }); +} + +// renders data from the data-hash to the element defined by the given +// ID. Set 3rd argument to true if it is likely that old data has been +// added. Latter is only required for scrolling. +window.chat.renderData = function(data, element, likelyWereOldMsgs) { + var elm = $('#'+element); + if(elm.is(':hidden')) return; + + // discard guids and sort old to new + var vals = $.map(data, function(v, k) { return [v]; }); + vals = vals.sort(function(a, b) { return a[0]-b[0]; }); + + // render to string with date separators inserted + var msgs = ''; + var prevTime = null; + $.each(vals, function(ind, msg) { + var nextTime = new Date(msg[0]).toLocaleDateString(); + if(prevTime && prevTime !== nextTime) + msgs += chat.renderDivider(nextTime); + msgs += msg[2]; + prevTime = nextTime; }); - var addTo = isFaction ? $('#chatfaction') : $('#chatpublic'); - - // if there is a change of day between two requests, handle the - // divider insertion here. - if(isOldMsgs) { - var ts = addTo.find('time:first').data('timestamp'); - var nextTime = new Date(ts).toLocaleDateString(); - if(prevTime && prevTime !== nextTime && ts) - msgs += chat.renderDivider(nextTime); - } - - if(isOldMsgs) - addTo.prepend(msgs); - else - addTo.append(msgs); + var scrollBefore = scrollBottom(elm); + elm.html(msgs); + chat.keepScrollPosition(elm, scrollBefore, likelyWereOldMsgs); } window.chat.renderDivider = function(text) { - return '─ '+text+' ────────────────────────────────────────────────────────────────────────────'; + return '─ '+text+' ──────────────────────────────────────────────────────────────────────────'; } @@ -486,27 +365,36 @@ window.chat.toggle = function() { window.chat.request = function() { console.log('refreshing chat'); - chat.requestNewFaction(); - chat.requestNewPublic(); + chat.requestFaction(false); + chat.requestPublic(false); } // checks if there are enough messages in the selected chat tab and // loads more if not. window.chat.needMoreMessages = function() { + var activeTab = chat.getActive(); + if(activeTab === 'debug') return; + var activeChat = $('#chat > :visible'); - if(scrollBottom(activeChat) !== 0 || activeChat.scrollTop() !== 0) return; - console.log('no scrollbar in active chat, requesting more msgs'); - if($('#chatcontrols a:last.active').length) - chat.requestOldFaction(); + + var hasScrollbar = scrollBottom(activeChat) !== 0 || activeChat.scrollTop() !== 0; + var nearTop = activeChat.scrollTop() <= CHAT_REQUEST_SCROLL_TOP; + if(hasScrollbar && !nearTop) return; + + console.log('No scrollbar or near top in active chat. Requesting more data.'); + + if(activeTab === 'faction') + chat.requestFaction(true); else - chat.requestOldPublic(); + chat.requestPublic(true); } window.chat.chooser = function(event) { var t = $(event.target); var tt = t.text(); + var span = $('#chatinput span'); $('#chatcontrols .active').removeClass('active'); @@ -520,24 +408,26 @@ window.chat.chooser = function(event) { case 'faction': span.css('color', ''); span.text('tell faction:'); - elm = $('#chatfaction'); break; case 'public': span.css('cssText', 'color: red !important'); span.text('broadcast:'); - elm = $('#chatpublic'); break; - case 'automated': + case 'compact': + case 'full': span.css('cssText', 'color: #bbb !important'); span.text('tell Jarvis:'); - chat.renderAutomatedMsgsTo(); - elm = $('#chatautomated'); break; + + default: + throw('chat.chooser was asked to handle unknown button: ' + tt); } + var elm = $('#chat' + tt); elm.show(); + eval('chat.render' + tt.capitalize() + '(false);'); if(elm.data('needsScrollTop')) { elm.data('ignoreNextScroll', true); elm.scrollTop(elm.data('needsScrollTop')); @@ -580,7 +470,10 @@ window.chat.setup = function() { $('#chatcontrols, #chat, #chatinput').show(); $('#chatcontrols a:first').click(window.chat.toggle); - $('#chatcontrols a:not(:first)').click(window.chat.chooser); + $('#chatcontrols a').each(function(ind, elm) { + if($.inArray($(elm).text(), ['full', 'compact', 'public', 'faction']) !== -1) + $(elm).click(window.chat.chooser); + }); $('#chatinput').click(function() { @@ -593,15 +486,15 @@ window.chat.setup = function() { $('#chatfaction').scroll(function() { var t = $(this); if(t.data('ignoreNextScroll')) return t.data('ignoreNextScroll', false); - if(t.scrollTop() < 200) chat.requestOldFaction(); - if(scrollBottom(t) === 0) chat.requestNewFaction(); + if(t.scrollTop() < CHAT_REQUEST_SCROLL_TOP) chat.requestFaction(true); + if(scrollBottom(t) === 0) chat.requestFaction(false); }); - $('#chatpublic, #chatautomated').scroll(function() { + $('#chatpublic, #chatfull, #chatcompact').scroll(function() { var t = $(this); if(t.data('ignoreNextScroll')) return t.data('ignoreNextScroll', false); - if(t.scrollTop() < 200) chat.requestOldPublic(); - if(scrollBottom(t) === 0) chat.requestNewPublic(); + if(t.scrollTop() < CHAT_REQUEST_SCROLL_TOP) chat.requestPublic(true); + if(scrollBottom(t) === 0) chat.requestPublic(false); }); chat.request(); @@ -636,38 +529,38 @@ window.chat.setupTime = function() { window.chat.setupPosting = function() { $('#chatinput input').keydown(function(event) { -try{ - - var kc = (event.keyCode ? event.keyCode : event.which); - if(kc === 13) { // enter - chat.postMsg(); - event.preventDefault(); - } else if (kc === 9) { // tab - event.preventDefault(); - window.chat.handleTabCompletion(); + try { + var kc = (event.keyCode ? event.keyCode : event.which); + if(kc === 13) { // enter + chat.postMsg(); + event.preventDefault(); + } else if (kc === 9) { // tab + event.preventDefault(); + window.chat.handleTabCompletion(); + } + } catch(error) { + console.log(error); + debug.printStackTrace(); } - - -} catch(error) { - console.log(error); - debug.printStackTrace(); -} }); $('#chatinput').submit(function(event) { - chat.postMsg(); event.preventDefault(); + chat.postMsg(); }); } window.chat.postMsg = function() { var c = chat.getActive(); - if(c === 'automated') return alert('Jarvis: A strange game. The only winning move is not to play. How about a nice game of chess?'); + if(c === 'full' || c === 'compact') + return alert('Jarvis: A strange game. The only winning move is not to play. How about a nice game of chess?'); var msg = $.trim($('#chatinput input').val()); if(!msg || msg === '') return; + if(c === 'debug') return new Function (msg)(); + var public = c === 'public'; var latlng = map.getCenter(); @@ -676,11 +569,15 @@ window.chat.postMsg = function() { lngE6: Math.round(latlng.lng*1E6), factionOnly: !public}; + var errMsg = 'Your message could not be delivered. You can copy&' + + 'paste it here and try again if you want:\n\n' + msg; + window.postAjax('sendPlext', data, - function() { if(public) chat.requestNewPublic(); else chat.requestNewFaction(); }, + function(response) { + if(response.error) alert(errMsg); + if(public) chat.requestPublic(false); else chat.requestFaction(false); }, function() { - alert('Your message could not be delivered. You can copy&' + - 'paste it here and try again if you want:\n\n'+msg); + alert(errMsg); } ); diff --git a/code/debugging.js b/code/debugging.js index 0a3ce734..b3f12ecc 100644 --- a/code/debugging.js +++ b/code/debugging.js @@ -42,3 +42,68 @@ window.debug.forceSync = function() { updateGameScore(); requestData(); } + +window.debug.console = function() { + $('#debugconsole').text(); +} + +window.debug.console.create = function() { + if($('#debugconsole').length) return; + $('#chatcontrols').append('debug'); + $('#chatcontrols a:last').click(function() { + $('#chatinput span').css('cssText', 'color: #bbb !important').text('debug:'); + $('#chat > div').hide(); + $('#debugconsole').show(); + $('#chatcontrols .active').removeClass('active'); + $(this).addClass('active'); + }); + $('#chat').append(''); +} + +window.debug.console.renderLine = function(text, errorType) { + debug.console.create(); + switch(errorType) { + case 'error': var color = '#FF424D'; break; + case 'warning': var color = '#FFDE42'; break; + case 'alert': var color = '#42FF90'; break; + default: var color = '#eee'; + } + if(typeof text !== 'string' && typeof text !== 'number') text = JSON.stringify(text); + var d = new Date(); + var ta = d.toLocaleTimeString(); // print line instead maybe? + var tb = d.toLocaleString(); + var t = ''; + var s = 'style="color:'+color+'"'; + var l = '

'+t+''+errorType+''+text+'

'; + $('#debugconsole').prepend(l); +} + +window.debug.console.log = function(text) { + debug.console.renderLine(text, 'notice'); +} + +window.debug.console.warn = function(text) { + debug.console.renderLine(text, 'warning'); +} + +window.debug.console.error = function(text) { + debug.console.renderLine(text, 'error'); +} + +window.debug.console.alert = function(text) { + debug.console.renderLine(text, 'alert'); +} + +window.debug.console.overwriteNative = function() { + window.debug.console.create(); + window.console = function() {} + window.console.log = window.debug.console.log; + window.console.warn = window.debug.console.warn; + window.console.error = window.debug.console.error; + window.alert = window.debug.console.alert; +} + +window.debug.console.overwriteNativeIfRequired = function() { + if(!window.console || L.Browser.mobile) + window.debug.console.overwriteNative(); +} diff --git a/code/game_status.js b/code/game_status.js index 6027bfe3..fe29b868 100644 --- a/code/game_status.js +++ b/code/game_status.js @@ -15,7 +15,7 @@ window.updateGameScore = function(data) { var es = ' '+Math.round(ep)+'%'; $('#gamestat').html(rs+es).one('click', function() { window.updateGameScore() }); // help cursor via “#gamestat span” - $('#gamestat').attr('title', 'Resistance:\t\t'+r+' MindUnits\nEnlightenment:\t'+e+' MindUnits'); + $('#gamestat').attr('title', 'Resistance:\t'+r+' MindUnits\nEnlightenment:\t'+e+' MindUnits'); window.setTimeout('window.updateGameScore', REFRESH_GAME_SCORE*1000); } diff --git a/code/map_data.js b/code/map_data.js index 7f8737c7..791d9705 100644 --- a/code/map_data.js +++ b/code/map_data.js @@ -75,7 +75,7 @@ window.handleDataResponse = function(data, textStatus, jqXHR) { if(getTypeByGuid(guid) === TYPE_FIELD && window.fields[guid] !== undefined) { $.each(window.fields[guid].options.vertices, function(ind, vertex) { if(window.portals[vertex.guid] === undefined) return true; - fieldArray = window.portals[vertex.guid].options.portalV2.linkedFields; + fieldArray = window.portals[vertex.guid].options.details.portalV2.linkedFields; fieldArray.splice($.inArray(guid, fieldArray), 1); }); } @@ -227,7 +227,7 @@ window.renderPortal = function(ent) { u = u || oo.level !== portalLevel; // nothing for the portal changed, so don’t update. Let resonators // manage themselves if they want to be updated. - if(!u) return renderResonators(ent); + if(!u) return renderResonators(ent, old); // there were changes, remove old portal removeByGuid(ent[0]); } @@ -237,7 +237,6 @@ window.renderPortal = function(ent) { // pre-loads player names for high zoom levels loadPlayerNamesForPortal(ent[2]); - var lvWeight = Math.max(2, portalLevel / 1.5); var lvRadius = Math.max(portalLevel + 3, 5); @@ -261,7 +260,7 @@ window.renderPortal = function(ent) { // all resonators have already removed by zooming if(isResonatorsShow()) { for(var i = 0; i <= 7; i++) - removeByGuid(portalResonatorGuid(portalGuid,i)); + removeByGuid(portalResonatorGuid(portalGuid, i)); } delete window.portals[portalGuid]; if(window.selectedPortal === portalGuid) { @@ -287,7 +286,7 @@ window.renderPortal = function(ent) { window.map.setView(latlng, 17); }); - window.renderResonators(ent); + window.renderResonators(ent, null); window.runHooks('portalAdded', {portal: p}); @@ -295,18 +294,28 @@ window.renderPortal = function(ent) { p.addTo(layerGroup); } -window.renderResonators = function(ent) { +window.renderResonators = function(ent, portalLayer) { + if(!isResonatorsShow()) return; + var portalLevel = getPortalLevel(ent[2]); if(portalLevel < getMinPortalLevel() && ent[0] != selectedPortal) return; - if(!isResonatorsShow()) return; - - for(var i=0; i < ent[2].resonatorArray.resonators.length; i++) { + var layerGroup = portalsLayers[parseInt(portalLevel)]; + var reRendered = false; + for(var i = 0; i < ent[2].resonatorArray.resonators.length; i++) { var rdata = ent[2].resonatorArray.resonators[i]; - if(rdata == null) continue; + // skip if resonator didn't change + if(portalLayer) { + var oldRes = findEntityInLeaflet(layerGroup, window.resonators, portalResonatorGuid(ent[0], i)); + if(oldRes && isSameResonator(oldRes.options.details, rdata)) continue; + } - if(window.resonators[portalResonatorGuid(ent[0],i)]) continue; + // skip and remove old resonator if no new resonator + if(rdata === null) { + if(oldRes) removeByGuid(oldRes.options.guid); + continue; + } // offset in meters var dn = rdata.distanceToPortal*SLOT_TO_LAT[rdata.slot]; @@ -332,13 +341,16 @@ window.renderResonators = function(ent) { level: rdata.level, details: rdata, pDetails: ent[2], - guid: portalResonatorGuid(ent[0],i) }); + guid: portalResonatorGuid(ent[0], i) }); r.on('remove', function() { delete window.resonators[this.options.guid]; }); r.on('add', function() { window.resonators[this.options.guid] = this; }); r.addTo(portalsLayers[parseInt(portalLevel)]); + reRendered = true; } + // if there is any resonator re-rendered, bring portal to front + if(reRendered && portalLayer) portalLayer.bringToFront(); } // append portal guid with -resonator-[slot] to get guid for resonators @@ -350,6 +362,15 @@ window.isResonatorsShow = function() { return map.getZoom() >= RESONATOR_DISPLAY_ZOOM_LEVEL; } +window.isSameResonator = function(oldRes, newRes) { + if(!oldRes && !newRes) return true; + if(typeof oldRes !== typeof newRes) return false; + if(oldRes.level !== newRes.level) return false; + if(oldRes.energyTotal !== newRes.energyTotal) return false; + if(oldRes.distanceToPortal !== newRes.distanceToPortal) return false; + return true; +} + window.portalResetColor = function(portal) { portal.setStyle({color: COLORS[getTeam(portal.options.details)]}); } diff --git a/code/portal_detail_display.js b/code/portal_detail_display.js index 98b33592..703af772 100644 --- a/code/portal_detail_display.js +++ b/code/portal_detail_display.js @@ -4,13 +4,14 @@ // methods that highlight the portal in the map view. window.renderPortalDetails = function(guid) { - var d = window.portals[guid].options.details; - if(!d) { + if(!window.portals[guid]) { unselectOldPortal(); urlPortal = guid; return; } + var d = window.portals[guid].options.details; + var update = selectPortal(guid); // collect some random data that’s not worth to put in an own method diff --git a/code/portal_detail_display_tools.js b/code/portal_detail_display_tools.js index 6bcbe96e..33571d6c 100644 --- a/code/portal_detail_display_tools.js +++ b/code/portal_detail_display_tools.js @@ -56,10 +56,10 @@ window.getModDetails = function(d) { } }); - var t = ''+mods[0]+'' - + ''+mods[1]+'' - + ''+mods[2]+'' - + ''+mods[3]+'' + var t = ''+mods[0]+'' + + ''+mods[1]+'' + + ''+mods[2]+'' + + ''+mods[3]+'' return t; } @@ -117,10 +117,10 @@ window.renderResonatorDetails = function(slot, level, nrg, dist, nick, isLeft) { var max = RESO_NRG[level]; var fillGrade = nrg/max*100; - var inf = 'energy:\t\t' + nrg + ' / ' + max + ' (' + Math.round(fillGrade) + '%)\n' - + 'level:\t\t' + level + '\n' + var inf = 'energy:\t' + nrg + ' / ' + max + ' (' + Math.round(fillGrade) + '%)\n' + + 'level:\t' + level + '\n' + 'distance:\t' + dist + 'm\n' - + 'owner:\t\t' + nick + '\n' + + 'owner:\t' + nick + '\n' + 'octant:\t' + OCTANTS[slot]; var style = 'width:'+fillGrade+'%; background:'+COLORS_LVL[level]+';'; @@ -159,8 +159,8 @@ window.getDestroyAP = function(d) { function tt(text) { var t = 'Destroy:\n'; t += resoCount + '×\tResonators\t= ' + digits(resoAp) + '\n'; - t += linkCount + '×\tLinks\t\t= ' + digits(linkAp) + '\n'; - t += fieldCount + '×\tFields\t\t= ' + digits(fieldAp) + '\n'; + t += linkCount + '×\tLinks\t= ' + digits(linkAp) + '\n'; + t += fieldCount + '×\tFields\t= ' + digits(fieldAp) + '\n'; t += 'Sum: ' + digits(sum) + ' AP'; return '' + digits(text) + ''; } diff --git a/code/request_handling.js b/code/request_handling.js index 042fc397..6fe0acd3 100644 --- a/code/request_handling.js +++ b/code/request_handling.js @@ -25,10 +25,8 @@ window.requests.abort = function() { window.activeRequests = []; window.failedRequestCount = 0; - window.chat._requestOldPublicRunning = false; - window.chat._requestNewPublicRunning = false; - window.chat._requestOldFactionRunning = false; - window.chat._requestNewFactionRunning = false; + window.chat._requestPublicRunning = false; + window.chat._requestFactionRunning = false; renderUpdateStatus(); } diff --git a/json_examples/chat_public_decay_msg.js b/json_examples/chat_public_decay_msg.js new file mode 100644 index 00000000..d7366372 --- /dev/null +++ b/json_examples/chat_public_decay_msg.js @@ -0,0 +1,49 @@ +[ + "431a30b634394956a16e62954222a37d.d", + 1360634878999, + { + "plext": { + "text": "Control Field @In the fishing village ( Lomse (Oktyabr'skaya ulitsa, 1, Kaliningrad, Kaliningrad Oblast, Russia) has decayed -108 MUs", + "markup": [ + [ + "TEXT", + { + "plain": "Control Field @" + } + ], + [ + "PORTAL", + { + "name": "In the fishing village ( Lomse", + "plain": "In the fishing village ( Lomse (Oktyabr'skaya ulitsa, 1, Kaliningrad, Kaliningrad Oblast, Russia)", + "team": "ALIENS", + "latE6": 54705182, + "address": "Oktyabr'skaya ulitsa, 1, Kaliningrad, Kaliningrad Oblast, Russia", + "lngE6": 20514959, + "guid": "6af16d09fb574c989b7fa09e718585a7.12" + } + ], + [ + "TEXT", + { + "plain": " has decayed -" + } + ], + [ + "TEXT", + { + "plain": "108" + } + ], + [ + "TEXT", + { + "plain": " MUs" + } + ] + ], + "plextType": "SYSTEM_BROADCAST", + "team": "NEUTRAL" + } + } +] diff --git a/main.js b/main.js index 64a0967d..320d0c9a 100644 --- a/main.js +++ b/main.js @@ -62,12 +62,14 @@ document.getElementsByTagName('head')[0].innerHTML = '' document.getElementsByTagName('body')[0].innerHTML = '' + '
Loading, please wait
' + '' + '' + '' + '' @@ -95,47 +97,55 @@ function wrapper() { L_PREFER_CANVAS = false; // CONFIG OPTIONS //////////////////////////////////////////////////// -var REFRESH = 30; // refresh view every 30s (base time) -var ZOOM_LEVEL_ADJ = 5; // add 5 seconds per zoom level -var REFRESH_GAME_SCORE = 5*60; // refresh game score every 5 minutes -var MAX_IDLE_TIME = 4; // stop updating map after 4min idling -var PRECACHE_PLAYER_NAMES_ZOOM = 17; // zoom level to start pre-resolving player names -var HIDDEN_SCROLLBAR_ASSUMED_WIDTH = 20; -var SIDEBAR_WIDTH = 300; +window.REFRESH = 30; // refresh view every 30s (base time) +window.ZOOM_LEVEL_ADJ = 5; // add 5 seconds per zoom level +window.REFRESH_GAME_SCORE = 5*60; // refresh game score every 5 minutes +window.MAX_IDLE_TIME = 4; // stop updating map after 4min idling +window.PRECACHE_PLAYER_NAMES_ZOOM = 17; // zoom level to start pre-resolving player names +window.HIDDEN_SCROLLBAR_ASSUMED_WIDTH = 20; +window.SIDEBAR_WIDTH = 300; // chat messages are requested for the visible viewport. On high zoom // levels this gets pretty pointless, so request messages in at least a // X km radius. -var CHAT_MIN_RANGE = 6; +window.CHAT_MIN_RANGE = 6; // this controls how far data is being drawn outside the viewport. Set // it 0 to only draw entities that intersect the current view. A value // of one will render an area twice the size of the viewport (or some- // thing like that, Leaflet doc isn’t too specific). Setting it too low // makes the missing data on move/zoom out more obvious. Setting it too // high causes too many items to be drawn, making drag&drop sluggish. -var VIEWPORT_PAD_RATIO = 0.3; +window.VIEWPORT_PAD_RATIO = 0.3; // how many items to request each query -var CHAT_PUBLIC_ITEMS = 200 -var CHAT_FACTION_ITEMS = 50 +window.CHAT_PUBLIC_ITEMS = 200; +window.CHAT_FACTION_ITEMS = 50; +// how many pixels to the top before requesting new data +window.CHAT_REQUEST_SCROLL_TOP = 200; +window.CHAT_SHRINKED = 60; // Leaflet will get very slow for MANY items. It’s better to display // only some instead of crashing the browser. -var MAX_DRAWN_PORTALS = 1000; -var MAX_DRAWN_LINKS = 400; -var MAX_DRAWN_FIELDS = 200; +window.MAX_DRAWN_PORTALS = 1000; +window.MAX_DRAWN_LINKS = 400; +window.MAX_DRAWN_FIELDS = 200; +// Minimum zoom level resonator will display +window.RESONATOR_DISPLAY_ZOOM_LEVEL = 17; - -var COLOR_SELECTED_PORTAL = '#f00'; -var COLORS = ['#FFCE00', '#0088FF', '#03FE03']; // none, res, enl -var COLORS_LVL = ['#000', '#FECE5A', '#FFA630', '#FF7315', '#E40000', '#FD2992', '#EB26CD', '#C124E0', '#9627F4']; -var COLORS_MOD = {VERY_RARE: '#F78AF6', RARE: '#AD8AFF', COMMON: '#84FBBD'}; +window.COLOR_SELECTED_PORTAL = '#f00'; +window.COLORS = ['#FFCE00', '#0088FF', '#03FE03']; // none, res, enl +window.COLORS_LVL = ['#000', '#FECE5A', '#FFA630', '#FF7315', '#E40000', '#FD2992', '#EB26CD', '#C124E0', '#9627F4']; +window.COLORS_MOD = {VERY_RARE: '#F78AF6', RARE: '#AD8AFF', COMMON: '#84FBBD'}; // circles around a selected portal that show from where you can hack // it and how far the portal reaches (i.e. how far links may be made // from this portal) -var ACCESS_INDICATOR_COLOR = 'orange'; -var RANGE_INDICATOR_COLOR = 'red'; +window.ACCESS_INDICATOR_COLOR = 'orange'; +window.RANGE_INDICATOR_COLOR = 'red'; + + +window.DEFAULT_PORTAL_IMG = 'http://commondatastorage.googleapis.com/ingress/img/default-portal-image.png'; +window.NOMINATIM = 'http://nominatim.openstreetmap.org/search?format=json&limit=1&q='; // INGRESS CONSTANTS ///////////////////////////////////////////////// // http://decodeingress.me/2012/11/18/ingress-portal-levels-and-link-range/ @@ -144,28 +154,19 @@ var MAX_XM_PER_LEVEL = [0, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]; var MIN_AP_FOR_LEVEL = [0, 10000, 30000, 70000, 150000, 300000, 600000, 1200000]; var HACK_RANGE = 40; // in meters, max. distance from portal to be able to access it var OCTANTS = ['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE']; -var DEFAULT_PORTAL_IMG = 'http://commondatastorage.googleapis.com/ingress/img/default-portal-image.png'; var DESTROY_RESONATOR = 75; //AP for destroying portal var DESTROY_LINK = 187; //AP for destroying link var DESTROY_FIELD = 750; //AP for destroying field // OTHER MORE-OR-LESS CONSTANTS ////////////////////////////////////// -var NOMINATIM = 'http://nominatim.openstreetmap.org/search?format=json&limit=1&q='; -var DEG2RAD = Math.PI / 180; var TEAM_NONE = 0, TEAM_RES = 1, TEAM_ENL = 2; var TEAM_TO_CSS = ['none', 'res', 'enl']; var TYPE_UNKNOWN = 0, TYPE_PORTAL = 1, TYPE_LINK = 2, TYPE_FIELD = 3, TYPE_PLAYER = 4, TYPE_CHAT = 5, TYPE_RESONATOR = 6; -// make PLAYER variable available in site context -var PLAYER = window.PLAYER; -var CHAT_SHRINKED = 60; -// Minimum zoom level resonator will display -var RESONATOR_DISPLAY_ZOOM_LEVEL = 17; - -// Constants for resonator positioning var SLOT_TO_LAT = [0, Math.sqrt(2)/2, 1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2]; var SLOT_TO_LNG = [1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2, 0, Math.sqrt(2)/2]; var EARTH_RADIUS=6378137; +var DEG2RAD = Math.PI / 180; // STORAGE /////////////////////////////////////////////////////////// // global variables used for storage. Most likely READ ONLY. Proper diff --git a/style.css b/style.css index 09654f4a..f179dc6a 100644 --- a/style.css +++ b/style.css @@ -191,7 +191,7 @@ em { top: 25px; } -#chatpublic, #chatautomated { +#chatpublic, #chatfull, #chatcompact, /* FIXME DEPRECATED: */#chatautomated { display: none; } @@ -226,6 +226,7 @@ time { display: inline-block; width: 44px; color: #bbb; + overflow: hidden; } mark { @@ -449,10 +450,6 @@ h3 { border: 1px solid #666; } -.mods span[title=""] { - cursor: auto; -} - .res .mods span, .res .meter { border: 1px solid #0076b6; } @@ -615,3 +612,17 @@ aside:nth-child(odd) span { #largepreview img { border: 2px solid #f8ff5e; } + +/* tooltips */ +.ui-tooltip { + background: #1E425D; + border: 1px solid #20A8B1; + box-shadow:0 0 7px #000; + color: #EEEEEE; + font-family: Verdana, sans-serif; + font-size:13px; + max-width: 300px; + padding: 8px; + position: absolute; + z-index: 9999; +}