/** * 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() { $in_quantity = in('quantity'); $in_item = in('item'); $gold = get_gold($this->sessionData['char_id']); $current_item_cost = 0; $no_funny_business = false; // Pull the item info from the database $item_costs = $this->itemForSaleCosts(); $item = getItemByID(item_id_from_display_name($in_item)); $quantity = whichever(positive_int($in_quantity), 1); $item_text = null; if ($item instanceof Item) { $item_text = $quantity > 1 ? $item->getPluralName() : $item->getName(); $purchaseOrder = new PurchaseOrder(); // Determine the quantity from input or as a fallback, default of 1. $purchaseOrder->quantity = $quantity; $purchaseOrder->item = $item; $potential_cost = isset($item_costs[$purchaseOrder->item->identity()]['item_cost']) ? $item_costs[$purchaseOrder->item->identity()]['item_cost'] : null; $current_item_cost = first_value($potential_cost, 0); $current_item_cost = $current_item_cost * $purchaseOrder->quantity; if (!$this->sessionData['char_id'] || !$purchaseOrder->item || $purchaseOrder->quantity < 1) { $no_funny_business = true; } else { if ($gold >= $current_item_cost) { // Has enough gold. try { add_item($this->sessionData['char_id'], $purchaseOrder->item->identity(), $purchaseOrder->quantity); subtract_gold($this->sessionData['char_id'], $current_item_cost); } catch (\Exception $e) { $invalid_item = $e->getMessage(); error_log('Invalid Item attempted :' . $invalid_item); $no_funny_business = true; } } } } else { $no_funny_business = true; } $parts = array('current_item_cost' => $current_item_cost, 'quantity' => $quantity, 'item_text' => $item_text, 'no_funny_business' => $no_funny_business, 'view_part' => 'buy'); return $this->render($parts); }
/** * Display the main admin area * * Includes player viewing, account duplicates checking, npc balacing * * @return ViewSpec|RedirectResponse */ public function index() { $result = $this->requireAdmin($this->self); if ($result instanceof RedirectResponse) { return $result; } $viewChar = null; // View a target non-self character $charName = in('char_name'); if (is_string($charName) && trim($charName)) { $viewChar = get_char_id($charName); } // If a request is made to view a character's info, show it. $viewChar = first_value($viewChar, in('view')); $dupes = AdminViews::duped_ips(); $stats = AdminViews::high_rollers(); $npcs = NpcFactory::allNonTrivialNpcs(); $trivialNpcs = NpcFactory::allTrivialNpcs(); $charInfos = null; $charInventory = null; $firstMessage = null; $firstChar = null; $firstAccount = null; $firstDescription = null; if ($viewChar) { $ids = explode(',', $viewChar); $firstChar = new Player(reset($ids)); $firstAccount = AccountFactory::findByChar($firstChar); $charInfos = AdminViews::split_char_infos($viewChar); $charInventory = AdminViews::char_inventory($viewChar); $firstMessage = $firstChar->message(); $firstDescription = $firstChar->description(); } $parts = ['stats' => $stats, 'first_char' => $firstChar, 'first_description' => $firstDescription, 'first_message' => $firstMessage, 'first_account' => $firstAccount, 'char_infos' => $charInfos, 'dupes' => $dupes, 'char_inventory' => $charInventory, 'char_name' => $charName, 'npcs' => $npcs, 'trivial_npcs' => $trivialNpcs]; return ['title' => 'Admin Actions', 'template' => 'ninjamaster.tpl', 'parts' => $parts, 'options' => null]; }
<?php use NinjaWars\core\control\ApiController; // How to call: http://nw.local/api.php?type=char_search&jsoncallback=alert&term=tchalvak&limit=10 // http://nw.local/api.php?type=facebook_login_sync&jsoncallback=alert // Can actually just use a script source for this, e.g.: // <script src="/api.php?type=char_search&jsoncallback=alert&term=tchalvak&limit=10"></script> $api = new ApiController(); $api->sendHeaders(); $result = $api->nw_json(in('type'), first_value(in('jsoncallback'), in('callback'))); // This is needed to keep output code out of the controller if ($result === json_encode(false)) { header('Content-Type: application/json; charset=utf8'); } echo $result;
protected function do_get_tables_list() { // **TODO**: read about 'sqlite_temp_master' $cmd = new SDBCommand("SELECT \"name\" FROM sqlite_master WHERE \"type\"='table'"); $res = $cmd->get_all(); $arr = array(); foreach ($res as $row) { $name = first_value($row); if (strlen($this->prefix) && strpos($name, $this->prefix) === 0) { $name = substr($name, strlen($this->prefix)); } $arr[] = $name; } return $arr; }
public function index() { $target = $player = first_value(in('ninja'), in('player'), in('find'), in('target')); $target_id = first_value(in('target_id'), in('player_id'), get_char_id($target)); // Find target_id if possible. $target_player_obj = Player::find($target_id); $viewed_name_for_title = null; if ($target_player_obj !== null) { $viewed_name_for_title = $target_player_obj->name(); } if ($target_player_obj === null) { $template = 'no-player.tpl'; $parts = array(); } else { $player_info = $target_player_obj->as_array(); // Pull the info out of the object. if (!$player_info) { $template = 'no-player.tpl'; $parts = array(); } else { $viewing_player_obj = Player::find(self_char_id()); $self = self_char_id() && self_char_id() == $player_info['player_id']; // Record whether this is a self-viewing. if ($viewing_player_obj !== null) { $char_info = $viewing_player_obj->dataWithClan(); $char_id = $viewing_player_obj->id(); $username = $viewing_player_obj->name(); } else { $char_info = []; } $player = $target = $player_info['uname']; // reset the target and target_id vars. $target_id = $player_info['player_id']; // Get the player's kills for this date. $kills_today = query_item('select sum(killpoints) from levelling_log where _player_id = :player_id and killsdate = CURRENT_DATE and killpoints > 0', array(':player_id' => $target_id)); $viewers_clan = $viewing_player_obj !== null ? ClanFactory::clanOfMember($viewing_player_obj) : null; // Attack Legal section $params = array('required_turns' => 0, 'ignores_stealth' => true); // 0 for unstealth. $attack_error = 'You must become a ninja first.'; $attack_allowed = false; if (null !== $viewing_player_obj) { $AttackLegal = new AttackLegal($viewing_player_obj, $target_player_obj, $params); $attack_allowed = $AttackLegal->check(false); $attack_error = $AttackLegal->getError(); } $sel_rank_spot = "SELECT rank_id FROM rankings WHERE player_id = :char_id limit 1"; $rank_spot = query_item($sel_rank_spot, array(':char_id' => $player_info['player_id'])); // Display the player info. $status_list = get_status_list($player); $gurl = $gravatar_url = $target_player_obj->avatarUrl(); if ($viewing_player_obj !== null && !$attack_error && !$self) { // They're not dead or otherwise unattackable. // Attack or Duel $skillDAO = new SkillDAO(); $is_admin = false; if ($viewing_player_obj) { $is_admin = $viewing_player_obj->isAdmin(); } if (!$is_admin) { $combat_skills = $skillDAO->getSkillsByTypeAndClass($viewing_player_obj->_class_id, 'combat', $viewing_player_obj->level); $targeted_skills = $skillDAO->getSkillsByTypeAndClass($viewing_player_obj->_class_id, 'targeted', $viewing_player_obj->level); } else { $combat_skills = $skillDAO->all('combat'); $targeted_skills = $skillDAO->all('targeted'); } // Pull the items and some necessary data about them. $items = inventory_counts($char_id); $valid_items = rco($items); // row count } // End of the there-was-no-attack-error section $set_bounty_section = ''; $communication_section = ''; $player_clan_section = ''; $clan = ClanFactory::clanOfMember($player_info['player_id']); $same_clan = false; // Player clan and clan members if ($clan) { $viewer_clan = $viewing_player_obj ? ClanFactory::clanOfMember($viewing_player_obj) : null; $clan_id = $clan->getID(); $clan_name = $clan->getName(); if ($viewer_clan) { $same_clan = $clan->getID() == $viewer_clan->getID(); $display_clan_options = $viewing_player_obj && !$self && $same_clan && $viewing_player_obj->isClanLeader(); } else { $same_clan = $display_clan_options = false; } } // Send the info to the template. $template = 'player.tpl'; $parts = get_certain_vars(get_defined_vars(), array('char_info', 'viewing_player_obj', 'target_player_obj', 'combat_skills', 'targeted_skills', 'player_info', 'self', 'rank_spot', 'kills_today', 'gravatar_url', 'status_list', 'clan', 'items')); } } return ['template' => $template, 'title' => 'Ninja' . ($viewed_name_for_title ? ": {$viewed_name_for_title}" : ' Profile'), 'parts' => $parts, 'options' => ['quickstat' => 'player']]; }
} $npc_template = 'npc.oni.tpl'; $combat_data = array('victory' => $oni_killed); } elseif (array_key_exists($victim, $npcs)) { /**** Abstracted NPCs *****/ $npc_stats = $npcs[$victim]; // Pull an npcs individual stats with generic fallbacks. /* ============= STANDARD NPCS ======================= */ $npco = new Npc($npc_stats); // Construct the npc object. $display_name = first_value(@$npc_stats['name'], ucfirst($victim)); $max_damage = $npco->max_damage(); $percent_damage = null; // Percent damage does to the player's health. $status_effect = whichever(@$npc_stats['status'], null); $reward_item = first_value(@$npc_stats['item'], null); $base_gold = $npco->gold(); $npc_gold = (int) @$npc_stats['gold']; $is_quick = $npco->speed() > $player->speed() ? true : false; // Beyond basic speed and they see you coming, so show that message. // If npc gold explicitly set to 0, then none will be given. $reward_gold = $npc_gold === 0 ? 0 : ($reward_item ? round($base_gold * 0.9) : $base_gold); // Hack a little off reward gold if items received. $bounty_mod = @$npc_stats['bounty']; $is_villager = $npco->has_trait('villager'); // Give the villager message with the bounty. $is_weaker = $npco->strength() * 3 < $player->strength(); // Npc much weaker? $is_stronger = $npco->strength() > $player->strength() * 2; // Npc More than twice as strong? //debug($is_villager, $npco->traits, $npco);die();
public static function getPostOrGet($val) { return first_value(static::getPost($val), static::get($val)); }
<?php require_once LIB_ROOT . 'control/lib_player_list.php'; require_once LIB_ROOT . 'control/lib_api.php'; // How to call: http://nw.local/api.php?type=char_search&jsoncallback=alert&term=tchalvak&limit=10 // http://nw.local/api.php?type=facebook_login_sync&jsoncallback=alert // Can actually just use a scrypt source for this, e.g.: // <script src="/api.php?type=char_search&jsoncallback=alert&term=tchalvak&limit=10"></script> // All the functions used by api.php are now in control/lib_api.php // Json P headers header('Content-Type: text/javascript; charset=utf8'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Max-Age: 3628800'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); $type = in('type'); $dirty_jsoncallback = first_value(in('jsoncallback'), in('callback')); echo nw_json($type, $dirty_jsoncallback); // Types are whitelisted, the callback is filtered // Make sure to default to private, just as a security reminder.
function display_static_page($page, $pages, $vars = array(), $options = array()) { if (!isset($pages[$page])) { // Unlisted page requested. error_log(' Invalid page (' . $page . ') requested on page.php.'); display_page('404.tpl', '404'); } else { if (!is_array($pages[$page])) { $template = "page." . $page . ".tpl"; $title = $page; // Display_page will prepend with 'Ninja Wars: ' } else { $page_info = $pages[$page]; $template = first_value(@$page_info['template'], "page." . $page . ".tpl"); $title = $page_info['title']; $callback = @$page_info['callback']; // TODO: Merge the vars array instead of overwriting. if ($callback && function_exists($callback)) { $vars = array_merge($callback(), $vars); // Call the callback to return the vars. } } cache_headers(24); // 24 hour caching. display_page($template, $title, $vars, $options); } }
$private = true; $alive = true; if ($error = init($private, $alive)) { display_error($error); die; } else { $link_back = in('link_back'); $selfTarget = in('selfTarget'); $item_in = in('item'); // Item identifier, either it's id or internal name $give = in('give'); $target_id = in('target_id'); $in_target = in('target'); ///TODO clean up this travesty $target_id = $target_id ? (int) $target_id : self_char_id(); $target = first_value($target_id, $in_target); $target_id = whichever($target_id, get_char_id($target)); $give = in_array($give, array('on', 'Give')); $user_id = self_char_id(); $player = new Player($user_id); $victim_alive = true; $using_item = true; $item_used = true; $stealthLost = false; $error = false; $suicide = false; $kill = false; $repeat = false; $ending_turns = null; $turns_change = null; $turns_to_take = null;
protected function do_get_tables_list() { $cmd = new SDBCommand("SHOW TABLES FROM @dbname"); $cmd->set('dbname', conf('db.name'), SDB::TableName); $res = $this->get_all($cmd); $arr = array(); foreach ($res as $row) { $name = first_value($row); if (strlen($this->prefix) && strpos($name, $this->prefix) === 0) { $name = substr($name, strlen($this->prefix)); } $arr[] = $name; } return $arr; }
/** * Handle Standard Abstract Npcs * * @param String $victim * @param Player $player * @param Array $npcs * @return array [$npc_template, $combat_data] */ private function attackAbstractNpc($victim, Player $player, $npcs) { $npc_stats = $npcs[$victim]; // Pull an npcs individual stats with generic fallbacks. $npco = new Npc($npc_stats); // Construct the npc object. $display_name = first_value(isset($npc_stats['name']) ? $npc_stats['name'] : null, ucfirst($victim)); $status_effect = isset($npc_stats['status']) ? $npc_stats['status'] : null; // TODO: Calculate and display damage verbs $reward_item = isset($npc_stats['item']) && $npc_stats['item'] ? $npc_stats['item'] : null; $is_quick = (bool) ($npco->speed() > $player->speed()); // Beyond basic speed and they see you coming, so show that message. $bounty_mod = isset($npc_stats['bounty']) ? $npc_stats['bounty'] : null; $is_weaker = $npco->strength() * 3 < $player->strength(); // Npc much weaker? $is_stronger = $npco->strength() > $player->strength() * 3; // Npc More than twice as strong? $image = isset($npc_stats['img']) ? $npc_stats['img'] : null; // Assume defeat... $victory = false; $received_gold = null; $received_display_items = null; $added_bounty = null; $is_rewarded = null; // Gets items or gold. $display_statuses = $display_statuses_classes = null; $image_path = null; // If the image exists, set the path to it for use on the page. if ($image && file_exists(SERVER_ROOT . 'www/images/characters/' . $image)) { $image_path = IMAGE_ROOT . 'characters/' . $image; } // ******* FIGHT Logic *********** $npc_damage = $npco->damage(); $survive_fight = $player->harm($npc_damage); $kill_npc = $npco->health() < $player->damage(); if ($survive_fight > 0) { // The ninja survived, they get any gold the npc has. $received_gold = $this->calcReceivedGold($npco, (bool) $reward_item); $player->set_gold($player->gold() + $received_gold); $received_display_items = array(); if ($kill_npc) { $victory = true; // Victory occurred, reward the poor sap. if ($npco->inventory()) { foreach (array_keys($npco->inventory()) as $l_item) { $item_info = item_info_from_identity($l_item); $received_display_items[] = $item_info['item_display_name']; add_item($player->id(), $item_info['item_internal_name'], 1); } } // Add bounty where applicable. if ((bool) $bounty_mod && $player->level > self::MIN_LEVEL_FOR_BOUNTY && $player->level <= self::MAX_LEVEL_FOR_BOUNTY) { $added_bounty = floor($player->level / 3 * $bounty_mod); $player->set_bounty($player->bounty() + $added_bounty); } } $is_rewarded = (bool) $received_gold || (bool) count($received_display_items); if (isset($npc_stats['status']) && null !== $npc_stats['status']) { $player->addStatus($npc_stats['status']); // Get the statuses and status classes for display. $display_statuses = implode(', ', get_status_list()); $display_statuses_classes = implode(' ', get_status_list()); // TODO: Take healthy out of the list since it's redundant. } } $player->save(); return ['npc.abstract.tpl', ['victim' => $victim, 'display_name' => $display_name, 'attack_damage' => $npc_damage, 'status_effect' => $status_effect, 'display_statuses' => $display_statuses, 'display_statuses_classes' => $display_statuses_classes, 'received_gold' => $received_gold, 'received_display_items' => $received_display_items, 'is_rewarded' => $is_rewarded, 'victory' => $victory, 'survive_fight' => $survive_fight, 'kill_npc' => $kill_npc, 'image_path' => $image_path, 'npc_stats' => $npc_stats, 'is_quick' => $is_quick, 'added_bounty' => $added_bounty, 'is_villager' => $npco->has_trait('villager'), 'race' => $npco->race(), 'is_weaker' => $is_weaker, 'is_stronger' => $is_stronger]]; }
<?php require_once LIB_ROOT . 'control/lib_inventory.php'; $private = false; $alive = false; if ($error = init($private, $alive)) { display_error($error); } else { require_once LIB_ROOT . "control/Skill.php"; require_once DB_ROOT . "SkillDAO.class.php"; require_once LIB_ROOT . "control/lib_clan.php"; require_once LIB_ROOT . "control/lib_player.php"; $target = $player = first_value(in('ninja'), in('player'), in('find'), in('target')); $target_id = first_value(in('target_id'), in('player_id'), get_char_id($target)); // Find target_id if possible. $target_player_obj = new Player($target_id); $viewed_name_for_title = null; if ($target_player_obj && $target_player_obj->name()) { $viewed_name_for_title = $target_player_obj->name(); } $combat_toggles = get_setting('combat_toggles'); // Pull the attack options toggled on and off. $last_item_used = get_setting("last_item_used"); // Pull the last item id used, if any. $char_info = self_info(); if (!$target_player_obj || !$target_player_obj->id() || !$target_player_obj->isActive()) { $template = 'no-player.tpl'; $parts = array(); } else { $player_info = $target_player_obj->as_array(); // Pull the info out of the object.