コード例 #1
0
 /**
  * Update profile
  *
  * @param Container
  */
 public function updateProfile(Container $p_dependencies)
 {
     $char = $p_dependencies['current_player'];
     $new_profile = trim(RequestWrapper::getPostOrGet('newprofile', null));
     $profile_changed = false;
     $error = '';
     if (!empty($new_profile)) {
         DatabaseConnection::getInstance();
         $statement = DatabaseConnection::$pdo->prepare('UPDATE players SET messages = :profile WHERE player_id = :char_id');
         $statement->bindValue(':profile', $new_profile);
         $statement->bindValue(':char_id', $char->id());
         $statement->execute();
         $profile_changed = true;
     } else {
         $error = 'Cannot enter a blank profile.';
     }
     $query_str = [];
     if ($profile_changed) {
         $query_str['profile_changed'] = 1;
     } else {
         $query_str['error'] = $error;
     }
     $raw_query_str = count($query_str) ? '?' . http_build_query($query_str, null, '&') : null;
     return new RedirectResponse('/stats' . $raw_query_str);
 }
コード例 #2
0
 public function testInputWithinEnvironment()
 {
     $id = RequestWrapper::getPostOrGet('id');
     $this->assertEquals(7, $id);
     $default_result = RequestWrapper::getPostOrGet('doesnotexist', 5);
     $this->assertEquals(5, $default_result);
 }
コード例 #3
0
 /**
  * Display the initial listing of skills for self-use
  *
  * @return Array
  */
 public function index()
 {
     $error = RequestWrapper::getPostOrGet('error');
     $skillsListObj = new Skill();
     $player = $this->player;
     $starting_turns = $player->turns;
     $starting_ki = $player->ki;
     $status_list = Player::getStatusList();
     $no_skills = true;
     $stealth = $skillsListObj->hasSkill('Stealth', $player);
     if ($stealth) {
         $no_skills = false;
     }
     $stealth_turn_cost = $skillsListObj->getTurnCost('Stealth');
     $unstealth_turn_cost = $skillsListObj->getTurnCost('Unstealth');
     $stalk = $skillsListObj->hasSkill('Stalk', $player);
     $stalk_turn_cost = $skillsListObj->getTurnCost('Stalk');
     $chi = $skillsListObj->hasSkill('Chi', $player);
     $speed = $skillsListObj->hasSkill('speed', $player);
     $hidden_resurrect = $skillsListObj->hasSkill('hidden resurrect', $player);
     $midnight_heal = $skillsListObj->hasSkill('midnight heal', $player);
     $kampo_turn_cost = $skillsListObj->getTurnCost('Kampo');
     $kampo = $skillsListObj->hasSkill('kampo', $player);
     $heal = $skillsListObj->hasSkill('heal', $player);
     $heal_turn_cost = $skillsListObj->getTurnCost('heal');
     $clone_kill = $skillsListObj->hasSkill('clone kill', $player);
     $clone_kill_turn_cost = $skillsListObj->getTurnCost('clone kill');
     $wrath = $skillsListObj->hasSkill('wrath', $player);
     $can_harmonize = $starting_ki;
     $parts = ['error' => $error, 'status_list' => $status_list, 'player' => $player, 'no_skills' => $no_skills, 'starting_turns' => $starting_turns, 'starting_ki' => $starting_ki, 'stealth' => $stealth, 'stealth_turn_cost' => $stealth_turn_cost, 'unstealth_turn_cost' => $unstealth_turn_cost, 'stalk' => $stalk, 'stalk_turn_cost' => $stalk_turn_cost, 'chi' => $chi, 'speed' => $speed, 'hidden_resurrect' => $hidden_resurrect, 'midnight_heal' => $midnight_heal, 'kampo_turn_cost' => $kampo_turn_cost, 'kampo' => $kampo, 'heal' => $heal, 'heal_turn_cost' => $heal_turn_cost, 'clone_kill' => $clone_kill, 'clone_kill_turn_cost' => $clone_kill_turn_cost, 'wrath' => $wrath, 'can_harmonize' => $can_harmonize];
     return new StreamedViewResponse('Your Skills', 'skills.tpl', $parts, ['quickstat' => 'player']);
 }
