Example #1
0
 public static function fight_($argc, $argv, $user, $fight, $params)
 {
     if ($fight) {
         return new FightMessage("danger", "You're already in a fight! Type `status` to see how you're doing");
     }
     if ($argc !== 2 || in_array($argv[1], self::$RESERVED)) {
         return new FightInfoMessage(["Usage: `fight @XXX | fight monster`", "Type `fight help` for more commands"]);
     }
     if ($argv[1] === "monster") {
         $opponent = FightAIController::getRandomMonster($user);
         $opponent->save();
     } else {
         $opponent = FightUserController::findUserByTag($user->alias->team_id, $argv[1]);
         if (!$opponent) {
             return new FightMessage("danger", "Sorry, `" . $argv[1] . "` is not recognized as a name");
         }
     }
     $otherExisting = FightModel::findOneWhere(["user_id" => $opponent->user_id, "channel_id" => $params["channel_id"], "status" => "progress"]);
     if ($otherExisting) {
         return new FightMessage("danger", "Sorry, " . $opponent->tag() . " is already in a fight. Maybe take this somewhere else?");
     }
     $INITIAL_HEALTH_1 = 100;
     $INITIAL_HEALTH_2 = 100;
     if (!$user->weapon) {
         $INITIAL_HEALTH_2 = 35;
     }
     if (!$opponent->weapon) {
         $INITIAL_HEALTH_1 = 35;
     }
     $leveldiff = $opponent->level - $user->level;
     if ($leveldiff >= 0) {
         $INITIAL_HEALTH_1 += $leveldiff * 3;
     } else {
         $INITIAL_HEALTH_2 += $leveldiff * 3;
     }
     $fightParams = ["user_id" => $user->user_id, "channel_id" => $params["channel_id"], "status" => "progress", "health" => $INITIAL_HEALTH_1];
     // Build fight 1
     $fight1 = FightModel::build($fightParams);
     if (!$fight1->save()) {
         throw new Exception("Server error. Code: 1");
     }
     // Build opponent's fight
     $fightParams["fight_id"] = $fight1->fight_id;
     $fightParams["user_id"] = $opponent->user_id;
     $fightParams["health"] = $INITIAL_HEALTH_2;
     $fight2 = FightModel::build($fightParams);
     if (!$fight2->save()) {
         throw new Exception("Server error. Code: 2");
     }
     // Register the action
     FightActionController::registerAction($user, $fight1->fight_id, $user->tag() . " challenges " . $opponent->tag() . " to a fight!");
     // If it's a monster (or slackbot) they get to go first
     if ($opponent->AI) {
         $computerMove = FightAIController::computerMove($user, $fight1, $opponent, $fight2);
         if (!is_array($computerMove)) {
             $computerMove = [$computerMove];
         }
         foreach ($computerMove as $action) {
             FightActionController::registerAction($opponent, $fight2->fight_id, $action->toString());
         }
         array_unshift($computerMove, new FightMessage("warning", "A wild " . $opponent->tag() . " appeared!"));
         return $computerMove;
     }
     return new FightMessage("good", "Bright it on, " . $opponent->tag() . "!!");
 }