Changeset 6 for trunk/www/server.php


Ignore:
Timestamp:
Jun 10, 2009, 4:57:51 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Třída pro ladění chyb MaNGOSu.
  • Přidáno: Třída záznamu operací.
  • Opraveno: Zpracování více příkazů najednou v plánovaných úlohách.
  • Přidáno: Kostra třídy pro správu záloh.
  • Přidáno: Funkce pro prvotní import celé databáze mangosu.
  • Upraveno: Inicializace a zrušení databáze.
  • Přidáno: Funce pro stažení a kompilaci emulátoru.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/www/server.php

    r5 r6  
    11<?php
    22
     3function ErrorHandler()
     4{
     5}
     6 
    37class Server extends Module
    48{
     
    1519    $this->Server = $DbResult->fetch_assoc();
    1620    $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE `Id`='.$this->Server['Database']);
    17     $this->Server['Database'] = $DbResult->fetch_assoc();
     21    if($DbResult->num_rows > 0) $this->Server['Database'] = $DbResult->fetch_assoc();
     22      else $this->Server['Database'] = array('Emulator' => 0);
    1823    $DbResult = $this->Database->query('SELECT * FROM `Emulator` WHERE `Id`='.$this->Server['Database']['Emulator']);
    19     $this->Server['Database']['Emulator'] = $DbResult->fetch_assoc();
     24    if($DbResult->num_rows > 0) $this->Server['Database']['Emulator'] = $DbResult->fetch_assoc();
     25      else $this->Server['Database']['Emulator'] = array('ClientVersion' => 0);
    2026    $DbResult = $this->Database->query('SELECT * FROM `ClientVersion` WHERE `Id`='.$this->Server['Database']['Emulator']['ClientVersion']);
    21     $this->Server['Database']['Emulator']['ClientVersion'] = $DbResult->fetch_assoc();
     27    if($DbResult->num_rows > 0) $this->Server['Database']['Emulator']['ClientVersion'] = $DbResult->fetch_assoc();
     28      else $this->Server['Database']['Emulator']['ClientVersion'] = array();
    2229  }
    2330 
     
    2734  }
    2835 
    29   function ImportDatabase()
     36  function CreateDatabase()
    3037  {
    31     $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE `Id`='.$this->Server['Database']);
    32     $Database = $DbResult->fetch_assoc();
    33     $DbResult = $this->Database->query('SELECT `Revision` FROM `Emulator` WHERE `Id`='.$Database['Emulator']);
    34     $Emulator = $DbResult->fetch_assoc();
    35     $this->AddTask('Inicializace databáze', array(
    36       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < emu/mangos/'.$Emulator['Revision'].'/udb/'.$Database['SourceFileName'],
    37       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emu/mangos/'.$Emulator['Revision'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_create_structure.sql',
    38       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emu/mangos/'.$Emulator['Revision'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_script_full.sql',
    39       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < emu/mangos/'.$Emulator['Revision'].'/source/src/bindings/ScriptDev2/sql/mangos_scriptname_full.sql',
    40       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_realmd < emu/mangos/'.$Emulator['Revision'].'/mangos/sql/realmd.sql',
    41       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_characters < emu/mangos/'.$Emulator['Revision'].'/mangos/sql/characters.sql'));
     38    $this->Database->query('CREATE DATABASE `server'.$this->Id.'_realmd`');
     39    $this->Database->query('CREATE DATABASE `server'.$this->Id.'_mangos`');
     40    $this->Database->query('CREATE DATABASE `server'.$this->Id.'_characters`');
     41    $this->Database->query('CREATE DATABASE `server'.$this->Id.'_scriptdev2`');
     42    $this->Database->query('CREATE USER "server'.$this->Id.'"@"localhost" IDENTIFIED BY "server'.$this->Id.'"');
     43    $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_realmd` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
     44    $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_mangos` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
     45    $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_characters` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
     46    $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_scriptdev2` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
     47  }
     48 
     49  function DeleteDatabase()
     50  {
     51    $this->Database->query('DROP DATABASE `server'.$this->Id.'_realmd`');
     52    $this->Database->query('DROP DATABASE `server'.$this->Id.'_mangos`');
     53    $this->Database->query('DROP DATABASE `server'.$this->Id.'_characters`');
     54    $this->Database->query('DROP DATABASE `server'.$this->Id.'_scriptdev2`');
     55    $this->Database->query('DROP USER "server'.$this->Id.'"@"localhost"');
     56  }
     57 
     58  function ImportDatabase($Delete = false)
     59  {
     60    $CommandList = array(
     61      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < database/'.$this->Server['Database']['Id'].'/'.$this->Server['Database']['SourceFileName'],
     62      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_create_structure.sql',
     63      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_script_full.sql',
     64      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/mangos_scriptname_full.sql',
     65      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_realmd < emulator/'.$this->Server['Database']['Emulator']['Id'].'/mangos/sql/realmd.sql',
     66      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_characters < emulator/'.$this->Server['Database']['Emulator']['Id'].'/mangos/sql/characters.sql',
     67    );
     68    if($Delete = true)
     69    {
     70      array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_mangos -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_mangos');
     71      array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_characters -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_characters');
     72      array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_realmd -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_realmd');
     73      array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_scriptdev2 -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_scriptdev2');
     74    }
     75    $this->AddTask('Inicializace databáze', $CommandList);
    4276  }
    4377 
     
    5084  {
    5185    $this->AddTask('Start emulátoru', array(
    52       'screen -A -m -d -S server'.$this->Id.'-realmd "emu/mangos/'.$this->Server['Database']['Emulator']['Revision'].'/bin/mangos-realmd -c server/'.$this->Id.'/etc/realmd.conf"',
    53       'screen -A -m -d -S server'.$this->Id.'-worldd emu/mangos/'.$this->Server['Database']['Emulator']['Revision'].'/bin/mangos-worldd -c server/'.$this->Id.'/etc/mangosd.conf',
     86      'screen -A -m -d -S server'.$this->Id.'-realmd "emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-realmd -c server/'.$this->Id.'/etc/realmd.conf"',
     87      'screen -A -m -d -S server'.$this->Id.'-worldd emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-worldd -c server/'.$this->Id.'/etc/mangosd.conf',
    5488    ));
    5589    return('Požadavek na start serveru zařazen.');
     
    67101  function NetworkPortState($Address, $Port, $Timeout = 1)
    68102  {
     103    set_error_handler('ErrorHandler');
    69104    if($Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
    70105    {
    71106      fclose($Socket);
    72       return(true);
    73     } else return(false);
     107      $Result = true;
     108    } else $Result = false;
     109    restore_error_handler();
     110    return($Result);
    74111  }
    75112 
     
    79116    $State['RealmdPortState'] = $this->NetworkPortState('localhost', $this->RealmdBaseNetworkPort + $this->Server['Id']);
    80117    $State['WorlddPortState'] = $this->NetworkPortState('localhost', $this->WorlddBaseNetworkPort + $this->Server['Id']);
     118    $State['Online'] = $State['RealmdPortState'] and $State['WorlddPortState'];
     119    $DbResult = $this->Database->query('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = "server'.$this->Id.'_characters"');
     120    $DbRow = $DbResult->fetch_row();
     121    if($DbRow[0] > 0)
     122    {
     123      $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_characters.characters AS T WHERE T.online = 1');
     124      $DbRow = $DbResult->fetch_row();
     125      $State['OnlinePlayerCount'] = $DbRow[0];
     126      $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_characters.characters AS T');
     127      $DbRow = $DbResult->fetch_row();
     128      $State['CharacterCount'] = $DbRow[0];
     129      $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_realmd.account AS T');
     130      $DbRow = $DbResult->fetch_row();
     131      $State['AccountCount'] = $DbRow[0];
     132    } else
     133    {
     134      $State['OnlinePlayerCount'] = 0;
     135      $State['CharacterCount'] = 0;
     136      $State['AccountCount'] = 0;
     137    }
    81138    return($State);
     139  }
     140 
     141  function UpdateRealmlist()
     142  {
     143    global $Config;
     144   
     145    $this->Database->query('UPDATE server'.$this->Id.'_realmd.realmlist SET name="'.addslashes($this->Server['Name']).'", address="'.$Config['Web']['Host'].'", port='.($this->WorlddBaseNetworkPort + $this->Id));
     146  }
     147 
     148  function SaveConfiguration()
     149  {
     150    $this->SetupConfigurationFiles();
     151    $this->UpdateRealmlist();
    82152  }
    83153 
    84154  function SetupConfigurationFiles()
    85155  {
     156    $EmulatorEtcDir = '../emulator/'.$this->Server['Database']['Emulator']['Id'].'/etc/';
     157    $ServerEtcDir = '../server/'.$this->Id.'/etc/';
     158    $ServerLogDir = '../server/'.$this->Id.'/log/';
     159    if(!file_exists($ServerEtcDir)) mkdir($ServerEtcDir, 0777, true);   
     160    if(!file_exists($ServerLogDir)) mkdir($ServerLogDir, 0777, true);   
     161   
    86162    // mangosd.conf
    87163    $Config = new MangosConfigurationFile($this->Database);
    88     $Config->Load('../emu/mangos/'.$this->Server['Database']['Emulator']['Revision'].'/etc/mangosd.conf');
     164    $Config->Load($EmulatorEtcDir.'mangosd.conf');
    89165    $Config->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
    90166    $Config->ParameterList['WorldDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_mangos';
     
    94170    $Config->ParameterList['Motd'] = $this->Server['Motd'];
    95171    $Config->ParameterList['DataDir'] = 'wowclient/'.$this->Server['Database']['Emulator']['ClientVersion']['Version'];
    96     $Config->Save('../server/'.$this->Id.'/etc/mangosd.conf');
     172    $Config->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
     173    $ServerTypes = array(0 => 0, 1 => 1, 2 => 6, 3 => 8);
     174    $Config->ParameterList['GameType'] = $ServerTypes[$this->Server['Type']];
     175    $Config->Save($ServerEtcDir.'mangosd.conf');
    97176   
    98177    // realmd.conf
    99178    $Config = new MangosConfigurationFile($this->Database);
    100     $Config->Load('../emu/mangos/'.$this->Server['Database']['Emulator']['Revision'].'/etc/realmd.conf');
     179    $Config->Load($EmulatorEtcDir.'realmd.conf');
    101180    $Config->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
    102181    $Config->ParameterList['RealmServerPort'] = $this->RealmdBaseNetworkPort + $this->Id;
    103     $Config->Save('../server/'.$this->Id.'/etc/realmd.conf');
     182    $Config->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
     183    $Config->Save($ServerEtcDir.'realmd.conf');
    104184   
    105185    // scriptdev2.conf
    106186    $Config = new MangosConfigurationFile($this->Database);
    107     $Config->Load('../emu/mangos/'.$this->Server['Database']['Emulator']['Revision'].'/etc/scriptdev2.conf');
     187    $Config->Load($EmulatorEtcDir.'scriptdev2.conf');
    108188    $Config->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_scriptdev2';
    109     $Config->Save('../server/'.$this->Id.'/etc/scriptdev2.conf'); 
     189    $Config->Save($ServerEtcDir.'scriptdev2.conf'); 
    110190  }
    111191}
Note: See TracChangeset for help on using the changeset viewer.