<?php

function RealmList()
{
  global $System, $html;
  
if(array_key_exists('Server', $_GET))
{
  $DbResult = $System->Database->query('SELECT Logon.Id, Host.Address, Logon.Port FROM Logon JOIN Host ON Host.Id=Logon.Host WHERE Logon.Id='.$_GET['Server'].' AND   Logon.Enabled=1');
  if($DbResult->num_rows > 0)
  {
    $Realm = $DbResult->fetch_assoc();
    echo('set realmlist '.$Realm['Address']);
    if($Realm['Port'] != 3724) echo(':'.$Realm['Port']);
    echo('</div>');
  } else echo('Neznámé Id serveru');
} else
{
  $Output = '<h2 class="PageTitle">Seznam světů</h2>';

  $DbResult = $System->Database->query('SELECT Logon.ClientVersion, Logon.Name, Logon.Id, Host.Address, Logon.Port FROM Logon JOIN Host ON Host.Id=Logon.Host WHERE Logon.Enabled=1');
  while($DbRow = $DbResult->fetch_assoc())
  {
    $Server = new Server($System, $DbRow['Id']);
    $Output .= '<div style="text-align: center">'.$DbRow['Name'].' '.$DbRow['ClientVersion'].' - realmlist: <a href="'.$html->Link('/svety/?Server='.$DbRow['Id']).'">'.$DbRow['Address'];
    if($DbRow['Port'] != 3724) $Output .= ':'.$DbRow['Port'];
    $Output .= '</a></div>';
    $Output .= '<table class="BaseTable">'.
    '<tr>'.
    '<th>Název</th>'.
    '<th>Násobek XP</th>'.
    '<th>Zaměření</th>'.
    '<th>Jazyk</th>'.
    '<th>Stav</th>'.
    '<th>Hráči online</th>'.
    '</tr>';
  
    $DbResult2 = $System->Database->query('SELECT *, '.$System->SQLURLName('Name').' AS URLName FROM Realm WHERE Logon='.$Server->Id.' AND Enabled=1 ORDER BY Name');
    while($DbRow = $DbResult2->fetch_assoc())
    {
      $Realm = new Realm($System, $DbRow['Id']);
      $Output .= '<tr><td><a href="'.$html->Link('/svety/'.$DbRow['URLName'].'/').'">'.$Realm->Data['Name'].'</a></td><td>'.$Realm->Data['Rate'].'</td><td>'.$Realm->Data['Type'].'</td><td>'.$Realm->Data['Locale'].'</td><td>'.$Server->OnlineStateImage($Realm->Data['Online']).'</td><td><a href="'.$html->Link('/svety/'.$DbRow['URLName'].'/online-hraci/').'">'.$Realm->OnlineCharactersCount().'</a></td></tr>';
      $Output .= '<tr><td colspan="6">'.$Realm->Data['Description'].'</td></tr>';
      //if(array_key_exists($Index + 1, $Config['Mangos']['RealmList'])) 
      $Output .= '<tr><td colspan="6">&nbsp;</td></tr>';
    }
    $Output .= '</table><br/>';
  }
  echo($Output);
}
}

function RealmInfo()
{
  global $System;
  
  $DbResult = $System->Database->query('SELECT * FROM Realm WHERE Id='.$_COOKIE['RealmIndex']);
  $Realm = $DbResult->fetch_assoc();
  echo('<h2 align="center">'.$Realm['Name'].'</h2>');
  echo($Realm['Information']);
}

if(count($QueryItems) > 1)
{
  $RealmName = $QueryItems[1];
  $DbResult = $System->Database->query('SELECT Id FROM Realm WHERE '.$System->SQLURLName('Name').'="'.$RealmName.'"');
  $DbRow = $DbResult->fetch_assoc();
  $_COOKIE['RealmIndex'] = $DbRow['Id'];
  
  if(count($QueryItems) > 2)
  {
    if($QueryItems[2] == 'online-hraci') include('online-hraci.php');
    else if($QueryItems[2] == 'prikazy') include('prikazy.php');
    else if($QueryItems[2] == 'akce') include('akce.php');
    else if($QueryItems[2] == 'arena') include('arena.php');
    else if($QueryItems[2] == 'nej-hraci') include('nej-hraci.php');
    else if($QueryItems[2] == 'mapa') include('mapa.php');
    else if($QueryItems[2] == 'spolek') include('spolek.php');
    else if($QueryItems[2] == 'spolky') include('spolky.php');
    else RealmInfo();
  } else RealmInfo();
} else RealmList();

?>
