Exemplo n.º 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;
 }
Exemplo n.º 2
0
 public static function getOpponent($fight)
 {
     self::requireFight($fight);
     $request = new \Fight\Data\Request();
     $request->Filter[] = new \Fight\Data\Filter("fight_id", $fight->fight_id);
     $request->Filter[] = new \Fight\Data\Filter("channel_id", $fight->channel_id);
     $request->Filter[] = new \Fight\Data\Filter("user_id", $fight->user_id, "!=");
     $otherFight = FightModel::findOne($request);
     if (!$otherFight) {
         throw new Exception("You are not in a fight!");
     }
     return [$otherFight, FightUserModel::findOneWhere(["user_id" => $otherFight->user_id])];
 }