<?php
/**
* motore Quest
*
* @author pagliaccio
*/
class Quest {
static $maxquest=6;
var $parser;
var $n = 0;
var $age = 0;
var $save_quest = false;
var $save_quest2 = false;
var $CONDICTION = array();
var $MASTER = null;
var $TITLE = null;
var $TEXT = null;
var $BACK = null;
var $REWARD = null;
var $tag;
var $c = 0;
var $chache = false;
var $read = false;
var $bool = false;
var $completed = false;
/**
* effettua costruisce le quest
*/
function __construct() {
global $fw;
$this->age = $fw->civ->getAge();
$this->n = $fw->civ->data->quest;
$this->MASTER = $fw->civ->data->master;
$this->parser = xml_parser_create();
$quest = $fw->db->sql_toarray("SELECT * FROM `" . QUEST_TABLE . "` WHERE `age`='" . $this->age . "' AND `n`='" . $this->n . "'");
if ($quest) {
$this->chache = true;
$this->MASTER = $quest['master'];
$this->TITLE = $quest['title'];
$this->TEXT = $quest['text'];
$this->BACK = $quest['back'];
$this->CONDICTION = unserialize($quest['condiction']);
$this->REWARD = unserialize($quest['reward']);
$this->read = ($fw->civ->data->read_quest ? true : false );
}
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
}
/**
* effettua il parsing delle quest
* @param Object $data
*/
function parse() {
$data = file_get_contents(PATH . "includes/xml/quest.xml");
xml_parse($this->parser, $data);
}
/**
*
* @param Resource $parser
* @param String $tag
* @param mixed $attributes
*/
function tag_open($parser, $tag, $attributes) {
$this->tag = $tag;
//debug("attribute", $attributes, "quest", 66, $this->bool);
//debug("tag", $tag, "quest", 67, $this->bool);
if (($attributes['ID'] == $this->age) && ($tag == "AGE"))
$this->save_quest2 = true;
if (($attributes['N'] == $this->n) && ($tag == "QUEST") && ($attributes['MASTER'] == $this->MASTER))
$this->save_quest = true;
elseif ($this->save_quest && $this->save_quest2) {
if ($tag == "CONDICTION")
$this->CONDICTION = $attributes;
if ($tag=="REWARD")
$this->REWARD=$attributes;
}
//debug("bool", $this->save_quest, "quest", 73, $this->bool);
}
/**
*
* @param Resource $parser
* @param mixed $cdata
*/
function cdata($parser, $cdata) {
$tag = $this->tag;
if ($this->save_quest && $this->save_quest2) {
if ($this->tag == "CONDICTION") {
if ($this->CONDICTION['PARAM'] == null)
$this->CONDICTION['PARAM'] = $cdata;
}
elseif ($this->tag == "REWARD") {
if ($this->REWARD['data'] == null)
$this->REWARD['data']=$cdata;
}
elseif (($this->tag != "AGE") && ($this->tag != "QUEST") && ($tag != "ROOT")&& ($tag != "REWARD")) {
if ($this->$tag == null) {
$this->$tag = $cdata;
}
}
}
}
/**
*
* @param Resourse $parser
* @param String $tag
*/
function tag_close($parser, $tag) {
if ($tag == "QUEST")
$this->save_quest = false;
if ($tag == "AGE")
$this->save_quest2 = false;
}
/**
* visualizza le quest e il questmaster
*/
function ShowQuest() {
global $fw;
debug("chache", $this->chache, "quest", 115, $this->bool);
if ($this->n>Quest::$maxquest) {
$this->TITLE="Le quest sono finite...";
$this->TEXT="...per ora...";
}
if (!$this->chache) {
$this->parse();
$fw->db->sql_query("INSERT INTO `" . QUEST_TABLE . "` SET `age`='" . $this->age . "' ,
`n`='" . $this->n . "' , `title`='" . $fw->db->t($this->TITLE, "html") . "' , `text`='" . $fw->db->t($this->TEXT, "html") . "' ,
`master`='" . $this->MASTER . "' , `back`='" . $fw->db->t($this->BACK, "html") . "' ,
`condiction`='" . serialize($this->CONDICTION) . "' , `reward`='" . serialize($this->REWARD) . "'") or die(mysql_error());
}
if ($this->control()&&($this->read)) {
$this->getreward();
echo '<div id="quest" class="quest"><h1>' . __('Obbiettivo Completato') . '</h1><div style="min-height: 80%;">' . $this->BACK . '</div><div style="text-align: right;"><button onclick="$(\'#quest\').hide();$(\'#fade\').hide()">chiudi</button></div></div>';
$fw->db->sql_query("UPDATE `" . CIV_TABLE . "` SET `read_quest`='0' , `quest`=`quest`+'1' where `civ_id`='".$fw->civ->cid."'");
$fw->civ->data->read_quest=false;
}
else {
echo '<div id="quest" class="quest"><h1>' . $this->TITLE . '</h1><div style="min-height: 80%;">' . $this->TEXT . '</div><div style="text-align: right;"><button onclick="$(\'#quest\').hide();$(\'#fade\').hide()">chiudi</button></div></div>';
if (!$this->read)
$fw->db->sql_query("UPDATE `" . CIV_TABLE . "` SET `read_quest`='1' where `civ_id`='".$fw->civ->cid."'");
}
}
/**
* controlla se i requisiti sono stati completati
* return bool
*/
function control() {
global $Building_Array,$fw;
debug("condition", $this->CONDICTION, "quest", 148, $this->bool);
$b = $this->CONDICTION['VALUE'];
if (in_array($this->CONDICTION['PARAM'], $Building_Array)) {
for ($i = 0; ($Building_Array[$i]) && ($Building_Array[$i] != $this->CONDICTION['PARAM']); $i++);
debug("i", $i, "quest", 151, $this->bool);
$pos = $fw->civ->building->getBildForType($i+1);
$a = ($pos < 0 ? false : $fw->civ->building->getLiv($pos) );
}
else {
switch ($this->CONDICTION['PARAM']) {
case "village": $a = $fw->civ->village->name;
break;
case "prod" : if (substr($this->CONDICTION['TYPE'], 0, 1)=="L") {
$pos = $fw->civ->building->getBildForType(PROD1);
$a = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
$pos = $fw->civ->building->getBildForType(PROD2);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a<$t) $a=$t;
$pos = $fw->civ->building->getBildForType(PROD3);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a<$t) $a=$t;
}
else {
$pos = $fw->civ->building->getBildForType(PROD1);
$a = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
$pos = $fw->civ->building->getBildForType(PROD2);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a>$t) $a=$t;
$pos = $fw->civ->building->getBildForType(PROD3);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a>$t) $a=$t;
}
break;
case "storage" : if (substr($this->CONDICTION['TYPE'], 0, 1)=="L") {
$pos = $fw->civ->building->getBildForType(STORAGE1);
$a = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
$pos = $fw->civ->building->getBildForType(STORAGE2);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a<$t) $a=$t;
}
else {
$pos = $fw->civ->building->getBildForType(STORAGE1);
$a = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
$pos = $fw->civ->building->getBildForType(STORAGE2);
$t = ($pos < 0 ? 0 : $fw->civ->building->getLiv($pos) );
if ($a>$t) $a=$t;
}
break;
case "attack" :
$a=$fw->civ->option->get("attack");
default : debug("errore", $this->CONDICTION['PARAM'], "quest", 179, $this->bool);
}
}
debug("a", $a, "quest", 161, $this->bool);
debug("b", $b, "quest", 162, $this->bool);
switch ($this->CONDICTION['TYPE']) {
case "NE":return ($a != $b);
break;
case "EX":return $a;
break;
case "GE":return ($a >= $b);
break;
case "LE":return ($a <= $b);
break;
case "G":return ($a > $b);
break;
case "L":return ($a < $b);
break;
case "EQ":return ($a = $b);
break;
default:return false;
break;
}
}
/**
* applica la riconpensa
*/
function getreward() {
global $fw;
switch ($this->REWARD['TYPE'])
{
case "RES": $fw->civ->aggResource(explode(",", $this->REWARD['data']),true);
break;
case "TR" : $tr=explode(",", $this->REWARD['data']);
$fw->db->sql_query("INSERT INTO `".TROOPERS."` SET `trooper_id`='".$tr[0]."'
, `numbers`='".$tr[1]."' , `civ_id`='".$fw->civ->cid."'
, `village_now`='".$fw->civ->getCurrentVillage()."'
, `village_prev`='".$fw->civ->getCurrentVillage()."'");
case "EV": $fw->db->sql_query("UPDATE `".CIV_TABLE."` SET `ev_ready`='1' WHERE `civ_id`='".$fw->civ->cid."'");
default : debug("errore", $this->REWARD, "quest", 250, $this->bool);
}
}
}
/* $dom = new DOMDocument;
$dom->Load('../xml/quest.xml');
if ($dom->validate()) {
echo "<h1>This document is valid! </h1>";
}
else echo "<h1>no!</h1>";
$q = new Quest(1, 1);
$q->parse();
print_r($q);
$print = htmlentities($data);
$print = str_replace("\n", "<br>", $print);
echo "<p>" . $print . "</p>"; */
?>