/** * Selects a celestial body that should be used for a new player. * @todo could use a better algorithm. Maybe place new players in areas that * are not too densly populated. * Or place them among other players just having started playing. * @return CelestialBody */ public function chooseCelestialBodyForNewPlayer() { $db = Instance::ensure($this->db, Connection::className()); /* @var $db Connection */ $sql = <<<'EOT' SELECT {{cb}}.[[id]] AS [[id]] FROM {{%celestial_body}} {{cb}} LEFT JOIN {{%base}} {{b}} ON {{b}}.[[id]] = {{cb}}.[[id]] WHERE {{b}}.[[id]] IS NULL ; EOT; $recordSets = $db->createCommand($sql)->queryAll(); $randomOffset = rand(0, count($recordSets) - 1); $randomRecordSet = $recordSets[$randomOffset]; return CelestialBody::findOne(['id' => $randomRecordSet['id']]); }
/** * Finds the CelestialBody model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return CelestialBody the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = CelestialBody::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }