Ir para o conteúdo

MediaWiki:Common.js

De Anarquia Brasil Wiki

Nota: Após publicar, você pode ter que limpar o "cache" do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
  • Opera: Pressione Ctrl-F5.
/* Anarquia Brasil Wiki: numeros ao vivo (cron wiki-dados.py -> /dados/*.json) */
( function () {
	function num( n, c ) { return Number( n ).toLocaleString( 'pt-BR', { minimumFractionDigits: c || 0, maximumFractionDigits: c || 0 } ); }
	function pega( url ) { return fetch( url, { cache: 'no-cache' } ).then( function ( r ) { return r.json(); } ); }
	$( function () {
		if ( $( '.ao-vivo-online' ).length ) {
			pega( '/dados/status.json' ).then( function ( s ) { $( '.ao-vivo-online' ).text( s.online ? num( s.players ) : 'offline' ); } ).catch( function () {} );
		}
		if ( $( '.ao-vivo-mundo, .ao-vivo-nether, .ao-vivo-overworld, .ao-vivo-contas' ).length ) {
			pega( '/dados/mundo.json' ).then( function ( m ) {
				var b = m.bytes || {}, gb = function ( x ) { x = ( x || 0 ) / 1e9; return num( x, x < 10 ? 1 : 0 ) + ' GB'; };
				$( '.ao-vivo-mundo' ).text( gb( ( b.overworld || 0 ) + ( b.nether || 0 ) + ( b.end || 0 ) ) );
				$( '.ao-vivo-nether' ).text( gb( b.nether ) );
				$( '.ao-vivo-overworld' ).text( gb( b.overworld ) );
				$( '.ao-vivo-contas' ).text( num( m.contas ) );
			} ).catch( function () {} );
		}
		if ( $( '.ao-vivo-dias' ).length ) {
			var h = new Date(), d = Math.floor( ( Date.UTC( h.getFullYear(), h.getMonth(), h.getDate() ) - Date.UTC( 2026, 10, 27 ) ) / 864e5 ) + 1;
			$( '.ao-vivo-dias' ).text( num( d ) );
		}
		var $r = $( '[data-ranking]' );
		if ( $r.length ) {
			pega( '/dados/rankings.json' ).then( function ( r ) {
				$r.each( function () {
					var $t = $( this ), lista = r[ $t.attr( 'data-ranking' ) ] || [], un = $t.attr( 'data-unidade' ) || '', c = +( $t.attr( 'data-casas' ) || 0 ), $b = $t.find( 'tbody' );
					$b.find( 'tr' ).slice( 1 ).remove();
					lista.slice( 0, 10 ).forEach( function ( x, i ) {
						$b.append( $( '<tr>' ).append( $( '<td>' ).text( i + 1 ), $( '<td>' ).text( x.nome ), $( '<td>' ).text( num( x.valor, c ) + ( un ? ' ' + un : '' ) ) ) );
					} );
				} );
				var dt = new Date( r.atualizado * 1000 );
				$( '.ao-vivo-ranking-data' ).text( dt.toLocaleString( 'pt-BR', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' } ) );
			} ).catch( function () {} );
		}
	} );
}() );