예제 #1
0
파일: Main.php 프로젝트: Epiphane/FightClub
 /**
  * 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);
     }
 }