quests util


/**
* Utility package to deal with quests.
*/

/**
* Error handler used with the dialogs
*
* @param integer $errno
* @param integer $errmsg
* @param string $filename
* @param integer $linenum
* @param array $vars
*/
function quest_error($errno, $errmsg, $filename, $linenum, $vars)
{
global $macroname;

if($errno == 8)
return;

echo "Error in the quest macro: '$macroname'.
You should check if you provide all the needed parameters.

\n";
}

/**
* Evaluate a PHP code (runs with some global variable accessible)
*
* @param string $code
*/
function my_eval($code)
{
global $username,$db,$userid,$uservals;

eval($code);
}

/**
* Show a dialog.
*/
function do_discussion()
{
global $db,$uservals,$quest,$userid,$titles,$basedir;
global $macroname;
global $havequery;
global $dialog,$npc;

include_once "$basedir/combat/combats_util.php";

$todebug=false;
$havequery=false;

$r=$db->Execute("SELECT MANAGER,NAME,EDITGROUP FROM LOCATIONS WHERE ID = ".$uservals["LOCATION"]);
$manager=$r->fields[0];
$locname=$r->fields[1];
$editgroup=$r->fields[2];
$r->Close();

$canedit=false;
if($manager == $userid)
$canedit=true;
else if($manager != 0 && $editgroup != 0)
{
$r=$db->Execute("SELECT USERID FROM EDITORS WHERE USERID=$userid AND EDITGROUP=".$editgroup);
if(!$r->EOF)
$canedit=true;
$r->Close();
}

$npc=$_GET["TALK"];
$dialog=$_GET["DIALOG"]+0;
if($dialog == 0)
$uservals["PREVDIALOG"]=0;

if($userid != 1)
$old_error_handler = set_error_handler("quest_error");

$hasright=true;

$r=$db->Execute("SELECT COUNT(USERID) FROM DIALOGACCESS WHERE USERID = $userid AND DIALOGID = $dialog");
$nb=$r->fields[0]+0;
$r->Close();

// Check if the user have the right to excute this dialog

if($dialog != 0 && $nb < 1 && $uservals["ONFINISH"] == "" && count($_POST) == 0)
{
if($todebug)
echo "No lines in the dialogaccess table
\n";
if($canedit)
echo "Warning: you access this dialog either from a link with a wrong \"TO ID\" or you did some other strange things as no actions will be executed on this dialog (due to security restrictions).
\n";
$hasright=false;
}

if(count($_POST) > 0)
$hasright=true;

if($userid == 1 && $todebug == true)
echo "Hasright: ".($hasright==true?"True":"False")."
\n";

$uservals["PREVDIALOG"]=$dialog;
$db->Execute("UPDATE PLAYER SET PREVDIALOG=$dialog,ONFINISH='' WHERE ID = $userid");
$db->Execute("DELETE FROM DIALOGACCESS WHERE USERID = $userid");

// Main text and code
$r=$db->Execute("SELECT CONTENT, DOCODE FROM DIALOGS WHERE NPC = ".$db->qstr($npc)." AND DIALOGID = $dialog AND LOCATION = {$uservals['LOCATION']}");
if($r->EOF)
{
$r->Close();
echo "Wrong dialog info
\n";
return;
}

if($_GET["INMAP"] == "TRUE")
{
echo "";
echo "\n";
echo "
".$locname."
\n";
}
else
{
echo "
";
echo "\n";
echo "
".$locname."
";
}

if($uservals["SEX"] == 'F')
echo preg_replace('/(\W+)[sS]ir(\W+)/','\1Dame\2',str_replace(array('$NAME','$TITLE'),array($uservals["USERNAME"],$titles[$uservals['SEX']][$uservals['TITLE']]),$r->fields[0]))."
\n";
else
echo str_replace(array('$NAME','$TITLE'),array($uservals["USERNAME"],$titles[$uservals['SEX']][$uservals['TITLE']]),$r->fields[0])."
\n";
if($r->fields[1] != "" && $hasright == true)
my_eval($r->fields[1]);

$r->Close();

$needtostop=false;

if($hasright)
{
// Eval code from the attached table
$sql="SELECT DIALOGCODE.LINE,DIALOGCOMMANDS.CODE,
DIALOGCODE.PARAM1,DIALOGCODE.PARAM2,
DIALOGCODE.PARAM3,DIALOGCODE.PARAM4,
DIALOGCOMMANDS.TYPE, DIALOGCOMMANDS.NAME
FROM DIALOGCODE,DIALOGCOMMANDS WHERE DIALOGCODE.LOCATION={$uservals['LOCATION']}
AND DIALOGCODE.NPC = ".$db->qstr($npc)." AND DIALOGCODE.DIALOGID = $dialog
AND DIALOGCODE.COMMAND = DIALOGCOMMANDS.ID ORDER BY DIALOGCODE.LINE";

$r=$db->Execute($sql);
while(!$r->EOF)
{
if($canedit == false || $r->fields[6] != "N")
{
$macroname=$r->fields[7];

$code=str_replace(array("@1","@2","@3","@4"),array(str_replace("\"","\\\"",$r->fields[2]),str_replace("\"","\\\"",$r->fields[3]),str_replace("\"","\\\"",$r->fields[4]),str_replace("\"","\\\"",$r->fields[5])),$r->fields[1]);
if($userid == 1 && $todebug == true)
echo "$code
\n";
eval($code);
if($r->fields[6] == "S" && count($_POST) == 0)
{
$needtostop=true;
if($userid == 1 && $todebug == true)
echo "Need to stop...
\n";
}
}
$r->MoveNext();
}
$r->Close();
}

if($needtostop)
{
return;
}

if($havequery)
{
echo "
\n";
if($userid != 1)
set_error_handler($old_error_handler);
}
// Dialog choices
echo "\n";
if($_GET["INMAP"] != "TRUE")
echo "\n";
if($userid != 1)
set_error_handler($old_error_handler);
}

