function level_up_if_possible($char_id, $auto_level = false) { // Setup values: $max_level = maximum_level(); $health_to_add = 100; $turns_to_give = 50; $stat_value_to_add = 5; $char_kills = get_kills($char_id); if ($char_kills < 0) { // If the character doesn't have any kills, shortcut the levelling process. return false; } else { $char_obj = new Player($char_id); $username = $char_obj->name(); $char_level = $char_obj->level(); if ($auto_level && $char_obj->isAdmin()) { // If the character is an admin, do not auto-level them. return false; } else { // For normal characters, do auto-level them. // Check required values: $nextLevel = $char_level + 1; $required_kills = required_kills_to_level($char_level); // Have to be under the max level and have enough kills. $level_up_possible = $nextLevel <= $max_level && $char_kills >= $required_kills; if ($level_up_possible) { // ****** Perform the level up actions ****** // // Explicitly call for the special case of kill changing to prevent an infinite loop. $userKills = change_kills($char_id, -1 * $required_kills, $auto_level_check = false); $userLevel = addLevel($char_id, 1); change_strength($char_id, $stat_value_to_add); change_speed($char_id, $stat_value_to_add); change_stamina($char_id, $stat_value_to_add); change_karma($char_id, 1); // Only add 1 to karma via levelling. change_ki($char_id, 50); // Add 50 ki points via levelling. addHealth($char_id, $health_to_add); addTurns($char_id, $turns_to_give); // Send a level-up message, for those times when auto-levelling happens. send_event($char_id, $char_id, "You levelled up! Your strength raised by {$stat_value_to_add}, speed by {$stat_value_to_add}, stamina by {$stat_value_to_add}, Karma by 1, and your Ki raised 50! You gained some health and turns as well! You are now a level {$userLevel} ninja! Go kill some stuff."); return true; } else { return false; } } } }
function change_kills($char_id, $amount, $auto_level_check = true) { $amount = (int) $amount; if (abs($amount) > 0) { // Ignore changes that amount to zero. if ($amount > 0 && $auto_level_check) { // For positive kill changes, check whether levelling occurs. level_up_if_possible($char_id, $auto_levelling = true); } query("UPDATE players SET kills = kills + \n\t\t CASE WHEN kills + :amount1 < 0 THEN kills*(-1) ELSE :amount2 END\n\t\t WHERE player_id = :player_id", array(':amount1' => array($amount, PDO::PARAM_INT), ':amount2' => array($amount, PDO::PARAM_INT), ':player_id' => $char_id)); } return get_kills($char_id); }