Skip to content

Commit

Permalink
Merge pull request #633 from xscreach/machina-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
modos189 authored May 30, 2023
2 parents 7a684e8 + 3f83ead commit 4d9aea1
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 17 deletions.
31 changes: 16 additions & 15 deletions core/code/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ window.chat.updateOldNewHash = function(newData, storageHash, isOlderMsgs, isAsc
}
};

window.chat.parseMsgData = function(data) {
window.chat.parseMsgData = function (data) {
var categories = data[2].plext.categories;
var isPublic = (categories & 1) === 1;
var isSecure = (categories & 2) === 2;
Expand All @@ -365,7 +365,7 @@ window.chat.parseMsgData = function(data) {
var msgToPlayer = msgAlert && (isPublic || isSecure);

var time = data[1];
var team = data[2].plext.team === 'RESISTANCE' ? TEAM_RES : TEAM_ENL;
var team = window.teamStringToId(data[2].plext.team);
var auto = data[2].plext.plextType !== 'PLAYER_GENERATED';
var systemNarrowcast = data[2].plext.plextType === 'SYSTEM_NARROWCAST';

Expand All @@ -374,17 +374,17 @@ window.chat.parseMsgData = function(data) {
var nick = '';
markup.forEach(function(ent) {
switch (ent[0]) {
case 'SENDER': // user generated messages
nick = ent[1].plain.replace(/: $/, ''); // cut “: ” at end
break;
case 'SENDER': // user generated messages
nick = ent[1].plain.replace(/: $/, ''); // cut “: ” at end
break;

case 'PLAYER': // automatically generated messages
nick = ent[1].plain;
team = ent[1].team === 'RESISTANCE' ? TEAM_RES : TEAM_ENL;
break;
case 'PLAYER': // automatically generated messages
nick = ent[1].plain;
team = window.teamStringToId(ent[1].team);
break;

default:
break;
default:
break;
}
});

Expand Down Expand Up @@ -457,8 +457,9 @@ window.chat.renderPortal = function (portal) {
};

window.chat.renderFactionEnt = function (faction) {
var name = faction.team === 'ENLIGHTENED' ? 'Enlightened' : 'Resistance';
var spanClass = faction.team === 'ENLIGHTENED' ? TEAM_TO_CSS[TEAM_ENL] : TEAM_TO_CSS[TEAM_RES];
var teamId = window.teamStringToId(faction.team);
var name = window.TEAM_NAMES[teamId];
var spanClass = window.TEAM_TO_CSS[teamId];
return $('<div>').html($('<span>')
.attr('class', spanClass)
.text(name)).html();
Expand Down Expand Up @@ -545,8 +546,8 @@ window.chat.renderMsgRow = function(data) {
var timeCell = chat.renderTimeCell(data.time, timeClass);

var nickClasses = ['nickname'];
if (data.player.team === TEAM_ENL || data.player.team === TEAM_RES) {
nickClasses.push(TEAM_TO_CSS[data.player.team]);
if (window.TEAM_TO_CSS[data.player.team]) {
nickClasses.push(window.TEAM_TO_CSS[data.player.team]);
}
// highlight things said/done by the player in a unique colour
// (similar to @player mentions from others in the chat text itself)
Expand Down
Binary file added plugins/images/marker-machina.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions plugins/machina-tracker.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.plugin-machina-tracker-popup a {
color: inherit;
text-decoration: underline;
text-decoration-style: dashed;
-moz-text-decoration-style: dashed;
-webkit-text-decoration-style: dashed;
}

.plugin-machina-tracker-popup-header {
white-space: nowrap;
}

ul.plugin-machina-tracker-link-list {
list-style: none;
padding-left: 1em;
}

ul.plugin-machina-tracker-link-list li {
position: relative;
display: flex;
justify-content: space-between;
white-space: nowrap;
}

ul.plugin-machina-tracker-link-list li a {
margin-right: 3px;
}

ul.plugin-machina-tracker-link-list li::before {
content: '↴';
position: absolute;
left: -1em;
}
212 changes: 212 additions & 0 deletions plugins/machina-tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// @name Machina tracker
// @author McBen
// @category Layer
// @version 1.0.0
// @description Show locations of Machina activities

/* exported setup --eslint */
/* global L */

// ensure plugin framework is there, even if iitc is not yet loaded
if (typeof window.plugin !== 'function') window.plugin = function () {};

// PLUGIN START ////////////////////////////////////////////////////////
// use own namespace for plugin
window.plugin.machinaTracker = function () {};
var machinaTracker = window.plugin.machinaTracker;

machinaTracker.MACHINA_TRACKER_MAX_TIME = 8 * 60 * 60 * 1000;
machinaTracker.MACHINA_TRACKER_MIN_ZOOM = 9;

machinaTracker.events = [];

machinaTracker.setup = () => {
$('<style>').prop('type', 'text/css').html('@include_string:machina-tracker.css@').appendTo('head');

var iconImage = '@include_img:images/marker-machina.png@';

machinaTracker.icon = L.icon({
iconUrl: iconImage,
iconSize: [26, 32],
iconAnchor: [12, 32],
});

machinaTracker.popup = new L.Popup({ offset: L.point([1, -34]) });
machinaTracker.drawnTraces = new L.LayerGroup([], { minZoom: machinaTracker.MACHINA_TRACKER_MIN_ZOOM });
window.addLayerGroup('Machina Tracker', machinaTracker.drawnTraces, true);

window.addHook('publicChatDataAvailable', machinaTracker.handleData);

window.map.on('zoomend', machinaTracker.zoomListener);
machinaTracker.zoomListener();
};

machinaTracker.onClickListener = (event) => {
var marker = event.target;

if (marker.options.desc) {
machinaTracker.popup.setContent(marker.options.desc);
machinaTracker.popup.setLatLng(marker.getLatLng());
window.map.openPopup(machinaTracker.popup);
}
};

machinaTracker.zoomListener = function () {
var ctrl = $('.leaflet-control-layers-list span:contains("Machina Tracker")').parent('label');
if (window.map.getZoom() < machinaTracker.MACHINA_TRACKER_MIN_ZOOM) {
machinaTracker.drawnTraces.clearLayers();
ctrl.addClass('disabled').attr('title', 'Zoom in to show those.');
// note: zoomListener is also called at init time to set up things, so we only need to do this in here
window.chat.backgroundChannelData('plugin.machinaTracker', 'all', false); // disable this plugin's interest in 'all' COMM
} else {
ctrl.removeClass('disabled').attr('title', '');
// note: zoomListener is also called at init time to set up things, so we only need to do this in here
window.chat.backgroundChannelData('plugin.machinaTracker', 'all', true); // enable this plugin's interest in 'all' COMM
}
};

machinaTracker.getLimit = function () {
return new Date().getTime() - machinaTracker.MACHINA_TRACKER_MAX_TIME;
};

machinaTracker.discardOldData = function () {
var limit = machinaTracker.getLimit();
machinaTracker.events = machinaTracker.events.reduce((result, event) => {
event.to = event.to.filter((to) => to.time >= limit);
if (event.to.length) {
result.push(event);
}
return result;
}, []);
};

machinaTracker.toLanLng = function (locationData) {
return L.latLng(locationData.latE6 / 1e6, locationData.lngE6 / 1e6);
};

machinaTracker.createEvent = function (json) {
var newEvent = { time: json[1] };
json[2].plext.markup.forEach((markup) => {
switch (markup[0]) {
case 'PLAYER':
newEvent.team = markup[1].team;
break;
case 'PORTAL':
if (!newEvent.from) {
newEvent.from = {
latLng: machinaTracker.toLanLng(markup[1]),
name: markup[1].name,
};
} else {
newEvent.to = [
{
latLng: machinaTracker.toLanLng(markup[1]),
name: markup[1].name,
time: json[1],
},
];
}
break;
}
});

return newEvent;
};

machinaTracker.processNewData = function (data) {
var limit = machinaTracker.getLimit();
data.result.forEach((json) => {
if (json[1] >= limit) {
var newEvent = machinaTracker.createEvent(json);
if (newEvent.from && newEvent.to && [window.TEAM_MAC, window.TEAM_NONE].includes(window.teamStringToId(newEvent.team))) {
var prevEvent = machinaTracker.events.find((e) => e.from.latLng.equals(newEvent.from.latLng));
if (!prevEvent) {
machinaTracker.events.push(newEvent);
} else {
var newTo = newEvent.to[0];
if (!prevEvent.to.some((to) => newTo.latLng.equals(to.latLng) && newTo.time === to.time)) {
prevEvent.to.push(newTo);
prevEvent.to.sort((a, b) => a.time - b.time);
prevEvent.time = prevEvent.to[0].time;
}
}
}
}
});
};

machinaTracker.ago = function (time, now) {
var s = (now - time) / 1000;
var h = Math.floor(s / 3600);
var m = Math.floor((s % 3600) / 60);
var returnVal = m + 'm';
if (h > 0) {
returnVal = h + 'h' + returnVal;
}
return returnVal + ' ago';
};

machinaTracker.createPortalLink = function (portal) {
return $('<a>')
.addClass('text-overflow-ellipsis')
.text(portal.name)
.prop({
title: portal.name,
href: window.makePermalink(portal.latLng),
})
.click((event) => {
window.selectPortalByLatLng(portal.latLng);
event.preventDefault();
return false;
})
.dblclick((event) => {
window.map.setView(portal.latLng, window.DEFAULT_ZOOM);
window.selectPortalByLatLng(portal.latLng);
event.preventDefault();
return false;
});
};

machinaTracker.drawData = function () {
var isTouchDev = window.isTouchDevice();

var split = machinaTracker.MACHINA_TRACKER_MAX_TIME / 4;
var now = new Date().getTime();

machinaTracker.events.forEach((event) => {
var ageBucket = Math.min((now - event.time) / split, 3);
var position = event.from.latLng;

var title = isTouchDev ? '' : machinaTracker.ago(event.time, now);
var icon = machinaTracker.icon;
var opacity = 1 - 0.2 * ageBucket;

var popup = $('<div>').addClass('plugin-machina-tracker-popup');
$('<div>').addClass('plugin-machina-tracker-popup-header').append(machinaTracker.createPortalLink(event.from)).appendTo(popup);

var linkList = $('<ul>').addClass('plugin-machina-tracker-link-list');
linkList.appendTo(popup);

event.to.forEach((to) => {
$('<li>').append(machinaTracker.createPortalLink(to)).append(' ').append(machinaTracker.ago(to.time, now)).appendTo(linkList);
});

var m = L.marker(position, { icon: icon, opacity: opacity, desc: popup[0], title: title });
m.addEventListener('spiderfiedclick', machinaTracker.onClickListener);

window.registerMarkerForOMS(m);
m.addTo(machinaTracker.drawnTraces);
});
};

machinaTracker.handleData = function (data) {
if (window.map.getZoom() < machinaTracker.MACHINA_TRACKER_MIN_ZOOM) return;

machinaTracker.discardOldData();
machinaTracker.processNewData(data);

machinaTracker.drawnTraces.clearLayers();
machinaTracker.drawData();
};

var setup = machinaTracker.setup;
6 changes: 4 additions & 2 deletions plugins/player-activity-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ window.plugin.playerTracker.setup = function() {
window.plugin.playerTracker.zoomListener();
});
window.plugin.playerTracker.zoomListener();

plugin.playerTracker.setupUserSearch();
}

Expand Down Expand Up @@ -169,7 +169,9 @@ window.plugin.playerTracker.processNewData = function(data) {
});

// skip unusable events
if(!plrname || !lat || !lng || !id || skipThisMessage) return true;
if (!plrname || !lat || !lng || !id || skipThisMessage || ![window.TEAM_RES, window.TEAM_ENL].includes(window.teamStringToId(json[2].plext.team))) {
return true;
}

var newEvent = {
latlngs: [[lat, lng]],
Expand Down

0 comments on commit 4d9aea1

Please sign in to comment.