/**
* Checks if a quest condition is met or not.
*
* @param string $table
* @param string $where
* @return boolean
*/
function macro_check($table,$where)
{
global $db,$userid,$uservals;

// Eval code from the attached table
$sql="SELECT DIALOGCHECKS.CODE,DIALOGCHECKS.CODE,
$table.PARAM1,$table.PARAM2,
$table.PARAM3,$table.PARAM4, DIALOGCHECKS.NAME
FROM $table,DIALOGCHECKS WHERE $table.COMMAND = DIALOGCHECKS.ID AND $where";

$code="";
$macroname="";
$r2=$db->Execute($sql);
while(!$r2->EOF)
{
if($code != "")
{
$code.=" && ";
$macroname.=" AND ";
}
$macroname.=$r2->fields[6];
$code.=str_replace(array("@1","@2","@3","@4"),array($r2->fields[2],$r2->fields[3],$r2->fields[4],$r2->fields[5]),$r2->fields[1]);
$r2->MoveNext();
}
$r2->Close();

if($code != "")
{
$res=false;
$code='if ('.$code.') $res=true;';
if($uservals["USERNAME"] == "test")
echo "$code
\n";
eval($code);
}
else
$res=true;

return $res;
}

/**
* Deprecated function.
*
* @param unknown_type $quest
* @param unknown_type $var
* @param unknown_type $part
* @param unknown_type $val
*/
function set_qv_part($quest,$var,$part,$val)
{
$str=get_quest_variable($quest,$var);
$str=set_quest_var_part($str,$part,$val);
set_quest_variable($quest,$var,$str);
}

/**
* Deprecated function
*
* @param unknown_type $quest
* @param unknown_type $var
* @param unknown_type $part
* @return unknown
*/
function get_qv_part($quest,$var,$part)
{
$str=get_quest_variable($quest,$var);
return get_quest_var_part($str,$part);
}

/**
* Deprecated function
*
* @param unknown_type $var
* @param unknown_type $part
* @param unknown_type $val
* @return unknown
*/
function set_quest_var_part($var,$part,$val)
{
if($part == 0)
return $val.substr($var,$part+1);
else if($part >= strlen($var))
{
$var=str_pad($var,$part,"0");
return substr($var,0,$part).$val.substr($var,$part+1);
}
else
return substr($var,0,$part).$val.substr($var,$part+1);
}

/**
* Deprecated function
*
* @param unknown_type $var
* @param unknown_type $part
* @return unknown
*/
function get_quest_var_part($var,$part)
{
return substr($var,$part,1);
}

/**
* Set the experience, level
*
* @param integer $exp
*/
function macro_gain_experience($exp)
{
global $db,$uservals,$basedir;

$r=$db->Execute("SELECT IP,USERNAME,LOCATIONS.MANAGER FROM PLAYER,LOCATIONS WHERE LOCATIONS.MANAGER=PLAYER.ID AND LOCATIONS.ID = ".$uservals["LOCATION"]);
$fromip=$r->fields[0];
$fromname=$r->fields[1];
$fromid=$r->fields[2];
$r->Close();

if($fromip == $uservals["IP"] && $fromip != "")
$db->Execute("INSERT INTO CROSS_EXPLOIT(FROMPLAYER,TOPLAYER,MESSAGE) VALUES($userid,$fromid,".$db->qstr("$username received via a quest managed by $fromname $exp Exp ( both using IP $fromip ).").")");

$l=$uservals['LEVEL']+0;
include "$basedir/libs/level_rules.php";

$dif=$max-$min;

if($exp > $dif/2 && $exp > 5000)
$exp=ceil($dif/2);
if(($exp+0) > 0)
echo "You gained $exp EXP.
\n";
else
echo "You lost ".(-$exp)." EXP.
\n";
gain_experience($exp);
}

