Ejemplo n.º 1
0
 public function save()
 {
     $this->deletedIdList = [];
     $this->errorIdList = [];
     if ($this->hasErrors()) {
         return false;
     }
     if (!($user = User::findOne(['api_key' => $this->apikey]))) {
         $this->addError('apikey', 'User does not exist.');
         return false;
     }
     foreach ($this->id as $id) {
         $battle = Battle::findOne(['id' => (int) (string) $id]);
         if (!$battle) {
             $this->errorIdList[] = ['id' => $id, 'error' => 'not found'];
             continue;
         }
         if ($battle->user_id != $user->id) {
             $this->errorIdList[] = ['id' => $id, 'error' => 'user not match'];
             continue;
         }
         if ($battle->is_automated) {
             $this->errorIdList[] = ['id' => $id, 'error' => 'automated result'];
             continue;
         }
         if (!$this->test) {
             $battle->delete();
         }
         $this->deletedIdList[] = ['id' => $id, 'error' => null];
     }
     return true;
 }
Ejemplo n.º 2
0
 public function init()
 {
     parent::init();
     $this->battle = null;
     if ($user = Yii::$app->user->identity) {
         $this->battle = Battle::findOne(['id' => Yii::$app->request->get('battle'), 'user_id' => $user->id]);
     }
 }
Ejemplo n.º 3
0
 public function actionDelete($id)
 {
     $battle = Battle::findOne(['id' => (int) (string) $id]);
     if (!$battle) {
         $this->stderr("Could not find specified battle \"{$id}\"\n", Console::FG_RED);
         return 1;
     }
     $battle->delete();
 }
Ejemplo n.º 4
0
 public function run()
 {
     $request = Yii::$app->getRequest();
     $battle = Battle::findOne(['id' => $request->get('battle')]);
     if (!$battle || !$battle->user) {
         throw new NotFoundHttpException(Yii::t('app', 'Could not find specified battle.'));
     }
     if ($battle->user->screen_name !== $request->get('screen_name')) {
         return $this->controller->redirect(['show/battle', 'screen_name' => $battle->user->screen_name, 'battle' => $battle->id]);
     }
     return $this->controller->render('battle.tpl', ['battle' => $battle]);
 }
Ejemplo n.º 5
0
 public function run()
 {
     $request = Yii::$app->getRequest();
     $user = User::findOne(['screen_name' => $request->get('screen_name')]);
     if (!$user) {
         throw new NotFoundHttpException('指定されたユーザが見つかりません');
     }
     $battle = Battle::findOne(['user_id' => $user->id, 'id' => $request->get('battle')]);
     if (!$battle) {
         throw new NotFoundHttpException('指定されたバトルが見つかりません');
     }
     return $this->controller->render('battle.tpl', ['user' => $user, 'battle' => $battle]);
 }
Ejemplo n.º 6
0
 public function getUserRankStat()
 {
     $subQuery = (new \yii\db\Query())->select(['id' => 'MAX({{battle}}.[[id]])'])->from('battle')->andWhere(['not', ['{{battle}}.[[rank_after_id]]' => null]])->andWhere(['not', ['{{battle}}.[[rank_exp_after]]' => null]])->andWhere(['{{battle}}.[[user_id]]' => $this->user->id]);
     if (!($battle = Battle::findOne(['id' => $subQuery]))) {
         return null;
     }
     $deviation = null;
     $avgRank = null;
     $avgRankExp = null;
     $standardDeviation = null;
     if ($entire = $this->getEntireRankStat()) {
         $exp = $this->calcGraphExp($battle->rankAfter->key, $battle->rank_exp_after);
         $deviation = ($exp - $entire->average) / $entire->standardDeviation * 10 + 50;
         $ranks = ['C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+', 'S', 'S+'];
         $avgExp = (int) round($entire->average);
         $avgRank = Yii::t('app-rank', $ranks[floor($avgExp / 100)]);
         $avgRankExp = $avgExp % 100;
         $standardDeviation = $entire->standardDeviation;
     }
     return (object) ['rank' => Yii::t('app-rank', $battle->rankAfter->name), 'rankExp' => (int) $battle->rank_exp_after, 'deviation' => $deviation, 'avgRank' => $avgRank, 'avgRankExp' => $avgRankExp, 'standardDeviation' => $standardDeviation];
 }
