main = $main; $this->lib = $this->main->getLib(); } function run($user, $gId, $dryRun = true, $planet = null, $player = null) { // Get the game and its main library $game = config::getGame($gId); if (is_null($game)) { return array('error' => 0); } $lib = $game->getLib(); // Check the game's status $status = $game->status(); if ($status != 'RUNNING' && $status != 'READY' && $status != 'ENDING') { return array('error' => 0); } // Check if the game is available and can be joined if (! $lib->call('canJoin')) { return array('error' => 0); } // Does the user play the game already? if ((int)$user < 1 || $lib->call('doesUserPlay', $user)) { return array('error' => 1); } // Has the current user played that game in the past? $returning = $lib->call("hasPlayed", $user); // If the user submitted the form, handle it if ($dryRun) { // Display a blank form return array( "game" => $gId, "gName" => $game->text, "desc" => $game->descriptions[getLanguage()], "planet" => "", "planetError" => 0, "player" => "", "playerError" => 0, "returning" => $returning ); } // Check the specified planet and player names $pErr = $lib->call('checkPlanetName', $planet); if ($returning) { $pnErr = $this->checkName($player); } else { $pnErr = 0; } if (!($pErr || $pnErr)) { // Try to register to this game $res = $lib->call('register', $user, $planet, $returning ? $player : null); switch ($res) : case 1: return array('error' => 2); case 2: $pnErr = 6; break; case 3: $pErr = 6; break; endswitch; } if ($pErr || $pnErr) { return array( "game" => $gId, "gName" => $game->text, "planet" => $planet, "planetError" => $pErr, "player" => $player, "playerError" => $pnErr, "returning" => $returning ); } return array("registered" => $gId); } function checkName($n) { if (strlen($n) > 15) { return array('error' => 1); } elseif (preg_match('/[^A-Za-z0-9_\.\-\+@\/'."'".' ]/', $n)) { return array('error' => 2); } elseif (preg_match('/^\s/', $n) || preg_match('/\s$/', $n)) { return array('error' => 3); } elseif (preg_match('/\s\s+/', $n)) { return array('error' => 4); } elseif (strlen($n) < 2) { return array('error' => 5); } return 0; } } ?>