/**
* Give some experience to a player
*
* @param integer $exp
* @param bool $display
* @return string
*/
function gain_experience($exp,$display=true)
{
global $db,$uservals,$userid,$basedir;

$res="";

$uservals["EXPERIENCE"]+=$exp;
if($uservals["EXPERIENCE"] < 0)
$uservals["EXPERIENCE"]=0;

$levelchaned=false;
while(true)
{
$l=$uservals['LEVEL']+0;
include "$basedir/libs/level_rules.php";

if(($uservals["EXPERIENCE"]+0) < $min)
{
$uservals["LEVEL"]--;
$res.="You lost a level ! You are now level ".$uservals["LEVEL"].".
";
continue;
}
else if(($uservals["EXPERIENCE"]+0) >= $max)
{
$uservals["LEVEL"]++;
$res.="You gained a level ! You are now level ".$uservals["LEVEL"].".
";
$levelchanged=true;
continue;
}
else
break;
}
if($levelchanged)
{
$red.="As your level increases, remember to assign stats points to your different stats.
";
}
$db->Execute("UPDATE PLAYER SET EXPERIENCE=".$uservals["EXPERIENCE"].", LEVEL=".$uservals["LEVEL"].", GOLD=".$uservals["GOLD"]." WHERE ID = $userid");

if($display)
echo "$res\n";

return $res;
}

/**
* Give some experience to a player
*
* @param integer $userid
* @param integer $exp
*/
function gain_user_experience($userid,$exp)
{
global $db,$basedir;

$uservals=read_db_entry($db,"SELECT * FROM PLAYER WHERE ID = $userid AND AUTHORIZED = 'Y'");

$uservals["EXPERIENCE"]+=$exp;
if($uservals["EXPERIENCE"] < 0)
$uservals["EXPERIENCE"]=0;

$levelchaned=false;
while(true)
{
$l=$uservals['LEVEL']+0;
//$min=($l-1)*($l-1)*($l-1)*100+($l-1)*200+round(floor(($l-1)/20)*($l-1)*($l-1)*1000*(($l-1)-20)/1.8);
//$max=(($l*$l*$l)*100)+$l*200+round(floor($l/20)*$l*$l*1000*($l-20)/1.8);
include "$basedir/libs/level_rules.php";

if(($uservals["EXPERIENCE"]+0) < $min)
{
$uservals["LEVEL"]--;
continue;
}
else if(($uservals["EXPERIENCE"]+0) >= $max)
{
$uservals["LEVEL"]++;
continue;
}
else
break;
}
$db->Execute("UPDATE PLAYER SET EXPERIENCE=".$uservals["EXPERIENCE"].", LEVEL=".$uservals["LEVEL"].", GOLD=".$uservals["GOLD"].",LASTACTION = LASTACTION WHERE ID = $userid");
}

/**
* Gives some gold to the player. Limited to 200'000
*
* @param integer $gold
*/
function gain_gold($gold)
{
global $db,$userid,$uservals;
global $dialog,$npc;

$r=$db->Execute("SELECT IP,USERNAME,LOCATIONS.MANAGER FROM PLAYER,LOCATIONS WHERE LOCATIONS.MANAGER=PLAYER.ID AND LOCATIONS.ID = ".$uservals["LOCATION"]);
$fromip=$r->fields[0];
$fromname=$r->fields[1];
$fromid=$r->fields[2];
$r->Close();

if($gold > 200000)
$gold=200000;

if($fromip == $uservals["IP"] && $fromip != "")
$db->Execute("INSERT INTO CROSS_EXPLOIT(FROMPLAYER,TOPLAYER,MESSAGE) VALUES($userid,$fromid,".$db->qstr("$username received via a quest managed by $fromname $gold Gold ( both using IP $fromip ).").")");

if(($gold+0) > 0)
echo "You received $gold Gold.
\n";
else
echo "You lost ".(-$gold)." Gold.
\n";

$uservals["GOLD"]+=$gold;
$db->Execute("UPDATE PLAYER SET GOLD=".$uservals["GOLD"]." WHERE ID = $userid");
}

