219 lines
8.3 KiB
PHP
219 lines
8.3 KiB
PHP
<?php
|
|
/**
|
|
* Assistent: Server-Status (BungeeCord Status Plugin) und MOTD-Formatierung.
|
|
*
|
|
* Ausgelagert aus inc/assistant-ajax.php; wird von dort geladen.
|
|
*
|
|
* @package Minecraft-Modern-Theme
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit;
|
|
|
|
// ============================================================
|
|
// INTENT: SERVER STATUS (BungeeCord Status Plugin)
|
|
// ============================================================
|
|
|
|
function mm_intent_server_status( $bot ) {
|
|
$lines = array();
|
|
|
|
// ── 1. Statische Infos aus Bot-Setup ──────────────────────
|
|
$ip = $bot['server_ip'] ?? '';
|
|
$ver = $bot['server_ver'] ?? '';
|
|
$specs = $bot['server_specs'] ?? '';
|
|
$discord = $bot['link_discord'] ?? '';
|
|
|
|
if ( $ip ) $lines[] = '🌐 <b>Adresse:</b> <code>' . esc_html( $ip ) . '</code>';
|
|
if ( $ver ) $lines[] = '🎮 <b>Version:</b> ' . esc_html( $ver );
|
|
if ( $specs ) $lines[] = '🖥️ <b>Server:</b> ' . esc_html( $specs );
|
|
|
|
// ── 2. Live-Daten via BungeeCord-Status-Plugin ─────────────
|
|
$servers = get_option( 'mcss_servers', array() );
|
|
|
|
if ( ! empty( $servers ) && function_exists( 'mcss_fetch_server_with_ranks' ) ) {
|
|
$srv = $servers[0];
|
|
$cache_key = 'mm_srv_' . md5( ( $srv['host'] ?? '' ) . ( $srv['player_port'] ?? '9191' ) );
|
|
$data = get_transient( $cache_key );
|
|
|
|
// Caching-Zeit erhöht auf 60 Sekunden
|
|
if ( false === $data ) {
|
|
try {
|
|
$data = mcss_fetch_server_with_ranks( $srv );
|
|
} catch ( \Throwable $e ) {
|
|
$data = null;
|
|
}
|
|
set_transient( $cache_key, $data, 60 );
|
|
}
|
|
|
|
if ( ! empty( $data ) && isset( $data['online'] ) ) {
|
|
$is_online = (bool) $data['online'];
|
|
$icon = $is_online ? '🟢' : '🔴';
|
|
$status = $is_online ? '<b>Online</b>' : '<b>Offline</b>';
|
|
|
|
// Spieler-Anzahl (players = Array von Objekten)
|
|
$player_list = isset( $data['players'] ) && is_array( $data['players'] ) ? $data['players'] : array();
|
|
$player_count = count( $player_list );
|
|
$player_text = $is_online ? ' · <b>' . $player_count . '</b> Spieler online' : '';
|
|
|
|
$srv_label = ! empty( $srv['name'] ) ? esc_html( $srv['name'] ) . ': ' : '';
|
|
$lines[] = $icon . ' ' . $srv_label . $status . $player_text;
|
|
|
|
// Version vom Server
|
|
if ( $is_online && ! empty( $data['version'] ) && empty( $ver ) ) {
|
|
$lines[] = '🎮 <b>Version:</b> ' . esc_html( $data['version'] );
|
|
}
|
|
|
|
// MOTD
|
|
if ( $is_online && ! empty( $data['motd'] ) ) {
|
|
$motd = is_array( $data['motd'] )
|
|
? implode( ' ', $data['motd'] )
|
|
: (string) $data['motd'];
|
|
$motd = trim( $motd );
|
|
if ( $motd ) {
|
|
$lines[] = '💬 ' . mm_format_motd( $motd );
|
|
}
|
|
}
|
|
|
|
// Spieler auflisten (bis 8)
|
|
if ( $is_online && ! empty( $player_list ) ) {
|
|
$names = array();
|
|
foreach ( array_slice( $player_list, 0, 8 ) as $p ) {
|
|
if ( is_array( $p ) && ! empty( $p['name'] ) ) {
|
|
$names[] = esc_html( $p['name'] );
|
|
} elseif ( is_string( $p ) ) {
|
|
$names[] = esc_html( $p );
|
|
}
|
|
}
|
|
if ( ! empty( $names ) ) {
|
|
$lines[] = '👥 <b>Spieler:</b> ' . implode( ', ', $names )
|
|
. ( $player_count > 8 ? ' <small>+' . ( $player_count - 8 ) . ' weitere</small>' : '' );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Weitere konfigurierte Server kurz anzeigen
|
|
if ( count( $servers ) > 1 ) {
|
|
$extra = array();
|
|
foreach ( array_slice( $servers, 1 ) as $s ) {
|
|
if ( empty( $s['name'] ) ) continue;
|
|
$ck = 'mm_srv_' . md5( ( $s['host'] ?? '' ) . ( $s['player_port'] ?? '9191' ) );
|
|
$d = get_transient( $ck );
|
|
if ( false === $d && function_exists( 'mcss_fetch_server_with_ranks' ) ) {
|
|
try {
|
|
$d = mcss_fetch_server_with_ranks( $s );
|
|
} catch ( \Throwable $e ) {
|
|
$d = null;
|
|
}
|
|
set_transient( $ck, $d, 60 );
|
|
}
|
|
$on = ! empty( $d['online'] );
|
|
$count = ( $on && ! empty( $d['players'] ) ) ? count( $d['players'] ) : 0;
|
|
$extra[] = ( $on ? '🟢' : '🔴' ) . ' ' . esc_html( $s['name'] )
|
|
. ( $on ? ' (' . $count . ')' : '' );
|
|
}
|
|
if ( ! empty( $extra ) ) {
|
|
$lines[] = '🗺️ <b>Weitere Server:</b> ' . implode( ' · ', $extra );
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── 3. Kein Plugin, keine Daten ───────────────────────────
|
|
if ( empty( $lines ) ) {
|
|
return array(
|
|
'source' => 'server_status',
|
|
'title' => '🖥️ Server-Status',
|
|
'content' => 'Server-Infos wurden noch nicht konfiguriert. Bitte im Bot-Setup eintragen.',
|
|
);
|
|
}
|
|
|
|
// ── 4. Links (nur Discord, kein Wiki-Link mehr) ─────────────
|
|
if ( $discord ) {
|
|
$lines[] = '💬 <a href="' . esc_url( $discord ) . '" target="_blank">Discord</a>';
|
|
}
|
|
|
|
return array(
|
|
'source' => 'server_status',
|
|
'title' => '🖥️ Server-Status',
|
|
'content' => implode( '<br>', $lines ),
|
|
);
|
|
}
|
|
|
|
// ============================================================
|
|
// HILFSFUNKTION: Minecraft §-Farbcodes → HTML
|
|
// Unterstützt §0-§f (Farben) und §l/§o/§n/§m/§r (Format)
|
|
// ============================================================
|
|
|
|
function mm_format_motd( $text ) {
|
|
if ( empty( $text ) ) return '';
|
|
|
|
$colors = array(
|
|
'0' => '#000000',
|
|
'1' => '#0000AA',
|
|
'2' => '#00AA00',
|
|
'3' => '#00AAAA',
|
|
'4' => '#AA0000',
|
|
'5' => '#AA00AA',
|
|
'6' => '#FFAA00',
|
|
'7' => '#AAAAAA',
|
|
'8' => '#555555',
|
|
'9' => '#5555FF',
|
|
'a' => '#55FF55',
|
|
'b' => '#55FFFF',
|
|
'c' => '#FF5555',
|
|
'd' => '#FF55FF',
|
|
'e' => '#FFFF55',
|
|
'f' => '#FFFFFF',
|
|
);
|
|
|
|
// Normalisieren: § und & beide unterstützen
|
|
$section = "\xC2\xA7"; // UTF-8 für §
|
|
$text = str_replace( '&', $section, $text );
|
|
|
|
// Aufteilen nach §X Segmenten
|
|
$parts = preg_split( '/\xC2\xA7([0-9a-fklmnorA-FKLMNOR])/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
|
$output = '';
|
|
$open = 0;
|
|
$bold = false;
|
|
$italic = false;
|
|
|
|
foreach ( $parts as $i => $part ) {
|
|
if ( $i % 2 === 0 ) {
|
|
// Text-Segment
|
|
if ( $part !== '' ) {
|
|
$output .= esc_html( $part );
|
|
}
|
|
} else {
|
|
// Code-Segment
|
|
$code = strtolower( $part );
|
|
|
|
// Offene Spans schließen
|
|
if ( $open > 0 ) {
|
|
$output .= str_repeat( '</span>', $open );
|
|
$open = 0;
|
|
}
|
|
if ( $bold ) { $output .= '</b>'; $bold = false; }
|
|
if ( $italic ) { $output .= '</i>'; $italic = false; }
|
|
|
|
if ( isset( $colors[ $code ] ) ) {
|
|
$output .= '<span style="color:' . $colors[ $code ] . ';">';
|
|
$open++;
|
|
} elseif ( $code === 'l' ) {
|
|
$output .= '<b>';
|
|
$bold = true;
|
|
} elseif ( $code === 'o' ) {
|
|
$output .= '<i>';
|
|
$italic = true;
|
|
} elseif ( $code === 'r' ) {
|
|
// Reset — alles schon geschlossen oben
|
|
}
|
|
// §k (obfuscated), §m (strike), §n (underline) ignorieren wir
|
|
}
|
|
}
|
|
|
|
// Offene Tags schließen
|
|
if ( $open > 0 ) $output .= str_repeat( '</span>', $open );
|
|
if ( $bold ) $output .= '</b>';
|
|
if ( $italic ) $output .= '</i>';
|
|
|
|
return $output;
|
|
}
|