<?php
$footer = "<a href='welcome.php'>return to main screen</a><br /></body></html>";
session_start();

if(!isset($_SESSION['uid']))
{
    echo "not logged<br />" . $footer;
    exit;
}
if(!isset($_GET['character']))
{
/*select character by session*/
    echo "who attack?<br />" . $footer;
    exit;
}
if(!isset($_GET['monster']))
{
    echo "no monster selected<br />" . $footer;
    exit;
}
if(!isset($_GET['item']))
{
    echo "attack with what ?<br />" . $footer;
    exit;
}

$uid = $_SESSION["uid"];
$cid = $_GET['character'];
$mid = $_GET["monster"];
$pid = $_GET["item"];
require('conf.php');
$item = mysql_fetch_array(mysql_query("SELECT * FROM items WHERE id = '" . $pid . "'"));
/*does item have owner*/
if($item['owner'] != $cid)
{
    /*perfer inventory based than owner based*/
    /*try to implement an inventory that hold item ID*/
    echo "item does not belong to the attacker<br />" . $footer;
    exit;
}
/*does character belong to user*/
$character = mysql_fetch_array(mysql_query("SELECT * FROM characters WHERE id = '" . $cid . "'"));
if($uid != $character['uid'])
{
    echo "character does not belong to the user<br />" . $footer;
    exit;
}
/*do monster exist*/
$monster = mysql_fetch_array(mysql_query("SELECT * FROM characters WHERE id = '" . $mid . "'"));
if($monster == NULL)
{
    echo "monster does not exist";
    exit;
}
/*is there a clone of the monster*/
$clone = mysql_fetch_array(mysql_query("SELECT * FROM characters WHERE clone = '" . $mid . "' AND uid = '".$uid."'"));
if($clone == NULL)
{
    /*here build a clone*/
    echo "no clone present<br />" . $footer;
    /*dont forget to update this if change is made on character database*/
    /*i added (x y map) variable to character but there is no use in a clone yet so i have not updated the copy code*/
    mysql_query("INSERT INTO characters(`name`, `sprite`, `hp`, `xp`, `force`, `speed`, `agility`, `psychic`, `fire`, `cold`, `storm`, `sonic`, `nercotic`, `poison`, `acid`) SELECT `name`, `sprite`, `hp`, `xp`, `force`, `speed`, `agility`, `psychic`, `fire`, `cold`, `storm`, `sonic`, `nercotic`, `poison`, `acid` FROM characters WHERE id = '" . $mid . "'");
    $cloneID = mysql_insert_id();
    mysql_query("UPDATE characters SET uid=".$uid.", clone=".$mid." WHERE id=".$cloneID);
    $clone = mysql_fetch_array(mysql_query("SELECT * FROM characters WHERE id =".$cloneID));
}

attack($clone,$item,$character);  //user attack monster, clone take damage
$reply = ai($original);             //but only the original have item to fight with
attack($character,$reply,$monster); //of course, the original monster do the attack and get the reward

function ai($mon)
{
    /*temporary, the AI is a damn lazy random number generator*/
    return mysql_fetch_array(mysql_query("SELECT * FROM items WHERE owner = '" . $mon['id'] . "' ORDER BY RAND()"));
}

function attack($mon, $itm, $char)
{
    echo $char['name'] . " is attacking " . $mon['name'] . " with a " . $itm['name'] . "<br />";
    $given = rand(0,$itm['damage']) + $char[$itm['attack']] - $mon[$itm['defense']];
    echo $char['name'] . " dealt " . $given . " " . $itm['attack'] . " damage to " . $mon['name'] . "<br />";
    $hp = $mon['hp'] - $given;
    $xp = round(((($mon['xp'] / 10) / (100-$given)) + 100), 0, PHP_ROUND_HALF_DOWN);
    echo $xp ." xp earned<br />";
    mysql_query("UPDATE characters SET xp=".($xp + $char['xp'])." WHERE id=".$char['id']);
    //do the damage , even if <0 hp remaining , the encounter will take care of the cadaver
    mysql_query("UPDATE characters SET hp=".$hp." WHERE id=".$mon['id']);
}

mysql_close($sql);
echo $footer;
?>
