Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 private function saveData(PostBattleForm $form)
 {
     $battle = $form->toBattle();
     if (!$battle->isMeaningful) {
         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()) {
         return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle')], 'system_' => $battle->getErrors()], 500);
     }
     foreach ($form->toDeathReasons($battle) as $reason) {
         if ($reason && !$reason->save()) {
             return $this->formatError(['system' => [Yii::t('app', 'Could not save to database: {0}', 'battle_death_reason')], 'system_' => $reason->getErrors()], 500);
         }
     }
     foreach ($form->toPlayers($battle) as $player) {
         if ($player && !$player->save()) {
             return $this->formatError(['system' => ['Could not save to database: battle_player'], 'system_' => $player->getErrors()], 500);
         }
     }
     $imageOutputDir = Yii::getAlias('@webroot/images');
     $imageArchiveOutputDir = Yii::$app->params['amazonS3'] && Yii::$app->params['amazonS3'][0]['bucket'] != '' ? Yii::getAlias('@app/runtime/image-archive/queue') . '/' . gmdate('Ymd', time() + 9 * 3600) : null;
     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, null, $imageArchiveOutputDir ? $imageArchiveOutputDir . '/' . sprintf('%d-judge.png', $battle->id) : null)) {
             return $this->formatError(['system' => [Yii::t('app', 'Could not convert "{0}" image.', 'judge')]], 500);
         }
         if (!$image->save()) {
             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, null, $imageArchiveOutputDir ? $imageArchiveOutputDir . '/' . sprintf('%d-result.png', $battle->id) : null)) {
             return $this->formatError(['system' => [Yii::t('app', 'Could not convert "{0}" image.', 'result')]], 500);
         }
         if (!$image->save()) {
             return $this->formatError(['system' => [Yii::t('app', 'Could not save {0}', 'battle_image(result)')]], 500);
         }
     }
     return $battle;
 }