implemented afterprocessing of the clan fights (requires database changes)

main
hecht 13 years ago
parent 4757721d27
commit 387acca439

@ -236,9 +236,9 @@ function acceptChallengeRequest($challenged_clan, array $user) {
}
// insert the participants :)
$sql = 'INSERT INTO clan_challenge_clans(clan_challenge_id, clan_id, clan_coordinator, elo) values('.$id.', '.$clan['id'].', '.$user['id'].', ' .$clan['elo'].')';
$sql = 'INSERT INTO clan_challenge_clans(clan_challenge_id, id, clan_id, clan_coordinator, elo) values('.$id.', 0, '.$clan['id'].', '.$user['id'].', ' .$clan['elo'].')';
mysql_query($sql);
$sql = 'INSERT INTO clan_challenge_clans(clan_challenge_id, clan_id, clan_coordinator, elo) values('.$id.', '.$oclan['id'].', ' .$clan_challenge['clan_challenge_creator'].', ' .$clan['elo'].')';
$sql = 'INSERT INTO clan_challenge_clans(clan_challenge_id, id, clan_id, clan_coordinator, elo) values('.$id.', 1, '.$oclan['id'].', ' .$clan_challenge['clan_challenge_creator'].', ' .$clan['elo'].')';
mysql_query($sql);
// we have everything we need for this challenge to be opened :)
@ -356,6 +356,11 @@ function getChallenge($challenge_id, $buffer = TRUE) {
return $GLOBALS['clan_challenge_buffered_instances'][$challenge_id]; // okay, we already buffered the clan :)
}
function getResult($challenge_id) {
// clan_challenge_participants
}
function getOpenChallengeRequests($clanid) {
$array = array();
if(isClanLocked($clanid)) {
@ -414,6 +419,24 @@ function getActiveChallengeIDs($clanid) {
return $array;
}
/**
* Returns the ChallengeIDs that are finished but not yet processed by the cronjob!!
* @param int $clanid
* @return array an array containing the ids of the clan_challenges
*/
function getAwatingChallengeIDs() {
// before doing anything check if we are locked
$array = array();
$sql = 'SELECT distinct(cc.clan_challenge_id) FROM clan_challenges AS cc inner join clan_challenge_clans AS ccp ON cc.clan_challenge_id = ccp.clan_challenge_id WHERE elo_change is null AND calculated = TRUE AND enddate < now()';
// echo $sql . '<br>';
$qry = mysql_query($sql);
while($row = mysql_fetch_row($qry)) {
$array[] = $row[0];
}
// return the ids
return $array;
}
// returns the last challenge ids (beginning with the latest to the first)
function getHistoricalChallengeIDs($clanid, $limit = NULL) {
@ -432,7 +455,7 @@ function getHistoricalChallengeIDs($clanid, $limit = NULL) {
function getParticipatingClanIDs($clan_challenge_id) {
$array = array();
$sql = 'SELECT clan_id FROM clan_challenge_clans AS ccp WHERE clan_challenge_id = ' . $clan_challenge_id;
$sql = 'SELECT clan_id FROM clan_challenge_clans AS ccp WHERE clan_challenge_id = ' . $clan_challenge_id . ' ORDER BY id';
$qry = mysql_query($sql);
while($row = mysql_fetch_row($qry)) {
$array[] = $row[0];
@ -441,6 +464,12 @@ function getParticipatingClanIDs($clan_challenge_id) {
return $array;
}
function getParticipatingClanData($clan_challenge_id, $clan_id) {
$sql = 'SELECT * FROM clan_challenge_clans AS ccp WHERE clan_challenge_id = ' . $clan_challenge_id . ' AND clan_id = ' .$clan_id;
$qry = mysql_query($sql);
return mysql_fetch_assoc($qry);
}
function isCharBusyWithClanfighting($char_id) {
$sql = 'SELECT count(*) as anzahl FROM clan_challenge_participants ccp INNER JOIN clan_challenges cc on ccp.clan_challenge_id = cc.clan_challenge_id WHERE cc.calculated = FALSE AND char_id = ' .$char_id;
$row = mysql_fetch_row(mysql_query($sql));
@ -498,7 +527,6 @@ function isClanLocked($clanid) {
$sql = 'INSERT INTO clan_locked(clanid, locked_until) values('.$clanid.', TIMESTAMPADD(HOUR, 24, CURRENT_TIMESTAMP))';
// echo $sql.'<br>';
mysql_query($sql);
// FIXME: We were not locked but now we are! -> deactivate all outstanding clan_challenge_requests
mysql_query('UPDATE clan_challenge_requests SET active = FALSE WHERE (clan_challenged = '.$clanid.' OR clan_requester = '.$clanid);
mysql_query('DELETE FROM clan_challenges WHERE calculated = FALSE AND clan_challenge_id IN (SELECT clan_challenge_id from clan_challenge_clans WHERE clan_id = '.$clanid.')');
} else if($lock['members'] < MIN_MEMBERS){ // we have to update the actual row
@ -753,18 +781,17 @@ function calculateChallenge($clan_challenge_id) {
}
$result = NULL;
// okay now determine the type of fight
switch($challenge['type']) {
case DAVY_BACK_FIGHT:
$result = calculateDavyBackClanFight($event_id, $clan_ids, $clan_chars, $start_time);
$result = calculateDavyBackClanFight($clan_challenge_id, $event_id, $clan_ids, $clan_chars, $start_time);
if(!is_numeric($result))
return $result;
return $result;
break;
case SURVIVAL:
return calculateSurvivalClanFight($event_id, $clan_ids, $clan_chars, $start_time);
$result = calculateSurvivalClanFight($clan_challenge_id, $event_id, $clan_ids, $clan_chars, $start_time);
if(!is_numeric($result))
return $result;
return $result;
break;
default:
// cannot create anything -.-
@ -772,17 +799,17 @@ function calculateChallenge($clan_challenge_id) {
}
$sql = 'UPDATE clan_challenges set event_id = '.$event_id.', enddate = \''.date("Y-m-d H:i:s",$result).'\' WHERE clan_challenge_id = ' .$clan_challenge_id;
$res = mysql_query($sql);
if(!$res) {
echo $sql.'<br>';
return 'Konnte Datenbank-Anfrage nicht durchf&uuml;hren ... breche Skript ab!';
}
return NULL;
}
function calculateDavyBackClanFight($event_id, array $clan_ids, array $clan_char_array, $start_time) {
function calculateDavyBackClanFight($clan_challenge_id, $event_id, array $clan_ids, array $clan_char_array, $start_time) {
$duration = DURATION_DAVY_BACK_FIGHT * 60;
// okay, davy back fights are 1 vs. 1 and the winner is the one that has won the most fights.
if(count($clan_char_array) != 2) {
@ -791,6 +818,7 @@ function calculateDavyBackClanFight($event_id, array $clan_ids, array $clan_char
if(count($clan_char_array[$clan_ids[0]]) != count($clan_char_array[$clan_ids[1]])) {
return 'Davy Back Fights können nur zwischen Clans mit der selben Anzahl von Chars ausgetragen werden!';
}
$result = 0;
$count_chars = count($clan_char_array[$clan_ids[0]]);
// okay ... now do the calculation!
for($runde = 1; $runde<=$count_chars;$runde++) {
@ -798,12 +826,21 @@ function calculateDavyBackClanFight($event_id, array $clan_ids, array $clan_char
$char_clan_2 = $clan_char_array[$clan_ids[1]][$runde];
// we do not need to know right now who won the battle! This is required in the post processing (cronjob)!
battle($event_id, $char_clan_1, $char_clan_2, ATTACK_SET_DAVY_BACK_FIGHT, $start_time + ($runde-1) * $duration, $start_time + $runde * $duration);
$winner = battle($event_id, $char_clan_1, $char_clan_2, ATTACK_SET_DAVY_BACK_FIGHT, $start_time + ($runde-1) * $duration, $start_time + $runde * $duration);
if($winner['id'] == $char_clan_1['id']){
$sql = 'UPDATE clan_challenge_clans SET points = points + 1 WHERE clan_challenge_id = ' . $clan_challenge_id . ' AND clan_id = ' .$clan_ids[0];
// echo $sql . '<br>';
mysql_query($sql);
} else {
$sql = 'UPDATE clan_challenge_clans SET points = points + 1 WHERE clan_challenge_id = ' . $clan_challenge_id . ' AND clan_id = ' .$clan_ids[1];
// echo $sql . '<br>';
mysql_query($sql);
}
}
return $start_time + ($runde-1) * $duration;
}
function calculateSurvivalClanFight($event_id, array $clan_ids, array $clan_char_array, $start_time) {
function calculateSurvivalClanFight($clan_challenge_id, $event_id, array $clan_ids, array $clan_char_array, $start_time) {
$duration = DURATION_SURVIVAL_FIGHT * 60;
// okay, survival fights are 1 vs. 1 and the winner is the one that has the last man standing.
if(count($clan_char_array) != 2) {
@ -828,14 +865,17 @@ function calculateSurvivalClanFight($event_id, array $clan_ids, array $clan_char
// the loser is of clan 1
$char_clan_1 = $clan_char_array[$clan_ids[0]][++$counter_clan_1];
$char_clan_2 = $sieger; // the new array contains the adjusted health parameter
mysql_query('UPDATE clan_challenge_clans SET points = points + 1 WHERE clan_challenge_id = ' . $clan_challenge_id . ' AND clan_id = ' .$clan_ids[0]);
} else {
// the loser is of clan 2
$char_clan_2 = $clan_char_array[$clan_ids[1]][++$counter_clan_2];
$char_clan_1 = $sieger; // the new array contains the adjusted health parameter
mysql_query('UPDATE clan_challenge_clans SET points = points + 1 WHERE clan_challenge_id = ' . $clan_challenge_id . ' AND clan_id = ' .$clan_ids[1]);
}
return $start_time + $runde * $duration;
}
$end_time = $start_time + $runde * $duration;
return $char_clan_1 != null?1:-1;
}
/**
@ -989,7 +1029,7 @@ function calculateClanExperience(array $clan1, array $clan2, $winner) {
} else {
$exp = (($clan1['level'] + $clan2['level'])/3)*$m*$g;
}
return $exp;
return round($exp);
}
/**

@ -0,0 +1,63 @@
<?php
include('db.php');
include('path.inc.php');
include_once(ROOT_PATH.'/include/clan.inc.php');
include_once(ROOT_PATH.'/include/elo.inc.php');
include_once(ROOT_PATH.'/include/semaphore.inc.php');
$ressource = 'CLAN_AUSWERTUNG';
if(!semaphoreUP($ressource)) {
echo 'konnte den cronjob nicht starten weil dieser schon in Gange ist!';
} else {
$ids = getAwatingChallengeIDs();
foreach($ids as $id) {
echo 'Werte Clanfight mit ID ' . $id . ' aus<br>';
$challenge = getChallenge($id);
$clan_ids = getParticipatingClanIDs($id);
$clan1 = getClan($clan_ids[0]);
$clan2 = getClan($clan_ids[1]);
$c_clan_data1 = getParticipatingClanData($id, $clan_ids[0]);
$c_clan_data2 = getParticipatingClanData($id, $clan_ids[1]);
if($c_clan_data1['points'] > $c_clan_data2['points']) {
$winner = $clan1['id'];
} else {
$winner = $clan2['id'];
}
$expClan1 = calculateClanExperience($clan1, $clan2, $winner);
$expClan2 = calculateClanExperience($clan2, $clan1, $winner);
$newEloClan1 = calculateElo($c_clan_data1['elo'], $c_clan_data2['elo'], $c_clan_data1['points'] - $c_clan_data2['points'], $challenge['anzahl_chars']);
$newEloClan2 = calculateElo($c_clan_data2['elo'], $c_clan_data1['elo'], $c_clan_data2['points'] - $c_clan_data1['points'], $challenge['anzahl_chars']);
$eloClan1 = ($newEloClan1 - $c_clan_data1['elo']);
$eloClan2 = ($newEloClan2 - $c_clan_data2['elo']);
if($_clan_data1['elo_change'] == null) {
$sql = 'UPDATE clan_challenge_clans SET elo_change = ' .$eloClan1 . ', exp_change = ' . $expClan1 . ' WHERE clan_challenge_id = ' . $id . ' AND clan_id = ' . $clan1['id'];
// echo $sql . '<br>';
mysql_query($sql);
addClanExp($clan1['id'], $expClan1);
mysql_query('UPDATE clan SET elo = elo + ' .$eloClan1 . ' WHERE id = ' . $clan1['id']);
}
if($_clan_data2['elo_change'] == null) {
$sql = 'UPDATE clan_challenge_clans SET elo_change = ' .$eloClan2 . ', exp_change = ' . $expClan2 . ' WHERE clan_challenge_id = ' . $id . ' AND clan_id = ' . $clan2['id'];
// echo $sql . '<br>';
mysql_query($sql);
addClanExp($clan2['id'], $expClan1);
mysql_query('UPDATE clan SET elo = elo + ' .$eloClan2 . ' WHERE id = ' . $clan2['id']);
}
}
semaphoreDown($ressource);
}
?>
Loading…
Cancel
Save