/** * 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 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() . "!!"); }