fix: only 1 hero can exist and is deleted upon new hero training start

Closes #261
This commit is contained in:
Martin Ambrus
2017-11-02 11:05:26 +01:00
parent 3105e0754c
commit 7c67e1e7b9
14 changed files with 368 additions and 205 deletions
+3 -2
View File
@@ -19,7 +19,7 @@ foreach ($_POST as $key => $value) {
$_POST[$key] = $database->escape($value);
}
if(isset($_POST['id'])) {
if(isset($_POST['id']) && isset($_POST['hid'])) {
$_POST['hname'] = trim(stripslashes($_POST['hname']));
if ($_POST['hname']=="") {
header("Location: ../../../Admin/admin.php?p=editHero&uid=".$_POST['id']."&e=1");
@@ -29,9 +29,10 @@ if(isset($_POST['id'])) {
include_once("../../Data/hero_full.php");
$id = (int) $_POST['id'];
$hid = (int) $_POST['hid'];
$q = "UPDATE ".TB_PREFIX."hero SET unit=".(int) $_POST['hunit'].", name='".$_POST['hname']."', level=".(int) $_POST['hlvl'].", points=".(int) $_POST['exp'].", experience=".(int) $hero_levels[$_POST['hlvl']].", health='".$_POST['hhealth']."',
attack=".(int) $_POST['hatk'].", defence=".(int) $_POST['hdef'].", attackbonus=".(int) $_POST['hob'].", defencebonus=".(int) $_POST['hdb'].", regeneration=".(int) $_POST['hrege']." WHERE uid = ".$id;
attack=".(int) $_POST['hatk'].", defence=".(int) $_POST['hdef'].", attackbonus=".(int) $_POST['hob'].", defencebonus=".(int) $_POST['hdb'].", regeneration=".(int) $_POST['hrege']." WHERE heroid = ".$hid." AND uid = ".$id;
$return=$database->query($q);
if($return) {
$database->query("Insert into ".TB_PREFIX."admin_log values (0,".(int) $_SESSION['id'].",'Changed hero info',".time().")");
+9 -2
View File
@@ -92,10 +92,17 @@ class funct {
header("Location: admin.php?p=player&uid=".$get['uid'].$error);
exit;
case "reviveHero":
$result=$database->query("SELECT * FROM ".TB_PREFIX."hero WHERE uid=".(int) $get['uid']);
$livingHeroesCount = mysqli_fetch_array($database->query("SELECT Count(*) as Total FROM ".TB_PREFIX."hero WHERE uid=".(int) $get['uid']." AND (dead = 0 OR inrevive = 1)"), MYSQLI_ASSOC);
if ($livingHeroesCount['Total'] > 0) {
header("Location: admin.php?p=player&uid=".$get['uid']."&re=1");
exit;
}
$result=$database->query("SELECT * FROM ".TB_PREFIX."hero WHERE heroid = ".(int) $get['hid']." AND uid=".(int) $get['uid']);
$hdata=mysqli_fetch_array($result);
$database->query("UPDATE ".TB_PREFIX."units SET hero = 1 WHERE vref = ".(int) $hdata['wref']);
$database->query("UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".time()." WHERE `uid` = ".(int) $get['uid']);
$database->query("UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".time()." WHERE `heroid` = ".(int) $get['hid']." AND `uid` = ".(int) $get['uid']);
header("Location: admin.php?p=player&uid=".$get['uid']."&rc=1");
exit;
case "addHero":
+2 -2
View File
@@ -4280,11 +4280,11 @@ class Automation {
$villunits = $database->getUnit($hdata['wref']);
if($villunits['hero'] == 0 && $hdata['trainingtime'] < time() && $hdata['inrevive'] == 1){
mysqli_query($GLOBALS['link'],"UPDATE " . TB_PREFIX . "units SET hero = 1 WHERE vref = ".(int) $hdata['wref']."");
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".(int) $hdata['trainingtime']." WHERE `uid` = ".(int) $hdata['uid']);
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".(int) $hdata['trainingtime']." WHERE `heroid` = ".(int) $hdata['heroid']);
}
if($villunits['hero'] == 0 && $hdata['trainingtime'] < time() && $hdata['intraining'] == 1){
mysqli_query($GLOBALS['link'],"UPDATE " . TB_PREFIX . "units SET hero = 1 WHERE vref = ".(int) $hdata['wref']);
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."hero SET `intraining` = '0', `lastupdate` = ".(int) $hdata['trainingtime']." WHERE `uid` = ".(int) $hdata['uid']);
mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."hero SET `intraining` = '0', `lastupdate` = ".(int) $hdata['trainingtime']." WHERE `heroid` = ".(int) $hdata['heroid']);
}
}
}
+3 -3
View File
@@ -3709,15 +3709,15 @@ class MYSQLi_DB implements IDbConnection {
return $totalunits;
}
function getHero($uid=0,$all=0) {
function getHero($uid=0, $all=0, $include_dead = false) {
list($uid,$all) = $this->escape_input((int) $uid,$all);
if ($all) {
$q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid=$uid";
$q = "SELECT * FROM ".TB_PREFIX."hero WHERE uid=$uid ORDER BY lastupdate DESC";
} elseif (!$uid) {
$q = "SELECT * FROM ".TB_PREFIX."hero";
} else {
$q = "SELECT * FROM ".TB_PREFIX."hero WHERE dead=0 AND uid=$uid LIMIT 1";
$q = "SELECT * FROM ".TB_PREFIX."hero WHERE ".($include_dead ? '' : "dead=0 AND ")."uid=$uid LIMIT 1";
}
$result = mysqli_query($this->dblink,$q);
if (!empty($result)) {
+97 -9
View File
@@ -711,18 +711,106 @@ exit;
}
}
public function Hero($uid,$all=0) {
public function Hero($uid, $all = 0, $include_dead = false) {
global $database;
$heroarray = $database->getHero($uid,$all);
$herodata = $GLOBALS["h".$heroarray[0]['unit']];
$heroarray = $database->getHero($uid, $all, $include_dead);
$herodata = false;
$singleHeroArrayID = 0;
$h_atk = $herodata['atk'] + 5 * floor($heroarray[0]['attack'] * $herodata['atkp'] / 5);
$h_di = $herodata['di'] + 5 * floor($heroarray[0]['defence'] * $herodata['dip'] / 5);
$h_dc = $herodata['dc'] + 5 * floor($heroarray[0]['defence'] * $herodata['dcp'] / 5);
$h_ob = 1 + 0.002 * $heroarray[0]['attackbonus'];
$h_db = 1 + 0.002 * $heroarray[0]['defencebonus'];
// no hero data found
if (!count($heroarray)) {
return false;
}
return array('heroid'=>$heroarray[0]['heroid'],'unit'=>$heroarray[0]['unit'],'atk'=>$h_atk,'di'=>$h_di,'dc'=>$h_dc,'ob'=>$h_ob,'db'=>$h_db,'health'=>$heroarray[0]['health']);
// check all heroes and load hero data for the one,
// whose data were updated more recently - if we're not getting all of them
if (!$all) {
foreach ($heroarray as $id => $hero) {
// try to load a hero who's alive first
if (!$herodata && $hero['dead'] != 1) {
// this global value comes from GameEngine/Data/unitdata.php
$herodata = $GLOBALS["h".$hero['unit']];
$singleHeroArrayID = $id;
break;
}
}
// if we couldn't get a living hero,
// resort to loading the first one from the list,
// as that would be the one most recently updated/used
if (!$herodata) {
// this global value comes from GameEngine/Data/unitdata.php
$herodata = $GLOBALS["h".$heroarray[0]['unit']];
}
$h_atk = $herodata['atk'] + 5 * floor($heroarray[$singleHeroArrayID]['attack'] * $herodata['atkp'] / 5);
$h_di = $herodata['di'] + 5 * floor($heroarray[$singleHeroArrayID]['defence'] * $herodata['dip'] / 5);
$h_dc = $herodata['dc'] + 5 * floor($heroarray[$singleHeroArrayID]['defence'] * $herodata['dcp'] / 5);
$h_ob = 1 + 0.002 * $heroarray[$singleHeroArrayID]['attackbonus'];
$h_db = 1 + 0.002 * $heroarray[$singleHeroArrayID]['defencebonus'];
return [
'heroid' => $heroarray[$singleHeroArrayID]['heroid'],
'unit' => $heroarray[$singleHeroArrayID]['unit'],
'name' => $heroarray[$singleHeroArrayID]['name'],
'inrevive' => $heroarray[$singleHeroArrayID]['inrevive'],
'intraining' => $heroarray[$singleHeroArrayID]['intraining'],
'trainingtime' => $heroarray[$singleHeroArrayID]['trainingtime'],
'level' => $heroarray[$singleHeroArrayID]['level'],
'attack' => $heroarray[$singleHeroArrayID]['attack'],
'atk' => $h_atk,
'defence' => $heroarray[$singleHeroArrayID]['defence'],
'di' => $h_di,
'dc' => $h_dc,
'attackbonus' => $heroarray[$singleHeroArrayID]['attackbonus'],
'ob' => $h_ob,
'defencebonus' => $heroarray[$singleHeroArrayID]['defencebonus'],
'db' => $h_db,
'regeneration' => $heroarray[$singleHeroArrayID]['regeneration'],
'health' => $heroarray[$singleHeroArrayID]['health'],
'dead' => $heroarray[$singleHeroArrayID]['dead'],
'points' => $heroarray[$singleHeroArrayID]['points'],
'experience' => $heroarray[$singleHeroArrayID]['experience']
];
} else {
// build up a full array of heroes and their stats
$heroes = [];
foreach ($heroarray as $id => $hero) {
$herodata = $GLOBALS["h".$heroarray[$id]['unit']];
$h_atk = $herodata['atk'] + 5 * floor($heroarray[$id]['attack'] * $herodata['atkp'] / 5);
$h_di = $herodata['di'] + 5 * floor($heroarray[$id]['defence'] * $herodata['dip'] / 5);
$h_dc = $herodata['dc'] + 5 * floor($heroarray[$id]['defence'] * $herodata['dcp'] / 5);
$h_ob = 1 + 0.002 * $heroarray[$id]['attackbonus'];
$h_db = 1 + 0.002 * $heroarray[$id]['defencebonus'];
$heroes[] = [
'heroid' => $heroarray[$id]['heroid'],
'unit' => $heroarray[$id]['unit'],
'name' => $heroarray[$id]['name'],
'inrevive' => $heroarray[$id]['inrevive'],
'intraining' => $heroarray[$id]['intraining'],
'trainingtime' => $heroarray[$id]['trainingtime'],
'level' => $heroarray[$id]['level'],
'attack' => $heroarray[$id]['attack'],
'atk' => $h_atk,
'defence' => $heroarray[$id]['defence'],
'di' => $h_di,
'dc' => $h_dc,
'attackbonus' => $heroarray[$id]['attackbonus'],
'ob' => $h_ob,
'defencebonus' => $heroarray[$id]['defencebonus'],
'db' => $h_db,
'regeneration' => $heroarray[$id]['regeneration'],
'health' => $heroarray[$id]['health'],
'dead' => $heroarray[$id]['dead'],
'points' => $heroarray[$id]['points'],
'experience' => $heroarray[$id]['experience']
];
}
return $heroes;
}
}
};