public static function canKill($clone1, $clone2) { // Input is transformed into if (!$clone1 instanceof Player) { if ($clone1 == Filter::toNonNegativeInt($clone1)) { $char1 = Player::find($clone1); } elseif (is_string($clone1)) { $char1 = Player::find($clone1); } } else { $char1 = $clone1; } if (!$clone2 instanceof Player) { if ($clone2 == Filter::toNonNegativeInt($clone2)) { $char2 = Player::find($clone2); } elseif (is_string($clone2)) { $char2 = Player::find($clone2); } } else { $char2 = $clone2; } // Reject invalid/nonexistent characters if ($char1 === null || $char2 === null) { return false; } // Reject same character if ($char1->id() == $char2->id()) { return false; } // Don't clone kill admins. if ($char1->isAdmin() || $char2->isAdmin()) { return false; } // Reject inactive characters if (!$char1->isActive() || !$char2->isActive()) { return false; } // TODO: Reject inoperative characters // TODO: You can't clone kill yourself.. $host = gethostname(); $server_ip = gethostbyname($host); $untouchable_ips = ['127.0.0.1', '173.203.99.229', $server_ip, '', null]; $account1 = Account::findByChar($char1); $account2 = Account::findByChar($char2); // Reject invalid custom ips if (in_array($account1->getLastIp(), $untouchable_ips) || in_array($account2->getLastIp(), $untouchable_ips)) { return false; } // If characters have the same joint account, and have been logged in recently... if ($account1->getLastIp() === $account2->getLastIp()) { // Activity was already tested above. return true; } return false; }
public function testCreatePlayerObjectHasUsefulInfo() { $char = Player::find($this->char_id); $this->assertTrue((bool) Filter::toNonNegativeInt($char->health)); $this->assertTrue((bool) Filter::toNonNegativeInt($char->getSpeed())); $this->assertTrue((bool) Filter::toNonNegativeInt($char->getStamina())); $this->assertTrue((bool) Filter::toNonNegativeInt($char->getStrength())); $this->assertTrue((bool) Filter::toNonNegativeInt($char->level)); $this->assertNotEmpty($char->name()); $this->assertTrue((bool) Filter::toNonNegativeInt($char->damage())); }
/** * Get all the chat messages info. */ private function getChats($chatlength = null) { $chatlength = Filter::toNonNegativeInt($chatlength); // Prevent negatives. $limit = $chatlength ? 'LIMIT :limit' : ''; $bindings = []; if ($limit) { $bindings[':limit'] = $chatlength; } $chats = query("SELECT sender_id, uname, message, date, age(now(), date) AS ago FROM chat\n JOIN players ON chat.sender_id = player_id ORDER BY chat_id DESC " . $limit, $bindings); return $chats; }
/** * Command for current user to purchase a quantity of a specific item * * @param quantity int The quantity of the item to purchase * @param item string The identity of the item to purchase * @return Array */ public function buy() { $request = RequestWrapper::$request; $in_quantity = $request->get('quantity'); $in_item = $request->get('item'); $player = Player::findPlayable($this->getAccountId()); $gold = $player ? $player->gold : null; $current_item_cost = 0; $no_funny_business = false; $no_such_item = false; $item = Item::findByIdentity($in_item); $quantity = max(Filter::toNonNegativeInt($in_quantity), self::DEFAULT_QUANTITY); $item_text = null; $valid = false; if (!$item instanceof Item) { $no_such_item = true; } else { $item_text = $quantity > 1 ? $item->getPluralName() : $item->getName(); $purchase_order = new PurchaseOrder(); // Determine the quantity from input or as a fallback, default of 1. $purchase_order->quantity = $quantity; $purchase_order->item = $item; $current_item_cost = $this->calculatePrice($purchase_order); if (!$player) { $no_funny_business = true; } elseif (!$purchase_order->item || $purchase_order->quantity < 1) { $no_such_item = true; } else { if ($gold >= $current_item_cost) { // Has enough gold. try { $inventory = new Inventory($player); $inventory->add($purchase_order->item->identity(), $purchase_order->quantity); $player->setGold($player->gold - $current_item_cost); $player->save(); $valid = true; } catch (\Exception $e) { $invalid_item = $e->getMessage(); error_log('Invalid Item attempted :' . $invalid_item); $no_funny_business = true; } } } } $parts = array('current_item_cost' => $current_item_cost, 'quantity' => $quantity, 'item_text' => $item_text, 'no_funny_business' => $no_funny_business, 'no_such_item' => $no_such_item, 'valid' => $valid, 'view_part' => 'buy'); return $this->render($parts); }
/** * Take in a url parameter of work and try to convert it to gold * * @param Container * @return StreamedViewResponse */ public function requestWork(Container $p_dependencies) { $earned = 0; $worked = Filter::toNonNegativeInt(RequestWrapper::getPostOrGet('worked')); // No negative work. $char = $p_dependencies['current_player']; if (!$char) { return new RedirectResponse('/work'); } $sufficient_turns = $worked <= $char->turns; if ($sufficient_turns) { $earned = $worked * self::WORK_MULTIPLIER; // calc amount worked $char->setGold($char->gold + $earned); $char->setTurns($char->turns - $worked); $char->save(); } $parts = ['recommended_to_work' => $worked, 'worked' => $worked, 'work_multiplier' => self::WORK_MULTIPLIER, 'authenticated' => $p_dependencies['session']->get('authenticated', false), 'gold_display' => number_format($char->gold), 'earned_gold' => number_format($earned), 'not_enough_energy' => !$sufficient_turns]; return $this->render($parts); }
public function testFilterToSimple() { $this->assertEquals('boba', Filter::toSimple("bobaä€")); $this->assertEquals("!@#^&()_+--", Filter::toSimple("!@#^&()_+'''\"\"''--")); }
/** * Handle account email confirmation * * @return Response */ public function confirm(Container $p_dependencies) { $request = RequestWrapper::$request; $admin_override_pass = '******'; // Just a weak passphrase for simply confirming players. $admin_override_request = $request->get('admin_override'); $acceptable_admin_override = $admin_override_pass === $admin_override_request; $confirm = $request->get('confirm'); $aid = Filter::toNonNegativeInt($request->get('aid')); $data = query_row(' SELECT player_id, uname, accounts.verification_number as verification_number, CASE WHEN active = 1 THEN 1 ELSE 0 END AS active, accounts.active_email, CASE WHEN accounts.confirmed = 1 THEN 1 ELSE 0 END as confirmed, status, member, days, players.created_date FROM accounts JOIN account_players ON _account_id = account_id JOIN players ON _player_id = player_id WHERE account_id = :acctId', array(':acctId' => $aid)); if (count($data)) { $check = $data['verification_number']; $confirmed = $data['confirmed']; $username = $data['uname']; } else { $check = $confirmed = $username = null; } $confirmation_confirmed = false; if ($confirmed != 1 && ($check && $confirm && $confirm == $check || $acceptable_admin_override)) { // Confirmation number not null and matches // or the admin override was met. query('UPDATE accounts SET operational = true, confirmed=1 WHERE account_id = :accountID', array(':accountID' => $aid)); $statement = DatabaseConnection::$pdo->prepare('UPDATE players SET active = 1 WHERE player_id in (SELECT _player_id FROM account_players WHERE _account_id = :accountID)'); $statement->bindValue(':accountID', $aid); $statement->execute(); // todo - test for success $confirmation_confirmed = true; } $parts = ['confirmed' => $confirmed, 'username' => $username, 'confirmation_confirmed' => $confirmation_confirmed]; $options = ['quickstat' => false]; return new StreamedViewResponse('Account Confirmation', 'assistance.confirm.tpl', $parts, $options); }
/** * 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); }
/** * @param int $type */ public function setType($type) { $cast_type = Filter::toNonNegativeInt($type); if ($cast_type != $type) { throw new \Exception('Account: The account type set was inappropriate.'); } $this->type = $cast_type; return $this->type; }
function testMakeSureThatNinjaAccountIsOperationalByDefault() { $ninja_id = $this->test_ninja_id; $this->assertTrue(Filter::toNonNegativeInt($ninja_id) > 0); $account_operational = query_item('SELECT operational FROM accounts JOIN account_players ON account_id = _account_id WHERE _player_id = :char_id', [':char_id' => $ninja_id]); $this->assertTrue($account_operational, 'Account is not being set as operational by default when created'); }
/** * Helper to find a player by either id or name * * @return Player */ private function findPlayer($token) { if (Filter::toNonNegativeInt($token)) { $target = Player::find(Filter::toNonNegativeInt($token)); } else { $target = Player::findByName($token); } return $target; }