コード例 #1
0
 /**
  * Find a user by user ID and team ID, and create the user if they don't exist.
  *
  * @param $team_id - Slack (or any < 16-char team id)
  * @param $user_id - Unique user id, up to 16 characters
  * @return [FightUserModel, FightAliasModel]
  */
 public static function findUser($team_id, $user_id)
 {
     $request = new \Data\Request();
     $request->Filter[] = new \Data\Filter("team_id", $team_id);
     $request->Filter[] = new \Data\Filter("slack_user_id", $user_id);
     $alias = FightAliasModel::findOneWhere(["team_id" => $team_id, "slack_user_id" => $user_id]);
     if (!$alias) {
         // Attempt to find user and connect him
         $email = FightAppController::getUserInfo($team_id, $user_id)["email"];
         $user = FightUserModel::findOneWhere(["email" => $email]);
         if (!$user) {
             $user = FightUserModel::findOneWhere(["team_id" => $team_id, "name" => $user_id]);
             if ($user) {
                 // Assign email
                 $user->update(["email" => $email]);
             }
         }
         if (!$user) {
             // Create user
             $user = FightUserModel::build(["team_id" => $team_id, "email" => $email, "name" => $user_id, "level" => 1, "experience" => 0, "email" => $email]);
             if (!$user->save()) {
                 return null;
             }
             // Give them a basic attack
             $attack = FightItemModel::build(["user_id" => $user->user_id, "name" => "attack", "stats" => ["physical" => 15], "type" => "move"]);
             $attack->save();
             // Give them a basic armor too
             $armor = FightItemModel::build(["user_id" => $user->user_id, "name" => "clothes", "stats" => ["alignment" => "none", "physical" => 2, "elemental" => 0, "defense" => 4], "type" => "item"]);
             $armor->save();
             $user->update(["armor" => $armor->item_id]);
         }
         $alias = FightAliasModel::build(["user_id" => $user->user_id, "slack_user_id" => $user_id, "team_id" => $team_id]);
         if (!$alias->save()) {
             throw new Exception("Alias not saved");
         }
     } else {
         $user = FightUserModel::findById($alias->user_id);
     }
     $user->alias = $alias;
     return $user;
 }
コード例 #2
0
 public static function useItem($user, $fight, $action)
 {
     if (strtolower($action) === "taunt") {
         FightActionController::registerAction($user, $fight->fight_id, $user->tag() . " uses Taunt!");
         return [new FightMessage("good", [$user->tag() . " uses Taunt!", "What an insult!"]), new FightReaction($fight->channel_id)];
     }
     $move = FightItemController::getMove($user, $action);
     if (!$move) {
         $result = [$action . " not available! Options:"];
         $moves = FightItemModel::findWhere(["user_id" => $user->user_id, "type" => "move", "deleted" => 0]);
         foreach ($moves->objects as $move) {
             $result[] = "`" . $move->name . "`";
         }
         return new FightMessage("danger", $result);
     } else {
         $action = FightItemController::useMove($move, $user, $fight);
         if (!is_array($action)) {
             $action = [$action];
         }
         $action[] = new FightReaction($fight->channel_id);
         return $action;
     }
 }
コード例 #3
0
 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;
 }
コード例 #4
0
 public static function createRandomEquip($monster, $categories)
 {
     $name = self::COOL_NAME() . " the " . self::randFromArray(self::$descriptors) . " " . self::randFromArray($categories);
     $stats = ["alignment" => self::randFromArray(FightItemController::$elements), "physical" => 15 + mt_rand(0, 30), "elemental" => 10 + mt_rand(0, 20), "defense" => mt_rand(0, 15)];
     $item = FightItemModel::build(["user_id" => $monster->user_id, "name" => $name, "stats" => $stats, "type" => "item"]);
     return $item->save();
 }
コード例 #5
0
 public static function craft_($argc, $argv, $user, $fight, $params)
 {
     $argS = implode(" ", array_slice($argv, 1));
     if (!$fight) {
         $itemCount = FightItemModel::findWhere(["user_id", $user->user_id]);
         if ($itemCount->size() >= 10) {
             return new FightDangerMessage("Sorry, you may not have more than 10 items. Type `item drop XXX` to drop an old item");
         }
         return FightCraftController::startCrafting($user, $params["channel_id"], $argS);
     } else {
         list($otherFight, $opponent) = self::getOpponent($fight);
         return FightCraftController::craft($fight, $user, $otherFight, $opponent, $argS);
     }
 }