Changeset 6 for scripts/online.php


Ignore:
Timestamp:
Jun 4, 2008, 9:01:09 AM (16 years ago)
Author:
george
Message:

Upraveno: Změna systému sledování procentuálního online stavu. Přidána tabulka servers_online pro uchování stavu serveru za poslední definovanou periodu. Dosavadní systém počítal online stav od počáteční registrace daného serveru což je nevyhovující a zavádějící.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • scripts/online.php

    r5 r6  
    33require ('../includes/config.php');
    44
    5 mysql_connect($db_config['host'], $db_config['user'], $db_config['pass']);
    6 mysql_select_db($db_config['name']);
     5$Database = new mysqli($db_config['host'], $db_config['user'], $db_config['pass']);
     6$Database->select_db($db_config['name']);
    77
    88function CheckPortStatus($Ip, $Port)
     
    1212  {
    1313    fclose($Socket);
    14     return(true);
     14    return(1);
    1515  } else
    1616  {
    17     return(false);
     17    return(0);
    1818  }
    1919}
    2020
     21$TextState = array('Offline', 'Online');
     22
    2123echo("Kontroluji stav serveru...<br>\n");
     24$PlayersCount = 0;
    2225$OnlineCount = 0;
    2326$OfflineCount = 0;
    24 $Servers = mysql_query("SELECT `id`,`ip`,`port`,`name` FROM `servers`");
    25 while($Server = mysql_fetch_array($Servers))
     27$Servers = $Database->query("SELECT `id`,`ip`,`port`,`name` FROM `servers`");
     28while($Server = $Servers->fetch_array())
    2629{
    2730  echo($Server['name'].' - ');
    28   if(CheckPortStatus($Server['ip'], $Server['port']))
    29   {
    30     mysql_query("UPDATE `servers` SET `online_checks` = `online_checks` + 1, `online` = `online` + 1 WHERE `id`=".$Server['id']);
    31     echo('Online');
    32     $OnlineCount++;
    33   } else
    34   {
    35     mysql_query("UPDATE `servers` SET `online_checks` = `online_checks` + 1 WHERE `id` =".$Server['id']);
    36     echo('Offline');
    37     $OfflineCount++;
    38   }
    39   echo("<br>\n");
     31  $OnlineState = CheckPortStatus($Server['ip'], $Server['port']);
     32  $OnlineCount += $OnlineState;
     33  $OfflineCount += 1 - $OnlineState;
     34  // Insert new online state
     35  $Database->query("INSERT INTO `servers_online` (`server` ,`time` ,`state` ,`players_count`) VALUES (".$Server['id'].", NOW() , ".$OnlineState.", ".$PlayersCount.")");
     36
     37  // Delete old records
     38  $Database->query("DELETE FROM `servers_online` WHERE `server` = ".$Server['id']." AND time < DATE_SUB(NOW(), INTERVAL 1 DAY)");
     39 
     40  // Calculate cached percentage online state
     41  $DbResult = $Database->query("SELECT COUNT(*) as `count`, SUM(`state`) as `online` FROM `servers_online` WHERE `server` = ".$Server['id']);
     42  $DbRow = $DbResult->fetch_array();
     43  $Database->query("UPDATE `servers` SET `online` = ".round($DbRow['online'] / $DbRow['count'] * 100, 2)." WHERE `id` = ".$Server['id']);
     44  $DbResult->free();
     45  echo($TextState[$OnlineState]."<br>\n");
     46  flush();
    4047}
    41 mysql_free_result($Servers);
     48$Servers->free();
    4249
    4350echo('Total: '.($OnlineCount + $OfflineCount).', Online: '.$OnlineCount.', Offline: '.$OfflineCount."<br>\n");
Note: See TracChangeset for help on using the changeset viewer.