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/server.php

    r54 r57  
    11<?php
    22
    3 function ErrorHandler()
    4 {
    5 }
    6  
    73class Server extends Module
    84{
     
    3531  {
    3632    $this->Database->query('CREATE DATABASE `server'.$this->Id.'_realmd`');
    37     $this->Database->query('CREATE DATABASE `server'.$this->Id.'_mangos`');
    38     $this->Database->query('CREATE DATABASE `server'.$this->Id.'_characters`');
    39     $this->Database->query('CREATE DATABASE `server'.$this->Id.'_scriptdev2`');
    4033    $this->Database->query('CREATE USER "server'.$this->Id.'"@"localhost" IDENTIFIED BY "server'.$this->Id.'"');
    4134    $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_realmd` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
    42     $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_mangos` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
    43     $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_characters` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
    44     $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_scriptdev2` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
    4535  }
    4636 
     
    4838  {
    4939    $this->Database->query('DROP DATABASE `server'.$this->Id.'_realmd`');
    50     $this->Database->query('DROP DATABASE `server'.$this->Id.'_mangos`');
    51     $this->Database->query('DROP DATABASE `server'.$this->Id.'_characters`');
    52     $this->Database->query('DROP DATABASE `server'.$this->Id.'_scriptdev2`');
    5340    $this->Database->query('DROP USER "server'.$this->Id.'"@"localhost"');
    5441  }
     
    6350    {
    6451      $CommandList = array_merge($CommandList, array(
    65         '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',
    66         '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',
    6752      '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',
    68       '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',
    6953      ));
    7054    }
     
    7458   
    7559    $CommandList = array_merge($CommandList, array(
    76       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < database/'.$Database['Id'].'/'.$Database['SourceFileName'],
    77       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_create_structure.sql',
    78       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_script_full.sql',
    79       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < emulator/'.$Database['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/mangos_scriptname_full.sql',
    8060      'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_realmd < emulator/'.$Database['Emulator']['Id'].'/share/mangos/sql/realmd.sql',
    81       'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_characters < emulator/'.$Database['Emulator']['Id'].'/share/mangos/sql/characters.sql',
    8261      'php www/shell.php ServerDatabaseChange '.$this->Id.' '.$Database['Id'],
    8362      'php www/shell.php ServerUnLock '.$this->Id,
     
    11695    return('Požadavek na zastavení serveru zařazen.');
    11796  }
    118  
    119   function NetworkPortState($Address, $Port, $Timeout = 1)
    120   {
    121     set_error_handler('ErrorHandler');
    122     if($Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
    123     {
    124       fclose($Socket);
    125       $Result = true;
    126     } else $Result = false;
    127     restore_error_handler();
    128     return($Result);
    129   }
    130  
     97   
    13198  function GetState()
    13299  {
     100    global $System;
     101   
    133102    $State = array();
    134     $State['RealmdPortState'] = $this->NetworkPortState('localhost', $this->Server['NetworkPortRealmd']);
     103    $State['RealmdPortState'] = $System->NetworkPortState('localhost', $this->Server['NetworkPortRealmd']);
    135104    $State['Online'] = $State['RealmdPortState'];
    136105    $DbResult = $this->Database->query('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = "server'.$this->Id.'_realmd"');
     
    160129    global $Config;
    161130   
    162     $this->Database->query('UPDATE server'.$this->Id.'_realmd.realmlist SET name="'.addslashes($this->Server['Name']).'", address="'.$Config['Web']['Host'].'", port='.($this->Server['NetworkPortWorldd']));
     131    $this->Database->query('TRUNCATE TABLE server'.$this->Id.'_realmd.realmlist');
     132    $DbResult = $this->Database->select('Realm', '*', 'Server = '.$this->Id);
     133    while($Realm = $DbResult->fetch_assoc())
     134    {
     135      $this->Database->insert('server'.$this->Id.'_realmd`.`realmlist', array('name' => addslashes($Realm['Name']), 'address' => $Config['Web']['Host'], 'port' => $Realm['NetworkPortWorldd']));
     136    }
    163137  }
    164138 
     
    175149   
    176150    $ServerBinDir = '../server/'.$this->Id.'/bin/';
    177     if(!file_exists($ServerBinDir)) mkdir($ServerBinDir, 0777, true);   
    178    
    179     // GDB script
    180     $ScriptFileName = '../server/'.$this->Id.'/bin/mangos.gdb';
    181     $Content = array
    182     (
    183       'run -c server/'.$this->Id.'/etc/mangosd.conf',
    184       'info thread',
    185     );
    186     for($I = 1; $I < $Config['MangosWorlddThreadCountMax']; $I++)
    187       $Content[] = 'thread apply '.$I.' bt full';
    188     file_put_contents($ScriptFileName, implode("\n", $Content));
    189     chmod($ScriptFileName, 0666);   
     151    if(!file_exists($ServerBinDir)) mkdir($ServerBinDir, 0777, true);       
    190152
    191153    // Start script
     
    198160      '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',
    199161      'fi',
    200       'if [ -z `ps -ef | grep \'SCREEN -A -m -d -S server'.$this->Id.'-worldd\' | grep -v grep | awk \'{print $2}\'` ]',
    201       'then',
    202       'screen -A -m -d -S server'.$this->Id.'-worldd server/'.$this->Id.'/bin/worldd_restarter.sh',
    203       'fi',
    204     );
    205     file_put_contents($ScriptFileName, implode("\n", $Content));
    206     chmod($ScriptFileName, 0777);   
    207 
    208     $ScriptFileName = '../server/'.$this->Id.'/bin/worldd_restarter.sh';
    209     $Content = array
    210     (
    211       '#!/bin/sh',
    212       'while [ 1=1 ] ; do',
    213       'gdb emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-worldd -x server/'.$this->Id.'/bin/mangos.gdb --batch >>server/'.$this->Id.'/log/mangos-worldd.log 2>>server/'.$this->Id.'/log/mangos-worldd.err',
    214       'cd www',
    215       'php shell.php ServerProcessLog '.$this->Id.' >>server/'.$this->Id.'/log/mangos_debug.log 2>>server/'.$this->Id.'/log/mangos_debug.err',
    216       'cd ..',
    217       'sleep 3',
    218       'done',
    219162    );
    220163    file_put_contents($ScriptFileName, implode("\n", $Content));
     
    227170      '#!/bin/sh',
    228171      'ps -ef | grep \'SCREEN -A -m -d -S server'.$this->Id.'-realmd\' | grep -v grep | awk \'{print $2}\' | xargs -i kill {}',
    229       'ps -ef | grep \'SCREEN -A -m -d -S server'.$this->Id.'-worldd\' | grep -v grep | awk \'{print $2}\' | xargs -i kill {}',
    230172    );
    231173    file_put_contents($ScriptFileName, implode("\n", $Content));
     
    243185    if(!file_exists($ServerLogDir)) mkdir($ServerLogDir, 0777, true);   
    244186   
    245     // mangosd.conf
    246     $EmulatorConfig = new MangosConfigurationFile($this->Database);
    247     $EmulatorConfig->Load($EmulatorEtcDir.'mangosd.conf.dist');
    248     $EmulatorConfig->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
    249     $EmulatorConfig->ParameterList['WorldDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_mangos';
    250     $EmulatorConfig->ParameterList['CharacterDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_characters';
    251     $EmulatorConfig->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_scriptdev2';
    252     $EmulatorConfig->ParameterList['WorldServerPort'] = $this->Server['NetworkPortWorldd'];
    253     $EmulatorConfig->ParameterList['Motd'] = $this->Server['Motd'];
    254     $EmulatorConfig->ParameterList['DataDir'] = 'wowclient/'.$this->Server['Database']['Emulator']['Client']['Version'];
    255     $EmulatorConfig->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
    256     $EmulatorConfig->ParameterList['LogLevel'] = 1;
    257     $EmulatorConfig->ParameterList['PlayerLimit'] = $Config['MaxOnlinePlayerCountPerServer'];
    258     $EmulatorConfig->ParameterList['PlayerSaveInterval'] = 300000;   
    259     $EmulatorConfig->ParameterList['LogSQL'] = 0;
    260     $ServerTypes = array(0 => 0, 1 => 1, 2 => 6, 3 => 8);
    261     $EmulatorConfig->ParameterList['GameType'] = $ServerTypes[$this->Server['Type']];
    262     $EmulatorConfig->Save($ServerEtcDir.'mangosd.conf');
    263    
    264187    // realmd.conf
    265188    $EmulatorConfig = new MangosConfigurationFile($this->Database);
     
    269192    $EmulatorConfig->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
    270193    $EmulatorConfig->Save($ServerEtcDir.'realmd.conf');
    271    
    272     // scriptdev2.conf
    273     $EmulatorConfig = new MangosConfigurationFile($this->Database);
    274     $EmulatorConfig->Load($EmulatorEtcDir.'scriptdev2.conf');
    275     $EmulatorConfig->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_scriptdev2';
    276     $EmulatorConfig->Save($ServerEtcDir.'scriptdev2.conf'); 
    277194  }
    278195 
     
    379296  function GetUsedMemory()
    380297  {
    381     $Output = array();
    382     if(isset($this->Server['Database']['Emulator']['Id']))
    383       exec('ps -aux|grep "emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-worldd -c server/'.$this->Id.'/etc/mangosd.conf"| grep -v grep', $Output);
    384     if(count($Output) > 0)
    385     {
    386       while(strpos($Output[0], '  ') > 0) $Output[0] = str_replace('  ', ' ', $Output[0]);
    387       $Parts = explode(' ', $Output[0]);
    388       return($Parts[4]);
    389     } else return(0);
     298    $UsedMemory = 0;
     299    $DbResult = $this->Database->select('Realm', 'Id', 'Server='.$this->Id);
     300    while($DbRow = $DbResult->fetch_assoc())
     301    {
     302      $Realm = new Realm($this->Database, $DbRow['Id']);
     303      $UsedMemory += $Realm->GetUsedMemory();
     304    }
     305    return($UsedMemory);
    390306  } 
    391307
     
    400316    }
    401317    return(implode("\n", $Output));
    402   }
    403  
    404   function ProcessLog()
    405   {
    406     $File = fopen('../server/'.$this->Id.'/log/mangos-worldd.log', 'r');
    407     while(true)
    408     {
    409       $Reders = array($File);
    410       if(stream_select($Reders, $Writers=null, $Except=null, 0, 15) < 1)
    411       {
    412         continue;
    413       } else
    414       {
    415         // read data from the fifo
    416         $Data = fread($File, 1024);
    417         echo($Data);
    418         $this->Database->insert('ServerLog', array('Time' => 'NOW()', 'Server' => $this->Id, 'Text' => $Data));
    419       }         
    420       sleep(1);
    421     }
    422     fclose($File);
    423   }
     318  }
     319 
     320  function RealmCount()
     321  {
     322    $DbResult = $this->Database->query('SELECT COUNT(*) FROM Realm WHERE Server='.$this->Id);
     323    $DbRow = $DbResult->fetch_row();
     324    return($DbRow[0]);
     325  } 
    424326}
    425327
Note: See TracChangeset for help on using the changeset viewer.