You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
6.2 KiB

<?php
/*
*
* @copyright (c) 2011 animegame.eu
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence
*
*/
include_once($_SERVER['DOCUMENT_ROOT'].'ag/include/user.inc.php');
include_once ($_SERVER['DOCUMENT_ROOT'] . 'ag/include/defines.inc.php');
defineIfNotDefined('EVENT_TEST', 0);
defineIfNotDefined('EVENT_SCHATZ_SUCHE', 1);
defineIfNotDefined('EVENT_TURNIER', 2);
defineIfNotDefined('EVENT_KAMPF', 3);
defineIfNotDefined('EVENT_LIGA', 4);
defineIfNotDefined('EVENT_WANTED', 5);
defineIfNotDefined('EVENT_CLAN_FIGHT', 6);
defineIfNotDefined('KEY_LOGE', 'LOGE');
defineIfNotDefined('KEY_SITZ', 'SITZ');
defineIfNotDefined('KEY_STEH', 'STEH');
defineIfNotDefined('KEY_EXP_CHAR1', 'EXP_CHAR1');
defineIfNotDefined('KEY_EXP_CHAR2', 'EXP_CHAR2');
defineIfNotDefined('KEY_GELD_CHAR1', 'GELD_CHAR1');
defineIfNotDefined('KEY_GELD_CHAR2', 'GELD_CHAR2');
defineIfNotDefined('KEY_AXP_CHAR1', 'AXP_CHAR1');
defineIfNotDefined('KEY_AXP_CHAR2', 'AXP_CHAR2');
defineIfNotDefined('KEY_ITM_CHAR1', 'ITM_CHAR1');
/**
* Creates an event for a given type and returns the id of this event!
* @param string $type
* @return int the id of the event
*/
function createEvent($type) {
while(!$finished) {
$sql = 'SELECT IFNULL(max(event_id),0) + 1 FROM events';
$row = mysql_fetch_row(mysql_query($sql));
mysql_query('INSERT INTO events(event_id, event_type) values('.$row[0].', \''.$type.'\')');
$finished = mysql_affected_rows() > 0;
$id = $row[0];
}
return $id;
}
/**
* Adds a participant to a given event
* @param int event_id
* @param array $char
* @return int the id of the participant (usually the charid), NULL on failure!
*/
function addParticipant($event_id, array $char) {
// Okay now add the char ;)
$user = getUser($char['besitzer']);
$hp = explode(',', $char['hp']);
$mp = explode(',', $char['mp']);
$sql = 'INSERT INTO event_chars(event_id, event_char_id, char_id, char_name, char_bild, user_id, user_name, hp, mp, strength, speed, defense, luck, stamina, block_begin, block_end)';
$sql .= ' value('. $event_id . ', '.$char['id'] .', '.$char['id'] .', \''.$char['name'].'\', \''.$char['bild'].'\', '.$char['besitzer'].', \''.$user['nickname'].'\', '.$hp[1].', '.$mp[1].', '.$char['starke'].', '.$char['speed'].', '.$char['verteidigung'].', '.$char['glueck'].', '.$char['ausdauer'].', now(), TIMESTAMPADD(Minute, 1, now()))';
// echo $sql . '<br>';
mysql_query($sql);
if(mysql_affected_rows() == 0)
return NULL;
return $char['id'];
}
/**
* Persists a fight of one event.
* @param int $event_id
* @param array combined array that is calculated in the kampf_wrapper
* @param int $startTimestamp unix timestamp for the start of the battle
* @param int $durationType the duration value (see the DURATION constants)
* @param int $durationValue the amount of time in minutes
* @return int the id of the fight
*/
function persistFight($event_id, array $combinedArray, $startTimestamp, $endTimestamp) {
// persist a fight of two chars (calculated by the wrapper) ^^"
//echo 'The fight was about ' . count($combinedArray) . ' rounds <br>';
$roundsArray = $combinedArray['rounds'];
$winnerId = $combinedArray['winner'];
$hostId = $combinedArray['host'];
$data = $combinedArray['data'];
// first get a fight id ;).
$finished = FALSE;
while(!$finished) {
$sql = 'SELECT IFNULL(max(event_fight_id),0) + 1 FROM event_fights';
$row = mysql_fetch_row(mysql_query($sql));
$sql = 'INSERT INTO event_fights(event_id, event_fight_id, host, winner, visible) values('.$event_id.', \''.$row[0].'\', '. $hostId . ', ' . $winnerId .', \''.date("Y-m-d H:i:s",$endTimestamp).'\')';
if(mysql_query($sql) === FALSE)
break;
$finished = mysql_affected_rows() > 0;
$event_fight_id = $row[0];
}
// now we have a $event_fight_id ;) lets insert the other stuff ;)
for($round = 0; $round < count($roundsArray) ; $round++) {
// persist this round ^^"
// first persist char1
$chara_1 = $roundsArray[$round]['char1_array'];
$sql = 'INSERT INTO event_fight_rounds(event_id, event_fight_id, round, event_char_id, hp, mp, strength, speed, defense, luck, stamina, attack, damage, visible)';
$sql .= ' values('.$event_id.','.$event_fight_id.','.$round.',' .$chara_1['id'].','. $chara_1['hp'].', '. $chara_1['mp'].', '. $chara_1['starke'].', '. $chara_1['speed'].', '. $chara_1['verteidigung'].', '. $chara_1['glueck'].', '. $chara_1['ausdauer'].', \''. $roundsArray[$round]['atk_char1'] .'\', \''. $roundsArray[$round]['dmg_char1'] .'\', \''.date("Y-m-d H:i:s",$endTimestamp).'\')';
// echo $sql .'<br>';
mysql_query($sql);
// now persist char2
$chara_2 = $roundsArray[$round]['char2_array'];
$sql = 'INSERT INTO event_fight_rounds(event_id, event_fight_id, round, event_char_id, hp, mp, strength, speed, defense, luck, stamina, attack, damage, visible)';
$sql .= ' values('.$event_id.','.$event_fight_id.','.$round.',' .$chara_2['id'].', '. $chara_2['hp'].', '. $chara_2['mp'].', '. $chara_2['starke'].', '. $chara_2['speed'].', '. $chara_2['verteidigung'].', '. $chara_2['glueck'].', '. $chara_2['ausdauer'].', \''. $roundsArray[$round]['atk_char2'] .'\', \''. $roundsArray[$round]['dmg_char2'] .'\', \''.date("Y-m-d H:i:s",$endTimestamp).'\')';
// echo $sql .'<br>';
mysql_query($sql);
}
// Okay now do we have any additional data left?
foreach ($data as $key => $value) {
$sql = 'INSERT INTO event_fight_metadata(event_id, event_fight_id, `key`, `value`) values('.$event_id.','.$event_fight_id.',\''.$key.'\',\''.$value.'\')';
// echo $sql . ' <br>';
mysql_query($sql);
}
mysql_query('UPDATE event_chars SET block_begin = \''.date("Y-m-d H:i:s",$startTimestamp).'\', block_end = \''.date("Y-m-d H:i:s",$endTimestamp).'\' WHERE event_id = ' . $event_id);
}
function getEventStatus($charid) {
$sql = 'SELECT event_type FROM event_chars ec inner join events e on ec.event_id = e.event_id where block_end > now() and char_id = ' . $charid;
$qry = mysql_query($sql);
$row = mysql_fetch_assoc($qry);
switch ($row['event_type']) {
case EVENT_TEST:
return 'Test';
case EVENT_SCHATZ_SUCHE:
return 'Schatzsuche';
case EVENT_TURNIER:
return 'Turnier';
case EVENT_KAMPF:
return 'Kampf';
case EVENT_LIGA:
return 'Liga';
case EVENT_WANTED:
return 'Wanted';
case EVENT_CLAN_FIGHT:
return 'Clanfight';
}
}
?>