Skip to content

Commit

Permalink
fix #51, ノックアウト率の集計をバッチに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
fetus-hina committed Jan 15, 2016
1 parent b820693 commit 5d440d0
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 76 deletions.
73 changes: 18 additions & 55 deletions actions/entire/KnockoutAction.php
@@ -1,6 +1,6 @@
<?php
/**
* @copyright Copyright (C) 2015 AIZAWA Hina
* @copyright Copyright (C) 2015-2016 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@bouhime.com>
*/
Expand All @@ -10,7 +10,7 @@
use Yii;
use yii\web\ViewAction as BaseAction;
use app\models\Rule;
use app\models\Map;
use app\models\Knockout;

class KnockoutAction extends BaseAction
{
Expand All @@ -21,71 +21,34 @@ public function run()
->innerJoinWith('mode')
->andWhere(['{{game_mode}}.[[key]]' => 'gachi']);
foreach ($query->all() as $rule) {
$rules[$rule->id] = Yii::t('app-rule', $rule->name);
$rules[$rule->key] = Yii::t('app-rule', $rule->name);
}
asort($rules);

$maps = [];
foreach (Map::find()->all() as $map) {
$maps[$map->id] = [
$map->key,
Yii::t('app-map', $map->name)
];
}
uasort($maps, function ($a, $b) {
return strnatcasecmp($a[1], $b[1]);
});

// init data
$data = [];
foreach (array_keys($maps) as $mapId) {
$data[$mapId] = [];
foreach (array_keys($rules) as $ruleId) {
$data[$mapId][$ruleId] = (object)[
'battle_count' => 0,
'ko_count' => 0,
foreach (Knockout::find()->with('map', 'rule')->each() as $row) {
$map = $row->map->key;
$rule = $row->rule->key;
if (!isset($data[$map])) {
$row->map->name = Yii::t('app-map', $row->map->name);
$data[$map] = (object)[
'map' => $row->map,
'rules' => new \stdClass(),
];
}
$data[$map]->rules->{$rule} = (object)[
'battles' => $row->battles,
'knockouts' => $row->knockouts,
];
}

// set data
foreach ($this->query() as $row) {
$ruleId = $row['rule_id'];
$mapId = $row['map_id'];
$data[$mapId][$ruleId]->battle_count = (int)$row['battle_count'];
$data[$mapId][$ruleId]->ko_count = (int)$row['ko_count'];
}
uasort($data, function ($a, $b) {
return strnatcasecmp($a->map->name, $b->map->name);
});

return $this->controller->render('knockout.tpl', [
'rules' => $rules,
'maps' => $maps,
'data' => $data,
]);
}

private function query()
{
$query = (new \yii\db\Query())
->select([
'rule_id' => '{{battle}}.[[rule_id]]',
'map_id' => '{{battle}}.[[map_id]]',
'battle_count' => 'COUNT({{battle}}.*)',
'ko_count' => sprintf(
'SUM(%s)',
'CASE WHEN {{battle}}.[[is_knock_out]] THEN 1 ELSE 0 END'
),
])
->from('battle')
->innerJoin('rule', '{{battle}}.[[rule_id]] = {{rule}}.[[id]]')
->innerJoin('game_mode', '{{rule}}.[[mode_id]] = {{game_mode}}.[[id]]')
->andWhere('{{battle}}.[[map_id]] IS NOT NULL')
->andWhere('{{battle}}.[[is_win]] IS NOT NULL')
->andWhere('{{battle}}.[[is_knock_out]] IS NOT NULL')
->andWhere([
'{{game_mode}}.[[key]]' => 'gachi',
'{{battle}}.[[is_automated]]' => true,
])
->groupBy(['{{battle}}.[[rule_id]]', '{{battle}}.[[map_id]]']);
return $query->createCommand()->queryAll();
}
}
62 changes: 62 additions & 0 deletions commands/StatController.php
Expand Up @@ -11,13 +11,19 @@
use yii\console\Controller;
use yii\helpers\Console;
use app\models\BattlePlayer;
use app\models\Knockout;
use app\models\Rule;
use app\models\StatEntireUser;
use app\models\StatWeapon;
use app\models\StatWeaponBattleCount;

class StatController extends Controller
{
/**
* 全体統計 - ブキ統計を更新します
*
* これを実行しないとブキ統計は表示されません。
*/
public function actionUpdateEntireWeapons()
{
$db = Yii::$app->db;
Expand Down Expand Up @@ -179,6 +185,11 @@ private function createSelectQueryForUpdateEntireWeaponsBattleCount()
return $query;
}

/**
* 全体統計 - 利用者数を更新します。
*
* これを実行しなくてもリアルタイム集計しますが数が増えると死にます。
*/
public function actionUpdateEntireUser()
{
// 集計対象期間を計算する
Expand Down Expand Up @@ -219,4 +230,55 @@ function ($row) {
->execute();
$transaction->commit();
}

/**
* 全体統計 - ノックアウト率統計を更新します
*
* これを実行しないとブキ統計は表示されません。
*/
public function actionUpdateKnockout()
{
$db = Yii::$app->db;
$transaction = $db->beginTransaction();
Knockout::deleteAll();
$db->createCommand()
->batchInsert(
Knockout::tableName(),
[ 'map_id', 'rule_id', 'battles', 'knockouts' ],
array_map(
function ($row) {
return [
$row['map_id'],
$row['rule_id'],
$row['battles'],
$row['knockouts'],
];
},
(new \yii\db\Query())
->select([
'map_id' => '{{battle}}.[[map_id]]',
'rule_id' => '{{battle}}.[[rule_id]]',
'battles' => 'COUNT({{battle}}.*)',
'knockouts' => 'SUM(CASE WHEN {{battle}}.[[is_knock_out]] THEN 1 ELSE 0 END)',
])
->from('battle')
->innerJoin('rule', '{{battle}}.[[rule_id]] = {{rule}}.[[id]]')
->innerJoin('game_mode', '{{rule}}.[[mode_id]] = {{game_mode}}.[[id]]')
->innerJoin('lobby', '{{battle}}.[[lobby_id]] = {{lobby}}.[[id]]')
->innerJoin('map', '{{battle}}.[[map_id]] = {{map}}.[[id]]')
->andWhere(['and',
['not', ['{{battle}}.[[is_win]]' => null]],
['not', ['{{battle}}.[[is_knock_out]]' => null]],
['not', ['{{lobby}}.[[key]]' => 'private']],
['{{game_mode}}.[[key]]' => 'gachi'],
['{{battle}}.[[is_automated]]' => true],
])
->groupBy(['{{battle}}.[[map_id]]', '{{battle}}.[[rule_id]]'])
->createCommand()
->queryAll()
)
)
->execute();
$transaction->commit();
}
}
23 changes: 23 additions & 0 deletions migrations/m160115_130125_knockout.php
@@ -0,0 +1,23 @@
<?php
use yii\db\Migration;

class m160115_130125_knockout extends Migration
{
public function up()
{
$this->createTable('knockout', [
'map_id' => $this->integer()->notNull(),
'rule_id' => $this->integer()->notNull(),
'battles' => $this->bigInteger()->notNull(),
'knockouts' => $this->bigInteger()->notNull(),
]);
$this->addPrimaryKey('pk_knockout', 'knockout', ['map_id', 'rule_id']);
$this->addForeignKey('fk_knockout_1', 'knockout', 'map_id', 'map', 'id');
$this->addForeignKey('fk_knockout_2', 'knockout', 'rule_id', 'rule', 'id');
}

public function down()
{
$this->dropTable('knockout');
}
}
72 changes: 72 additions & 0 deletions models/Knockout.php
@@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (C) 2016 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@bouhime.com>
*/

namespace app\models;

use Yii;

/**
* This is the model class for table "knockout".
*
* @property integer $map_id
* @property integer $rule_id
* @property integer $battles
* @property integer $knockouts
*
* @property Map $map
* @property Rule $rule
*/
class Knockout extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'knockout';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['map_id', 'rule_id', 'battles', 'knockouts'], 'required'],
[['map_id', 'rule_id', 'battles', 'knockouts'], 'integer']
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'map_id' => 'Map ID',
'rule_id' => 'Rule ID',
'battles' => 'Battles',
'knockouts' => 'Knockouts',
];
}

