Beispiel #1
0
 public function join()
 {
     $data = Input::only(['name', 'room_id']);
     print_r($data);
     $room = Room::find($data['room_id']);
     if (!$room) {
         return Redirect::action('PrepareController@room');
     }
     if ($room->state !== 'open') {
         throw new BadRequestHttpException('既にゲームは開始しています。');
     }
     if ($room->mates->count() >= $room->member_number) {
         throw new BadRequestHttpException('全ユーザーが揃っています');
     }
     if (!isset($data['name']) || $data['name'] == '') {
         //名前指定していない場合
         return Redirect::action('PrepareController@rooms');
     }
     $i = 0;
     do {
         $hash = sha1(date("Y/m/d H:i:s.u") . 'zCeZu12X' . $data['name'] . $data['room_id']);
         $hashed_mate = Mate::where('hash', $hash)->select('hash')->first();
         $i++;
         if ($i > 50) {
             throw new InternalErrorException('ユーザー作成に失敗しました hash衝突しまくり');
         }
     } while ($hashed_mate && $i < 100);
     $mate = Mate::create(['name' => $data['name'], 'last_state' => 'open', 'hash' => $hash, 'cast_id' => 0, 'room_id' => $data['room_id'], 'select_user_id' => '', 'is_alive' => 1]);
     if (!$mate) {
         throw new InternalErrorException('ユーザー作成に失敗しました');
     }
     $room->touch();
     return Redirect::action('PlayController@index', ['hash' => $hash]);
 }
Beispiel #2
0
 public function isMaster()
 {
     $lastMate = Mate::where('room_id', $this->room_id)->where('is_alive', 1)->orderBy('created_at', 'ASC')->first();
     if (!$lastMate) {
         return null;
     } else {
         if ($lastMate->room_id == $this->room_id) {
             return true;
         }
     }
     return false;
 }
Beispiel #3
0
 public function ajax_loadRoom()
 {
     $query = $this->__getQuery();
     $returnCode = new AjaxReturn();
     if (!$this->__initInfoFromQuery()) {
         return $returnCode->error('room is missing');
     }
     if (!$this->__joinsThisRoom()) {
         return $returnCode->error('not joined the room');
     }
     $callTime = 0;
     while ($callTime < 30) {
         // masterのみの追加処理
         if ($this->myInfo->ismaster()) {
             if ($this->__checkState() === 'open' && $this->room->sitDownAllMate()) {
                 $this->__advanceState('open');
                 break;
             }
             //状態が変化した場合に、情報を知らせる
             if ($this->__hasChangedState()) {
                 if (isset($this->room->last_dead_user_id)) {
                     $returnCode->add('dead', $this->room->last_dead_user_id);
                     break;
                 }
             } else {
                 if ($this->room->state == 'end') {
                     $returnCode->add('result', $this->__checkTriumph());
                     break;
                 }
             }
             //最後に全員が選択済みであれば自動的に進める
             $result = $this->__allSelectStateFlow();
             $result |= $this->__timedStateFlow();
             if ($result) {
                 break;
             }
             // master is clear
             if ($this->__checkstate() === 'open' && $this->room->sitdownallmate()) {
                 $this->__advancestate('open');
                 break;
             }
         }
         // 初回のみLongPollingなし
         if (!isset($query['state']) || $query['state'] === '') {
             break;
         }
         $newRoom = Room::find($this->room->id);
         if ($newRoom->updated_at > $this->room->updated_at) {
             $this->room = $newRoom;
             $this->room->mates = Mate::where('room_id', $newRoom->id)->get();
             break;
         }
         $callTime += 3;
         usleep(3000 * 1000);
     }
     //$returnCode->add('jsstate', $this->getJSState());
     $returnCode->add('state', $this->__getState());
     $returnCode->add('room', $this->room);
     $returnCode->add('mates', $this->room->mates);
     $returnCode->add('myInfo', $this->myInfo);
     $returnCode->add('myCast', $this->myInfo->cast);
     //print_r($this->myInfo->cast());
     return $returnCode->success();
 }
Beispiel #4
0
 protected function __advanceState($nowState)
 {
     if (!isset($this->room)) {
         throw new InternalErrorException('room was not set');
     }
     if ($nowState !== $this->room->state) {
         //現在の状態が異なっている場合
         return null;
     }
     if (!$this->room->isAllMatesStateChanged()) {
         return null;
     }
     $nowState = $this->__checkState();
     $resultArray = ['result' => 'success'];
     switch ($nowState) {
         case 'open':
             //人の募集
             if ($this->room->SitDownAllMate()) {
                 $this->room->shuffleCast();
                 $newMyInfo = Mate::find($this->myInfo->id);
                 $this->myInfo = $newMyInfo;
                 $this->room->setStateTimeLimit('20seconds');
                 $this->room->setState('ready');
             }
             break;
         case 'ready':
             //準備完了
             $this->__initMates();
             $this->room->day = 0;
             $this->room->setStateTimeLimit('200 second');
             $this->room->setState('day');
             break;
         case 'day':
             //お昼
             $this->room->setState('day_select');
             break;
         case 'day_select':
             // お昼の選択(追放会議)
             if ($this->__voteAndKill() === null) {
                 //まだ追放が決まらない場合
                 $this->room->setStateTimeLimit('80 second');
                 $this->room->setState('day_append');
                 break;
             }
             $this->room->push();
             $triumph = $this->__checkTriumph();
             if ($triumph == null) {
                 $this->room->setState('night_select');
             } else {
                 //$this->room->setState('end');
                 $this->room->setState('end_' . $triumph);
             }
             break;
         case 'day_append':
             //追加の追放会議
             $this->room->setState('day_append_select');
             break;
         case 'day_append_select':
             // 追加の追放会議選択
             if ($this->__voteAndKill() === null) {
                 //まだ追放が決まらない場合
                 $this->room->setState('night_select');
                 $this->room->setStateTimeLimit('80 second');
                 break;
             }
             $triumph = $this->__checkTriumph();
             if ($triumph != null) {
                 //$this->room->setState('end');
                 $this->room->setState('end_' . $triumph);
             } else {
                 $this->room->setState('night_select');
             }
             break;
         case 'night_select':
             //夜のアクション選択
             $this->__jinrohAttackWithAvoid();
             $triumph = $this->__checkTriumph();
             if ($triumph != null) {
                 $this->room->setState('end_' . $triumph);
             } else {
                 $this->room->day++;
                 $this->room->setStateTimeLimit('200 second');
                 $this->room->setState('day');
             }
             break;
         default:
             return null;
             break;
     }
     if (isset($triumph)) {
         $resultArray['triumph'] = $triumph;
     }
     return $resultArray;
 }