private function getEntireWeaponsByRule(Rule $rule) { $query = StatWeapon::find()->with(['weapon', 'weapon.subweapon', 'weapon.special'])->andWhere(['{{stat_weapon}}.[[rule_id]]' => $rule->id]); $totalPlayers = 0; $list = array_map(function ($model) use(&$totalPlayers) { $totalPlayers += $model->players; return (object) ['key' => $model->weapon->key, 'name' => Yii::t('app-weapon', $model->weapon->name), 'subweapon' => (object) ['key' => $model->weapon->subweapon->key, 'name' => Yii::t('app-subweapon', $model->weapon->subweapon->name)], 'special' => (object) ['key' => $model->weapon->special->key, 'name' => Yii::t('app-special', $model->weapon->special->name)], 'count' => (int) $model->players, 'avg_kill' => $model->players > 0 ? $model->total_kill / $model->players : null, 'sum_kill' => $model->total_kill, 'avg_death' => $model->players > 0 ? $model->total_death / $model->players : null, 'sum_death' => $model->total_death, 'wp' => $model->players > 0 ? $model->win_count * 100 / $model->players : null, 'win_count' => $model->win_count, 'avg_inked' => $model->point_available > 0 ? $model->total_point / $model->point_available : null]; }, $query->all()); usort($list, function ($a, $b) { foreach (['count', 'wp', 'avg_kill', 'avg_death'] as $key) { $tmp = $b->{$key} - $a->{$key}; if ($tmp != 0) { return $tmp; } } return strnatcasecmp($a->name, $b->name); }); $battleCount = StatWeaponBattleCount::findOne(['rule_id' => $rule->id]); return (object) ['battle_count' => $battleCount ? $battleCount->count : 0, 'player_count' => $totalPlayers, 'weapons' => $list]; }
public function actionUpdateEntireWeapons() { $db = Yii::$app->db; $transaction = $db->beginTransaction(); echo "Delete old data...\n"; StatWeapon::deleteAll(); StatWeaponBattleCount::deleteAll(); echo "INSERT stat_weapon...\n"; $select = $this->createSelectQueryForUpdateEntireWeapons(); $sql = sprintf('INSERT INTO %s (%s) %s', $db->quoteTableName(StatWeapon::tableName()), implode(', ', array_map(function ($k) use($db) { return $db->quoteColumnName($k); }, array_keys($select->select))), $select->createCommand()->rawSql); $db->createCommand($sql)->execute(); echo "INSERT stat_weapon_battle_count..\n"; $select = $this->createSelectQueryForUpdateEntireWeaponsBattleCount(); $sql = sprintf('INSERT INTO %s (%s) %s', $db->quoteTableName(StatWeaponBattleCount::tableName()), implode(', ', array_map(function ($k) use($db) { return $db->quoteColumnName($k); }, array_keys($select->select))), $select->createCommand()->rawSql); $db->createCommand($sql)->execute(); echo "done.\n"; $transaction->commit(); }