コード例 #4
0
 /**
  * User command for betting on the coin toss game in the casino
  *
  * @param Container
  * @param bet int The amount of money to bet on the coin toss game
  * @return Response
  *
  * @note
  * If the player bets within ~1% of the maximum bet, they will receive a
  * reward item
  */
 public function bet(Container $p_dependencies)
 {
     $player = $p_dependencies['current_player'];
     if (!$player) {
         return new RedirectResponse('/casino/?error=' . rawurlencode('Become a ninja first!'));
     }
     $bet = intval(RequestWrapper::getPostOrGet('bet'));
     $pageParts = ['reminder-max-bet'];
     if ($bet < 0) {
         $pageParts = ['result-cheat'];
         $player->harm(self::CHEAT_DMG);
     } else {
         if ($bet > $player->gold) {
             $pageParts = ['result-no-gold'];
         } else {
             if ($bet > 0 && $bet <= self::MAX_BET) {
                 if (rand(0, 1) === 1) {
                     $pageParts = ['result-win'];
                     $player->setGold($player->gold + $bet);
                     if ($bet >= round(self::MAX_BET * 0.99)) {
                         // within about 1% of the max bet & you win, you get a reward item.
                         $inventory = new Inventory($player);
                         $inventory->add(self::REWARD, 1);
                     }
                 } else {
                     $player->setGold($player->gold - $bet);
                     $pageParts = ['result-lose'];
                 }
             }
         }
     }
     $player->save();
     return $this->render(['pageParts' => $pageParts, 'player' => $player]);
 }
コード例 #5
0
 /**
  * Take an enemy off a pc's list.
  */
 public function deleteEnemy(Container $p_dependencies)
 {
     $enemy = Player::find(RequestWrapper::getPostOrGet('remove_enemy'));
     if ($enemy) {
         $this->removeEnemyFromPlayer(Player::find(SessionFactory::getSession()->get('player_id')), $enemy);
     }
     return new RedirectResponse('/enemies');
 }
コード例 #6
0
ファイル: lib_input.php プロジェクト: NinjaWars/ninjawars
/**
 * Input function that by default LEAVES INPUT COMPLETELY UNFILTERED
 * To not filter some input, you have to explicitly pass in null for the third parameter,
 * e.g. in('some_url_parameter', null, null)
 */
function in($var_name, $default_val = null, $filter_callback = null)
{
    $req = RequestWrapper::getPostOrGet($var_name);
    $result = isset($req) ? $req : $default_val;
    // Check that the filter function sent in exists.
    if ($filter_callback && function_exists($filter_callback)) {
        $result = $filter_callback($result);
    }
    return $result;
}
コード例 #7
0
 /**
  * Display standard login page.
  */
 public function index(Container $p_dependencies)
 {
     $login_error_message = RequestWrapper::getPostOrGet('error');
     // Error to display after unsuccessful login and redirection.
     $stored_username = isset($_COOKIE['username']) ? $_COOKIE['username'] : null;
     $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
     if (SessionFactory::getSession()->get('authenticated', false)) {
         return new RedirectResponse(WEB_ROOT);
     } else {
         $parts = ['authenticated' => false, 'login_error_message' => $login_error_message, 'referrer' => $referrer, 'stored_username' => $stored_username];
         return $this->render('Login', $parts);
     }
 }
コード例 #8
0
ファイル: ChatController.php プロジェクト: BitLucid/ninjawars
 /**
  * Take in a chat and record it to the database.
  *
  * @return Response
  */
 public function receive()
 {
     $char_id = SessionFactory::getSession()->get('player_id');
     $message = RequestWrapper::getPostOrGet('message');
     $error = null;
     if (!empty($message)) {
         if ($char_id) {
             Message::sendChat($char_id, $message);
         } else {
             $error = 'You must be logged in to chat.';
         }
     }
     return new RedirectResponse('/village/' . ($error ? '?error=' . rawurlencode($error) : ''));
 }
