Skip to content

Commit

Permalink
chat: use server order
Browse files Browse the repository at this point in the history
  • Loading branch information
le-jeu committed Dec 24, 2020
1 parent 278923f commit c013ebf
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions core/code/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ window.chat.genPostData = function(channel, storageHash, getOlderMsgs) {
// after the request is finished – i.e. there would be one almost
// useless request.
chat._faction.data = {};
chat._faction.guids = [];
chat._faction.oldestTimestamp = -1;
chat._faction.newestTimestamp = -1;
delete chat._faction.oldestGUID;
delete chat._faction.newestGUID;

chat._public.data = {};
chat._public.guids = [];
chat._public.oldestTimestamp = -1;
chat._public.newestTimestamp = -1;
delete chat._public.oldestGUID;
delete chat._public.newestGUID;

chat._alerts.data = {};
chat._alerts.guids = [];
chat._alerts.oldestTimestamp = -1;
chat._alerts.newestTimestamp = -1;
delete chat._alerts.oldestGUID;
Expand Down Expand Up @@ -164,7 +167,7 @@ window.chat.requestFaction = function(getOlderMsgs, isRetry) {
}


window.chat._faction = {data:{}, oldestTimestamp:-1, newestTimestamp:-1};
window.chat._faction = {data:{}, guids: [], oldestTimestamp:-1, newestTimestamp:-1};
window.chat.handleFaction = function(data, olderMsgs, ascendingTimestampOrder) {
chat._requestFactionRunning = false;
$("#chatcontrols a:contains('faction')").removeClass('loading');
Expand All @@ -186,7 +189,7 @@ window.chat.handleFaction = function(data, olderMsgs, ascendingTimestampOrder) {
}

window.chat.renderFaction = function(oldMsgsWereAdded) {
chat.renderData(chat._faction.data, 'chatfaction', oldMsgsWereAdded);
chat.renderData(chat._faction, 'chatfaction', oldMsgsWereAdded);
}


Expand All @@ -212,7 +215,7 @@ window.chat.requestPublic = function(getOlderMsgs, isRetry) {
);
}

window.chat._public = {data:{}, oldestTimestamp:-1, newestTimestamp:-1};
window.chat._public = {data:{}, guids: [], oldestTimestamp:-1, newestTimestamp:-1};
window.chat.handlePublic = function(data, olderMsgs, ascendingTimestampOrder) {
chat._requestPublicRunning = false;
$("#chatcontrols a:contains('all')").removeClass('loading');
Expand All @@ -235,7 +238,7 @@ window.chat.handlePublic = function(data, olderMsgs, ascendingTimestampOrder) {
}

window.chat.renderPublic = function(oldMsgsWereAdded) {
chat.renderData(chat._public.data, 'chatall', oldMsgsWereAdded);
chat.renderData(chat._public, 'chatall', oldMsgsWereAdded);
}


Expand All @@ -262,7 +265,7 @@ window.chat.requestAlerts = function(getOlderMsgs, isRetry) {
}


window.chat._alerts = {data:{}, oldestTimestamp:-1, newestTimestamp:-1};
window.chat._alerts = {data:{}, guids: [], oldestTimestamp:-1, newestTimestamp:-1};
window.chat.handleAlerts = function(data, olderMsgs, ascendingTimestampOrder) {
chat._requestAlertsRunning = false;
$("#chatcontrols a:contains('alerts')").removeClass('loading');
Expand All @@ -285,7 +288,7 @@ window.chat.handleAlerts = function(data, olderMsgs, ascendingTimestampOrder) {
}

window.chat.renderAlerts = function(oldMsgsWereAdded) {
chat.renderData(chat._alerts.data, 'chatalerts', oldMsgsWereAdded);
chat.renderData(chat._alerts, 'chatalerts', oldMsgsWereAdded);
}


Expand Down Expand Up @@ -401,6 +404,10 @@ window.chat.writeDataToHash = function(newData, storageHash, isPublicChannel, is

// format: timestamp, autogenerated, HTML message, nick, additional data (parsed, plugin specific data...)
storageHash.data[parsedData.guid] = [parsedData.time, parsedData.auto, chat.renderMsgRow(parsedData), parsedData.player.name, parsedData];
if (isAscendingOrder)
storageHash.guids.push(parsedData.guid);
else
storageHash.guids.unshift(parsedData.guid);
});
}

Expand Down Expand Up @@ -562,19 +569,20 @@ window.chat.renderDivider = function(text) {
// 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) {
window.chat.renderData = function(storageHash, element, likelyWereOldMsgs) {
var elm = $('#'+element);
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]; });
// 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) {
storageHash.guids.forEach(function(guid) {
var msg = storageHash.data[guid];
var nextTime = new Date(msg[0]).toLocaleDateString();
if(prevTime && prevTime !== nextTime)
msgs += chat.renderDivider(nextTime);
Expand Down

0 comments on commit c013ebf

Please sign in to comment.