/**
* Add a journal entry.
*
* @param integer $quest
* @param string $entry
*/
function add_journal($quest,$entry)
{
global $db,$userid,$uservals;

echo "Journal updated.
\n";
$db->Execute("INSERT INTO JOURNAL(QUEST,USERID,LOCATION,ENTRY) VALUES($quest,$userid,".$uservals["LOCATION"].",".$db->qstr($entry).")");
}

/**
* Define a given quest as finished.
*
* @param integer $quest
*/
function finish_quest($quest)
{
global $db,$userid;

$db->Execute("INSERT INTO FINISHEDQUESTS(USERID,QUESTID) VALUES($userid,$quest)");
}

/**
* Check if a quest as been started.
*
* @param integer $quest
* @return boolean
*/
function is_quest_started($quest)
{
global $db,$userid;

$r=$db->Execute("SELECT COUNT(ENTRY) FROM JOURNAL WHERE QUEST = $quest AND USERID = $userid");
$nb=$r->fields[0]+0;
$r->Close();

if($nb == 0)
{
$r=$db->Execute("SELECT COUNT(VARID) FROM QUESTVARIABLES WHERE QUESTID = $quest AND USERID = $userid");
$nb+=$r->fields[0]+0;
$r->Close();
}

if($nb > 0)
return true;
else
return false;
}

/**
* Checks if a quest has been finished.
*
* @param integer $quest
* @return boolean
*/
function is_quest_finished($quest)
{
global $db,$userid;

$r=$db->Execute("SELECT COUNT(QUESTID) FROM FINISHEDQUESTS WHERE QUESTID = $quest AND USERID = $userid");
$nb=$r->fields[0]+0;
$r->Close();

if($nb == 0)
return false;
else
return true;
}

/**
* Returns true on the first call, and then false;
* Used to show only 1 of the dialog link.
*
* @return boolean
*/
function first_item()
{
static $firstitem=true;

if($firstitem)
{
$firstitem=false;
return true;
}
return false;
}

/**
* Returns a number between 0-100. On mulitple runs it will return the same number.
*
* @return integer
*/
function random_number()
{
static $firstitem=true;
static $rand=0;

if($firstitem)
{
$firstitem=false;
$rand=mt_rand(0,100);
}
return $rand;
}

/**
* Returns the count of the monster of this type killed by the player.
*
* @param integer $monsterid
* @return integer
*/
function monster_kills($monsterid)
{
global $db,$userid;

$r=$db->Execute("SELECT NB FROM TABLE_HUNTING WHERE USERID = $userid AND MONSTERID = $monsterid");
$nb=$r->fields[0]+0;
$r->Close();

return $nb;
}

/**
* Change player alignement.
*
* @param integer $val
*/
function change_alignement($val)
{
global $db,$userid,$uservals;

$uservals["ALIGNEMENT"]+=$val;
if($uservals["ALIGNEMENT"] < 0)
$uservals["ALIGNEMENT"]=0;
if($uservals["ALIGNEMENT"] > 80)
$uservals["ALIGNEMENT"]=80;

$db->Execute("UPDATE PLAYER SET ALIGNEMENT = ".$uservals["ALIGNEMENT"]." WHERE ID = ".$userid);
}

/**
* Transfer the player to a new location.
*
* @param integer $loc
*/
function change_location($loc)
{
global $db,$userid,$uservals;

$uservals["LOCATION"]=$loc;
$db->Execute("UPDATE PLAYER SET LOCATION = ".$uservals["LOCATION"]." WHERE ID = ".$userid);
}

/**
* Changes player HP.
*
* @param integer $nb
*/
function add_hp($nb)
{
global $db,$userid,$uservals;

if(($nb+0) > 0)
echo "You recovered $nb HP.
\n";
else
echo "You lost $nb HP.
\n";
$uservals["HP"]+=$nb;
if($uservals["HP"] > ($uservals["MAXHP"]+$uservals["HPMOD"]+$uservals["TEMP_HP"]))
$uservals["HP"]=($uservals["MAXHP"]+$uservals["HPMOD"]+$uservals["TEMP_HP"]);
if($uservals["HP"] < 1)
{
$uservals["HP"]=1;
echo "You died.
\n";
$db->Execute("UPDATE PLAYER SET LOCATION = RESURECT WHERE ID = ".$userid);
}
$db->Execute("UPDATE PLAYER SET HP = ".$uservals["HP"]." WHERE ID = ".$userid);
}

