Example #1
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);
 }
Example #2
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);
 }