コード例 #9
0
 /**
  * View clan messages
  *
  * @param Container
  */
 public function viewClan(Container $p_dependencies)
 {
     $ninja = $p_dependencies['current_player'];
     $page = max(1, (int) RequestWrapper::getPostOrGet('page'));
     $limit = 25;
     $offset = ($page - 1) * $limit;
     $type = 1;
     // Clan chat or normal messages.
     $message_count = Message::countByReceiver($ninja, $type);
     // To count all the messages
     Message::markAsRead($ninja, $type);
     // mark messages as read for next viewing.
     $parts = array_merge($this->configure(), ['messages' => Message::findByReceiver($ninja, $type, $limit, $offset), 'message_count' => $message_count, 'pages' => ceil($message_count / $limit), 'current_page' => $page, 'current_tab' => 'clan', 'has_clan' => (bool) Clan::findByMember($ninja)]);
     return $this->render($parts, 'Clan Messages');
 }
コード例 #10
0
ファイル: NewsController.php プロジェクト: BitLucid/ninjawars
 /**
  * Create new post
  */
 public function create()
 {
     try {
         $create_role = $this->hasCreateRole($this->pc);
     } catch (InvalidArgumentException $e) {
         $error = "Sorry, you must be logged in to create a news post.";
         return new RedirectResponse('/news/?error=' . rawurlencode($error));
     }
     if (!$create_role) {
         $error = 'Sorry, you do not have permission to create a news post.';
         return new RedirectResponse('/news/?error=' . rawurlencode($error));
     }
     $title = 'Make New Post';
     $error = (bool) RequestWrapper::getPostOrGet('error');
     $parts = ['error' => $error, 'heading' => $title, 'authenticated' => SessionFactory::getSession()->get('authenticated', false)];
     return new StreamedViewResponse($title, 'news.create.tpl', $parts);
 }
コード例 #11
0
ファイル: WorkController.php プロジェクト: BitLucid/ninjawars
 /**
  * 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);
 }
コード例 #12
0
 /**
  * Use an item on a target
  *
  * http://nw.local/item/use/shuriken/10/
  *
  * @return Response
  * @note
  * /use/ is aliased to useItem externally because use is a php reserved keyword
  */
 public function useItem(Container $p_dependencies)
 {
     $slugs = $this->parseSlugs();
     $target = $this->findPlayer($slugs['in_target']);
     $player = Player::find(SessionFactory::getSession()->get('player_id'));
     $inventory = new Inventory($player);
     $had_stealth = $player->hasStatus(STEALTH);
     $error = false;
     $turns_to_take = 1;
     // Take away one turn even on attacks that fail to prevent page reload spamming
     $bounty_message = '';
     $display_message = '';
     $extra_message = '';
     $attacker_label = $player->name();
     $loot = null;
     try {
         $item = $this->findItem($slugs['item_in']);
         $article = $item ? self::getIndefiniteArticle($item->getName()) : '';
     } catch (\InvalidArgumentException $e) {
         return new RedirectResponse(WEB_ROOT . 'inventory?error=noitem');
     }
     if (empty($target)) {
         $error = 2;
     } else {
         if ($this->itemCount($player, $item) < 1) {
             $error = 3;
         } else {
             if ($target->id() === $player->id()) {
                 return $this->selfUse();
             } else {
                 $params = ['required_turns' => $item->getTurnCost(), 'ignores_stealth' => $item->ignoresStealth()];
                 $attack_legal = new AttackLegal($player, $target, $params);
                 if (!$attack_legal->check()) {
                     $error = 1;
                     $display_message = $attack_legal->getError();
                 } else {
                     if (!$item->isOtherUsable()) {
                         $error = 1;
                         $display_message = 'This item cannot be used on others!';
                     } else {
                         $result = $this->applyItemEffects($player, $target, $item);
                         if ($result['success']) {
                             $message_to_target = "{$attacker_label} has used {$article} " . $item->getName() . " on you{$result['notice']}";
                             Event::create($player->id(), $target->id(), str_replace('  ', ' ', $message_to_target));
                             $inventory->remove($item->identity(), 1);
                             if ($target->health <= 0) {
                                 // Target was killed by the item
                                 $attacker_label = $player->hasStatus(STEALTH) ? "A Stealthed Ninja" : $player->name();
                                 $gold_mod = $item->hasEffect('death') ? 0.25 : 0.15;
                                 $loot = floor($gold_mod * $target->gold);
                                 $target->setGold($target->gold - $loot);
                                 $player->setGold($player->gold + $loot);
                                 $player->addKills(1);
                                 $bounty_message = Combat::runBountyExchange($player, $target);
                                 //Rewards or increases bounty.
                                 $this->sendKillMails($player, $target, $attacker_label, $article, $item->getName(), $loot);
                             }
                         }
                         $display_message = $result['message'];
                         $extra_message = $result['extra_message'];
                     }
                 }
                 $player->changeTurns(-1 * $turns_to_take);
                 $target->save();
                 $player->save();
             }
         }
     }
     return $this->renderUse(['action' => 'use', 'return_to' => in_array(RequestWrapper::getPostOrGet('link_back'), ['', 'player']) ? 'player' : 'inventory', 'error' => $error, 'target' => $target, 'resultMessage' => $display_message, 'alternateResultMessage' => $extra_message, 'stealthLost' => $had_stealth && $player->hasStatus(STEALTH), 'repeat' => $target->health > 0 && empty($error), 'item' => $item, 'bountyMessage' => $bounty_message, 'article' => $article, 'loot' => $loot]);
 }