/**
* Changes the player MP.
*
* @param integer $nb
*/
function add_mp($nb)
{
global $db,$userid,$uservals;

if(($nb+0) > 0)
echo "You recovered $nb MP.
\n";
$uservals["MP"]+=$nb;
if($uservals["MP"] > ($uservals["MAXMP"]+$uservals["MPMOD"]+$uservals["TEMP_MP"]))
$uservals["MP"]=($uservals["MAXMP"]+$uservals["MPMOD"]+$uservals["TEMP_MP"]);
if($uservals["MP"] < 0)
$uservals["MP"]=0;
$db->Execute("UPDATE PLAYER SET MP = ".$uservals["MP"]." WHERE ID = ".$userid);
}

/**
* Kills the player.
*/
function kill_player()
{
global $db,$userid,$uservals;

echo "You died.
\n";
$uservals["HP"]=1;
$db->Execute("UPDATE PLAYER SET HP = 1, LOCATION = RESURECT WHERE ID = ".$userid);
}

/**
* Start a combat with the specified monster.
* The $onfinish parameter will be used as "callback"
* link once the combat is finished.
*
* @param integer $monster
* @param string $onfinish
*/
function monster_combat($monster,$onfinish)
{
global $db,$userid,$uservals,$basedir;

ob_end_clean();
ob_start();
include_once "$basedir/libs/combats_util.php";

$db->Execute("UPDATE PLAYER SET ONFINISH = ".$db->qstr($onfinish)." WHERE ID = $userid");
start_combat($monster);
}

/**
* Ask a question during a dialog.
*
* @param string $str
* @param integer $id
*/
function add_query_box($str,$id)
{
global $havequery;

if($_POST["ANSWER"] <> "")
return;

echo "
\n";
echo "$str:
\n";
echo "
\n";
$havequery=true;
}

/**
* Changes the "sickness" of a player
*
* @param integer $s
*/
function change_sickness($s)
{
global $db,$userid,$uservals;

if(($s+0) < 0)
$s=0;
if(($s+0) > 200)
$s=200;

$db->Execute("UPDATE PLAYER SET SICKNESS=$s WHERE ID = $userid");
}

/**
* Gives a blueprint to the player.
*
* @param integer $id object ID
*/
function blueprint_add($id)
{
global $db,$userid;

if(is_craftable($id))
$db->Execute("INSERT INTO PLAYER_BLUEPRINTS(USERID,OBJECTID) VALUES($userid,$id)");
}

/**
* Checks if the player is member of a given clan.
*
* @param integer $id
* @return boolean
*/
function is_member($id)
{
global $db,$userid;

$r=$db->Execute("SELECT ROLE FROM CLAN_MEMBERS WHERE USERID = $userid AND CLANID = $id");
if ($r->fields[0]=="")
$role=-1;
else
$role=$r->fields[0]+0;
$r->Close();

if($role >= 0)
return true;
else
return false;
}

/**
* Blocks most of the menu options.
*
*/
function block_gui()
{
global $db,$userid,$uservals;

$db->Execute("UPDATE PLAYER SET GUIBLOCKED='Y' WHERE ID=$userid");
$uservals["GUIBLOCKED"]="Y";
}

/**
* Unblocks the menu.
*
*/
function unblock_gui()
{
global $db,$userid,$uservals;

$db->Execute("UPDATE PLAYER SET GUIBLOCKED='N' WHERE ID=$userid");
$uservals["GUIBLOCKED"]="N";
}

/**
* Checks if a date is between a specified dd/mm
*
* @param integer $ds
* @param integer $ms
* @param integer $de
* @param integer $me
* @return boolean
*/
function is_date_in_between($ds,$ms,$de,$me)
{
$now=getdate();
$cur=$now["mon"]*31+$now["mday"];
$s=$ds+$ms*31;
$e=$de+$me*31;
if($cur >= $s && $cur <= $e)
return true;
return false;
}

/**
* Check if the date is exactly at a specified dd/mm
*
* @param unknown_type $d
* @param unknown_type $m
* @return unknown
*/
function is_date($d,$m)
{
$now=getdate();
if($now["mon"]*31+$now["mday"] == $d+$m*31)
return true;
return false;
}
?>


Wyszukiwarka

Podobne podstrony:
quests util
quests util
Util js
group util ?lay
TSM UTIL
java util MissingResourceException
java util Properties
java util ListResourceBundle
map util
Package java util zip
java util zip Adler32
java util zip ZipInputStream
java util zip DeflaterOutputStream
image map util
java util zip InflaterInputStream
java util zip GZIPOutputStream
java util Vector
java util NoSuchElementException
java util zip CheckedOutputStream

więcej podobnych podstron