Ejemplo n.º 7
0
 private function runPost()
 {
     $request = Yii::$app->getRequest();
     $form = new PostBattleForm();
     $form->attributes = $request->getBodyParams();
     foreach (['image_judge', 'image_result'] as $key) {
         if ($form->{$key} == '') {
             $form->{$key} = UploadedFile::getInstanceByName($key);
         }
     }
     if (!$form->validate()) {
         return $this->formatError($form->getErrors(), 400);
     }
     $transaction = Yii::$app->db->beginTransaction();
     $tmpFiles = [];
     try {
         $battle = $form->toBattle();
         if (!$battle->isMeaningful) {
             $transaction->rollback();
             return $this->formatError(['system' => [Yii::t('app', 'Please send meaningful data.')]], 400);
         }
         if ($form->agent != '' || $form->agent_version != '') {
             $agent = Agent::findOne(['name' => (string) $form->agent, 'version' => (string) $form->agent_version]);
             if (!$agent) {
                 $agent = new Agent();
                 $agent->name = (string) $form->agent;
                 $agent->version = (string) $form->agent_version;
                 if (!$agent->save()) {
                     return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'agent')], 'system_' => $battle->getErrors()], 500);
                 }
             }
             $battle->agent_id = $agent->id;
         }
         if (!$battle->save()) {
             $transaction->rollback();
             return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle')], 'system_' => $battle->getErrors()], 500);
         }
         if ($battle->isNawabari) {
             $nawabari = $form->toBattleNawabari($battle);
             if ($nawabari->isMeaningful) {
                 if (!$nawabari->save()) {
                     $transaction->rollback();
                     return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle_nawabari')], 'system_' => $nawabari->getErrors()], 500);
                 }
             }
         } elseif ($battle->isGachi) {
             $gachi = $form->toBattleGachi($battle);
             if ($gachi->isMeaningful) {
                 if (!$gachi->save()) {
                     $transaction->rollback();
                     return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle_gachi')], 'system_' => $gachi->getErrors()], 500);
                 }
             }
         }
         foreach ($form->toDeathReasons($battle) as $reason) {
             if ($reason && !$reason->save()) {
                 $transaction->rollback();
                 return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle_death_reason')], 'system_' => $reason->getErrors()], 500);
             }
         }
         $imageOutputDir = Yii::getAlias('@webroot/images');
         if ($image = $form->toImageJudge($battle)) {
             $binary = is_string($form->image_judge) ? $form->image_judge : file_get_contents($form->image_judge->tempName, false);
             if (!ImageConverter::convert($binary, $imageOutputDir . '/' . $image->filename, $imageOutputDir . '/' . str_replace('.jpg', '.webp', $image->filename))) {
                 $transaction->rollback();
                 return $this->formatError(['system' => [Yii::t('app', 'Could not convert "{0}" image.', 'judge')]], 500);
             }
             if (!$image->save()) {
                 $transaction->rollback();
                 return $this->formatError(['system' => [Yii::t('app', 'Could not save {0}', 'battle_image(judge)')]], 500);
             }
         }
         if ($image = $form->toImageResult($battle)) {
             $binary = is_string($form->image_result) ? $form->image_result : file_get_contents($form->image_result->tempName, false);
             if (!ImageConverter::convert($binary, $imageOutputDir . '/' . $image->filename, $imageOutputDir . '/' . str_replace('.jpg', '.webp', $image->filename))) {
                 $transaction->rollback();
                 return $this->formatError(['system' => [Yii::t('app', 'Could not convert "{0}" image.', 'result')]], 500);
             }
             if (!$image->save()) {
                 $transaction->rollback();
                 return $this->formatError(['system' => [Yii::t('app', 'Could not save {0}', 'battle_image(result)')]], 500);
             }
         }
         $transaction->commit();
     } catch (\Exception $e) {
         $transaction->rollback();
         return $this->formatError(['system' => [$e->getMessage()]], 500);
     }
     // 保存時間の読み込みのために再読込する
     $battle = Battle::findOne(['id' => $battle->id]);
     return $this->runGetImpl($battle);
 }
Ejemplo n.º 8
0
 private function runPost()
 {
     $request = Yii::$app->getRequest();
     $form = new PostBattleForm();
     $form->attributes = $request->getBodyParams();
     foreach (['image_judge', 'image_result'] as $key) {
         if ($form->{$key} == '') {
             $form->{$key} = UploadedFile::getInstanceByName($key);
         }
     }
     if (!$form->validate()) {
         return $this->formatError($form->getErrors(), 400);
     }
     // テストモード用
     if ($form->isTest) {
         // validate のみなら既に validate は完了しているので適当なレスポンスボディを返して終わり
         if ($form->test === 'validate') {
             $resp = Yii::$app->getResponse();
             $resp->format = 'json';
             $resp->statusCode = 200;
             return ['validate' => true];
         }
         // dry_run
         // 整形用のダミーデータを準備
         $battle = $form->toBattle();
         $battle->validate();
         $deathReasons = [];
         foreach ($form->toDeathReasons($battle) as $reason) {
             if ($reason) {
                 $deathReasons[] = $reason;
             }
         }
         $players = [];
         foreach ($form->toPlayers($battle) as $player) {
             if ($player) {
                 $players[] = $player;
             }
         }
         $agent = null;
         if ($form->agent != '' || $form->agent_version != '') {
             $agent = new Agent();
             $agent->name = (string) $form->agent;
             $agent->version = (string) $form->agent_version;
         }
         return $this->runGetImpl2($battle, $deathReasons, $players, $agent);
     }
     $transaction = Yii::$app->db->beginTransaction();
     try {
         $battle = $this->saveData($form);
         if (!$battle instanceof Battle) {
             return $battle;
         }
         $transaction->commit();
     } catch (\Exception $e) {
         $transaction->rollback();
         return $this->formatError(['system' => [$e->getMessage()]], 500);
     }
     // 保存時間の読み込みのために再読込する
     $battle = Battle::findOne(['id' => $battle->id]);
     return $this->runGetImpl($battle);
 }