public static function getRandomMonster($user) { $monster = FightUserModel::build(["team_id" => $user->team_id, "AI" => 1, "name" => FightAIController::COOL_NAME() . " the " . FightAIController::COOL_DESC(), "level" => mt_rand(1, $user->level + 10)]); $monster->save(); $monster->update(["weapon" => FightAIController::createRandomWeapon($monster)->item_id, "armor" => FightAIController::createRandomArmor($monster)->item_id]); return $monster; }
/** * 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; }
public static function login($params) { if (!$params["email"]) { return self::packageData(400, "Email is required.", null); } $result = []; $user = FightUserModel::findByEmail($params["email"]); if (!$params["password"]) { $result[] = new FightData($user && $user->password ? 1 : 0); } else { if (!$user) { return self::packageData(400, new FightErrorMessage("No user found for " . $params["email"])); } else { $md5 = md5($params["password"]); if ($md5 === $user->password) { $result[] = new FightData($user->user_id); } else { if (!$user->password) { return self::packageData(400, new FightErrorMessage("No account set up for this email yet")); } else { return self::packageData(400, new FightErrorMessage("No match found for this email and password")); } } } } return self::packageData(200, $result); }
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])]; }