<?php
/*
* Alliances Manager (Gestionador de Alianzas) - Travian
* Copyright (C) 2008-12 Viva Mayer, Francisco Mesías <hide@address.com>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class ModeloAldeas extends ModeloBase
{
private function actualizar()
{
$sql = "UPDATE `".$this->config->get('PREFIX')."usuario` SET `last_change_aldea` = NOW( ) WHERE nombre=\"".$_SESSION['usuario']."\"";
mysql_query($sql, $this->db) or die(mysql_error($this->db));
}
public function propias()
{
// Estraemos todas las aldeas que tenga cargadas este usuario
$sql = "SELECT `nombre`, `x`, `y`, `es_capital`, `es_principal`, `tipo`, `granero`, `muralla`, `residencia`, `descripcion` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`=\"".$_SESSION['usuario']."\" ORDER BY `nombre` ASC";
$resultado = mysql_query($sql,$this->db) or die(mysql_error($this->db));
return $resultado;
} // fin funcion propias()
public function borrar()
{
$sql = "DELETE FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`= \"".$_SESSION['usuario']."\" AND `x`= ".$_GET['x']." AND `y`= ".$_GET['y'];
mysql_query($sql, $this->db) or die(mysql_error($this->db));
$this->actualizar();
header("Location: index.php?ctrl=Aldeas&mod=aldeas");
}
public function nueva()
{
isset($_POST['capital']) ? $es_capital = 1 : $es_capital = 0;
isset($_POST['principal']) ? $es_principal = 1 : $es_principal = 0;
if($_POST['nombre']=="")// BIEN
{
$error='EPA20';
return $error;
}
if(($_POST['x']=="") or ($_POST['y']==""))// BIEN
{
return 'EPA21';
}
if((($_POST['x']<-400) or ($_POST['x']>400)) or (($_POST['y']<-400) or ($_POST['y']>400)))
{// BIEN
$error='EPA25';
return $error;
}
$sql = "SELECT `x`, `y` FROM `".$this->config->get('PREFIX')."aldea` WHERE `x`= ".$_POST['x']." AND `y`= ".$_POST['y'];
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$reg = mysql_fetch_assoc($resultado);
if(($reg['x']==$_POST['x']) and ($reg['y']==$_POST['y'])) // BIEN
{
$error='EPA22';
return $error;
}
if($es_capital==1)
{
$sql = "SELECT `es_capital` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`= \"".$_SESSION['usuario']."\" AND `es_capital`= 1";
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$num_row = mysql_num_rows($resultado);
if($num_row!=0)
{
$error='EPA23';
return $error; }
}
if($es_principal==1)
{
$sql = "SELECT `es_principal` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`= \"".$_SESSION['usuario']."\" AND `es_principal`= 1";
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$num_row = mysql_num_rows($resultado);
if($num_row!=0) return 'EPA24';
}
$sql = "INSERT INTO `".$this->config->get('PREFIX')."aldea` (`x`,`y`,`usuario_nombre`,`nombre`,`tipo`,`es_capital`,`es_principal`,`descripcion`,`residencia`,`muralla`,`granero`) VALUES (".$_POST['x'].", ".$_POST['y'].", \"".$_SESSION['usuario']."\", \"".$_POST['nombre']."\", \"".$_POST['t_aldea']."\", ".$es_capital.", ".$es_principal.", \"".$_POST['descrip']."\", ".(int) $_POST['n_rp'].", ".(int) $_POST['muro'].", ".(int) $_POST['granero'].")";
mysql_query($sql, $this->db) or die(mysql_error($this->db));
$this->actualizar();
header("Location: index.php?ctrl=Aldeas&mod=aldeas");
}
public function ver_datos()
{
$sql ="SELECT `x`, `y`, `usuario_nombre`, `nombre`, `tipo`, `es_capital`, `es_principal`, `descripcion`, `residencia`, `muralla`, `granero` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`=\"".$_SESSION['usuario']."\" AND `x`= ".$_GET['x']." AND `y`= ".$_GET['y'];
$sql_aldeas = mysql_query($sql, $this->db) or die(mysql_error($this->db));
return mysql_fetch_assoc($sql_aldeas);
}
public function editar()
{
isset($_POST['capital']) ? $es_capital = 1 : $es_capital = 0;
isset($_POST['principal']) ? $es_principal = 1 : $es_principal = 0;
if($_POST['nombre']=="")
{
$error='EPA20';
return $error;
}
if(($_POST['x']=="") or ($_POST['y']==""))
{
$error='EPA21';
return $error;
}
if((($_POST['x']<=-400) or ($_POST['x']>=400)) or (($_POST['y']<=-400) or ($_POST['y']>=400)))
{
$error='EPA25';
return $error;
}
if($es_capital==1)
{
$sql = "SELECT `es_capital`, `x`, `y` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`= \"".$_SESSION['usuario']."\" AND `es_capital`= 1";
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$num_row = mysql_num_rows($resultado);
if($num_row!=0)
{
$ac = mysql_fetch_assoc($resultado); // Sacamos la aldea capital para
// comprobar que no si se está
// editando o no dicha aldea
if ($ac['x']!=$_POST['aldeax'] and $ac['y']!=$_POST['aldeay'])
return 'EPA23';
}
}
if($es_principal==1)
{
$sql = "SELECT `es_principal`, `x`, `y` FROM `".$this->config->get('PREFIX')."aldea` WHERE `usuario_nombre`= \"".$_SESSION['usuario']."\" AND `es_principal`= 1";
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$num_row = mysql_num_rows($resultado);
if($num_row!=0)
{
$ap = mysql_fetch_assoc($resultado); // Sacamos la aldea principal para
// comprobar que no si se está
// editando o no dicha aldea
if ($ap['x']!=$_POST['aldeax'] and $ap['y']!=$_POST['aldeay'])
return 'EPA24';
}
}
$sql = "UPDATE `".$this->config->get('PREFIX')."aldea` SET
`x`=".$_POST['x'].",
`y`=".$_POST['y'].",
`nombre`=\"".$_POST['nombre']."\",
`tipo`=\"".$_POST['t_aldea']."\",
`es_capital`=".$es_capital.",
`es_principal`=".$es_principal.",
`descripcion`=\"".$_POST['descrip']."\",
`residencia`=".$_POST['n_rp'].",
`muralla`=".$_POST['muro'].",
`granero`=".$_POST['granero']."
WHERE `x`=".$_POST['aldeax']." AND `y`=".$_POST['aldeay'];
mysql_query($sql, $this->db) or die(mysql_error($this->db));
$this->actualizar();
header("Location: index.php?ctrl=Aldeas&mod=aldeas");
}
public function resumen()
{
$arreglo = array();
// primero obtenemos los datos de todos los usuarios activos
$sql = "SELECT `nombre`
FROM `".$this->config->get('PREFIX')."usuario`
WHERE `estado`='Activo'
ORDER BY `nombre` ASC";
$jugadores = mysql_query($sql, $this->db) or die(mysql_error($this->db));
$cont = 0;
while ( $player = mysql_fetch_assoc($jugadores) )
{
$arreglo[$cont] = array(
'player'=> $player['nombre'],
'15c'=> 0, '9c'=> 0, 'Normal'=> 0, 'Barro'=> 0, 'Hierro'=> 0,
'Madera'=> 0
);
// Obtenemos las aldeas de dicho jugador
$sql = "SELECT `tipo`
FROM `".$this->config->get('PREFIX')."aldea`
WHERE `usuario_nombre`=\"".$player['nombre']."\"";
$resultado = mysql_query($sql, $this->db) or die(mysql_error($this->db));
while ( $aldea = mysql_fetch_assoc($resultado) )
{
$arreglo[$cont][$aldea['tipo']] += 1;
}
++$cont;
mysql_free_result( $resultado );
}
mysql_free_result( $jugadores );
return $arreglo;
}
} // Fin clase
?>