/**
* @return \yii\db\ActiveQuery
*/
public function getMap()
{
return $this->hasOne(Map::className(), ['id' => 'map_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getRule()
{
return $this->hasOne(Rule::className(), ['id' => 'rule_id']);
}
}
49 changes: 28 additions & 21 deletions views/entire/knockout.tpl
Expand Up @@ -17,22 +17,15 @@
{{AdWidget}}
{{SnsWidget}}

<p>
{{'Excluded: Private Battles'|translate:'app'|escape}}
</p>
<div class="table-responsive table-responsive-force">
<table class="table table-condensed graph-container">
<thead>
<tr>
{{$width = 100 / ($rules|count + 1)}}
<th style="width:{{$width|escape}}%;min-width:200px"></th>
{{foreach $rules as $ruleKey => $ruleName}}
<th style="width:{{$width|escape}}%;min-width:200px">
{{$ruleName|escape}}
</th>
{{/foreach}}
</tr>
</thead>
<tbody>
<tr>
<th>
<th style="width:{{$width|escape}}%;min-width:200px">
<div style="display:inline-block;border:2px solid #ddd;padding:2px 5px">
<div style="display:inline-block">
<span style="display:inline-block;width:1.618em;height:1em;line-height:1px" class="legend-bg" data-color="ko"></span>
Expand All @@ -51,13 +44,26 @@
{{/registerJs}}
</div>
</th>
{{foreach $rules as $ruleKey => $ruleName}}
<th style="width:{{$width|escape}}%;min-width:200px">
{{$ruleName|escape}}
</th>
{{/foreach}}
</tr>
</thead>
<tbody>
<tr>
<th>
</th>
{{foreach $rules as $ruleKey => $ruleName}}
{{$totalBattleCount = 0}}
{{$totalKOCount = 0}}
{{foreach $maps as $mapKey => $mapName}}
{{$_ = $data[$mapKey][$ruleKey]}}
{{$totalBattleCount = $totalBattleCount + $_->battle_count|default:0}}
{{$totalKOCount = $totalKOCount + $_->ko_count|default:0}}
{{foreach $data as $map}}
{{$_ = $map->rules->$ruleKey}}
{{if $_}}
{{$totalBattleCount = $totalBattleCount + $_->battles}}
{{$totalKOCount = $totalKOCount + $_->knockouts}}
{{/if}}
{{/foreach}}
<td>
{{if $totalBattleCount > 0}}
Expand All @@ -75,21 +81,22 @@
height: auto;
}
{{/registerCss}}
{{foreach $maps as $mapKey => $mapName}}
{{foreach $data as $_}}
{{$map = $_->map}}
<tr>
<th>
{{$mapName.1|escape}}<br>
{{$mapFile = 'daytime/'|cat:$mapName.0:'.jpg'}}
{{$map->name|escape}}<br>
{{$mapFile = 'daytime/'|cat:$map->key:'.jpg'}}
<img src="{{$app->assetManager->getAssetUrl(
$app->assetManager->getBundle('app\assets\MapImageAsset'),
$mapFile
)}}" class="map-image">
</th>
{{foreach $rules as $ruleKey => $ruleName}}
<td>
{{$_ = $data[$mapKey][$ruleKey]}}
{{if $_->battle_count|default:0 > 0}}
{{$tmp = ['battle' => $_->battle_count, 'ko' => $_->ko_count]}}
{{$a = $_->rules->$ruleKey}}
{{if $a && $a->battles > 0}}
{{$tmp = ['battle' => $a->battles, 'ko' => $a->knockouts]}}
<div class="pie-flot-container" data-json="{{$tmp|json_encode|escape}}">
</div>
{{/if}}
Expand Down

0 comments on commit 5d440d0

Please sign in to comment.