Мод.Автоматическое обновление статистики.
для XGP Сборок.
Создать файл CalculateStats.php в includes/functions и в него запихнуть это
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">Скрытая информация: "CalculateStats.php" <input type="button" value="Показать" style="width:60px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Скрыть'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Показать'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
<?php
##############################################################################
# * #
# * XG PROYECT #
# * #
# * @copyright Copyright © 2008 - 2009 By lucky from Xtreme-gameZ.com.ar #
# * #
# * #
# * This program is free software: you can redistribute it and/or modify #
# * it under the terms of the GNU General Public License as published by #
# * the Free Software Foundation, either version 3 of the License, or #
# * (at your option) any later version. #
# * #
# * This program is distributed in the hope that it will be useful, #
# * but WITHOUT ANY WARRANTY; without even the implied warranty of #
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# * GNU General Public License for more details. #
# * #
##############################################################################
/**
* Ejecuta una consulta en la base de datos
*
* @param string $query Consulta
* @param string $fetch Usar mysql_fetch_assoc y devolver resultado
*
* @return mixed Resultado de la consulta o FALSE en caso de error
*/
function sql_query($query, $fetch = false)
{
// Escapar consulta
$query = preg_replace('/\#(.+?)\#/e', '\'{{table}}\\1\'', $query);
// Ejecutar consulta
$result = @doquery($query, '', $fetch);
return $result;
}
/**
* Intenta obtener un bloqueo en la base de datos
*
* @param string $name Nombre
* @param boolean $timeout Tiempo mбximo de espera hasta para obtener el bloqueo
*
* @return boolean true si se ha obtenido el bloqueo o false caso contrario
*/
function sql_get_lock($name, $timeout = 0)
{
// Obtener bloqueo
$result = sql_query("SELECT COALESCE(GET_LOCK('".$name."', $timeout), 0)");
if ((boolean)@mysql_result($result, 0) == true)
{
return true;
}
return false;
}
/**
* Libera un bloqueo en la base de datos
*
* @param string $name Nombre
*/
function sql_release_lock($name)
{
if (!empty($name))
{
sql_query("DO RELEASE_LOCK('".$name."')");
}
}
/**
* Actualizaciуn de las estadнsticas
*/
function game_update_stats($ignore = false)
{
global $game_config, $resource, $pricelist, $reslist;
// Initial Time
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
//Initial Memory
$__result['initial_memory']= array(round(memory_get_usage() / 1024,1),round(memory_get_usage(1) / 1024,1));
// Adaptacion a XGP - instalacion automatica
if (!isset($game_config['stats_update_interval'])) {
// Insertar
sql_query("INSERT INTO #config# SET config_name = 'stats_update_interval', config_value = '3600'");
sql_query("INSERT INTO #config# SET config_name = 'stats_last_update', config_value = '0'");
sql_query("INSERT INTO #config# SET config_name = 'game_optimize_interval', config_value = '86400'");
sql_query("INSERT INTO #config# SET config_name = 'game_last_optimize', config_value = '0'");
sql_query("INSERT INTO #config# SET config_name = 'game_cleanup_interval', config_value = '86400'");
sql_query("INSERT INTO #config# SET config_name = 'game_last_cleanup', config_value = '".time()."'");
// Emular
$game_config['stats_update_interval'] = 3600;
$game_config['stats_last_update'] = 0;
$game_config['game_optimize_interval'] = 86400;
$game_config['game_last_optimize'] = 0;
$game_config['game_cleanup_interval'] = 86400;
$game_config['game_last_cleanup'] = time();
}
// Comprobar ъltima actualizaciуn
if (((int)$game_config['stats_update_interval'] + (int)$game_config['stats_last_update']) < time() OR $ignore == true)
{
// Intentar obtener un bloqueo para actualizar
if (sql_get_lock('game_stats_updater'))
{
global $resource, $pricelist, $reslist;
// Bloquear tablas
sql_query('LOCK TABLES #alliance# WRITE, #banned# WRITE, #buddy# WRITE, #config# WRITE, #errors# WRITE, #fleets# WRITE, #galaxy# WRITE, #messages# WRITE, #notes# WRITE, #planets# WRITE, #rw# WRITE, #statpoints# WRITE, #users# WRITE');
// Hora
$time = time();
// Rotar estadнsticas
sql_query('DELETE FROM #statpoints# WHERE stat_code = 2');
sql_query('UPDATE #statpoints# SET stat_code = stat_code + 1');
// Volver a calcular los miembros que tienen las alianzas
sql_query("UPDATE #alliance# SET ally_members = 0");
// Seleccionar usuarios
$users = sql_query('SELECT * FROM #users#');
while($CurrentUser = mysql_fetch_assoc($users))
{
if ((int)$CurrentUser['ally_id'] > 0)
{
sql_query("UPDATE #alliance# SET ally_members = ally_members + 1 WHERE (id = $CurrentUser[ally_id])");
}
// No se procesan los usuarios baneados
if (!empty($CurrentUser['bana']))
{
continue;
}
// Estadнsticas viejas
$old_stats = sql_query("SELECT total_rank, tech_rank, build_rank, defs_rank, fleet_rank FROM #statpoints# WHERE (stat_type = 1 AND id_owner = $CurrentUser[id])", true);
if ($old_stats)
{
$stat_total_rank = $old_stats['total_rank'];
$stat_tech_rank = $old_stats['tech_rank'];
$stat_build_rank = $old_stats['build_rank'];
$stat_defs_rank = $old_stats['defs_rank'];
$stat_fleet_rank = $old_stats['fleet_rank'];
}
else
{
$stat_total_rank = 0;
$stat_tech_rank = 0;
$stat_build_rank = 0;
$stat_defs_rank = 0;
$stat_fleet_rank = 0;
}
unset($old_stats);
// Investigaciones
$stat_total_points = 0;
$stat_total_count = 0;
$stat_points = 0;
$stat_count = 0;
foreach ($reslist['tech'] as $n => $Techno)
{
if ($CurrentUser[ $resource[ $Techno ] ] > 0)
{
for ($Level = 1; $Level <= $CurrentUser[ $resource[ $Techno ] ]; $Level++)
{
$units = $pricelist[ $Techno ]['metal'] + $pricelist[ $Techno ]['crystal'] + $pricelist[ $Techno ]['deuterium'];
$LevelMul = pow( $pricelist[ $Techno ]['factor'], $Level );
$stat_points += ($units * $LevelMul);
$stat_count += 1;
}
}
}
$user_tech_points = round($stat_points / $game_config['stat_settings']);
$user_tech_count = $stat_count;
$stat_total_points += $user_tech_points;
$stat_total_count += $user_tech_count;
unset($units, $LevelMul, $Level, $Techno, $n);
// Flotas en vuelo
$stat_points = 0;
$stat_count = 0;
$user_fleet_points = 0;
$user_fleet_count = 0;
$my_fleets = sql_query("SELECT fleet_array FROM #fleets# WHERE (fleet_owner = $CurrentUser[id])");
while ($row = mysql_fetch_assoc($my_fleets))
{
$fleet = explode(";", $row['fleet_array']);
if (!empty($fleet))
{
foreach($fleet as $index => $group)
{
if (!empty($group))
{
$ships = explode(',', $group);
$units = $pricelist[ $ships[0] ]['metal'] + $pricelist[ $ships[0] ]['crystal'] + $pricelist[ $ships[0] ]['deuterium'];
$stat_points += ($units * $ships[1]);
$stat_count += $ships[1];
}
}
}
}
$user_fleet_points = round($stat_points / $game_config['stat_settings']);
$user_fleet_count = $stat_count;
$stat_total_points += $user_fleet_points;
$stat_total_count += $user_fleet_count;
unset($my_fleets, $units, $ships, $index, $group, $fleet, $row);
// Edificios, defensas, y flotas en los planetas del jugador
$user_build_points = 0;
$user_build_count = 0;
$user_defs_points = 0;
$user_defs_count = 0;
$my_planets = sql_query("SELECT * FROM #planets# WHERE (id_owner = $CurrentUser[id])");
while($CurrentPlanet = mysql_fetch_assoc($my_planets))
{
// Edificios
$stat_points = 0;
$stat_count = 0;
$user_planet_points = 0;
foreach($reslist['build'] as $n => $Building)
{
if ($CurrentPlanet[ $resource[ $Building ] ] > 0)
{
for ($Level = 1; $Level <= $CurrentPlanet[ $resource[ $Building ] ]; $Level++)
{
$units = $pricelist[ $Building ]['metal'] + $pricelist[ $Building ]['crystal'] + $pricelist[ $Building ]['deuterium'];
$LevelMul = pow($pricelist[ $Building ]['factor'], $Level);
$stat_points += ($units * $LevelMul);
$stat_count += 1;
}
}
}
$user_build_points += round($stat_points / $game_config['stat_settings']);
$user_build_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_build_count;
unset($LevelMul, $units, $Level, $n, $Building);
// Defensas
$stat_points = 0;
$stat_count = 0;
foreach($reslist['defense'] as $n => $Defense)
{
if ($CurrentPlanet[ $resource[ $Defense ] ] > 0)
{
$units = $pricelist[ $Defense ]['metal'] + $pricelist[ $Defense ]['crystal'] + $pricelist[ $Defense ]['deuterium'];
$stat_points += ($units * $CurrentPlanet[ $resource[ $Defense ] ]);
$stat_count += $CurrentPlanet[ $resource[ $Defense ] ];
}
}
$user_defs_points += round($stat_points / $game_config['stat_settings']);
$user_defs_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_defs_count;
unset($units, $n, $Defense);
// Flotas
$stat_points = 0;
$stat_count = 0;
foreach($reslist['fleet'] as $n => $Fleet)
{
if ($CurrentPlanet[ $resource[ $Fleet ] ] > 0)
{
$units = $pricelist[ $Fleet ]['metal'] + $pricelist[ $Fleet ]['crystal'] + $pricelist[ $Fleet ]['deuterium'];
$stat_points += ($units * $CurrentPlanet[ $resource[ $Fleet ] ]);
$stat_count += $CurrentPlanet[ $resource[ $Fleet ] ];
}
}
$user_fleet_points += round($stat_points / $game_config['stat_settings']);
$user_fleet_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_fleet_count;
unset($units, $n, $Fleet);
// Puntos totales
$stat_total_points += $user_planet_points;
// Actualizar puntos del planeta
sql_query("UPDATE #planets# SET points = $user_planet_points WHERE (id = $CurrentPlanet[id])");
}
// Agregar estadнsticas del jugador
sql_query("INSERT INTO #statpoints# SET id_owner = $CurrentUser[id], id_ally = $CurrentUser[ally_id], stat_type = 1, stat_code = 1, tech_points = $user_tech_points, tech_count = $user_tech_count, tech_old_rank = $stat_tech_rank, build_points = $user_build_points, build_count = $user_build_count, build_old_rank = $stat_build_rank, defs_points = $user_defs_points, defs_count = $user_defs_count, defs_old_rank = $stat_defs_rank, fleet_points = $user_fleet_points, fleet_count = $user_fleet_count, fleet_old_rank = $stat_fleet_rank, total_points = $stat_total_points, total_count = $stat_total_count, total_old_rank = $stat_total_rank, stat_date = $time");
}
unset($stat_total_rank, $stat_total_count, $stat_total_points, $stat_fleet_rank, $user_fleet_count, $user_fleet_points, $stat_defs_rank, $user_defs_count, $user_defs_points, $stat_build_rank, $user_build_count, $user_build_points, $stat_tech_rank, $user_tech_count, $user_tech_points, $user_planet_points, $stat_points, $stat_count);
// Ranking tecnologнas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY tech_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET tech_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking edificios
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY build_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET build_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking defensas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY defs_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET defs_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking flotas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY fleet_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET fleet_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking general
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY total_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET total_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
unset($rank);
// Limpieza
if (((int)$game_config['game_cleanup_interval'] + (int)$game_config['game_last_cleanup']) < time())
{
// Planetas abandonados
sql_query('DELETE FROM #galaxy# WHERE (id_planet = 0)');
// Mensajes viejos (15 dнas)
sql_query('DELETE FROM #messages# WHERE (message_time < '.(time() + (-14 * 24 * 60 * 60)).')');
// Chat viejos (5 dнas)
#sql_query('DELETE FROM #chat# WHERE (timestamp < '.(time() + (-5 * 24 * 60 * 60)).')');
// Reportes de batallas viejos (365 dнas)
sql_query('DELETE FROM #rw# WHERE (date < '.(time() + (60 * 60 * 24 * 365)).')');
// Usuarios inactivos + usuarios que quieran eliminar su cuenta
$result = sql_query('SELECT id, ally_id FROM #users# WHERE (onlinetime < '.(time() + (-90 * 24 * 60 * 60)).' OR (delete_account = 1 AND (delete_time > 0 AND delete_time < '.time().')))');
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_assoc($result))
{
$planets = sql_query("SELECT id FROM #planets# WHERE (id_owner = $row[id])");
if (mysql_num_rows($planets) > 0)
{
while($planet = mysql_fetch_assoc($planets))
{
sql_query("DELETE FROM #galaxy# WHERE id_planet = $planet[id]");
}
unset($planets, $planet);
}
// Alianza
if ($row['ally_id'] > 0)
{
// Informaciуn de la alianza
$ally = sql_query("SELECT ally_members FROM #alliance# WHERE (id = $row[ally_id])", true);
if (!empty($ally) && is_array($ally))
{
// Restar un miembro
$ally['ally_members'] -= 1;
// Si no quedan miembros...
if ($ally['ally_members'] <= 0)
{
// Eliminar alianza
sql_query("DELETE FROM #alliance# WHERE (id = $row[ally_id])");
sql_query("DELETE FROM #statpoints# WHERE (stat_type = 2 AND id_owner = $row[ally_id])");
}
else
{
sql_query("UPDATE #alliance# SET ally_members = ally_members - 1 WHERE (id = $row[ally_id])");
}
}
}
// Baneados
sql_query("DELETE FROM #banned# WHERE (user_id = $row[id])");
// Estadнsticas
sql_query("DELETE FROM #statpoints# WHERE (stat_type = 1 AND id_owner = $row[id])");
// Planetas
sql_query("DELETE FROM #planets# WHERE (id_owner = $row[id])");
// Mensajes
sql_query("DELETE FROM #messages# WHERE (message_sender = $row[id] OR message_owner = $row[id])");
// Notas
sql_query("DELETE FROM #notes# WHERE (owner = $row[id])");
// Flotas
sql_query("DELETE FROM #fleets# WHERE (fleet_owner = $row[id])");
// Lista de amigos
sql_query("DELETE FROM #buddy# WHERE (sender = $row[id] OR owner = $row[id])");
// Usuario
sql_query("DELETE FROM #users# WHERE (id = $row[id])");
}
}
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'game_last_cleanup\')');
}
// Alianzas
$allys = sql_query("SELECT * FROM #alliance#");
while($CurrentAlly = mysql_fetch_assoc($allys))
{
// Estadнsticas viejas
$old_stats = sql_query("SELECT total_rank, tech_rank, build_rank, defs_rank, fleet_rank FROM #statpoints# WHERE (stat_type = 2 AND id_owner = $CurrentAlly[id])", true);
if ($old_stats)
{
$stat_total_rank = $old_stats['total_rank'];
$stat_tech_rank = $old_stats['tech_rank'];
$stat_build_rank = $old_stats['build_rank'];
$stat_defs_rank = $old_stats['defs_rank'];
$stat_fleet_rank = $old_stats['fleet_rank'];
}
else
{
$stat_total_rank = 0;
$stat_tech_rank = 0;
$stat_build_rank = 0;
$stat_defs_rank = 0;
$stat_fleet_rank = 0;
}
// Puntaje
$points = sql_query("SELECT SUM(tech_points) AS TechPoints, SUM(tech_count) AS TechCount, SUM(build_points) AS BuildPoints, SUM(build_count) AS BuildCount, SUM(defs_points) AS DefsPoints, SUM(defs_count) AS DefsCount, SUM(fleet_points) AS FleetPoints, SUM(fleet_count) AS FleetCount, SUM(total_points) AS TotalPoints, SUM(total_count) AS TotalCount FROM #statpoints# WHERE (stat_type = 1 AND id_ally = $CurrentAlly[id])", true);
// ¬_¬ Fix
$points['TechCount'] = round($points['TechCount'] / 2);
$points['TechPoints'] = round($points['TechPoints'] / 2);
$points['BuildCount'] = round($points['BuildCount'] / 2);
$points['BuildPoints'] = round($points['BuildPoints'] / 2);
$points['DefsCount'] = round($points['DefsCount'] / 2);
$points['DefsPoints'] = round($points['DefsPoints'] / 2);
$points['FleetCount'] = round($points['FleetCount'] / 2);
$points['FleetPoints'] = round($points['FleetPoints'] / 2);
$points['TotalCount'] = round($points['TotalCount'] / 2);
$points['TotalPoints'] = round($points['TotalPoints'] / 2);
// Actualizar
sql_query("INSERT INTO #statpoints# SET id_owner = $CurrentAlly[id], id_ally = 0, stat_type = 2, stat_code = 1, tech_points = $points[TechPoints], tech_count = $points[TechCount], tech_old_rank = $stat_tech_rank, build_points = $points[BuildPoints], build_count = $points[BuildCount], build_old_rank = $stat_build_rank, defs_points = $points[DefsPoints], defs_count = $points[DefsCount], defs_old_rank = $stat_defs_rank, fleet_points = $points[FleetPoints], fleet_count = $points[FleetCount], fleet_old_rank = $stat_fleet_rank, total_points = $points[TotalPoints], total_count = $points[TotalCount], total_old_rank = $stat_total_rank, stat_date = $time");
}
// Optimizar tablas
if (((int)$game_config['game_optimize_interval'] + (int)$game_config['game_last_optimize']) < time())
{
// Bloquear tablas
sql_query('OPTIMIZE TABLES #banned#,#alliance#,#fleets#,#galaxy#,#messages#,#notes#,#planets#,#rw#,#statpoi
nts#,#users#,#buddy#');
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'game_last_optimize\')');
}
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'stats_last_update\')');
}
// Desbloquear tablas
sql_query('UNLOCK TABLES');
// Liberar bloqueo
sql_release_lock('game_stats_updater');
}
// Calcul de la duree de traitement (calcul)
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$__result['stats_time'] = $stats_time;
$__result['totaltime'] = ($endtime - $starttime);
$__result['memory_peak'] = array(round(memory_get_peak_usage() / 1024,1),round(memory_get_peak_usage(1) / 1024,1));
$__result['end_memory'] = array(round(memory_get_usage() / 1024,1),round(memory_get_usage(1) / 1024,1));
$__result['amount_per_block'] =$amount_per_block;
return $__result;
}
</div>
</div>
</div>
затем открыть common.php
это
заменить на
далее открыть adm/statbuilder.php
где строки
и
удалить.
и тут же это
заменить на
пробуем тестируем и отписываем
для XGP Сборок.
Создать файл CalculateStats.php в includes/functions и в него запихнуть это
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">Скрытая информация: "CalculateStats.php" <input type="button" value="Показать" style="width:60px;font-size:10px;margin:0px;padding:0px;" onClick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Скрыть'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Показать'; }">
</div>
<div class="alt2" style="margin: 0px; padding: 6px; border: 1px inset;">
<div style="display: none;">
<?php
##############################################################################
# * #
# * XG PROYECT #
# * #
# * @copyright Copyright © 2008 - 2009 By lucky from Xtreme-gameZ.com.ar #
# * #
# * #
# * This program is free software: you can redistribute it and/or modify #
# * it under the terms of the GNU General Public License as published by #
# * the Free Software Foundation, either version 3 of the License, or #
# * (at your option) any later version. #
# * #
# * This program is distributed in the hope that it will be useful, #
# * but WITHOUT ANY WARRANTY; without even the implied warranty of #
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# * GNU General Public License for more details. #
# * #
##############################################################################
/**
* Ejecuta una consulta en la base de datos
*
* @param string $query Consulta
* @param string $fetch Usar mysql_fetch_assoc y devolver resultado
*
* @return mixed Resultado de la consulta o FALSE en caso de error
*/
function sql_query($query, $fetch = false)
{
// Escapar consulta
$query = preg_replace('/\#(.+?)\#/e', '\'{{table}}\\1\'', $query);
// Ejecutar consulta
$result = @doquery($query, '', $fetch);
return $result;
}
/**
* Intenta obtener un bloqueo en la base de datos
*
* @param string $name Nombre
* @param boolean $timeout Tiempo mбximo de espera hasta para obtener el bloqueo
*
* @return boolean true si se ha obtenido el bloqueo o false caso contrario
*/
function sql_get_lock($name, $timeout = 0)
{
// Obtener bloqueo
$result = sql_query("SELECT COALESCE(GET_LOCK('".$name."', $timeout), 0)");
if ((boolean)@mysql_result($result, 0) == true)
{
return true;
}
return false;
}
/**
* Libera un bloqueo en la base de datos
*
* @param string $name Nombre
*/
function sql_release_lock($name)
{
if (!empty($name))
{
sql_query("DO RELEASE_LOCK('".$name."')");
}
}
/**
* Actualizaciуn de las estadнsticas
*/
function game_update_stats($ignore = false)
{
global $game_config, $resource, $pricelist, $reslist;
// Initial Time
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
//Initial Memory
$__result['initial_memory']= array(round(memory_get_usage() / 1024,1),round(memory_get_usage(1) / 1024,1));
// Adaptacion a XGP - instalacion automatica
if (!isset($game_config['stats_update_interval'])) {
// Insertar
sql_query("INSERT INTO #config# SET config_name = 'stats_update_interval', config_value = '3600'");
sql_query("INSERT INTO #config# SET config_name = 'stats_last_update', config_value = '0'");
sql_query("INSERT INTO #config# SET config_name = 'game_optimize_interval', config_value = '86400'");
sql_query("INSERT INTO #config# SET config_name = 'game_last_optimize', config_value = '0'");
sql_query("INSERT INTO #config# SET config_name = 'game_cleanup_interval', config_value = '86400'");
sql_query("INSERT INTO #config# SET config_name = 'game_last_cleanup', config_value = '".time()."'");
// Emular
$game_config['stats_update_interval'] = 3600;
$game_config['stats_last_update'] = 0;
$game_config['game_optimize_interval'] = 86400;
$game_config['game_last_optimize'] = 0;
$game_config['game_cleanup_interval'] = 86400;
$game_config['game_last_cleanup'] = time();
}
// Comprobar ъltima actualizaciуn
if (((int)$game_config['stats_update_interval'] + (int)$game_config['stats_last_update']) < time() OR $ignore == true)
{
// Intentar obtener un bloqueo para actualizar
if (sql_get_lock('game_stats_updater'))
{
global $resource, $pricelist, $reslist;
// Bloquear tablas
sql_query('LOCK TABLES #alliance# WRITE, #banned# WRITE, #buddy# WRITE, #config# WRITE, #errors# WRITE, #fleets# WRITE, #galaxy# WRITE, #messages# WRITE, #notes# WRITE, #planets# WRITE, #rw# WRITE, #statpoints# WRITE, #users# WRITE');
// Hora
$time = time();
// Rotar estadнsticas
sql_query('DELETE FROM #statpoints# WHERE stat_code = 2');
sql_query('UPDATE #statpoints# SET stat_code = stat_code + 1');
// Volver a calcular los miembros que tienen las alianzas
sql_query("UPDATE #alliance# SET ally_members = 0");
// Seleccionar usuarios
$users = sql_query('SELECT * FROM #users#');
while($CurrentUser = mysql_fetch_assoc($users))
{
if ((int)$CurrentUser['ally_id'] > 0)
{
sql_query("UPDATE #alliance# SET ally_members = ally_members + 1 WHERE (id = $CurrentUser[ally_id])");
}
// No se procesan los usuarios baneados
if (!empty($CurrentUser['bana']))
{
continue;
}
// Estadнsticas viejas
$old_stats = sql_query("SELECT total_rank, tech_rank, build_rank, defs_rank, fleet_rank FROM #statpoints# WHERE (stat_type = 1 AND id_owner = $CurrentUser[id])", true);
if ($old_stats)
{
$stat_total_rank = $old_stats['total_rank'];
$stat_tech_rank = $old_stats['tech_rank'];
$stat_build_rank = $old_stats['build_rank'];
$stat_defs_rank = $old_stats['defs_rank'];
$stat_fleet_rank = $old_stats['fleet_rank'];
}
else
{
$stat_total_rank = 0;
$stat_tech_rank = 0;
$stat_build_rank = 0;
$stat_defs_rank = 0;
$stat_fleet_rank = 0;
}
unset($old_stats);
// Investigaciones
$stat_total_points = 0;
$stat_total_count = 0;
$stat_points = 0;
$stat_count = 0;
foreach ($reslist['tech'] as $n => $Techno)
{
if ($CurrentUser[ $resource[ $Techno ] ] > 0)
{
for ($Level = 1; $Level <= $CurrentUser[ $resource[ $Techno ] ]; $Level++)
{
$units = $pricelist[ $Techno ]['metal'] + $pricelist[ $Techno ]['crystal'] + $pricelist[ $Techno ]['deuterium'];
$LevelMul = pow( $pricelist[ $Techno ]['factor'], $Level );
$stat_points += ($units * $LevelMul);
$stat_count += 1;
}
}
}
$user_tech_points = round($stat_points / $game_config['stat_settings']);
$user_tech_count = $stat_count;
$stat_total_points += $user_tech_points;
$stat_total_count += $user_tech_count;
unset($units, $LevelMul, $Level, $Techno, $n);
// Flotas en vuelo
$stat_points = 0;
$stat_count = 0;
$user_fleet_points = 0;
$user_fleet_count = 0;
$my_fleets = sql_query("SELECT fleet_array FROM #fleets# WHERE (fleet_owner = $CurrentUser[id])");
while ($row = mysql_fetch_assoc($my_fleets))
{
$fleet = explode(";", $row['fleet_array']);
if (!empty($fleet))
{
foreach($fleet as $index => $group)
{
if (!empty($group))
{
$ships = explode(',', $group);
$units = $pricelist[ $ships[0] ]['metal'] + $pricelist[ $ships[0] ]['crystal'] + $pricelist[ $ships[0] ]['deuterium'];
$stat_points += ($units * $ships[1]);
$stat_count += $ships[1];
}
}
}
}
$user_fleet_points = round($stat_points / $game_config['stat_settings']);
$user_fleet_count = $stat_count;
$stat_total_points += $user_fleet_points;
$stat_total_count += $user_fleet_count;
unset($my_fleets, $units, $ships, $index, $group, $fleet, $row);
// Edificios, defensas, y flotas en los planetas del jugador
$user_build_points = 0;
$user_build_count = 0;
$user_defs_points = 0;
$user_defs_count = 0;
$my_planets = sql_query("SELECT * FROM #planets# WHERE (id_owner = $CurrentUser[id])");
while($CurrentPlanet = mysql_fetch_assoc($my_planets))
{
// Edificios
$stat_points = 0;
$stat_count = 0;
$user_planet_points = 0;
foreach($reslist['build'] as $n => $Building)
{
if ($CurrentPlanet[ $resource[ $Building ] ] > 0)
{
for ($Level = 1; $Level <= $CurrentPlanet[ $resource[ $Building ] ]; $Level++)
{
$units = $pricelist[ $Building ]['metal'] + $pricelist[ $Building ]['crystal'] + $pricelist[ $Building ]['deuterium'];
$LevelMul = pow($pricelist[ $Building ]['factor'], $Level);
$stat_points += ($units * $LevelMul);
$stat_count += 1;
}
}
}
$user_build_points += round($stat_points / $game_config['stat_settings']);
$user_build_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_build_count;
unset($LevelMul, $units, $Level, $n, $Building);
// Defensas
$stat_points = 0;
$stat_count = 0;
foreach($reslist['defense'] as $n => $Defense)
{
if ($CurrentPlanet[ $resource[ $Defense ] ] > 0)
{
$units = $pricelist[ $Defense ]['metal'] + $pricelist[ $Defense ]['crystal'] + $pricelist[ $Defense ]['deuterium'];
$stat_points += ($units * $CurrentPlanet[ $resource[ $Defense ] ]);
$stat_count += $CurrentPlanet[ $resource[ $Defense ] ];
}
}
$user_defs_points += round($stat_points / $game_config['stat_settings']);
$user_defs_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_defs_count;
unset($units, $n, $Defense);
// Flotas
$stat_points = 0;
$stat_count = 0;
foreach($reslist['fleet'] as $n => $Fleet)
{
if ($CurrentPlanet[ $resource[ $Fleet ] ] > 0)
{
$units = $pricelist[ $Fleet ]['metal'] + $pricelist[ $Fleet ]['crystal'] + $pricelist[ $Fleet ]['deuterium'];
$stat_points += ($units * $CurrentPlanet[ $resource[ $Fleet ] ]);
$stat_count += $CurrentPlanet[ $resource[ $Fleet ] ];
}
}
$user_fleet_points += round($stat_points / $game_config['stat_settings']);
$user_fleet_count += $stat_count;
$user_planet_points += round($stat_points / $game_config['stat_settings']);
$stat_total_count += $user_fleet_count;
unset($units, $n, $Fleet);
// Puntos totales
$stat_total_points += $user_planet_points;
// Actualizar puntos del planeta
sql_query("UPDATE #planets# SET points = $user_planet_points WHERE (id = $CurrentPlanet[id])");
}
// Agregar estadнsticas del jugador
sql_query("INSERT INTO #statpoints# SET id_owner = $CurrentUser[id], id_ally = $CurrentUser[ally_id], stat_type = 1, stat_code = 1, tech_points = $user_tech_points, tech_count = $user_tech_count, tech_old_rank = $stat_tech_rank, build_points = $user_build_points, build_count = $user_build_count, build_old_rank = $stat_build_rank, defs_points = $user_defs_points, defs_count = $user_defs_count, defs_old_rank = $stat_defs_rank, fleet_points = $user_fleet_points, fleet_count = $user_fleet_count, fleet_old_rank = $stat_fleet_rank, total_points = $stat_total_points, total_count = $stat_total_count, total_old_rank = $stat_total_rank, stat_date = $time");
}
unset($stat_total_rank, $stat_total_count, $stat_total_points, $stat_fleet_rank, $user_fleet_count, $user_fleet_points, $stat_defs_rank, $user_defs_count, $user_defs_points, $stat_build_rank, $user_build_count, $user_build_points, $stat_tech_rank, $user_tech_count, $user_tech_points, $user_planet_points, $stat_points, $stat_count);
// Ranking tecnologнas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY tech_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET tech_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking edificios
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY build_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET build_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking defensas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY defs_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET defs_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking flotas
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY fleet_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET fleet_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
// Ranking general
$rank = 1;
$result = sql_query('SELECT id_owner FROM #statpoints# WHERE stat_type = 1 AND stat_code = 1 ORDER BY total_points DESC');
while($row = mysql_fetch_assoc($result))
{
sql_query("UPDATE #statpoints# SET total_rank = '$rank' WHERE (stat_type = 1 AND stat_code = 1 AND id_owner = $row[id_owner])");
$rank++;
}
unset($rank);
// Limpieza
if (((int)$game_config['game_cleanup_interval'] + (int)$game_config['game_last_cleanup']) < time())
{
// Planetas abandonados
sql_query('DELETE FROM #galaxy# WHERE (id_planet = 0)');
// Mensajes viejos (15 dнas)
sql_query('DELETE FROM #messages# WHERE (message_time < '.(time() + (-14 * 24 * 60 * 60)).')');
// Chat viejos (5 dнas)
#sql_query('DELETE FROM #chat# WHERE (timestamp < '.(time() + (-5 * 24 * 60 * 60)).')');
// Reportes de batallas viejos (365 dнas)
sql_query('DELETE FROM #rw# WHERE (date < '.(time() + (60 * 60 * 24 * 365)).')');
// Usuarios inactivos + usuarios que quieran eliminar su cuenta
$result = sql_query('SELECT id, ally_id FROM #users# WHERE (onlinetime < '.(time() + (-90 * 24 * 60 * 60)).' OR (delete_account = 1 AND (delete_time > 0 AND delete_time < '.time().')))');
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_assoc($result))
{
$planets = sql_query("SELECT id FROM #planets# WHERE (id_owner = $row[id])");
if (mysql_num_rows($planets) > 0)
{
while($planet = mysql_fetch_assoc($planets))
{
sql_query("DELETE FROM #galaxy# WHERE id_planet = $planet[id]");
}
unset($planets, $planet);
}
// Alianza
if ($row['ally_id'] > 0)
{
// Informaciуn de la alianza
$ally = sql_query("SELECT ally_members FROM #alliance# WHERE (id = $row[ally_id])", true);
if (!empty($ally) && is_array($ally))
{
// Restar un miembro
$ally['ally_members'] -= 1;
// Si no quedan miembros...
if ($ally['ally_members'] <= 0)
{
// Eliminar alianza
sql_query("DELETE FROM #alliance# WHERE (id = $row[ally_id])");
sql_query("DELETE FROM #statpoints# WHERE (stat_type = 2 AND id_owner = $row[ally_id])");
}
else
{
sql_query("UPDATE #alliance# SET ally_members = ally_members - 1 WHERE (id = $row[ally_id])");
}
}
}
// Baneados
sql_query("DELETE FROM #banned# WHERE (user_id = $row[id])");
// Estadнsticas
sql_query("DELETE FROM #statpoints# WHERE (stat_type = 1 AND id_owner = $row[id])");
// Planetas
sql_query("DELETE FROM #planets# WHERE (id_owner = $row[id])");
// Mensajes
sql_query("DELETE FROM #messages# WHERE (message_sender = $row[id] OR message_owner = $row[id])");
// Notas
sql_query("DELETE FROM #notes# WHERE (owner = $row[id])");
// Flotas
sql_query("DELETE FROM #fleets# WHERE (fleet_owner = $row[id])");
// Lista de amigos
sql_query("DELETE FROM #buddy# WHERE (sender = $row[id] OR owner = $row[id])");
// Usuario
sql_query("DELETE FROM #users# WHERE (id = $row[id])");
}
}
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'game_last_cleanup\')');
}
// Alianzas
$allys = sql_query("SELECT * FROM #alliance#");
while($CurrentAlly = mysql_fetch_assoc($allys))
{
// Estadнsticas viejas
$old_stats = sql_query("SELECT total_rank, tech_rank, build_rank, defs_rank, fleet_rank FROM #statpoints# WHERE (stat_type = 2 AND id_owner = $CurrentAlly[id])", true);
if ($old_stats)
{
$stat_total_rank = $old_stats['total_rank'];
$stat_tech_rank = $old_stats['tech_rank'];
$stat_build_rank = $old_stats['build_rank'];
$stat_defs_rank = $old_stats['defs_rank'];
$stat_fleet_rank = $old_stats['fleet_rank'];
}
else
{
$stat_total_rank = 0;
$stat_tech_rank = 0;
$stat_build_rank = 0;
$stat_defs_rank = 0;
$stat_fleet_rank = 0;
}
// Puntaje
$points = sql_query("SELECT SUM(tech_points) AS TechPoints, SUM(tech_count) AS TechCount, SUM(build_points) AS BuildPoints, SUM(build_count) AS BuildCount, SUM(defs_points) AS DefsPoints, SUM(defs_count) AS DefsCount, SUM(fleet_points) AS FleetPoints, SUM(fleet_count) AS FleetCount, SUM(total_points) AS TotalPoints, SUM(total_count) AS TotalCount FROM #statpoints# WHERE (stat_type = 1 AND id_ally = $CurrentAlly[id])", true);
// ¬_¬ Fix
$points['TechCount'] = round($points['TechCount'] / 2);
$points['TechPoints'] = round($points['TechPoints'] / 2);
$points['BuildCount'] = round($points['BuildCount'] / 2);
$points['BuildPoints'] = round($points['BuildPoints'] / 2);
$points['DefsCount'] = round($points['DefsCount'] / 2);
$points['DefsPoints'] = round($points['DefsPoints'] / 2);
$points['FleetCount'] = round($points['FleetCount'] / 2);
$points['FleetPoints'] = round($points['FleetPoints'] / 2);
$points['TotalCount'] = round($points['TotalCount'] / 2);
$points['TotalPoints'] = round($points['TotalPoints'] / 2);
// Actualizar
sql_query("INSERT INTO #statpoints# SET id_owner = $CurrentAlly[id], id_ally = 0, stat_type = 2, stat_code = 1, tech_points = $points[TechPoints], tech_count = $points[TechCount], tech_old_rank = $stat_tech_rank, build_points = $points[BuildPoints], build_count = $points[BuildCount], build_old_rank = $stat_build_rank, defs_points = $points[DefsPoints], defs_count = $points[DefsCount], defs_old_rank = $stat_defs_rank, fleet_points = $points[FleetPoints], fleet_count = $points[FleetCount], fleet_old_rank = $stat_fleet_rank, total_points = $points[TotalPoints], total_count = $points[TotalCount], total_old_rank = $stat_total_rank, stat_date = $time");
}
// Optimizar tablas
if (((int)$game_config['game_optimize_interval'] + (int)$game_config['game_last_optimize']) < time())
{
// Bloquear tablas
sql_query('OPTIMIZE TABLES #banned#,#alliance#,#fleets#,#galaxy#,#messages#,#notes#,#planets#,#rw#,#statpoi
nts#,#users#,#buddy#');
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'game_last_optimize\')');
}
// Actualizar
sql_query('UPDATE #config# SET config_value = '.$time.' WHERE (config_name = \'stats_last_update\')');
}
// Desbloquear tablas
sql_query('UNLOCK TABLES');
// Liberar bloqueo
sql_release_lock('game_stats_updater');
}
// Calcul de la duree de traitement (calcul)
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$__result['stats_time'] = $stats_time;
$__result['totaltime'] = ($endtime - $starttime);
$__result['memory_peak'] = array(round(memory_get_peak_usage() / 1024,1),round(memory_get_peak_usage(1) / 1024,1));
$__result['end_memory'] = array(round(memory_get_usage() / 1024,1),round(memory_get_usage(1) / 1024,1));
$__result['amount_per_block'] =$amount_per_block;
return $__result;
}
</div>
</div>
</div>
затем открыть common.php
это
Код:
if ( ( time() >= ( $game_config['stat_last_update'] + ( 60 * $game_config['stat_update_time'] ) ) ) )
{
include($xgp_root . 'adm/statfunctions.' . $phpEx);
$result = MakeStats();
update_config('stat_last_update', $result['stats_time']);
}
Код:
include($xgp_root . 'includes/functions/CalculateStats.' . $phpEx);
game_update_stats();
где строки
Код:
include($xgp_root . 'adm/statfunctions.' . $phpEx);
Код:
update_config( 'stat_last_update', $result['stats_time']);
и тут же это
Код:
$result = MakeStats();
Код:
$result = game_update_stats(true);
пробуем тестируем и отписываем