/** * Command for the current user to offer their money as bounty on another player * * @param Container * @param target String The username of the player to offer a bounty on * @param amount int The amount of gold to spend on offering the bounty * @return StreamedViewResponse * * @TODO simplify the conditional branching */ public function offerBounty(Container $p_dependencies) { $request = RequestWrapper::$request; $targetName = $request->get('target'); $char = Player::findPlayable($this->getAccountId()); $target = Player::findByName($targetName); $amountIn = $request->get('amount'); $amount = intval($amountIn) !== 0 ? intval($amountIn) : null; $quickstat = false; $success = false; $authenticated = $char !== null; if (!$char) { $error = 2; // You don't have enough gold. } elseif (!$target) { $error = 1; // Target not found } elseif ($target->id() === $char->id()) { $error = 6; // Can't put a bounty on yourself. } else { $error = $this->validateBountyOffer($char, $target->id(), $amount); $amount = self::calculateMaxOffer($target->bounty, $amount); if (!$error) { $char->setGold($char->gold - $amount); // Subtract the gold. $target->setBounty($target->bounty + $amount); $target->save(); $char = $char->save(); Event::create($char->id(), $target->id(), $char->name() . " has offered " . $amount . " gold in reward for your head!"); $success = true; $quickstat = 'player'; } } return $this->render(['error' => $error, 'authenticated' => $authenticated, 'success' => $success, 'quickstat' => $quickstat, 'amount_in' => $amountIn, 'amount' => $amount, 'command' => 'offer', 'location' => 0, 'target' => $target]); }
/** * @return void */ private function logDuel($attacker, $target, $winner, $killpoints) { $duel_log_msg = "%s has dueled {$target->name()} and "; if ($attacker !== $winner) { $duel_log_msg .= "lost at " . date("F j, Y, g:i a"); } else { if ($killpoints > 1 || $killpoints < 0) { $duel_log_msg .= "won {$killpoints} killpoints."; } else { $duel_log_msg = ''; } } if ($duel_log_msg !== '') { Event::create((int) "SysMsg", (int) "SysMsg", sprintf($duel_log_msg, $attacker->name(), $target->name())); GameLog::sendLogOfDuel($attacker->name(), $target->name(), $attacker === $winner, $killpoints); } }
public function truncateMessages() { self::shortenChat(); Event::deleteOldEvents(); }
/** * Perform the effects of a clonekill. * @return string outcome or false */ public static function kill(Player $self, Player $clone1, Player $clone2) { if (self::canKill($clone1, $clone2)) { $today = date("F j, Y, g:i a"); $clone1_health = $clone1->health; $clone2_health = $clone2->health; $clone1_turns = $clone1->turns; $clone2_turns = $clone2->turns; $clone1->changeTurns(-1 * $clone1->turns); $clone1->death(); $clone2->changeTurns(-1 * $clone2->turns); $clone2->death(); $clone1->save(); $clone2->save(); $result_message = "You obliterate the clone {$clone1->name()} for {$clone1_health} health, {$clone1_turns} turns\n and the clone {$clone2->name()} for {$clone2_health} health, {$clone2_turns} turns."; Event::create($self->id(), $clone1->id(), "You and {$clone2->name()} were Clone Killed at {$today}."); Event::create($self->id(), $clone2->id(), "You and {$clone1->name()} were Clone Killed at {$today}."); return $result_message; } else { return false; } }
/** * Attack a specific npc * * @param Container * @return Response */ public function attack(Container $p_dependencies) { $request = RequestWrapper::$request; $url_part = $request->getRequestUri(); if (preg_match('#\\/(\\w+)(\\/)?$#', $url_part, $matches)) { $victim = $matches[1]; } else { $victim = null; // No match, victim is null. } $turn_cost = 1; $health = true; $combat_data = []; $player = $p_dependencies['current_player']; $error_template = 'npc.no-one.tpl'; // Error template also used down below. $npc_template = $error_template; // Error condition by default. $npcs = NpcFactory::npcsData(); $possible_npcs = array_merge(array_column(NpcFactory::customNpcs(), 'identity'), array_keys($npcs)); $victim = in_array($victim, $possible_npcs) ? $victim : null; // Filter to only the correct options. $standard_npcs = ['peasant' => 'attackVillager', 'merchant' => 'attackMerchant', 'guard' => 'attackGuard']; $method = null; if ($player && $player->turns > 0 && !empty($victim)) { // Strip stealth when attacking special NPCs if ($player->hasStatus('stealth') && in_array(strtolower($victim), self::$STEALTH_REMOVING_NPCS)) { $player->subtractStatus(STEALTH); } if ($this->startRandomEncounter()) { $method = 'randomEncounter'; } elseif (array_key_exists($victim, $npcs)) { list($npc_template, $combat_data) = $this->attackAbstractNpc($victim, $player, $npcs); } else { if (array_key_exists($victim, $standard_npcs)) { $method = $standard_npcs[$victim]; } else { if ($victim == "samurai") { if ($player->level < 2) { $turn_cost = 0; $npc_template = 'npc.samurai-too-weak.tpl'; } else { if ($player->kills < 1) { $turn_cost = 0; $npc_template = 'npc.samurai-too-tired.tpl'; } else { $method = 'attackSamurai'; } } } else { if ($victim == 'thief') { // Check the counter to see whether they've attacked a thief multiple times in a row. $counter = $this->getThiefCounter($p_dependencies); $this->setThiefCounter($counter + 1, $p_dependencies); // Incremement the current state of the counter. if ($counter > 20 && rand(1, 3) == 3) { // Only after many attacks do you have the chance to be attacked back by the group of thieves. $this->setThiefCounter(0, $p_dependencies); // Reset the counter to zero. $method = 'attackGroupOfThieves'; } else { $method = 'attackNormalThief'; } } } } } if (is_callable([$this, $method], false)) { list($npc_template, $combat_data) = $this->{$method}($player); } if ($player->health <= 0) { // FINAL CHECK FOR DEATH $player->death(); $health = false; Event::create((int) "SysMsg", $player->id(), "DEATH: You have been killed by a {$victim}."); } // Subtract the turn cost for attacking an npc // almost always 1 apart from perhaps oni or group-of-thieves $player->changeTurns(-1 * $turn_cost); $player->save(); } // Uses a sub-template inside for specific npcs. $parts = ['victim' => $victim, 'npc_template' => $npc_template, 'attacked' => 1, 'turns' => $player ? $player->turns : null, 'health' => $health]; return new StreamedViewResponse('Battle', 'npc.tpl', $parts + $combat_data, ['quickstat' => 'player']); }
/** * Use, the skills_mod equivalent * * @note Test with urls like: * http://nw.local/skill/use/Fire%20Bolt/10 * http://nw.local/skill/self_use/Unstealth/ * http://nw.local/skill/self_use/Heal/ */ public function useSkill($self_use = false) { // Template vars. $display_sight_table = $generic_skill_result_message = $generic_state_change = $killed_target = $loot = $added_bounty = $bounty = $suicided = $destealthed = null; $error = null; $char_id = SessionFactory::getSession()->get('player_id'); $player = Player::find($char_id); $path = RequestWrapper::getPathInfo(); $slugs = $this->parseSlugs($path); // (fullpath0) /skill1/use2/Fire%20Bolt3/tchalvak4/(beagle5/) $act = isset($slugs[3]) ? $slugs[3] : null; $target = isset($slugs[4]) ? $slugs[4] : null; $target2 = isset($slugs[5]) ? $slugs[5] : null; if (!Filter::toNonNegativeInt($target)) { if ($self_use) { $target = $char_id; } else { if ($target !== null) { $targetObj = Player::findByName($target); $target = $targetObj ? $targetObj->id() : null; } else { $target = null; } } } if ($target2 && !Filter::toNonNegativeInt($target2)) { $target2Obj = Player::findByName($target2); $target2 = $target2Obj ? $target2Obj->id() : null; } $skillListObj = new Skill(); // *** Before level-based addition. $turn_cost = $skillListObj->getTurnCost(strtolower($act)); $ignores_stealth = $skillListObj->getIgnoreStealth($act); $self_usable = $skillListObj->getSelfUse($act); $use_on_target = $skillListObj->getUsableOnTarget($act); $ki_cost = 0; // Ki taken during use. $reuse = true; // Able to reuse the skill. $today = date("F j, Y, g:i a"); // Check whether the user actually has the needed skill. $has_skill = $skillListObj->hasSkill($act, $player); assert($turn_cost >= 0); if ($self_use) { // Use the skill on himself. $return_to_target = false; $target = $player; $target_id = null; } else { if ($target != '' && $target != $player->player_id) { $target = Player::find($target); $target_id = $target->id(); $return_to_target = true; } else { // For target that doesn't exist, e.g. http://nw.local/skill/use/Sight/zigzlklkj error_log('Info: Attempt to use a skill on a target that did not exist.'); return new RedirectResponse(WEB_ROOT . 'skill/?error=' . rawurlencode('Invalid target for skill [' . rawurlencode($act) . '].')); } } $covert = false; $victim_alive = true; $attacker_id = $player->name(); $attacker_char_id = $player->id(); $starting_turns = $player->turns; $level_check = $player->level - $target->level; if ($player->hasStatus(STEALTH)) { $attacker_id = 'A Stealthed Ninja'; } $use_attack_legal = true; if ($act == 'Clone Kill' || $act == 'Harmonize') { $has_skill = true; $use_attack_legal = false; $attack_allowed = true; $attack_error = null; $covert = true; } else { // *** Checks the skill use legality, as long as the target isn't self. $params = ['required_turns' => $turn_cost, 'ignores_stealth' => $ignores_stealth, 'self_use' => $self_use]; $AttackLegal = new AttackLegal($player, $target, $params); $update_timer = isset($this->update_timer) ? $this->update_timer : true; $attack_allowed = $AttackLegal->check($update_timer); $attack_error = $AttackLegal->getError(); } if (!$attack_error) { // Only bother to check for other errors if there aren't some already. if (!$has_skill || $act == '') { // Set the attack error to display that that skill wasn't available. $attack_error = 'You do not have the requested skill.'; } elseif ($starting_turns < $turn_cost) { $turn_cost = 0; $attack_error = "You do not have enough turns to use {$act}."; } } if (!$attack_error) { // Nothing to prevent the attack from happening. // Initial attack conditions are alright. $result = ''; if ($act == 'Sight') { $covert = true; $sight_data = $this->pullSightData($target); $display_sight_table = true; } elseif ($act == 'Steal') { $covert = true; $gold_decrease = min($target->gold, rand(5, 50)); $player->setGold($player->gold + $gold_decrease); $player->save(); $target->setGold($target->gold - $gold_decrease); $target->save(); $msg = "{$attacker_id} stole {$gold_decrease} gold from you."; Event::create($attacker_char_id, $target->id(), $msg); $generic_skill_result_message = "You have stolen {$gold_decrease} gold from __TARGET__!"; } else { if ($act == 'Unstealth') { $state = 'unstealthed'; if ($target->hasStatus(STEALTH)) { $target->subtractStatus(STEALTH); $generic_state_change = "You are now {$state}."; } else { $turn_cost = 0; $generic_state_change = "__TARGET__ is already {$state}."; } } else { if ($act == 'Stealth') { $covert = true; $state = 'stealthed'; if (!$target->hasStatus(STEALTH)) { $target->addStatus(STEALTH); $target->subtractStatus(STALKING); $generic_state_change = "__TARGET__ is now {$state}."; } else { $turn_cost = 0; $generic_state_change = "__TARGET__ is already {$state}."; } } else { if ($act == 'Stalk') { $state = 'stalking'; if (!$target->hasStatus(STALKING)) { $target->addStatus(STALKING); $target->subtractStatus(STEALTH); $generic_state_change = "__TARGET__ is now {$state}."; } else { $turn_cost = 0; $generic_state_change = "__TARGET__ is already {$state}."; } } else { if ($act == 'Kampo') { $covert = true; // *** Get Special Items From Inventory *** $user_id = $player->id(); $root_item_type = 7; $itemCount = query_item('SELECT sum(amount) AS c FROM inventory WHERE owner = :owner AND item_type = :type GROUP BY item_type', [':owner' => $user_id, ':type' => $root_item_type]); $turn_cost = min($itemCount, $starting_turns - 1, 2); // Costs 1 or two depending on the number of items. if ($turn_cost && $itemCount > 0) { // *** If special item count > 0 *** $inventory = new Inventory($player); $inventory->remove('ginsengroot', $itemCount); $inventory->add('tigersalve', $itemCount); $generic_skill_result_message = 'With intense focus you grind the ' . $itemCount . ' roots into potent formulas.'; } else { // *** no special items, give error message *** $turn_cost = 0; $generic_skill_result_message = 'You do not have the necessary ginsengroots or energy to create any Kampo formulas.'; } } else { if ($act == 'Poison Touch') { $covert = true; $target->addStatus(POISON); $target->addStatus(WEAKENED); // Weakness kills strength. $target_damage = rand(self::MIN_POISON_TOUCH, $this->maxPoisonTouch()); $victim_alive = $target->harm($target_damage); $generic_state_change = "__TARGET__ has been poisoned!"; $generic_skill_result_message = "__TARGET__ has taken {$target_damage} damage!"; $msg = "You have been poisoned by {$attacker_id}"; Event::create($attacker_char_id, $target->id(), $msg); } elseif ($act == 'Fire Bolt') { $target_damage = $this->fireBoltBaseDamage($player) + rand(1, $this->fireBoltMaxDamage($player)); $generic_skill_result_message = "__TARGET__ has taken {$target_damage} damage!"; $victim_alive = $target->harm($target_damage); $msg = "You have had fire bolt cast on you by " . $player->name(); Event::create($player->id(), $target->id(), $msg); } else { if ($act == 'Heal' || $act == 'Harmonize') { // This is the starting template for self-use commands, eventually it'll be all refactored. $harmonize = false; if ($act == 'Harmonize') { $harmonize = true; } $hurt = $target->is_hurt_by(); // Check how much the TARGET is hurt (not the originator, necessarily). // Check that the target is not already status healing. if ($target->hasStatus(HEALING) && !$player->isAdmin()) { $turn_cost = 0; $generic_state_change = '__TARGET__ is already under a healing aura.'; } elseif ($hurt < 1) { $turn_cost = 0; $generic_skill_result_message = '__TARGET__ is already fully healed.'; } else { if (!$harmonize) { $original_health = $target->health; $heal_points = $player->getStamina() + 1; $new_health = $target->heal($heal_points); // Won't heal more than possible $healed_by = $new_health - $original_health; } else { $start_health = $player->health; // Harmonize those chakra! $player = $this->harmonizeChakra($player); $healed_by = $player->health - $start_health; $ki_cost = $healed_by; } $target->addStatus(HEALING); $generic_skill_result_message = "__TARGET__ healed by {$healed_by} to " . $target->health . "."; if ($target->id() != $player->id()) { Event::create($attacker_char_id, $target->id(), "You have been healed by {$attacker_id} for {$healed_by}."); } } } else { if ($act == 'Ice Bolt') { if ($target->hasStatus(SLOW)) { $turn_cost = 0; $generic_skill_result_message = '__TARGET__ is already iced.'; } else { if ($target->turns >= 10) { $turns_decrease = rand(1, 5); $target->turns = $target->turns - $turns_decrease; // Changed ice bolt to kill stealth. $target->subtractStatus(STEALTH); $target->subtractStatus(STALKING); $target->addStatus(SLOW); $msg = "Ice bolt cast on you by {$attacker_id}, your turns have been reduced by {$turns_decrease}."; Event::create($attacker_char_id, $target->id(), $msg); $generic_skill_result_message = "__TARGET__'s turns reduced by {$turns_decrease}!"; } else { $turn_cost = 0; $generic_skill_result_message = "__TARGET__ does not have enough turns for you to take."; } } } else { if ($act == 'Cold Steal') { if ($target->hasStatus(SLOW)) { $turn_cost = 0; $generic_skill_result_message = '__TARGET__ is already iced.'; } else { $critical_failure = rand(1, 100); if ($critical_failure > 7) { // *** If the critical failure rate wasn't hit. if ($target->turns >= 10) { $turns_diff = rand(2, 7); $target->turns = $target->turns - $turns_diff; $player->turns = $player->turns + $turns_diff; // Stolen $target->addStatus(SLOW); $msg = "You have had Cold Steal cast on you for {$turns_diff} by {$attacker_id}"; Event::create($attacker_char_id, $target->id(), $msg); $generic_skill_result_message = "You cast Cold Steal on __TARGET__ and take {$turns_diff} turns."; } else { $turn_cost = 0; $generic_skill_result_message = '__TARGET__ did not have enough turns to give you.'; } } else { // *** CRITICAL FAILURE !! $player->addStatus(FROZEN); $unfreeze_time = date('F j, Y, g:i a', mktime(date('G') + 1, 0, 0, date('m'), date('d'), date('Y'))); $failure_msg = "You have experienced a critical failure while using Cold Steal. You will be unfrozen on {$unfreeze_time}"; Event::create((int) 0, $player->id(), $failure_msg); $generic_skill_result_message = "Cold Steal has backfired! You are frozen until {$unfreeze_time}!"; } } } else { if ($act == 'Clone Kill') { // Obliterates the turns and the health of similar accounts that get clone killed. $reuse = false; // Don't give a reuse link. $clone1 = Player::findByName($target); $clone2 = Player::findByName($target2); if (!$clone1 || !$clone2) { $not_a_ninja = $target; if (!$clone2) { $not_a_ninja = $target2; } $generic_skill_result_message = "There is no such ninja as {$not_a_ninja}."; } elseif ($clone1->id() == $clone2->id()) { $generic_skill_result_message = '__TARGET__ is just the same ninja, so not the same thing as a clone at all.'; } elseif ($clone1->id() == $char_id || $clone2->id() == $char_id) { $generic_skill_result_message = 'You cannot clone kill yourself.'; } else { // The two potential clones will be obliterated immediately if the criteria are met in CloneKill. $kill_or_fail = CloneKill::kill($player, $clone1, $clone2); if ($kill_or_fail !== false) { $generic_skill_result_message = $kill_or_fail; } else { $generic_skill_result_message = "Those two ninja don't seem to be clones."; } } } } } } } } } } } } // ************************** Section applies to all skills ****************************** if (!$victim_alive) { // Someone died. if ($target->player_id == $player->player_id) { // Attacker killed themself. $loot = 0; $suicided = true; } else { // Attacker killed someone else. $killed_target = true; $gold_mod = 0.15; $loot = floor($gold_mod * $target->gold); $player->setGold($player->gold + $loot); $target->setGold($target->gold - $loot); $player->addKills(1); $added_bounty = floor($level_check / 5); if ($added_bounty > 0) { $player->setBounty($player->bounty + $added_bounty * 25); } else { if ($target->bounty > 0 && $target->id() !== $player->id()) { // No suicide bounty, No bounty when your bounty getting ++ed. $player->setGold($player->gold + $target->bounty); // Reward the bounty $target->setBounty(0); // Wipe the bounty } } $target_message = "{$attacker_id} has killed you with {$act} and taken {$loot} gold."; Event::create($attacker_char_id, $target->id(), $target_message); $attacker_message = "You have killed {$target} with {$act} and taken {$loot} gold."; Event::create($target->id(), $player->id(), $attacker_message); } } if (!$covert && $player->hasStatus(STEALTH)) { $player->subtractStatus(STEALTH); $destealthed = true; } $target->save(); } // End of the skill use SUCCESS block. $player->turns = $player->turns - max(0, $turn_cost); // Take the skill use cost. $player->save(); $ending_turns = $player->turns; $target_ending_health = $target->health; $target_name = $target->name(); $parts = get_defined_vars(); $options = ['quickstat' => 'player']; return new StreamedViewResponse('Skill Effect', 'skills_mod.tpl', $parts, $options); }
/** * Delete a clan after sending a message to all clan members. */ public function disband() { DatabaseConnection::getInstance(); $leader = $this->getLeaderID(); $message = "Your leader has disbanded your clan. You are alone again."; $statement = DatabaseConnection::$pdo->prepare("SELECT _player_id FROM clan_player WHERE _clan_id = :clan"); $statement->bindValue(':clan', $this->id); $statement->execute(); while ($data = $statement->fetch()) { $memberId = $data[0]; Message::create(['send_from' => $leader, 'send_to' => $memberId, 'message' => $message, 'type' => 0]); // Create both an event and a message! Event::create(0, $memberId, $message); } // Deletion of the clan_player connections should cascade from the deletion of the clan, at least ideally. $statement = DatabaseConnection::$pdo->prepare("DELETE FROM clan WHERE clan_id = :clan"); $statement->bindValue(':clan', $this->id); $statement->execute(); }
/** * Send out the killed messages. * * @return void */ private function sendKillMails(Player $attacker, Player $target, $attacker_label, $article, $item, $loot) { $target_email_msg = "You have been killed by {$attacker_label} with {$article} {$item} and lost {$loot} gold."; Event::create($attacker->name() === $attacker_label ? $attacker->id() : 0, $target->id(), $target_email_msg); $user_email_msg = "You have killed " . $target->name() . " with {$article} {$item} and received {$loot} gold."; Event::create($target->id(), $attacker->id(), $user_email_msg); }
/** * Leveling up Function * * @return boolean */ public function levelUp() { $health_to_add = 100; $turns_to_give = 50; $ki_to_give = 50; $stat_value_to_add = 5; $karma_to_give = 1; if ($this->isAdmin()) { // If the character is an admin, do not auto-level return false; } else { // For normal characters, do auto-level // Have to be under the max level and have enough kills. $level_up_possible = $this->level + 1 <= MAX_PLAYER_LEVEL && $this->kills >= $this->killsRequiredForNextLevel(); if ($level_up_possible) { // Perform the level up actions $this->setHealth($this->health + $health_to_add); $this->setTurns($this->turns + $turns_to_give); $this->setKi($this->ki + $ki_to_give); // Must read from VO for these as accessors return modified values $this->setStamina($this->vo->stamina + $stat_value_to_add); $this->setStrength($this->vo->strength + $stat_value_to_add); $this->setSpeed($this->vo->speed + $stat_value_to_add); // no mutator for these yet $this->vo->kills = max(0, $this->kills - $this->killsRequiredForNextLevel()); $this->vo->karma = $this->karma + $karma_to_give; $this->vo->level = $this->level + 1; $this->save(); GameLog::recordLevelUp($this->id()); $account = Account::findByChar($this); $account->setKarmaTotal($account->getKarmaTotal() + $karma_to_give); $account->save(); // Send a level-up message, for those times when auto-levelling happens. Event::create($this->id(), $this->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 {$karma_to_give}, and your Ki raised {$ki_to_give}! You gained some health and turns, as well! You are now a level {$this->level} ninja! Go kill some stuff."); return true; } else { return false; } } }