MediaWiki:Event.js

De Guild Wars 2 Wiki
Aller à la navigation Aller à la recherche

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
/*	État événement
 *
 *	Fourni l'état d'un évènement sur l'ensemble des serveurs en utilisant l'API
 *	officielle d'ArenaNet.
 *
 *	Fonctionnalités HTML5 : CORS, Web Storage
 *
 *	Compatibilité : Chrome, Firefox, Opera, IE10
 *	Incompatibilité : IE9 et précédents
 */

(function () {
	var now = new Date().getTime();
	var cachelength = 1000 * 60 * 60 * 24; // 24 hours
	var storeid = 'gw2-api';
	var store = {};
	var ready = 0;

	var infobox = $('div.infobox.événement').first();

	function loaded(type) {
		localStorage[storeid] = JSON.stringify(store);
		ready += 1;

		if (ready === 2) {
			store['age'] = now;
			localStorage[storeid] = JSON.stringify(store);
			inject();
		}
	}

	function notloaded(type) {
		if (store[type] !== undefined) {
			ready += 1;

			if (ready === 2) {
				store['age'] = now;
				localStorage[storeid] = JSON.stringify(store);
				inject();
			}
		} else {
			console.log('API ' + type + ' didn\'t load.');
		}
	}

	function inject() {
		if (infobox.get(0)) {
			if (store.events[wgTitle.toLowerCase() + (wgTitle[wgTitle.length - 1] !== '.' ? '.' : '')]) {
				infobox.append('<strong>En cours sur <a>[voir]</a></strong>')

				var text = infobox.children().last();
				var link = text.children().last();

				text.css('margin', '0 3px');
				link.css({
					'font-size' : '85%',
					'font-weight' : 'normal',
					'width' : '3em',
					'float' : 'right',
					'text-align' : 'center',
					'margin-left' : '1em',
					'cursor' : 'pointer'
				});

				link.click(getStatus);
			}
		}
	}

	function getStatus() {
		var uri = 'https://api.guildwars2.com/v1/events.json';
		uri += '?event_id=' + store.events[wgTitle.toLowerCase() + (wgTitle[wgTitle.length - 1] !== '.' ? '.' : '')];

		$('#liste-mondes').remove();

		$.getJSON(uri, displayStatus);
	}

	function displayStatus(status) {
		var display = [];
		var html = '';
		var running = [];
		var preparing = [];

		status.events.forEach(function (event) {
			if (event.state === 'Active') {
				running.push(store.worlds[event.world_id]);
			} else if (event.state === 'Preparation') {
				preparing.push(store.worlds[event.world_id] + ' (' + event.state.toLowerCase() + ')');
			}
		});

		running.sort();
		preparing.sort();
		display = running.concat(preparing);

		if (display.length > 0) {
			html += '<ul id="liste-mondes">';
			display.forEach(function (world) {
				html += '<li>' + world + '</li>';
			});
			html += '</ul>';
		} else {
			html += '<p id="liste-mondes" style="margin:0 5px;">aucun monde actuellement</p>';
		}

		infobox.append(html);
	}

	if (localStorage[storeid] === undefined) {
		localStorage[storeid] = JSON.stringify(store);
	} else {
		store = JSON.parse(localStorage[storeid]);
	}

	if (store['age'] === undefined || now > store['age'] + cachelength) {
		$.getJSON('https://api.guildwars2.com/v1/world_names.json?lang=fr', function (data) {
			var worlds = {};

			data.forEach(function (world) {worlds[world.id] = world.name;});

			store.worlds = worlds;
		})
			.done(function () {loaded('worlds');})
			.fail(function () {notloaded('worlds');});

		$.getJSON('https://api.guildwars2.com/v1/event_names.json?lang=fr', function (data) {
			var events = {};

			data.forEach(function (event) {events[event.name.toLowerCase()] = event.id;});

			store.events = events;
		})
			.done(function () {loaded('events');})
			.fail(function () {notloaded('events');});
	} else {
		inject();
	}
})();