Ignore:
Timestamp:
Aug 4, 2009, 5:32:06 PM (15 years ago)
Author:
george
Message:
  • Opraveno: Dále posunuta funkčnost podpory více světů na server.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/www/model/realm.php

    r54 r57  
    1414    $DbResult = $this->Database->query('SELECT * FROM `Realm` WHERE `Id`='.$Id);
    1515    if($DbResult->num_rows > 0)
     16    {
    1617      $this->Data = $DbResult->fetch_assoc();
     18      $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE `Id`='.$this->Data['Database']);
     19      if($DbResult->num_rows > 0) $this->Data['Database'] = $DbResult->fetch_assoc();
     20        else $this->Data['Database'] = array('Emulator' => 0);
     21      $DbResult = $this->Database->query('SELECT * FROM `Emulator` WHERE `Id`='.$this->Data['Database']['Emulator']);
     22      if($DbResult->num_rows > 0) $this->Data['Database']['Emulator'] = $DbResult->fetch_assoc();
     23        else $this->Data['Database']['Emulator'] = array('Client' => 0);
     24      $DbResult = $this->Database->query('SELECT * FROM `Client` WHERE `Id`='.$this->Data['Database']['Emulator']['Client']);
     25      if($DbResult->num_rows > 0) $this->Data['Database']['Emulator']['Client'] = $DbResult->fetch_assoc();
     26        else $this->Data['Database']['Emulator']['Client'] = array();
     27    }
    1728  }
    1829
    1930  function GetState()
    2031  {
     32    global $System;
     33   
    2134    $State = array();
    22     $State['WorlddPortState'] = $this->NetworkPortState('localhost', $this->Server['NetworkPortWorldd']);
     35    $State['WorlddPortState'] = $System->NetworkPortState('localhost', $this->Data['NetworkPortWorldd']);
    2336    $State['Online'] = $State['WorlddPortState'];
    2437    $DbResult = $this->Database->query('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = "realm'.$this->Id.'_characters"');
     
    3952    return($State);
    4053  } 
     54 
     55  function CreateDatabase()
     56  {
     57    $this->Database->query('CREATE DATABASE `realm'.$this->Id.'_mangos`');
     58    $this->Database->query('CREATE DATABASE `realm'.$this->Id.'_characters`');
     59    $this->Database->query('CREATE DATABASE `realm'.$this->Id.'_scriptdev2`');
     60    $this->Database->query('GRANT ALL PRIVILEGES ON `realm'.$this->Id.'\_mangos` . * TO "server'.$this->Data['Server'].'"@"localhost" WITH GRANT OPTION');
     61    $this->Database->query('GRANT ALL PRIVILEGES ON `realm'.$this->Id.'\_characters` . * TO "server'.$this->Data['Server'].'"@"localhost" WITH GRANT OPTION');
     62    $this->Database->query('GRANT ALL PRIVILEGES ON `realm'.$this->Id.'\_scriptdev2` . * TO "server'.$this->Data['Server'].'"@"localhost" WITH GRANT OPTION');
     63  }
     64 
     65  function DeleteDatabase()
     66  {
     67    $this->Database->query('DROP DATABASE `realm'.$this->Id.'_mangos`');
     68    $this->Database->query('DROP DATABASE `realm'.$this->Id.'_characters`');
     69    $this->Database->query('DROP DATABASE `realm'.$this->Id.'_scriptdev2`');
     70  }
     71
     72  function ImportDatabase($Delete = false)
     73  {
     74    $this->Lock();
     75    $CommandList = array(
     76      'php www/shell.php RealmLock '.$this->Id,
     77    );
     78    if($Delete == true)
     79    {
     80      $CommandList = array_merge($CommandList, array(
     81        'mysql --silent --skip-column-names -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_mangos -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_mangos',
     82        'mysql --silent --skip-column-names -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_characters -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_characters',
     83      'mysql --silent --skip-column-names -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_scriptdev2 -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Data['Server'].' -pserver'.$this->Data['Server'].' realm'.$this->Id.'_scriptdev2',
     84      ));
     85    }
     86    // Lookup nearest database with full import
     87    $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE (`Emulator` <> 0) AND (`Revision` <= '.$this->Server['Database']['Revision'].') AND (`SourceFileName` <> "") ORDER BY `Revision` DESC');
     88    $Database = $DbResult->fetch_assoc();
     89   
     90    $CommandList = array_merge($CommandList, array(
     91      'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_mangos < database/'.$Database['Id'].'/'.$Database['SourceFileName'],
     92      'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_scriptdev2 < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_create_structure.sql',
     93      'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_scriptdev2 < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_script_full.sql',
     94      'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_mangos < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/mangos_scriptname_full.sql',
     95      'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_characters < emulator/'.$Database['Emulator']['Id'].'/share/mangos/sql/characters.sql',
     96      'php www/shell.php RealmDatabaseChange '.$this->Id.' '.$Database['Id'],
     97      'php www/shell.php RealmUnLock '.$this->Id,
     98    ));   
     99    $this->Task->Add('Inicializace databáze', $CommandList);
     100   
     101    if($Database['Id'] != $this->Server['Database']['Id'])
     102    {
     103      $NewDatabaseId = $this->Server['Database']['Id'];
     104      $this->Server['Database']['Id'] = $Database['Id'];
     105      $this->Update($NewDatabaseId, false, false);
     106    }
     107    return('Úloha načtení nové databáze zařazena do fronty.');
     108  }
     109
     110  function Start()
     111  {
     112    $this->Lock();
     113    $this->SaveConfiguration();
     114    $this->Task->Add('Start světa', array(
     115      'php www/shell.php RealmLock '.$this->Id,
     116      'realm/'.$this->Id.'/bin/start.sh',
     117      'php www/shell.php RealmUnLock '.$this->Id,
     118    ));
     119    return('Požadavek na start světa zařazen.');
     120  }
     121 
     122  function Stop()
     123  {
     124    $this->Lock();
     125    $this->Task->Add('Zastavení světa', array(
     126      'php www/shell.php RealmLock '.$this->Id,
     127      'realm/'.$this->Id.'/bin/stop.sh',
     128      'php www/shell.php RealmUnLock '.$this->Id,
     129    ));
     130    return('Požadavek na zastavení světa zařazen.');
     131  }
     132
     133  function UpdateRealmlist()
     134  {
     135    $Server = new Server($this->Database, $this->Data['Server']);
     136    $Server->UpdateRealmList();
     137  }
     138 
     139  function SaveConfiguration()
     140  {
     141    $this->SetupConfigurationFiles();
     142    $this->UpdateRealmlist();
     143    $this->UpdateScripts();
     144  }
     145
     146  function UpdateScripts()
     147  {
     148    global $Config;
     149   
     150    $RealmBinDir = '../realm/'.$this->Id.'/bin/';
     151    if(!file_exists($RealmBinDir)) mkdir($RealmBinDir, 0777, true);   
     152   
     153    // GDB script
     154    $ScriptFileName = '../realm/'.$this->Id.'/bin/mangos.gdb';
     155    $Content = array
     156    (
     157      'run -c realm/'.$this->Id.'/etc/mangosd.conf',
     158      'info thread',
     159    );
     160    for($I = 1; $I < $Config['MangosWorlddThreadCountMax']; $I++)
     161      $Content[] = 'thread apply '.$I.' bt full';
     162    file_put_contents($ScriptFileName, implode("\n", $Content));
     163    chmod($ScriptFileName, 0666);   
     164
     165    // Start script
     166    $ScriptFileName = '../realm/'.$this->Id.'/bin/start.sh';
     167    $Content = array
     168    (
     169      '#!/bin/sh',
     170      'if [ -z `ps -ef | grep \'SCREEN -A -m -d -S realm'.$this->Id.'-worldd\' | grep -v grep | awk \'{print $2}\'` ]',
     171      'then',
     172      'screen -A -m -d -S realm'.$this->Id.'-worldd realm/'.$this->Id.'/bin/worldd_restarter.sh',
     173      'fi',
     174    );
     175    file_put_contents($ScriptFileName, implode("\n", $Content));
     176    chmod($ScriptFileName, 0777);   
     177
     178    $ScriptFileName = '../realm/'.$this->Id.'/bin/worldd_restarter.sh';
     179    $Content = array
     180    (
     181      '#!/bin/sh',
     182      'while [ 1=1 ] ; do',
     183      'gdb emulator/'.$this->Data['Database']['Emulator']['Id'].'/bin/mangos-worldd -x realm/'.$this->Id.'/bin/mangos.gdb --batch >>realm/'.$this->Id.'/log/mangos-worldd.log 2>>realm/'.$this->Id.'/log/mangos-worldd.err',
     184      'cd www',
     185      'php shell.php ServerProcessLog '.$this->Id.' >>realm/'.$this->Id.'/log/mangos_debug.log 2>>realm/'.$this->Id.'/log/mangos_debug.err',
     186      'cd ..',
     187      'sleep 3',
     188      'done',
     189    );
     190    file_put_contents($ScriptFileName, implode("\n", $Content));
     191    chmod($ScriptFileName, 0777);   
     192
     193    // Stop script
     194    $ScriptFileName = '../realm/'.$this->Id.'/bin/stop.sh';
     195    $Content = array
     196    (
     197      '#!/bin/sh',
     198      'ps -ef | grep \'SCREEN -A -m -d -S realm'.$this->Id.'-worldd\' | grep -v grep | awk \'{print $2}\' | xargs -i kill {}',
     199    );
     200    file_put_contents($ScriptFileName, implode("\n", $Content));
     201    chmod($ScriptFileName, 0777);   
     202  }
     203 
     204  function SetupConfigurationFiles()
     205  {
     206    global $Config;
     207   
     208    $EmulatorEtcDir = '../emulator/'.$this->Data['Database']['Emulator']['Id'].'/etc/';
     209    $RealmEtcDir = '../realm/'.$this->Id.'/etc/';
     210    $RealmLogDir = '../realm/'.$this->Id.'/log/';
     211    if(!file_exists($RealmEtcDir)) mkdir($RealmEtcDir, 0777, true);   
     212    if(!file_exists($RealmLogDir)) mkdir($RealmLogDir, 0777, true);   
     213   
     214    // mangosd.conf
     215    $EmulatorConfig = new MangosConfigurationFile($this->Database);
     216    $EmulatorConfig->Load($EmulatorEtcDir.'mangosd.conf.dist');
     217    $EmulatorConfig->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
     218    $EmulatorConfig->ParameterList['WorldDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_mangos';
     219    $EmulatorConfig->ParameterList['CharacterDatabaseInfo'] = 'localhost;3306;server'.$this->Data['Server'].';server'.$this->Data['Server'].';realm'.$this->Id.'_characters';
     220    $EmulatorConfig->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Data['Server'].';server'.$this->Data['Server'].';realm'.$this->Id.'_scriptdev2';
     221    $EmulatorConfig->ParameterList['WorldServerPort'] = $this->Data['NetworkPortWorldd'];
     222    $EmulatorConfig->ParameterList['Motd'] = $this->Data['Motd'];
     223    print_r($this->Data);
     224    $EmulatorConfig->ParameterList['DataDir'] = 'wowclient/'.$this->Data['Database']['Emulator']['Client']['Version'];
     225    $EmulatorConfig->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
     226    $EmulatorConfig->ParameterList['LogLevel'] = 1;
     227    $EmulatorConfig->ParameterList['PlayerLimit'] = $Config['MaxOnlinePlayerCountPerServer'];
     228    $EmulatorConfig->ParameterList['PlayerSaveInterval'] = 300000;   
     229    $EmulatorConfig->ParameterList['LogSQL'] = 0;
     230    $ServerTypes = array(0 => 0, 1 => 1, 2 => 6, 3 => 8);
     231    $EmulatorConfig->ParameterList['GameType'] = $ServerTypes[$this->Data['Type']];
     232    $EmulatorConfig->Save($RealmEtcDir.'mangosd.conf');
     233     
     234    // scriptdev2.conf
     235    $EmulatorConfig = new MangosConfigurationFile($this->Database);
     236    $EmulatorConfig->Load($EmulatorEtcDir.'scriptdev2.conf');
     237    $EmulatorConfig->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Data['Server'].';server'.$this->Data['Server'].';realm'.$this->Id.'_scriptdev2';
     238    $EmulatorConfig->Save($RealmEtcDir.'scriptdev2.conf'); 
     239  }
     240
     241  function Update($DatabaseId, $DoBackup = true, $DoStop = true)
     242  {
     243    $this->Lock();
     244    $Output = '';
     245   
     246    // Stop server before update
     247    if($DoStop)
     248    {
     249      $Output .= $this->Stop();
     250    }
     251   
     252    // Backup current
     253    if($DoBackup)
     254    {
     255      $Backup = new Backup($this->Database, 0);
     256      $Output .= '<br />'.$Backup->Create($this->Id);
     257    }
     258   
     259    // Do update
     260    $Commands = array(
     261     'php www/shell.php RealmLock '.$this->Id,
     262    );
     263    $DbResult = $this->Database->query('SELECT `Revision` FROM `Database` WHERE `Id` = '.$this->Server['Database']['Id']);
     264    $DbRow = $DbResult->fetch_assoc();
     265    $DatabaseRevisionStart = $DbRow['Revision'];
     266    $DbResult = $this->Database->query('SELECT `Revision` FROM `Database` WHERE `Id` = '.$DatabaseId);
     267    $DbRow = $DbResult->fetch_assoc();
     268    $DatabaseRevisionEnd = $DbRow['Revision'];
     269    $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE (`Revision` > '.$DatabaseRevisionStart.') AND (`Revision` <= '.$DatabaseRevisionEnd.') ORDER BY `Revision`');
     270    while($DbRow = $DbResult->fetch_assoc())
     271    {
     272      $Updates = explode("\n", $DbRow['Update']);
     273      foreach($Updates as $Update)
     274      if($Update != '')
     275      {
     276        $Parts = explode('|', $Update);
     277        $Command = 'mysql --user=server'.$this->Data['Server'].' --password=server'.$this->Data['Server'].' realm'.$this->Id.'_'.$Parts[0].' < database/'.$DbRow['Id'].'/'.$Parts[1];
     278        $Commands[] = $Command;
     279      }     
     280    }
     281    $Commands = array_merge($Commands, array(
     282      'php www/shell.php RealmDatabaseChange '.$this->Id.' '.$DatabaseId,
     283      'php www/shell.php RealmUnLock '.$this->Id,
     284    ));
     285
     286    $this->Task->Add('Aktualizace databáze', $Commands);
     287    $Output .= '<br />Úloha aktualizace serveru byla přidána do fronty.';
     288    return($Output);
     289  }
     290 
     291  function Lock()
     292  {
     293    $this->Database->update('Realm', 'Id='.$this->Id, array('Lock' => 1));
     294  }
     295 
     296  function UnLock()
     297  {
     298    $this->Database->update('Realm', 'Id='.$this->Id, array('Lock' => 0));
     299  }
     300 
     301  function ChangeDatabaseId($Id)
     302  {
     303    $this->Database->update('Realm', 'Id='.$this->Id, array('Database' => $Id));
     304    $this->SaveConfiguration();
     305  }
     306 
     307  function GetUsedMemory()
     308  {
     309    $Output = array();
     310    if(isset($this->Server['Database']['Emulator']['Id']))
     311      exec('ps -aux|grep "emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-worldd -c realm/'.$this->Id.'/etc/mangosd.conf"| grep -v grep', $Output);
     312    if(count($Output) > 0)
     313    {
     314      while(strpos($Output[0], '  ') > 0) $Output[0] = str_replace('  ', ' ', $Output[0]);
     315      $Parts = explode(' ', $Output[0]);
     316      return($Parts[4]);
     317    } else return(0);
     318  }
     319 
     320  function ProcessLog()
     321  {
     322    $File = fopen('../realm/'.$this->Id.'/log/mangos-worldd.log', 'r');
     323    while(true)
     324    {
     325      $Reders = array($File);
     326      if(stream_select($Reders, $Writers=null, $Except=null, 0, 15) < 1)
     327      {
     328        continue;
     329      } else
     330      {
     331        // read data from the fifo
     332        $Data = fread($File, 1024);
     333        echo($Data);
     334        $this->Database->insert('RealmLog', array('Time' => 'NOW()', 'Realm' => $this->Id, 'Text' => $Data));
     335      }         
     336      sleep(1);
     337    }
     338    fclose($File);
     339  }
     340 
     341  function GetUser()
     342  {
     343    $DbResult = $this->Database->select('Server', 'User', 'Id='.$this->Data['Server']);
     344    $DbRow = $DbResult->fetch_assoc();
     345    return($DbRow['User']);
     346  }
    41347}
    42348
Note: See TracChangeset for help on using the changeset viewer.