コード例 #13
0
 /**
  * Command for a user to reduce their bounty by paying their own gold
  *
  * @param Container
  * @param bribe int The amount to spend on reducing bounty
  * @return StreamedViewRespons
  */
 public function bribe(Container $p_dependencies)
 {
     $bribe = intval(RequestWrapper::getPostOrGet('bribe'));
     $char = Player::findPlayable($this->getAccountId());
     $error = 0;
     $quickstat = false;
     if ($bribe <= $char->gold && $bribe > 0) {
         $char->setGold($char->gold - $bribe);
         $char->setBounty(max(0, $char->bounty - floor($bribe / self::BRIBERY_DIVISOR)));
         $char->save();
         $location = 1;
         $quickstat = 'viewinv';
     } else {
         if ($bribe < self::MIN_BRIBE) {
             $this->doshinAttack($char);
             $location = 2;
             $quickstat = 'player';
         } else {
             $location = 0;
             $error = 5;
         }
     }
     return $this->render(['error' => $error, 'quickstat' => $quickstat, 'location' => $location, 'command' => 'bribe']);
 }
コード例 #14
0
ファイル: Router.php プロジェクト: BitLucid/ninjawars
 /**
  * Breaks the request URI into an array of route segments
  *
  * @param Request The request object being routed
  * @return array Each segment of the route
  *
  * @note
  * If there is no path in the request, this will return an array with the
  * default route for the application
  *
  * @note
  * If there is only 1 route segment and the special input parameter
  * "command" is set in the request, the value of "command" will be used
  * as the second element of the return value. If there is no command
  * parameter, the default action for the application will be used
  *
  * @todo stop supporting ?command=action
  */
 public static function parseRoute($p_request)
 {
     // split the requested path by slash
     $routeSegments = explode('/', trim($p_request->getPathInfo(), '/'));
     if (empty($routeSegments[0])) {
         // Route is just forward-slash
         $mainRoute = self::DEFAULT_ROUTE;
     } else {
         // determine the canonical route being requested
         $mainRoute = self::translateRoute(self::sanitizeRoute($routeSegments[0]));
     }
     // if there are 2 route segments use the second one as the command
     if (isset($routeSegments[1]) && !empty($routeSegments[1])) {
         $command = $routeSegments[1];
     } else {
         // without a 2nd route segment, look for command in the input
         $command = (string) RequestWrapper::getPostOrGet(self::COMMAND_PARAM);
     }
     if (empty($command)) {
         if (isset(self::$routes[$mainRoute]['actions'][self::DEFAULT_COMMAND])) {
             $command = self::$routes[$mainRoute]['actions'][self::DEFAULT_COMMAND];
         } else {
             $command = self::DEFAULT_ACTION;
         }
     }
     return [$mainRoute, $command];
 }
