/** * Handle the event. * * @param OpenBattleCompleted $event * @return void */ public function handle(OpenBattleCompleted $event) { $openBattle = $event->openBattle; // create new Battle $battle = new Battle(); $battle->rapper1_id = $openBattle->rapper1_id; $battle->rapper2_id = $openBattle->rapper2_id; $battle->video = $event->outfilename; $battle->save(); // delete OpenBattle $openBattle->delete(); }
/** * Test for getVideo */ public function testGetVideo() { //$this->withoutMiddleware(); $user1 = factory(App\Models\User::class)->create(['rating' => 3]); $user2 = factory(App\Models\User::class)->create(['rating' => 6]); $battle = new Battle(); $battle->rapper1_id = $user1->id; $battle->rapper2_id = $user2->id; $battle->video = "/path/to/file"; $battle->votes_rapper1 = 45; $battle->votes_rapper2 = 86; $battle->save(); //echo $battle->id; $this->get('/video/{id}', ['id' => $battle->id]); }
public function run() { Yii::$app->response->format = 'json'; $request = Yii::$app->getRequest(); $screenName = $request->get('screen_name'); $user = null; if ($screenName != '') { if (is_scalar($screenName) && ($user = User::findOne(['screen_name' => $screenName]))) { // ok } else { return ['error' => ['screen_name' => ['not found']]]; } } $now = @$_SERVER['REQUEST_TIME'] ?: time(); $subQuery = (new \yii\db\Query())->select(['id' => 'MAX({{battle}}.[[id]])'])->from('battle')->andWhere(['>=', '{{battle}}.[[at]]', gmdate('Y-m-d H:i:sO', $now - 60 * 86400)])->groupBy('{{battle}}.[[user_id]]'); if ($user) { $subQuery->andWhere(['{{battle}}.[[user_id]]' => $user->id]); } $battles = Battle::find()->andWhere(['in', '{{battle}}.[[id]]', $subQuery])->with(['user', 'user.userStat', 'lobby', 'rule', 'map', 'weapon', 'weapon.subweapon', 'weapon.special', 'rank', 'battleImageResult', 'battleImageJudge'])->limit(500)->orderBy('{{battle}}.[[id]] DESC'); $ret = []; foreach ($battles->each() as $model) { $json = $model->user->toJsonArray(); $json['latest_battle'] = $model->toJsonArray(['user', 'death_reasons']); $ret[] = $json; } if ($user) { return count($ret) >= 1 ? array_shift($ret) : null; } return $ret; }
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; }
public function run() { $request = Yii::$app->getRequest(); $user = User::findOne(['screen_name' => $request->get('screen_name')]); if (!$user) { throw new NotFoundHttpException(Yii::t('app', 'Could not find user')); } // リスト表示モード切替 if ($request->get('v') != '') { $view = $request->get('v'); if ($view === 'simple' || $view === 'standard') { Yii::$app->response->cookies->add(new Cookie(['name' => 'battle-list', 'value' => $view, 'expire' => time() + 86400 * 366])); } $next = $_GET; unset($next['v']); $next[0] = 'show/user'; $this->controller->redirect(Url::to($next)); return; } $battle = Battle::find()->with(['lobby', 'rule', 'rule.mode', 'map', 'weapon', 'weapon.subweapon', 'weapon.special', 'rank', 'rankAfter']); $filter = new BattleFilterForm(); $filter->load($_GET); $filter->screen_name = $user->screen_name; if ($filter->validate()) { $battle->filter($filter); } $summary = $battle->summary; $permLink = Url::to(array_merge(['show/user', 'screen_name' => $user->screen_name], $filter->hasErrors() ? [] : $filter->toPermLink()), true); $isPjax = $request->isPjax; $template = $this->viewMode === 'simple' ? 'user.simple.tpl' : 'user.tpl'; return $this->controller->render($template, array_merge(['user' => $user, 'battleDataProvider' => new ActiveDataProvider(['query' => $battle, 'pagination' => ['pageSize' => 100]]), 'summary' => $summary, 'filter' => $filter, 'permLink' => $permLink], $this->makeFilterFormData($user))); }
public function getWPData() { $query = Battle::find()->with(['rule'])->innerJoinWith(['rule'])->joinWith(['lobby'])->andWhere(['{{battle}}.[[user_id]]' => $this->user->id, '{{rule}}.[[key]]' => 'nawabari'])->andWhere(['not', ['{{battle}}.[[is_win]]' => null]])->andWhere(['or', ['{{battle}}.[[lobby_id]]' => null], ['<>', '{{lobby}}.[[key]]', 'private']])->orderBy('{{battle}}.[[id]] DESC'); $battles = []; foreach ($query->each() as $battle) { $battles[] = (object) ['index' => -1 * count($battles), 'is_win' => $battle->is_win, 'rule' => $battle->rule->key, 'totalWP' => null, 'movingWP' => null, 'movingWP50' => null]; } if (empty($battles)) { return []; } $battles = array_reverse($battles); $fMoving = function ($range, $currentIndex) use(&$battles) { if ($currentIndex + 1 < $range) { return null; } $tmp = array_slice($battles, $currentIndex + 1 - $range, $range); $win = count(array_filter($tmp, function ($a) { return $a->is_win; })); return $win * 100 / $range; }; $totalWin = 0; $totalCount = 0; foreach ($battles as $i => $battle) { ++$totalCount; if ($battle->is_win) { ++$totalWin; } $battle->totalWP = $totalWin * 100 / $totalCount; $battle->movingWP = $fMoving(20, $i); $battle->movingWP50 = $fMoving(50, $i); } return $battles; }
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]); } }
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(); }
/** * Tests whether the data can be retrieved from the database */ public function testExtract() { // create users $user1 = factory(App\Models\User::class)->create(); $user2 = factory(App\Models\User::class)->create(); $user3 = factory(App\Models\User::class)->create(); // create battle $battle = new Battle(); $battle->rapper1_id = $user1->id; $battle->rapper2_id = $user2->id; $battle->save(); // create some votes $vote1 = new Vote(); $vote1->user_id = $user1->id; $vote1->battle_id = $battle->id; $vote1->rapper_number = 1; $vote1->save(); $vote2 = new Vote(); $vote2->user_id = $user2->id; $vote2->battle_id = $battle->id; $vote2->rapper_number = 0; $vote2->save(); $vote3 = new Vote(); $vote3->user_id = $user3->id; $vote3->battle_id = $battle->id; $vote3->rapper_number = 0; $vote3->save(); // can user be retrieved? $this->assertEquals($user3->id, $vote3->user->id); // can battle be retrieved? $this->assertEquals($battle->id, $vote3->battle->id); // can a battle get it's votes? $battlevotecnt = $battle->votes()->count(); $this->assertEquals(3, $battlevotecnt); $battlevotes = $battle->votes()->get()->values()->keyBy('user_id'); $this->assertNotNull($battlevotes->get($user1->id)); $this->assertNotNull($battlevotes->get($user2->id)); $this->assertNotNull($battlevotes->get($user3->id)); // can a user get their votes? $vote1id = $user1->votes()->get()->values()->keyBy('user_id')->get($user1->id)->id; $this->assertEquals($vote1->id, $vote1id); }
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]); }
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]); }
/** * Test for battles() * * @return void */ public function testBattles() { $user1 = factory(App\Models\User::class)->create(); $user2 = factory(App\Models\User::class)->create(); $user3 = factory(App\Models\User::class)->create(); $battle1 = new Battle(); $battle1->rapper1_id = $user1->id; $battle1->rapper2_id = $user2->id; $battle1->save(); $battle2 = new Battle(); $battle2->rapper1_id = $user2->id; $battle2->rapper2_id = $user3->id; $battle2->save(); $battle3 = new Battle(); $battle3->rapper1_id = $user3->id; $battle3->rapper2_id = $user1->id; $battle3->save(); // get battles of user2 $battles = $user2->battles()->lists('id')->toArray(); $this->assertCount(2, $battles); $this->assertContains($battle1->id, $battles); $this->assertContains($battle2->id, $battles); $this->assertNotContains($battle3->id, $battles); }
/** * Get all battles of this user */ public function battles() { $ids_r1 = $this->battlesRapper1()->lists('rapper1_id'); $ids_r2 = $this->battlesRapper2()->lists('rapper2_id'); return Battle::whereIn('rapper1_id', $ids_r1)->orWhereIn('rapper2_id', $ids_r2); }
/** * @return \yii\db\ActiveQuery */ public function getBattles() { return $this->hasMany(Battle::className(), ['fest_title_id' => 'id']); }
/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); // config $usercnt = 20; $userRappercnt = 20; $battlecnt = 20; $battleRequestcnt = rand($userRappercnt / 4, $userRappercnt / 2); // max $userRappercnt / 2 $openBattlecnt = rand($userRappercnt / 4, $userRappercnt / 2); // max $userRappercnt / 2 // create users $users = factory(App\Models\User::class, $usercnt)->create(); // rapper and non-rapper $usersRapper = factory(App\Models\User::class, 'rapper', $userRappercnt)->create(); // rapper only //---------------------------------------------- // create battles for ($i = 0; $i < $battlecnt; $i++) { $battle = new Battle(); // get first rapper $battle->rapper1_id = $usersRapper->random()->id; // get second rapper != first rapper do { $battle->rapper2_id = $usersRapper->random()->id; } while ($battle->rapper1_id == $battle->rapper2_id); $battle->save(); //----------------------------------------- // create votes // create list of all created users $usersAll = $users->keyBy('id')->merge($usersRapper->keyBy('id')); $usersAll->shuffle(); $userVotescnt = rand(0, $usersAll->count()); for ($j = 0; $j < $userVotescnt; $j++) { $vote = new Vote(); $vote->user_id = $usersAll->get($j)->id; $vote->battle_id = $battle->id; $vote->rapper_number = rand(0, 1); $vote->save(); // update vote counter if ($vote->rapper_number == 0) { $battle->votes_rapper1++; } else { $battle->votes_rapper2++; } } // save vote count in battle $battle->save(); $battle->rapper1->updateRating(); $battle->rapper2->updateRating(); } //---------------------------------------------- // create battle_requests for ($i = 0; $i < $battleRequestcnt * 2; $i += 2) { $battleRequest = new BattleRequest(); $battleRequest->challenger_id = $usersRapper->get($i)->id; $battleRequest->challenged_id = $usersRapper->get($i + 1)->id; $battleRequest->save(); } //---------------------------------------------- // create open battles $usersRapper->shuffle(); for ($i = 0; $i < $openBattlecnt * 2; $i += 2) { $openBattle = new OpenBattle(); $openBattle->rapper1_id = $usersRapper->get($i)->id; $openBattle->rapper2_id = $usersRapper->get($i + 1)->id; $openBattle->phase = rand(1, 2); // TODO: how many phases? $openBattle->beat1_id = rand(0, 2); $openBattle->beat2_id = rand(0, 2); $openBattle->save(); } Model::reguard(); }
/** * Test for scopeOpenVoting and scopeCompleted */ public function testScopesOpenCompleted() { // create rappers $user1 = factory(App\Models\User::class)->create(); $user2 = factory(App\Models\User::class)->create(); $votingperiod = config('rap-battle.votingperiod', 24); // battles older than this date are closed: $timeoldest = new Carbon(); $timeoldest->subHours($votingperiod + 1); // create two battles $battle1 = new Battle(); $battle1->rapper1_id = $user1->id; $battle1->rapper2_id = $user2->id; $battle1->save(); $battle2 = new Battle(); $battle2->rapper1_id = $user1->id; $battle2->rapper2_id = $user2->id; $battle2->created_at = $timeoldest->toDateTimeString(); $battle2->save(); // test scopeOpenVoting // get battles from database $openBattles = Battle::openVoting()->get()->values()->keyBy('id'); $this->assertTrue($openBattles->has($battle1->id)); $this->assertFalse($openBattles->has($battle2->id)); // test scopeCompleted // get battles from database $completedBattles = Battle::completed()->get()->values()->keyBy('id'); $this->assertFalse($completedBattles->has($battle1->id)); $this->assertTrue($completedBattles->has($battle2->id)); }
/** * @return \yii\db\ActiveQuery */ public function getBattles() { return $this->hasMany(Battle::className(), ['id' => 'battle_id'])->viaTable('battle_death_reason', ['reason_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getBattles() { return $this->hasMany(Battle::className(), ['gender_id' => 'id']); }
private function runGetImpl(Battle $battle) { $ret = ['id' => $battle->id, 'url' => Url::to(['show/battle', 'screen_name' => $battle->user->screen_name, 'battle' => $battle->id], true), 'user' => $battle->user ? $battle->user->toJsonArray() : null, 'lobby' => $battle->lobby ? $battle->lobby->toJsonArray() : null, 'rule' => $battle->rule ? $battle->rule->toJsonArray() : null, 'map' => $battle->map ? $battle->map->toJsonArray() : null, 'weapon' => $battle->weapon ? $battle->weapon->toJsonArray() : null, 'rank' => $battle->rank ? $battle->rank->toJsonArray() : null, 'rank_exp' => $battle->rank_exp, 'rank_after' => $battle->rankAfter ? $battle->rankAfter->toJsonArray() : null, 'rank_exp_after' => $battle->rank_exp_after, 'level' => $battle->level, 'level_after' => $battle->level_after, 'cash' => $battle->cash, 'cash_after' => $battle->cash_after, 'result' => $battle->is_win === true ? 'win' : ($battle->is_win === false ? 'lose' : null), 'rank_in_team' => $battle->rank_in_team, 'kill' => $battle->kill, 'death' => $battle->death, 'kill_ratio' => isset($battle->kill_ratio) ? (double) $battle->kill_ratio : null, 'death_reasons' => array_map(function ($model) { return $model->toJsonArray(); }, $battle->getBattleDeathReasons()->with(['reason', 'reason.type'])->all()), 'gender' => $battle->gender ? $battle->gender->toJsonArray() : null, 'fest_title' => $battle->gender && $battle->festTitle ? $battle->festTitle->toJsonArray($battle->gender) : null, 'my_team_color' => ['hue' => $battle->my_team_color_hue, 'rgb' => $battle->my_team_color_rgb], 'his_team_color' => ['hue' => $battle->his_team_color_hue, 'rgb' => $battle->his_team_color_rgb], 'image_judge' => $battle->battleImageJudge ? Url::to(Yii::getAlias('@web/images') . '/' . $battle->battleImageJudge->filename, true) : null, 'image_result' => $battle->battleImageResult ? Url::to(Yii::getAlias('@web/images') . '/' . $battle->battleImageResult->filename, true) : null, 'agent' => ['name' => $battle->agent ? $battle->agent->name : null, 'version' => $battle->agent ? $battle->agent->version : null], 'start_at' => $battle->start_at != '' ? DateTimeFormatter::unixTimeToJsonArray(strtotime($battle->start_at)) : null, 'end_at' => $battle->end_at != '' ? DateTimeFormatter::unixTimeToJsonArray(strtotime($battle->end_at)) : null, 'register_at' => DateTimeFormatter::unixTimeToJsonArray(strtotime($battle->at))]; if ($battle->isNawabari) { $nawabari = $battle->battleNawabari; $ret = array_merge($ret, ['my_point' => $nawabari ? (int) $nawabari->my_point : null, 'my_team_final_point' => $nawabari ? $nawabari->my_team_final_point : null, 'his_team_final_point' => $nawabari ? $nawabari->his_team_final_point : null, 'my_team_final_percent' => $nawabari ? $nawabari->my_team_final_percent : null, 'his_team_final_percent' => $nawabari ? $nawabari->his_team_final_percent : null]); } if ($battle->isGachi) { $gachi = $battle->battleGachi; $ret = array_merge($ret, ['knock_out' => $gachi ? $gachi->is_knock_out : null, 'my_team_count' => $gachi ? $gachi->my_team_count : null, 'his_team_count' => $gachi ? $gachi->his_team_count : null]); } $resp = Yii::$app->getResponse(); $resp->format = 'json'; return $ret; }
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]; }
public function run() { $active = Battle::find()->andWhere(['in', '{{battle}}.[[id]]', $this->getActiveUserBattleIdList()])->with(['user', 'rule', 'map', 'battleImageResult'])->all(); return $this->controller->render('index.tpl', ['active' => $active]); }
/** * Test for postVote */ public function testPostVote() { $user1 = factory(App\Models\User::class)->create(); $user2 = factory(App\Models\User::class)->create(); // create two battles $battle = new Battle(); $battle->rapper1_id = $user1->id; $battle->rapper2_id = $user2->id; $battle->votes_rapper1 = 0; $battle->votes_rapper2 = 0; $battle->save(); $this->actingAs($user1)->post('/battle/' . $battle->id . '/vote', ['rapper_number' => 1]); // TODO: check if vote exists $this->assertEquals(1, $battle->votes()->count()); $this->assertEquals(0, $battle->votes_rapper1); $this->assertEquals(1, $battle->votes_rapper2); }
private function runGetImpl2(Battle $battle, array $deathReasons, array $players = null, Agent $agent = null) { $ret = $battle->toJsonArray(); $ret['death_reasons'] = array_map(function ($model) { return $model->toJsonArray(); }, $deathReasons); $ret['players'] = is_array($players) && !empty($players) ? array_map(function ($model) { return $model->toJsonArray(); }, $players) : null; $ret['agent']['name'] = $agent ? $agent->name : null; $ret['agent']['version'] = $agent ? $agent->version : null; $resp = Yii::$app->getResponse(); $resp->format = 'json'; return $ret; }
public function run() { $subQuery = (new \yii\db\Query())->select(['id' => 'MAX({{battle}}.[[id]])'])->from('battle')->andWhere(['>=', '{{battle}}.[[at]]', gmdate('Y-m-d H:i:sO', time() - 60 * 86400)])->groupBy('{{battle}}.[[user_id]]'); $battles = Battle::find()->andWhere(['in', '{{battle}}.[[id]]', $subQuery])->with(['user', 'rule', 'map', 'battleImageResult'])->limit(500)->each(); return $this->controller->render('users.tpl', ['battles' => $battles]); }
/** * @return \yii\db\ActiveQuery */ public function getBattle() { return $this->hasOne(Battle::className(), ['id' => 'battle_id']); }
public function postVote(Request $request, $battle_id) { $this->validate($request, ['rapper_number' => 'required|integer']); $user = Auth::user(); $battle = Battle::find($battle_id); if (is_null($user)) { return response('', 403); } // forbidden if (is_null($battle)) { return response('', 404); } // Not found // check if battle is votable if (!$battle->isOpenVoting()) { return response('', 405); } // Method not allowed // don't let the user change a vote if ($user->votes()->where('battle_id', $battle->id)->get()->first() == null) { // build new vote $vote = new Vote(); $vote->user_id = $user->id; $vote->battle_id = $battle->id; $vote->rapper_number = $request->input('rapper_number'); // update vote counter if ($vote->rapper_number == 0) { $battle->votes_rapper1++; } else { if ($vote->rapper_number == 1) { $battle->votes_rapper2++; } else { return response('', 422); } } // Unprocessable Entity // save vote count in battle $vote->save(); $battle->save(); } else { return response('', 405); // Method not allowed } }
public function getLatestBattle() { return $this->hasOne(Battle::className(), ['user_id' => 'id'])->orderBy('{{battle}}.[[id]] DESC')->limit(1); }