From 88591964c501c8e59ef18192fb71efeaaf8889e9 Mon Sep 17 00:00:00 2001 From: hecht Date: Wed, 16 Nov 2011 22:28:35 +0000 Subject: [PATCH] added some more functionality (adjusted the database aswell) --- ag/include/clan_fights.inc.php | 128 +++++++++++++++++++++++++++------ ag/include/halloffame.inc.php | 23 +++--- ag/turnier.php | 2 +- cronjob/klein_Turnier2.php | 2 +- db/animegame_svn.sql | 3 +- 5 files changed, 123 insertions(+), 35 deletions(-) diff --git a/ag/include/clan_fights.inc.php b/ag/include/clan_fights.inc.php index 685c230..a2f79ea 100644 --- a/ag/include/clan_fights.inc.php +++ b/ag/include/clan_fights.inc.php @@ -18,6 +18,7 @@ include_once (ROOT_PATH . '/include/event.inc.php'); defineIfNotDefined('MIN_MEMBERS', 5); defineIfNotDefined('MAX_OPEN_REQUESTS', 3); +defineIfNotDefined('MAX_LEADER_ASSIGNED_CHARS', 1); defineIfNotDefined('CLANFIGHTS_MAX_FIGHTS_PER_24H', 6); defineIfNotDefined('DAVY_BACK_FIGHT', 1); defineIfNotDefined('SURVIVAL', 2); @@ -51,8 +52,8 @@ function challengeClan(array $user, $clanid, $anzahl = 5, $type = DAVY_BACK_FIGH return 'Entweder bist du nicht in einem Clan oder der andere Clan existiert nicht mehr!!'; } - if($clan['leader'] != $user['id'] && $clan['co_leader'] != $user['id']){ - return 'Nur der Leader, bzw. der Co-Leader kann andere Clans herausfordern!!'; + if(isAuthorizedClanfightCoordinator($user['id'])){ + return 'Nur der Leader, Co-Leader oder freigeschaltete Mitglieder können andere Clans herausfordern!!'; } if($clan['id'] == $oclan['id']) { @@ -159,8 +160,8 @@ function acceptChallengeRequest($challenged_clan, array $user) { return 'Du bist in keinem Clan!!'; } - if($clan['leader'] != $user['id'] && $clan['co_leader'] != $user['id']){ - return 'Nur der Leader, bzw. der Co-Leader kann herausforderungen anderer Clans annehmen!!'; + if(isAuthorizedClanfightCoordinator($user['id'])){ + return 'Nur der Leader, Co-Leader oder freigeschaltete Mitglieder können Herausforderungen anderer Clans annehmen!!'; } $sql = 'SELECT * FROM clan_challenge_requests WHERE clan_challenged = ' .$user['clan'] . ' AND clan_requester = ' .$challenged_clan . ' AND active = TRUE AND challenged_time > TIMESTAMPADD(day, -1, now())'; @@ -184,7 +185,7 @@ function acceptChallengeRequest($challenged_clan, array $user) { // now calculate how many open fights we currently had or have! // FIXME: Good idea to pach this in a semaphore? $sql = 'select count(*) as anzahl from (SELECT count(*) as anz FROM clan_challenges c inner join clan_challenge_clans ccc on c.clan_challenge_id = ccc.clan_challenge_id where calculated = 0 or TIMESTAMPADD(day, -1, now()) < enddate and clan_id IN ('.$clan_challenge['clan_challenged'].', '.$clan_challenge['clan_requester'].') group by clan_id) k where k.anz >= ' . CLANFIGHTS_MAX_FIGHTS_PER_24H; -// echo $sql . '
'; + // echo $sql . '
'; $row = mysql_fetch_assoc(mysql_query($sql)); if($row['anzahl'] > 0) { @@ -241,8 +242,8 @@ function denyChallengeRequest($challenged_clan, array $user) { return 'Du bist in keinem Clan!!'; } - if($clan['leader'] != $user['id'] && $clan['co_leader'] != $user['id']){ - return 'Nur der Leader, bzw. der Co-Leader kann Herausforderungen anderer Clans ablehnen!!'; + if(isAuthorizedClanfightCoordinator($user['id'])){ + return 'Nur der Leader, Co-Leader oder freigeschaltete Mitglieder können Herausforderungen anderer Clans ablehnen!!'; } $clan_challenge = mysql_fetch_assoc(mysql_query('SELECT * FROM clan_challenge_requests WHERE clan_challenged = ' .$user['clan'] . ' AND clan_requester = ' .$challenged_clan . ' AND active = TRUE AND challenged_time > TIMESTAMPADD(day, -1, now())')); @@ -275,7 +276,7 @@ function getChallenge($challenge_id, $buffer = TRUE) { $qry = mysql_query('SELECT * from clan_challenges WHERE clan_challenge_id = ' .$challenge_id); $GLOBALS['clan_challenge_buffered_instances'][$challenge_id] = mysql_fetch_assoc($qry); return $GLOBALS['clan_challenge_buffered_instances'][$challenge_id]; // okay, we already buffered the clan :) - } +} function getOpenChallengeRequests($clanid) { $array = array(); @@ -284,7 +285,7 @@ function getOpenChallengeRequests($clanid) { } $sql = 'SELECT * from clan_challenge_requests WHERE challenged_time > TIMESTAMPADD(day, -1, now()) and active = TRUE AND (clan_requester = '.$clanid.' OR clan_challenged = '.$clanid.')'; -// echo $sql . '
'; + // echo $sql . '
'; $qry = mysql_query($sql); while($row = mysql_fetch_assoc($qry)) { $array[] = $row; @@ -297,7 +298,7 @@ function getOpenChallengeRequestCount($clanid) { return 0; } $sql = 'SELECT count(*) from clan_challenge_requests where challenged_time > TIMESTAMPADD(day, -1, now()) and active = TRUE AND (clan_requester = '.$clanid.' OR clan_challenged = '.$clanid.')'; -// echo $sql . '
'; + // echo $sql . '
'; $row = mysql_fetch_row(mysql_query($sql)); return $row[0]; } @@ -368,8 +369,8 @@ function isClanLocked($clanid) { return true; } // we really want to know if we are locked at the moment. So readout everything at once ;), right join so we always have a row :D - $sql = 'SELECT *, locked_until < now() as `lock`, (Select count(id) from user where clan = c2.id) as members FROM clan_locked right join clan c2 on c2.id = clan_locked.clanid where c2.id = ' . $clanid; -// echo $sql.'
'; + $sql = 'SELECT *, locked_until > now() as `lock`, (Select count(id) from user where clan = c2.id) as members FROM clan_locked right join clan c2 on c2.id = clan_locked.clanid where c2.id = ' . $clanid; + // echo $sql.'
'; $lock = mysql_fetch_assoc(mysql_query($sql)); if($lock['lock'] == 0) { // don't matter if it is 0 or NULL @@ -385,12 +386,12 @@ function isClanLocked($clanid) { } else if($lock['lock'] === NULL){ // we need to insert a row // INSERT $sql = 'INSERT INTO clan_locked(clanid, locked_until) values('.$clanid.', TIMESTAMPADD(HOUR, 24, CURRENT_TIMESTAMP))'; -// echo $sql.'
'; + // echo $sql.'
'; mysql_query($sql); } else if($lock['members'] < MIN_MEMBERS){ // we have to update the actual row // UPDATE $sql = 'UPDATE clan_locked set locked_until = TIMESTAMPADD(HOUR, 24, CURRENT_TIMESTAMP) WHERE clanid = ' .$clanid; -// echo $sql.'
'; + // echo $sql.'
'; mysql_query($sql); } } @@ -410,11 +411,18 @@ function joinChallenge($clan_challenge_id, array $user, $charid, $slot ) { $clan = getClan($user['clan']); $char = getChar($charid); + $forced = false; + if(!$clan) { return 'Du bist in keinem Clan!!'; } if($char['besitzer'] != $user['id']) { + // the current user is NOT the owner of the char --> check if it is a coordinator + if(isAuthorizedClanfightCoordinator($clan_id, $user['id'], $clan_challenge_id)) { + // okay we may assign an additional char ;) // check if we have reached the limit is later!! + $forced = true; + } return 'Du kannst den Char nicht an dem Kampf teilnehmen lassen, da er dir nicht gehört!'; } @@ -424,7 +432,7 @@ function joinChallenge($clan_challenge_id, array $user, $charid, $slot ) { $clan_challenge = getChallenge($clan_challenge_id); - if($slot < 1 && $slot > $clan_challenge['anzahl_chars']) { + if($slot < 1 || $slot > $clan_challenge['anzahl_chars']) { return 'Der Slot ' . $slot . ' ist für diesen Kampf nicht verfügbar!!'; } @@ -458,10 +466,39 @@ function joinChallenge($clan_challenge_id, array $user, $charid, $slot ) { return 'Konnte den Char nicht hinzufügen. Da du schon einen Char angemeldet hast!'; } - mysql_query('INSERT INTO clan_challenge_participants(clan_challenge_id, clan_id, char_id, slot) values('.$clan_challenge_id.', '.$user['clan'].', '.$charid.', '.$slot.')'); - if(mysql_affected_rows() == 0) { + + if($forced && isForceForCharAcknowledged($char['id'])) { + + } + + + $resource2 = 'Clan:'.$clan_id; + if($forced && !semaphoreUP($ressource2)) { + return 'Du kannst den Char nicht zuweisen, da exakt im gleichen Moment jemand aus deinem Clan auch einen Char zuweist!'; + } + + // okay and now check if we reached the maximum amount of leader assigned chars!! + if($forced && getCountLeaderAssignedChars($clan_id, $clan_challenge_id) >= MAX_LEADER_ASSIGNED_CHARS) { + // shit!! semaphoreDown($ressource); - return 'Konnte den Char nicht hinzufügen. Slot mittlerweile schon belegt?'; + semaphoreDown($ressource2); + return 'Du kannst den Char nicht zuweisen, da die maximale Anzahl der Zuweisungen schon erreicht ist!'; + } + + mysql_query('INSERT INTO clan_challenge_participants(clan_challenge_id, clan_id, char_id, slot, forced) values('.$clan_challenge_id.', '.$user['clan'].', '.$charid.', '.$slot.', ' .$forced .')'); + if(mysql_affected_rows() == 0) { + if(!$forced) { + // maybe the leader has already assigned the char and we now give the official okay for that! + mysql_query('UPDATE clan_challenge_participants SET forced = FALSE, slot = '.$slot.' WHERE clan_challenge_id = '.$clan_challenge_id.' AND clan_id = '.$user['clan'].' AND char_id = '.$charid); + } + + if(mysql_affected_rows() == 0) { + semaphoreDown($ressource); + if($forced) { + semaphoreDown($ressource2); + } + return 'Konnte den Char nicht hinzufügen. Slot mittlerweile schon belegt?'; + } } // okay, now try to update the challenge to close the challenge and to start the calculating (as long we can do it ;)) @@ -477,6 +514,9 @@ function joinChallenge($clan_challenge_id, array $user, $charid, $slot ) { mysql_query($sql); $rows = mysql_affected_rows(); semaphoreDown($ressource); + if($forced) { + semaphoreDown($ressource2); + } if($rows > 0) { // okay we now have a complete competition and the challenge is closed :) @@ -501,7 +541,10 @@ function leaveChallenge($clan_challenge_id, array $user, $charid) { } if($char['besitzer'] != $user['id']) { - return 'Du kannst den Char nicht vom Kampf zurückziehen, da er dir nicht gehört!'; + if(!isAuthorizedClanfightCoordinator($clan_id, $user['id'], $clan_challenge_id)) { + // authorized coordinators may remove a char!! But this one is not authorized + return 'Du kannst den Char nicht vom Kampf zurückziehen, da er dir nicht gehört!'; + } } // Okay, the rest we can check with the delete statement much easier! @@ -546,12 +589,12 @@ function calculateChallenge($clan_challenge_id) { case DAVY_BACK_FIGHT: $result = calculateDavyBackClanFight($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); if(!is_numeric($result)) - return $result; + return $result; break; default: // cannot create anything -.- @@ -614,7 +657,7 @@ function calculateSurvivalClanFight($event_id, array $clan_ids, array $clan_char $char_clan_2 = $clan_char_array[$clan_ids[1]][++$counter_clan_2]; $char_clan_1 = $sieger; // the new array contains the adjusted health parameter } - return $start_time + $runde * $duration; + return $start_time + $runde * $duration; } } @@ -691,6 +734,47 @@ function getClanFightCapableClanIds() { * @param int $clan_fight_id the optional clan_fights that is in scope */ function isAuthorizedClanfightCoordinator($clan_id, $user_id, $clan_fight_id = NULL) { + // do we require this information for a specific clan_fight??? + if($clan_fight_id === NULL) { + $sql = 'SELECT COUNT(*) FROM `clan_challenge_clans` WHERE `clan_coordinator` = ' .$user_id. ' AND `clan_id` = '.$clan_id . ' AND `clan_challenge_id` = ' .$clan_fight_id; + $row = mysql_fetch_row(mysql_query($sql)); + if($row[0] <= 0) { + // okay, maybe its a leader ;) + $clan = getClan($clan_id); + if($clan['leader'] == $user_id || $clan['co_leader'] = $user_id) { + // the leader or the co_leader is always authorized! + return true; + } + // not authorized! + return false; + } + // the coordinator is always authorized! + return true; + + } else { + // it is a more general thing ;) + $sql = 'SELECT COUNT(*) FROM `clan_challenge_whitelist` WHERE `user_id` = ' .$user_id. ' AND `clan_id` = '.$clan_id; + $row = mysql_fetch_row(mysql_query($sql)); + } + return true; +} + +/** + * Returns how many chars were assigned by the leader for the given fight + * @param int $clan_id + * @param int $clan_fight_id + */ +function getCountLeaderAssignedChars($clan_id, $clan_fight_id) { + $sql = 'SELECT COUNT(*) FROM `clan_challenge_participants` WHERE `clan_challenge_id` = ' .$clan_fight_id . ' AND `clan_id` = '.$clan_id .' AND `forced` = TRUE'; + $row = mysql_fetch_row(mysql_query($sql)); + return $row[0]; +} + +/** + * This function checks if there is a char-rule that allows forcing the char for leader assignment! + * @param int $char_id + */ +function isForceForCharAcknowledged($char_id) { return true; } diff --git a/ag/include/halloffame.inc.php b/ag/include/halloffame.inc.php index 230886c..4072133 100644 --- a/ag/include/halloffame.inc.php +++ b/ag/include/halloffame.inc.php @@ -9,7 +9,7 @@ include_once(ROOT_PATH.'/include/char.inc.php'); /** - * + * * @param unknown_type $title * @param unknown_type $charid * @param unknown_type $userid @@ -19,16 +19,16 @@ function insertIntoHallOfFame($title, $charid, $userid){ $row = mysql_fetch_assoc(mysql_query('Select max(id) as round from online')); $char = getChar($charid); $user = mysql_fetch_assoc(mysql_query('Select * from user where id = '.$userid)); - + if(!$row || is_null($char) || is_null($user)){ return false; } - + return insertIntoHallOfFameFull($row['round'], $title, $charid, $char['name'], $userid, $user['nickname'], 'now()'); } /** - * + * * @param unknown_type $round * @param unknown_type $title * @param unknown_type $charid @@ -46,7 +46,7 @@ function insertIntoHallOfFameFull($round = '', $title = '', $charid = '', $charn } $columns = 'art, runde, datum, charname, username'; $values = '\''.$title.'\','.$round.','.$date.',\''.$charname.'\',\''.$username.'\''; - + if(is_numeric($charid)){ $columns .= ',charid'; $values .= ','.$charid; @@ -61,14 +61,17 @@ function insertIntoHallOfFameFull($round = '', $title = '', $charid = '', $charn return mysql_affected_rows() > 0; } -function getHallOfFame($title, $userid) { +function getHallOfFameEntryCount($title, $userid) { $row = mysql_fetch_assoc(mysql_query('Select max(id) as round from online')); - return getHallOfFameFull($row['round'],$title, $userid); + return getHallOfFameEntryCountFull($row['round'],$title, $userid); } -function getHallOfFameFull($round = '', $title = '', $userid = '') { - $sql = 'select count(*) from highscore where runde='.$round.' and userid='.$userid; - $row = mysql_fetch_row($sql); +function getHallOfFameEntryCountFull($round = '', $title = '', $userid = '') { + if(!is_numeric($round) && !is_numeric($userid)) { + return 0; + } + $sql = 'select count(*) from highscore where art=\''.$title.'\' runde='.$round.' and userid='.$userid; + $row = mysql_fetch_row(mysql_query($sql)); return $row[0]; } diff --git a/ag/turnier.php b/ag/turnier.php index 0e74dcd..3dbbbd9 100644 --- a/ag/turnier.php +++ b/ag/turnier.php @@ -43,7 +43,7 @@ function anmelden($user, $charid, $art){ return; } - if($art == 'klein' && getHallOfFame('Anfängerturnier', $user['id']) >= 2) { + if($art == 'klein' && getHallOfFameEntryCount('Anfängerturnier', $user['id']) >= 2) { displayErrorMessage(NULL,'Du hast das Anfängerturnier schon zu oft gewonnen!!!','weiter...'); } diff --git a/cronjob/klein_Turnier2.php b/cronjob/klein_Turnier2.php index 9663d3e..cf1ebbb 100644 --- a/cronjob/klein_Turnier2.php +++ b/cronjob/klein_Turnier2.php @@ -20,7 +20,7 @@ $rang1 = getChar($results[0][0]); // insertIntoHallOfFame($title, $charid, $userid); insertIntoHallOfFame('Anfängerturnier', $rang1['id'], $rang1['besitzer']); -if(getHallOfFame('Anfängerturnier', $rang1['id']) >= 2) { +if(getHallOfFameEntryCount('Anfängerturnier', $rang1['id']) >= 2) { // remove all chars that are registered mysql_query('DELETE FROM turnier1 WHERE art = \'klein\' AND besitzer = '.$rang1['besitzer']); } diff --git a/db/animegame_svn.sql b/db/animegame_svn.sql index cb9549e..7710f7f 100644 --- a/db/animegame_svn.sql +++ b/db/animegame_svn.sql @@ -405,6 +405,7 @@ CREATE TABLE `clan_challenge_participants` ( `clan_id` int(11) NOT NULL, `char_id` int(11) NOT NULL, `slot` int(11) NOT NULL, + `forced` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`clan_challenge_id`,`clan_id`,`slot`), UNIQUE KEY `new_index` (`clan_challenge_id`,`clan_id`,`slot`), KEY `clan_challenge_participants_clan_id` (`clan_id`), @@ -630,7 +631,7 @@ CREATE TABLE `event_fights` ( `event_id` int(10) unsigned NOT NULL, `event_fight_id` int(10) unsigned NOT NULL, `visible` timestamp NOT NULL, - `started` timestamp NOT NULL, + `starting` timestamp NOT NULL, `host` int(10) unsigned NOT NULL, `winner` int(10) unsigned NOT NULL, PRIMARY KEY (`event_id`,`event_fight_id`),