Example #1
0
 public static function getRandomMonster($user)
 {
     $monster = FightUserModel::build(["team_id" => $user->team_id, "AI" => 1, "name" => FightAIController::COOL_NAME() . " the " . FightAIController::COOL_DESC(), "level" => mt_rand(1, $user->level + 10)]);
     $monster->save();
     $monster->update(["weapon" => FightAIController::createRandomWeapon($monster)->item_id, "armor" => FightAIController::createRandomArmor($monster)->item_id]);
     return $monster;
 }
 public static function useMove($move, $user, $fight, $opponent, $otherFight)
 {
     if (!$opponent) {
         list($otherFight, $opponent) = FightController::getOpponent($fight);
     }
     $message = [$user->tag() . " uses " . $move->name];
     $weapon = FightItemModel::findById($user->weapon);
     $armor = FightItemModel::findById($opponent->armor);
     if ($weapon) {
         $message[0] .= " with their " . $weapon->name;
     }
     $message[0] .= "!";
     $critical = 0;
     $effective = 0;
     $moveAlignment = $move->stats["alignment"] ?: "none";
     $weaponAlignment = $weapon ? $weapon->stats["alignment"] : "none";
     $armorAlignment = $armor ? $armor->stats["alignment"] : "none";
     if (!$weaponAlignment) {
         $weaponAlignment = "none";
     }
     if (!$armorAlignment) {
         $armorAlignment = "none";
     }
     // Super and not effective
     if (in_array($armorAlignment, self::$superEffective[$moveAlignment])) {
         $move->stats["physical"] *= 1.5;
         $move->stats["elemental"] *= 1.5;
         $effective += 1;
     }
     if (in_array($armorAlignment, self::$notEffective[$moveAlignment])) {
         $move->stats["physical"] /= 2;
         $move->stats["elemental"] /= 2;
         $effective -= 1;
     }
     // Critical stuff
     if (rand(0, 100) <= 5 + $move->stats["luck"]) {
         $move->stats["physical"] *= 1.5;
         $move->stats["elemental"] *= 1.5;
         $critical += 1;
     }
     // Compute damage
     $physicalDamage = $move->stats["physical"];
     $elementalDamage = [$moveAlignment => $move->stats["elemental"]];
     // Weapon stuff
     if ($weapon) {
         if (rand(0, 100) <= 5 + $weapon->stats["luck"]) {
             $weapon->stats["physical"] *= 1.5;
             $weapon->stats["elemental"] *= 1.5;
             $critical += 1;
         }
         if (in_array($armorAlignment, self::$superEffective[$weaponAlignment])) {
             $weapon->stats["physical"] *= 1.5;
             $weapon->stats["elemental"] *= 1.5;
             $effective += 1;
         }
         if (in_array($armorAlignment, self::$notEffective[$weaponAlignment])) {
             $weapon->stats["physical"] /= 2;
             $weapon->stats["elemental"] /= 2;
             $effective -= 1;
         }
         $physicalDamage += $weapon->stats["physical"];
         $elementalDamage[$weaponAlignment] += $weapon->stats["elemental"];
     }
     // Physical attack
     $damage = max(2, $physicalDamage * (1 - $armor->stats["physical"] / 100) - $armor->stats["defense"]);
     foreach ($elementalDamage as $element => $dmg) {
         if ($armorAlignment === $element || in_array($armorAlignment, self::$notEffective[$element])) {
             $damage += max(2, $dmg * (1 - $armor->stats["elemental"] / 100));
         }
     }
     $damage = round($damage);
     if ($critical === 1) {
         $message[] = "Critical hit!";
     } elseif ($critical === 2) {
         $message[] = "HYPERCritical hit!!!!";
     }
     if ($effective === -2) {
         $message[] = "But it hardly does anything!";
     } elseif ($effective === -1) {
         $message[] = "It wasn't very effective...";
     } elseif ($effective === 1) {
         $message[] = "It's super effective!";
     } elseif ($effective === 2) {
         $message[] = "OMIGOD YOU KNOCKED HIS SOCKS OFF SO EFFECTIVE!!";
     }
     $color = null;
     if ($move->stats["alignment"] === "iron") {
         $color = "#FF0000";
     }
     if ($move->stats["alignment"] === "earth") {
         $color = "#00FF00";
     }
     if ($move->stats["alignment"] === "water") {
         $color = "#0000FF";
     }
     // Fight result
     $newHealth = $otherFight->health - $damage;
     if ($newHealth > 0) {
         $otherFight->update(["health" => $newHealth]);
         $message[] = "(" . $damage . " damage) " . $opponent->tag() . " now has " . $newHealth . " health.";
     } else {
         $levelUp = FightController::registerVictory($user, $fight, $opponent, $otherFight);
         $otherFight->update(["health" => $newHealth]);
         $message[] = "(" . $damage . " damage) " . $opponent->tag() . " fainted!!";
         if ($opponent->AI && ($item = FightAIController::dropitem($opponent))) {
             $item->update(["user_id" => $user->user_id]);
             return [new FightMessage($color, $message), new FightMessage("good", "You picked up: " . $item->name . "!")];
         }
         return [new FightMessage($color, $message), $levelUp];
     }
     $result = new FightMessage($color, $message);
     FightActionController::registerAction($user, $fight->fight_id, $result->toString());
     if ($opponent->AI) {
         $opponentMove = FightAIController::computerMove($user, $fight, $opponent, $otherFight);
         if (!is_array($opponentMove)) {
             $opponentMove = [$opponentMove];
         }
         array_unshift($opponentMove, $result);
         return $opponentMove;
     }
     return $result;
 }
Example #3
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() . "!!");
 }