/** * Respond to command (from API, SlackWrapper, whatever) * * @param method - The method to call (fight, status, equip, etc) * @param params - Additional parameters * Ex [ * "text" => "<@USLACKBOT>, * "user_id" => "U0B2QPTNU", * "team_id" => "T0B2LSLP6", * "channel_id" => "C0CS03RK4", * "user_name" => "thomassteinke" ** Optional * ] * @return array - [ * status => HTTP status, * data => array of FightAttachments (could be FightErrorAttachment) * ] */ public static function main($method, $params) { $user = FightUserController::findUser($params["team_id"], $params["user_id"]); if ($params["user_name"] && $params["user_name"] !== $user->alias->slack_name) { $user->alias->update(["slack_name" => $params["user_name"]]); } // Make sure the method is fine if (!self::isMethod($method)) { return self::packageData(400, [new FightErrorMessage("Command `" . $method . "` isn't available")]); } try { $argv = explode(" ", $method . " " . $params["text"]); $argc = count($argv); $fight = FightController::findFight($user, $params["channel_id"]); $method .= "_"; $result = FightController::$method($argc, $argv, $user, $fight, $params); return self::packageData(200, $result, $user); } catch (Exception $e) { if ($e->getCode() === 200) { $message = new FightMessage("warning", $e->getMessage()); } else { $message = new FightErrorMessage($e->getMessage()); } return self::packageData($e->getCode(), $message, $user); } }
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; }