MediaWiki:Gadget-events.js: Difference between revisions
Created page with "//<nowiki> →* * Interface for events * * @version 1.1 * @author JaydenKieran * @author Elessar2 * @author ThePsionic *: /*global jQuery, mediaWiki, mw, gswiki, rs..." |
No edit summary |
||
| Line 1,390: | Line 1,390: | ||
); | ); | ||
}); | }); | ||
// Update resets | |||
moment.relativeTimeThreshold('d', 31); | |||
moment.relativeTimeThreshold('h', 24); | |||
var dailyReset = moment(now).add(1, 'days').startOf('day'), | |||
dailyTitle = dailyReset.format('MMM D') + ' at ' + dailyReset.format('HH:mm') + ' game time', | |||
monthlyReset = moment(now).add(1, 'months').startOf('month'), | |||
monthlyTitle = monthlyReset.format('MMM D') + ' at ' + monthlyReset.format('HH:mm') + ' game time', | |||
weeklyReset = moment(now).day(3).startOf('day'); // reset is a wednesday | |||
if (now.isAfter(weeklyReset)) { | |||
weeklyReset.add(1, 'week'); | |||
} | |||
var weeklyTitle = weeklyReset.format('MMM D') + ' at ' + weeklyReset.format('HH:mm') + ' game time'; | |||
$content.find('.countdown-day').attr({'datetime':dailyReset.format(), 'title':dailyTitle }).text( dailyReset.fromNow(true) ); | |||
$content.find('.countdown-week').attr({'datetime':weeklyReset.format(), 'title':weeklyTitle }).text( weeklyReset.fromNow(true) ); | |||
$content.find('.countdown-month').attr({'datetime':monthlyReset.format(), 'title':monthlyTitle }).text( monthlyReset.fromNow(true) ); | |||
moment.relativeTimeThreshold('h', 22); | |||
moment.relativeTimeThreshold('d', 26); | |||
// Events team events | |||
$content.find('.events-team').empty(); | |||
if (self.settings.et_events) { | |||
$content.find('.events-team').append( | |||
$('<h3>').html('<a href="GSWiki:Events_Team" title="Events Team events">Events Team</a>'), | |||
$('<ul>') | |||
.addClass('events-list etevents-list') | |||
.append( | |||
$('<li>').text('No upcoming events') | |||
) | |||
); | |||
var events_array = []; | |||
et_events.forEach( function (e) { | |||
var emom = moment(e.date).utc(), | |||
time = emom.fromNow(true), | |||
sort = emom.diff(now, 'seconds'), | |||
dtime = emom.format(), | |||
title = emom.format('MMM D') + ' at ' + emom.format('HH:mm') + ' game time'; | |||
if (sort >= 0) { | |||
if (sort < (e.length * 60)) { | |||
sort = 0; | |||
time = 'now!'; | |||
} | |||
events_array.push( {name:e.name, txt:time, sort:sort, dtime:dtime, title:title} ); | |||
} else if (sort > -(e.length * 60)) { | |||
sort = 0; | |||
time = 'now!'; | |||
events_array.push( {name:e.name, txt:time, sort:sort, dtime:dtime, title:title} ); | |||
} | |||
}); | |||
// Sort and trim | |||
events_array.sort( function (a,b) { | |||
return a.sort - b.sort; | |||
}); | |||
if (self.settings.num_events > 0) { | |||
events_array = events_array.slice(0,self.settings.num_events); | |||
} | |||
// Print Events | |||
if (events_array.length > 0) { | |||
$content.find('.etevents-list').empty(); | |||
events_array.forEach( function (e) { | |||
var eClass = 'event'; | |||
if (e.sort === 0) { eClass += ' now'; } | |||
$content.find('.etevents-list').append( | |||
$('<li>') | |||
.addClass(eClass) | |||
.append( | |||
$('<label>').addClass('event-title').text(e.name), | |||
$('<time>').addClass('event-timer').attr({ 'datetime':e.dtime, 'title':e.title }).text(e.txt) | |||
) | |||
); | |||
}); | |||
} | |||
} | |||
}, | |||
/** | |||
* Update the Voice of Seren values in the Events popup | |||
* @return {undefined} | |||
*/ | |||
updateVos: function () { | |||
// Current UTC time | |||
var now = moment.utc(); | |||
// Error updating VoS | |||
var vosError = function (jqXHR, status, error) { | |||
console.warn('Error loading VoS:\n' + status + ': ' + error); | |||
$content.find('.VoS-time').empty().append('There was an error loading the Voice of Seren. Please try again later.<br/>If this issue persists, please <a href="/w/RS:AR">contact an administrator</a>.'); | |||
$content.find('.VoS-container').empty(); | |||
$content.find('.VoS-last').empty().append('not found'); | |||
}; | |||
// Update Voice of Seren (VoS) | |||
var vosSucc = function (vosjson, status, jqXHR) { | |||
mw.log(vosjson); | |||
if (!(vosjson.data && vosjson.data[0] && vosjson.data[0].districts)) { | |||
vosError({}, 'Missing districts', 'returned json did not contain a districts array'); | |||
return; | |||
} | |||
var voschng = moment(now).add(1, 'hours').startOf('hour'), | |||
chngtitle = voschng.format('MMM D') + ' at ' + voschng.format('HH:mm') + ' game time', | |||
currvos = vosjson.data[0].districts, | |||
lastvos = vosjson.data[1].districts; | |||
$content.find('.VoS-time').empty().append( | |||
$('<time>').addClass('time').attr({ 'datetime':voschng.format(), 'title':chngtitle }).text(voschng.fromNow(true)), | |||
' until new districts are active.' | |||
); | |||
$content.find('.VoS-container').empty().append( | |||
$('<div>').addClass('event-box VoS-district').append( | |||
$('<div>').addClass('VoS-image VoS-'+currvos[0]), | |||
$('<div>').addClass('VoS-text').append( | |||
$('<span>').addClass('by-line').attr('title', voslocs[currvos[0]][1]).text(voslocs[currvos[0]][0]), | |||
$('<label>').addClass('event-name').html('<a href="/w/' + currvos[0] + '_Clan" title="' + currvos[0] + ' Clan">' + currvos[0] + '</a>') | |||
) | |||
), | |||
$('<div>').addClass('event-box VoS-district').append( | |||
$('<div>').addClass('VoS-image VoS-'+currvos[1]), | |||
$('<div>').addClass('VoS-text').append( | |||
$('<span>').addClass('by-line').attr('title', voslocs[currvos[1]][1]).text(voslocs[currvos[1]][0]), | |||
$('<label>').addClass('event-name').html('<a href="/w/' + currvos[1] + '_Clan" title="' + currvos[1] + ' Clan">' + currvos[1] + '</a>') | |||
) | |||
) | |||
); | |||
$content.find('.VoS-last').empty(); | |||
$content.find('.VoS-last').append(lastvos[0] + ', ' + lastvos[1]); | |||
// Set time for next check | |||
self.nextvos = moment(now).startOf('hour').add(61, 'minutes').format(); | |||
}; | |||
// Make api Call | |||
$content.find('.VoS-time').empty().append('Loading current VoS...'); | |||
$content.find('.VoS-container').empty(); | |||
$content.find('.VoS-last').empty().append('Loading last VoS...'); | |||
$.ajax({ | |||
dataType: 'json', | |||
url: vosurl, | |||
error: vosError, | |||
success: vosSucc | |||
}); | |||
}, | |||
/** | |||
* Update the Voice of Seren time display only | |||
* @return {undefined} | |||
*/ | |||
updateVostime: function () { | |||
// Next change | |||
var voschng = moment.utc().add(1, 'hours').startOf('hour'), | |||
chngtitle = voschng.format('MMM D') + ' at ' + voschng.format('HH:mm') + ' game time'; | |||
// Update time display | |||
$content.find('.VoS-time').empty().append( | |||
$('<time>').addClass('time').attr({ 'datetime':voschng.format(), 'title':chngtitle }).text(voschng.fromNow(true)), | |||
' until new districts are active.' | |||
); | |||
}, | |||
/** | |||
* Updates the values that are per day in the Events popup | |||
* Raven, spotlights (D&D, ED, MiniGame), PVM, Travelling Merchant | |||
* @return {undefined} | |||
*/ | |||
updateSpots: function () { | |||
// Current UTC time | |||
var now = moment.utc(); | |||
// Update raven | |||
$content.find('.raven').empty(); | |||
if (self.settings.raven) { | |||
var $raven = $('<div>').addClass('raven-status by-line now').text('There is a raven currently spawned in Prifddinas.'), | |||
days_passed = -( moment('0', 'X').utc().add(6, 'days').diff(now, 'days') ), | |||
days_into = days_passed % 13; | |||
if (days_into > 0) { | |||
var when = moment(now).add((13 - days_into), 'days').startOf('day'), | |||
whenttl = when.format('MMM D') + ' at ' + when.format('HH:mm') + ' game time'; | |||
$raven = $('<div>').addClass('raven-status by-line').append( | |||
$('<time>').addClass('time').attr({ 'datetime':when.format(), 'title':whenttl }).text( when.fromNow(true) ), | |||
' until the raven spawns in Prifddinas.' | |||
); | |||
} | |||
$content.find('.raven').append( | |||
$('<h3>').html('<a href="/w/Raven_(Prifddinas)" title="Prifddinas Raven">Raven</a>'), | |||
$raven | |||
); | |||
} | |||
// Travelling Merchant Stock | |||
var tmsimg = function (item) { | |||
if (item == 'uim') { | |||
return '<a href="/w/Uncharted_island_map_(Deep_Sea_Fishing)" title="Uncharted island map (Deep Sea Fishing)"><img alt="Uncharted island map (Deep Sea Fishing).png" src="/images/0/0d/Uncharted_island_map_%28Deep_Sea_Fishing%29.png?93a55"></a>'; | |||
} else if (item == 'Silverhawk down') { | |||
return '<a href="/w/' + item + '" title="' + item + '"><img alt="' + item +' 5.png" src="' + rs.getFileURL(item + ' 5.png') + '?1234"></a>'; | |||
} else { | |||
return '<a href="/w/' + item + '" title="' + item + '"><img alt="' + item +'.png" src="' + rs.getFileURL(item + '.png') + '?1234"></a>'; | |||
} | |||
}; | |||
$content.find('.tms-pof .tms').remove(); | |||
if (self.settings.merch) { | |||
var $tms = $('<div>').addClass('tms'); | |||
// Stock for today and tomorrow | |||
$tms.append( | |||
$('<h3>').html('<a href="/w/Travelling_Merchant\'s_Shop" title="Travelling Merchant\'s Shop">Travelling Merchant\'s Shop</a>'), | |||
$('<div>').addClass('tms-pof-cols').append( | |||
$('<div>').addClass('tms-today').append( | |||
$('<div>').addClass('by-line').text('Today\'s stock'), | |||
$('<div>').addClass('event-boxes inventory-image').append( | |||
$('<div>') | |||
.addClass('event-box') | |||
.html( tmsimg('uim') ) | |||
) | |||
), | |||
$('<div>').addClass('tms-tomorrow').append( | |||
$('<div>').addClass('by-line').text('Tomorrow\'s stock'), | |||
$('<div>').addClass('event-boxes inventory-image').append( | |||
$('<div>') | |||
.addClass('event-box') | |||
.html( tmsimg('uim') ) | |||
) | |||
) | |||
) | |||
); | |||
merchstock.tod.forEach( function (i) { | |||
var name = i.name.replace(/&/g, '&'); | |||
$tms.find('.tms-today .event-boxes').append( | |||
$('<div>') | |||
.addClass('event-box') | |||
.html( tmsimg(name) ) | |||
); | |||
}); | |||
merchstock.tmr.forEach( function (i) { | |||
var name = i.name.replace(/&/g, '&'); | |||
$tms.find('.tms-tomorrow .event-boxes').append( | |||
$('<div>') | |||
.addClass('event-box') | |||
.html( tmsimg(name) ) | |||
); | |||
}); | |||
if (self.settings.farm) { | |||
$tms.addClass('col col-l'); | |||
$content.find('.tms-pof').addClass('usecols'); | |||
} else { | |||
$tms.find('.tms-pof-cols').addClass('usecols'); | |||
$content.find('.tms-pof').removeClass('usecols'); | |||
} | |||
$content.find('.tms-pof').append( | |||
$tms | |||
); | |||
} | |||
/** | |||
* Gets the current spotlight item, the next one, and time till next | |||
* @param {Array.<string>} items Array of the possible spotlights in the correct order | |||
* @param {number} duration Duration in days of a spotlight item | |||
* @param {number} offset Offset from 0 Unix time that rotation started in days | |||
* @return {Array.<string>} An array containing the current item, time till next, and next item | |||
*/ | |||
var spotlights = function (items, duration, offset) { | |||
// Start rotation from Tuesday 1 January 1970, with an offset | |||
var days_passed = -( moment('0', 'X').utc().add(offset, 'days').diff(now, 'days') ), | |||
days_into = days_passed % (duration * items.length), | |||
rotation = Math.floor(days_into / duration), | |||
days_till = duration - (days_into % duration), | |||
when = moment(now).add(days_till, 'days').startOf('day'), | |||
whenttl = when.format('MMM D') + ' at ' + when.format('HH:mm') + ' game time', | |||
next = rotation + 1; | |||
if (next >= items.length) { | |||
next -= items.length; | |||
} | |||
mw.log(items); | |||
mw.log(days_into); | |||
mw.log(rotation); | |||
mw.log(items[rotation]); | |||
mw.log(days_till); | |||
mw.log(when.format()); | |||
mw.log(when.fromNow(true)); | |||
mw.log(items[next]); | |||
return [items[rotation], when.fromNow(true), items[next], when.format(), whenttl]; | |||
}; | |||
// Player-owned farm | |||
$content.find('.tms-pof .pof').remove(); | |||
if (self.settings.farm) { | |||
var small = moment(now).add(1, 'days').startOf('day'), | |||
smallttl = small.format('MMM D') + ' at ' + small.format('HH:mm') + ' game time', | |||
med_array = spotlights(['med'], 2, 1), | |||
large_array = spotlights(['large'], 3, 0), | |||
medr_array = spotlights(['med'], 3, 0), | |||
hardreq = moment(now).day(3).startOf('day'); | |||
if (now.isAfter(hardreq)) { | |||
hardreq.add(1, 'week'); | |||
} | |||
var hardreqttl = hardreq.format('MMM D') + ' at ' + hardreq.format('HH:mm') + ' game time'; | |||
var $pof = $('<div>').addClass('pof').append( | |||
$('<h3>').html('<a href="/w/Player-owned_farm" title="Player-owned farm">Player-owned farm</a>'), | |||
$('<div>').addClass('tms-pof-cols').append( | |||
$('<div>').addClass('buyers').append( | |||
$('<div>').addClass('by-line').text('Animal buyers'), | |||
$('<div>').addClass('event-boxes').append( | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Small'), | |||
$('<time>').addClass('event-name').attr({'datetime':small.format(), 'title':smallttl}).text( small.fromNow(true) ) | |||
), | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Medium'), | |||
$('<time>').addClass('event-name').attr({'datetime':med_array[3], 'title':med_array[4]}).text( med_array[1] ) | |||
), | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Large'), | |||
$('<time>').addClass('event-name').attr({'datetime':large_array[3], 'title':large_array[4]}).text( large_array[1] ) | |||
) | |||
) | |||
), | |||
$('<div>').addClass('requests').append( | |||
$('<div>').addClass('by-line').text('Requests'), | |||
$('<div>').addClass('event-boxes').append( | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Easy'), | |||
$('<time>').addClass('event-name').attr({'datetime':small.format(), 'title':smallttl}).text( small.fromNow(true) ) | |||
), | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Medium'), | |||
$('<time>').addClass('event-name').attr({'datetime':medr_array[3], 'title':medr_array[4]}).text( medr_array[1] ) | |||
), | |||
$('<div>').addClass('event-box').append( | |||
$('<label>').addClass('by-line').text('Hard'), | |||
$('<time>').addClass('event-name').attr({'datetime':hardreq.format(), 'title':hardreqttl}).text( hardreq.fromNow(true) ) | |||
) | |||
) | |||
) | |||
) | |||
); | |||
if (self.settings.merch) { | |||
$pof.addClass('col col-r'); | |||
$content.find('.tms-pof').addClass('usecols'); | |||
} else { | |||
$pof.find('.tms-pof-cols').addClass('usecols'); | |||
$content.find('.tms-pof').removeClass('usecols'); | |||
} | |||
$content.find('.tms-pof').append( $pof ); | |||
} | |||
// PvM | |||
$content.find('.pvm').empty(); | |||
if (self.settings.pvm) { | |||
var $vorago = $('<div>').addClass('pvm-value vorago-value'), | |||
$araxxor = $('<div>').addClass('pvm-value araxxor-value'), | |||
$rots = $('<div>').addClass('pvm-value rots-value'); | |||