コード例 #15
0
ファイル: ClanController.php プロジェクト: BitLucid/ninjawars
 /**
  * Sends a message to all members of the clan of the current player
  *
  * @param Container
  * @return Response
  */
 public function message(Container $p_dependencies)
 {
     $player = $p_dependencies['current_player'];
     $message = RequestWrapper::getPostOrGet('message', null);
     if ($player) {
         $myClan = Clan::findByMember($player);
         if ($myClan) {
             $target_id_list = $myClan->getMemberIds();
             Message::sendToGroup($player, $target_id_list, $message, 1);
             $parts = ['clan' => $myClan, 'title' => 'Your clan', 'action_message' => 'Message sent to your clan.', 'pageParts' => ['info', 'member-list']];
             if ($this->playerIsLeader($player, $myClan)) {
                 array_unshift($parts['pageParts'], 'manage');
             } else {
                 array_unshift($parts['pageParts'], 'non-leader-panel');
             }
             return $this->render($parts);
         } else {
             return $this->listClans();
         }
     } else {
         return $this->listClans();
     }
 }
コード例 #16
0
ファイル: DojoController.php プロジェクト: BitLucid/ninjawars
 /**
  * Action to request class change form AND execute class change
  *
  * @todo split form request and execute into separate funcs
  * @param Container
  * @return Response
  */
 public function changeClass(Container $p_dependencies)
 {
     if ($p_dependencies['session']->get('authenticated', false)) {
         $player = $p_dependencies['current_player'];
         $classes = query_array('select class_id, identity, class_name, class_note, class_tier, class_desc, class_icon, theme from class where class_active = true');
         $requestedIdentity = RequestWrapper::getPostOrGet('requested_identity');
         $currentClass = $player->identity;
         $showMonks = false;
         $parts = [];
         if (isset($classes[$requestedIdentity])) {
             $error = $this->classChangeReqs($player, self::CLASS_CHANGE_COST);
             if ($currentClass != $requestedIdentity && !$error) {
                 $error = $this->changePlayerClass($player, $requestedIdentity);
             }
             $currentClass = $player->identity;
             if (!$error) {
                 $parts['pageParts'] = ['success-class-change'];
                 $showMonks = true;
             } else {
                 $parts['error'] = $error;
             }
         } else {
             $parts['pageParts'] = ['form-class-change'];
         }
         unset($classes[$currentClass]);
         $parts['classOptions'] = $classes;
         return $this->render($parts, $player, $showMonks);
     } else {
         return $this->accessDenied();
     }
 }
コード例 #17
0
 /**
  * Command to heal the current player by the specified amount
  *
  * @param Container
  * @param heal_points Mixed The amount of healing desires as an int or the special value 'max'
  * @return Response
  * @see _heal
  */
 public function heal(Container $p_dependencies)
 {
     $skillController = new Skill();
     $player = $p_dependencies['current_player'];
     $healAmount = RequestWrapper::getPostOrGet('heal_points');
     if ($healAmount === 'max') {
         $healAmount = max(1, $this->calculateMaxHeal($player));
     }
     try {
         $this->_heal($player, (int) $healAmount);
         $pageParts = $this->servicesNeeded($player);
         array_unshift($pageParts, 'result-heal');
         $player->save();
         return $this->render(['pageParts' => $pageParts, 'player' => $player, 'has_chi' => $skillController->hasSkill('Chi', $player->name())]);
     } catch (\RuntimeException $e) {
         return $this->renderError($e->getMessage(), $player);
     } catch (\InvalidArgumentException $e) {
         return $this->renderError($e->getMessage(), $player);
     }
 }