diff --git a/code/chat.js b/code/chat.js index 3101aaa2..928092a7 100644 --- a/code/chat.js +++ b/code/chat.js @@ -1,5 +1,21 @@ window.chat = function() {}; +//WORK IN PROGRESS - NOT YET USED!! +window.chat.commTabs = [ +// channel: the COMM channel ('tab' parameter in server requests) +// name: visible name +// inputPrompt: string for the input prompt +// inputColor: (optional) color for input +// sendMessage: (optional) function to send the message (to override the default of sendPlext) +// globalBounds: (optional) if true, always use global latLng bounds + {channel:'all', name:'All', inputPrompt: 'broadcast:', inputColor:'#f66'}, + {channel:'faction', name:'Aaction', inputPrompt: 'tell faction:'}, + {channel:'alerts', name:'Alerts', inputPrompt: 'tell Jarvis:', inputColor: '#666', globalBounds: true, sendMessage: function() { + alert("Jarvis: A strange game. The only winning move is not to play. How about a nice game of chess?\n(You can't chat to the 'alerts' channel!)"); + }}, +]; + + window.chat.handleTabCompletion = function() { var el = $('#chatinput input'); var curPos = el.get(0).selectionStart; @@ -38,8 +54,8 @@ window.chat.handleTabCompletion = function() { window.chat._oldBBox = null; -window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) { - if(typeof isFaction !== 'boolean') throw('Need to know if public or faction chat.'); +window.chat.genPostData = function(channel, storageHash, getOlderMsgs) { + if (typeof channel !== 'string') throw ('API changed: isFaction flag now a channel string - all, faction, alerts'); var b = clampLatLngBounds(map.getBounds()); @@ -66,6 +82,10 @@ window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) { chat._public.oldestTimestamp = -1; chat._public.newestTimestamp = -1; + chat._alerts.data = {}; + chat._alerts.oldestTimestamp = -1; + chat._alerts.newestTimestamp = -1; + chat._oldBBox = b; } @@ -79,7 +99,7 @@ window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) { maxLngE6: Math.round(ne.lng*1E6), minTimestampMs: -1, maxTimestampMs: -1, - tab: isFaction ? 'faction' : 'all' + tab: channel, } if(getOlderMsgs) { @@ -123,7 +143,7 @@ window.chat.requestFaction = function(getOlderMsgs, isRetry) { if(isIdle()) return renderUpdateStatus(); chat._requestFactionRunning = true; - var d = chat.genPostData(true, chat._faction, getOlderMsgs); + var d = chat.genPostData('faction', chat._faction, getOlderMsgs); var r = window.postAjax( 'getPlexts', d, @@ -153,8 +173,6 @@ window.chat.handleFaction = function(data, olderMsgs) { runHooks('factionChatDataAvailable', {raw: data, processed: chat._faction.data}); window.chat.renderFaction(oldMsgsWereAdded); - - if(data.success.length >= CHAT_FACTION_ITEMS) chat.needMoreMessages(); } window.chat.renderFaction = function(oldMsgsWereAdded) { @@ -163,7 +181,7 @@ window.chat.renderFaction = function(oldMsgsWereAdded) { // -// public +// all // window.chat._requestPublicRunning = false; @@ -172,7 +190,7 @@ window.chat.requestPublic = function(getOlderMsgs, isRetry) { if(isIdle()) return renderUpdateStatus(); chat._requestPublicRunning = true; - var d = chat.genPostData(false, chat._public, getOlderMsgs); + var d = chat.genPostData('all', chat._public, getOlderMsgs); var r = window.postAjax( 'getPlexts', d, @@ -200,46 +218,63 @@ window.chat.handlePublic = function(data, olderMsgs) { runHooks('publicChatDataAvailable', {raw: data, processed: chat._public.data}); - switch(chat.getActive()) { - case 'public': window.chat.renderPublic(oldMsgsWereAdded); break; - case 'compact': window.chat.renderCompact(oldMsgsWereAdded); break; - case 'full': window.chat.renderFull(oldMsgsWereAdded); break; - } + window.chat.renderPublic(oldMsgsWereAdded); - if(data.success.length >= CHAT_PUBLIC_ITEMS) chat.needMoreMessages(); } window.chat.renderPublic = function(oldMsgsWereAdded) { - // only keep player data - var data = $.map(chat._public.data, function(entry) { - if(!entry[1]) return [entry]; - }); - chat.renderData(data, 'chatpublic', oldMsgsWereAdded); + chat.renderData(chat._public.data, 'chatall', oldMsgsWereAdded); } -window.chat.renderCompact = function(oldMsgsWereAdded) { - var data = {}; - $.each(chat._public.data, function(guid, entry) { - // skip player msgs - if(!entry[1]) return true; - var nick = entry[3]; - // ignore if player has newer data - if(data[nick] && data[nick][0] > entry[0]) return true; - data[nick] = entry; - }); - // data keys are now player nicks instead of message guids. However, - // it is all the same to renderData. - chat.renderData(data, 'chatcompact', oldMsgsWereAdded); + +// +// alerts +// + +window.chat._requestAlertsRunning = false; +window.chat.requestAlerts = function(getOlderMsgs, isRetry) { + if(chat._requestAlertsRunning && !isRetry) return; + if(isIdle()) return renderUpdateStatus(); + chat._requestAlertsRunning = true; + + var d = chat.genPostData('alerts', chat._alerts, getOlderMsgs); + var r = window.postAjax( + 'getPlexts', + d, + function(data, textStatus, jqXHR) { chat.handleAlerts(data, getOlderMsgs); }, + isRetry + ? function() { window.chat._requestAlertsRunning = false; } + : function() { window.chat.requestAlerts(getOlderMsgs, true) } + ); } -window.chat.renderFull = function(oldMsgsWereAdded) { - // only keep automatically generated data - var data = $.map(chat._public.data, function(entry) { - if(entry[1]) return [entry]; - }); - chat.renderData(data, 'chatfull', oldMsgsWereAdded); + +window.chat._alerts = {data:{}, oldestTimestamp:-1, newestTimestamp:-1}; +window.chat.handleAlerts = function(data, olderMsgs) { + chat._requestAlertsRunning = false; + + if(!data || !data.success) { + window.failedRequestCount++; + return console.warn('alerts chat error. Waiting for next auto-refresh.'); + } + + if(data.success.length === 0) return; + + var old = chat._alerts.oldestTimestamp; + chat.writeDataToHash(data, chat._alerts, undefined, olderMsgs); //NOTE: isPublic passed as undefined - it's nether public or private! + var oldMsgsWereAdded = old !== chat._alerts.oldestTimestamp; + +// no hoot for alerts - API change planned here... +// runHooks('alertsChatDataAvailable', {raw: data, processed: chat._alerts.data}); + + window.chat.renderAlerts(oldMsgsWereAdded); } +window.chat.renderAlerts = function(oldMsgsWereAdded) { + chat.renderData(chat._alerts.data, 'chatalerts', oldMsgsWereAdded); +} + + // // common @@ -271,7 +306,7 @@ window.chat.writeDataToHash = function(newData, storageHash, isPublicChannel, is if (storageHash.newestTimestamp === -1 || storageHash.newestTimestamp < time) storageHash.newestTimestamp = time; //remove "Your X on Y was destroyed by Z" from the faction channel - if (systemNarrowcast && !isPublicChannel) return true; +// if (systemNarrowcast && !isPublicChannel) return true; var msg = '', nick = ''; $.each(json[2].plext.markup, function(ind, markup) { @@ -326,21 +361,21 @@ window.chat.writeDataToHash = function(newData, storageHash, isPublicChannel, is }); - //skip secure messages on the public channel - if (isPublicChannel && isSecureMessage) return true; +// //skip secure messages on the public channel +// if (isPublicChannel && isSecureMessage) return true; - //skip public messages (e.g. @player mentions) on the secure channel - if ((!isPublicChannel) && (!isSecureMessage)) return true; +// //skip public messages (e.g. @player mentions) on the secure channel +// if ((!isPublicChannel) && (!isSecureMessage)) return true; - //NOTE: these two are currently redundant with the above two tests - but code can change... + //NOTE: these two are redundant with the above two tests in place - but things have changed... //from the server, private channel messages are flagged with a SECURE string '[secure] ', and appear in //both the public and private channels //we don't include this '[secure]' text above, as it's redundant in the faction-only channel //let's add it here though if we have a secure message in the public channel, or the reverse if a non-secure in the faction one - if (isPublicChannel && isSecureMessage) msg = '[secure] ' + msg; + if (!systemNarrowcast && !(isPublicChannel===false) && isSecureMessage) msg = '[faction] ' + msg; //and, add the reverse - a 'public' marker to messages in the private channel - if ((!isPublicChannel) && (!isSecureMessage)) msg = '[public] ' + msg; + if (!systemNarrowcast && !(isPublicChannel===true) && (!isSecureMessage)) msg = '[public] ' + msg; // format: timestamp, autogenerated, HTML message @@ -367,6 +402,7 @@ window.chat.renderData = function(data, element, likelyWereOldMsgs) { if(elm.is(':hidden')) return; // discard guids and sort old to new +//TODO? stable sort, to preserve server message ordering? or sort by GUID if timestamps equal? var vals = $.map(data, function(v, k) { return [v]; }); vals = vals.sort(function(a, b) { return a[0]-b[0]; }); @@ -433,7 +469,7 @@ window.chat.getActive = function() { window.chat.tabToChannel = function(tab) { if (tab == 'faction') return 'faction'; if (tab == 'alerts') return 'alerts'; - return 'all'; //for 'full', 'compact' and 'public' + return 'all'; }; @@ -491,6 +527,9 @@ window.chat.request = function() { // the 'public', 'full' and 'compact' tabs are all based off the 'public' COMM data chat.requestPublic(false); } + if (channel == 'alerts' || (window.chat.backgroundChannels && window.chat.backgroundChannels['alerts'])) { + chat.requestAlerts(false); + } } @@ -516,54 +555,60 @@ window.chat.needMoreMessages = function() { }; -window.chat.chooseAnchor = function(t) { +window.chat.chooseTab = function(tab) { + if (tab != 'all' && tab != 'faction' && tab != 'alerts') { + console.warn('chat tab "'+t+'" requested - but only "all", "faction" and "alerts" are valid - assuming "all" wanted'); + tab = 'all'; + } + var oldTab = chat.getActive(); - var oldChannel = chat.tabToChannel(oldTab); - var tt = t.text(); - - localStorage['iitc-chat-tab'] = tt; + localStorage['iitc-chat-tab'] = tab; var mark = $('#chatinput mark'); var input = $('#chatinput input'); $('#chatcontrols .active').removeClass('active'); - $("#chatcontrols a:contains('" + tt + "')").addClass('active'); + $("#chatcontrols a:contains('" + tab + "')").addClass('active'); - var newChannel = chat.tabToChannel(tt); - if (newChannel != oldChannel) startRefreshTimeout(0.1*1000); //only chat uses the refresh timer stuff, so a perfect way of forcing an early refresh after a tab change + if (tab != oldTab) startRefreshTimeout(0.1*1000); //only chat uses the refresh timer stuff, so a perfect way of forcing an early refresh after a tab change $('#chat > div').hide(); var elm; - switch(tt) { + switch(tab) { case 'faction': input.css('color', ''); mark.css('color', ''); mark.text('tell faction:'); + + chat.renderFaction(false); break; - case 'public': + case 'all': input.css('cssText', 'color: #f66 !important'); mark.css('cssText', 'color: #f66 !important'); mark.text('broadcast:'); + + chat.renderPublic(false); break; - case 'compact': - case 'full': + case 'alerts': mark.css('cssText', 'color: #bbb !important'); input.css('cssText', 'color: #bbb !important'); mark.text('tell Jarvis:'); + + //chat.renderAlerts(false); break; default: throw('chat.chooser was asked to handle unknown button: ' + tt); } - var elm = $('#chat' + tt); + var elm = $('#chat' + tab); elm.show(); - eval('chat.render' + tt.capitalize() + '(false);'); + if(elm.data('needsScrollTop')) { elm.data('ignoreNextScroll', true); elm.scrollTop(elm.data('needsScrollTop')); @@ -577,13 +622,13 @@ window.chat.show = function(name) { : $('#updatestatus').show(); $('#chat, #chatinput').show(); - var t = $(''+name+''); - window.chat.chooseAnchor(t); + window.chat.chooseTab(name); } window.chat.chooser = function(event) { var t = $(event.target); - window.chat.chooseAnchor(t); + var tab = t.text(); + window.chat.chooseTab(tab); } // contains the logic to keep the correct scroll position. @@ -614,15 +659,14 @@ window.chat.keepScrollPosition = function(box, scrollBefore, isOldMsgs) { window.chat.setup = function() { if (localStorage['iitc-chat-tab']) { - var t = $(''+localStorage['iitc-chat-tab']+''); - window.chat.chooseAnchor(t); - } + chat.chooseTab(localStorage['iitc-chat-tab']); + } $('#chatcontrols, #chat, #chatinput').show(); $('#chatcontrols a:first').click(window.chat.toggle); $('#chatcontrols a').each(function(ind, elm) { - if($.inArray($(elm).text(), ['full', 'compact', 'public', 'faction']) !== -1) + if($.inArray($(elm).text(), ['all', 'faction', 'alerts']) !== -1) $(elm).click(window.chat.chooser); }); @@ -641,13 +685,20 @@ window.chat.setup = function() { if(scrollBottom(t) === 0) chat.requestFaction(false); }); - $('#chatpublic, #chatfull, #chatcompact').scroll(function() { + $('#chatall').scroll(function() { var t = $(this); if(t.data('ignoreNextScroll')) return t.data('ignoreNextScroll', false); if(t.scrollTop() < CHAT_REQUEST_SCROLL_TOP) chat.requestPublic(true); if(scrollBottom(t) === 0) chat.requestPublic(false); }); + $('#chatalerts').scroll(function() { + var t = $(this); + if(t.data('ignoreNextScroll')) return t.data('ignoreNextScroll', false); + if(t.scrollTop() < CHAT_REQUEST_SCROLL_TOP) chat.requestAlerts(true); + if(scrollBottom(t) === 0) chat.requestAlerts(false); + }); + window.requests.addRefreshFunction(chat.request); var cls = PLAYER.team === 'RESISTANCE' ? 'res' : 'enl'; @@ -708,8 +759,8 @@ window.chat.setupPosting = function() { window.chat.postMsg = function() { var c = chat.getActive(); - 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?'); + if(c == 'alerts') + return alert("Jarvis: A strange game. The only winning move is not to play. How about a nice game of chess?\n(You can't chat to the 'alerts' channel!)"); var msg = $.trim($('#chatinput input').val()); if(!msg || msg === '') return; @@ -727,13 +778,12 @@ window.chat.postMsg = function() { return result; } - var publik = c === 'public'; var latlng = map.getCenter(); var data = {message: msg, latE6: Math.round(latlng.lat*1E6), lngE6: Math.round(latlng.lng*1E6), - tab: publik ? 'all' : 'faction'}; + tab: c}; var errMsg = 'Your message could not be delivered. You can copy&' + 'paste it here and try again if you want:\n\n' + msg; @@ -741,7 +791,8 @@ window.chat.postMsg = function() { window.postAjax('sendPlext', data, function(response) { if(response.error) alert(errMsg); - if(publik) chat.requestPublic(false); else chat.requestFaction(false); }, + startRefreshTimeout(0.1*1000); //only chat uses the refresh timer stuff, so a perfect way of forcing an early refresh after a send message + }, function() { alert(errMsg); } diff --git a/main.js b/main.js index b81da567..f82d03f0 100644 --- a/main.js +++ b/main.js @@ -72,14 +72,14 @@ document.getElementsByTagName('body')[0].innerHTML = '' + '