This is the documentation of the easy moola game PHP API. Using it, you will be able to create moola enabled games which take care of all the money transfers automatically by implementing only 2 files. The first file is the "brain" of the game which takes care of turns/winners and game logic. The second file is the GUI part of the game, which displays the game stuff to the user. This tutorial will show you how to make a simple "guess the closest number" game. Lets start off with the game logic file. This is a simple template: setGameState('number', rand(0, 9)); } } public function turnPlayer($args = array()) { if ($this->canGo()) { // Is it their turn? /* $this->player is the current player id. $this->state is saved and loaded automatically and is persisted. So yeah, now we save their guess. */ if (array_key_exists('guess', $args) && is_numeric($args['guess'])) { parent::turnPlayer($args); $this->state[$this->player]['guess'] = intval($args['guess']); } } } public function turnBot() { /* Optional autoplayer turn function. Only if $this->bot = true; */ $player = $this->player; $this->setPlayer(Moola::BOT); $this->turnPlayer(array('guess' => rand(4, 5))); $this->setPlayer($player); } public function winner() { /* This decides the winner. First check if a winner has already been declared (previously or due to a timeout). If not it returns null and you can detect who won yourself. Save and return the winner. */ $winner = parent::winner(); if ($winner === null) { $number = $this->getGameState('number'); $p1 = $this->getPlayer(1); $p2 = $this->getPlayer(2); $diffp1 = abs($number - $this->state[$p1]['guess']); $diffp2 = abs($number - $this->state[$p2]['guess']); if ($diffp1 == $diffp2) { $winner = 0; } else if ($diffp1 < $diffp2) { $winner = $p1; } else if ($diffp2 < $diffp1) { $winner = $p2; } $this->setGameState('winner', $winner); } return $winner; } public function completeForPlayer($player) { if (parent::completeForPlayer($player)) { return true; } elseif (isset($this->state[$player]['guess'])) { return true; } return false; } public function addPlayer($player, $info = null) { /* Optional. Add the player to the game. Set any player state specific variables. */ if (parent::addPlayer($player, $info)) { $this->state[$player]['foo'] = 'bar'; } } public function completeForPlayer($player) { /* Is the game complete for the player? */ if (parent::completeForPlayer($player)) { /* Timed out or already complete. */ return true; } elseif (isset($this->state[$player]['guess'])) { /* They made a guess */ return true; } return false; } public function canGo() { /* Optional. Can the player make their move? */ if (!parent::canGo() == false) { /* not their turn in MULTITURN_WAIT games, or game already complete for them. */ return false; } return true; } } return new Game_NumberGuess(isset($_SESSION['GAMEID']) ? $_SESSION['GAMEID'] : null); // Return an instance of the game. ?> Another useful method of the class is ->opponent(); It returns the opponent's id if this->player is set The current player is automatically set by the game controller class (to be discussed in the GUI part of the game classes. Next comes the GUI part of the game classes. It is pretty simple. This is the basic template: mode()) { case MoolaGameController::GAMEREPORT_COMPLETE: /* $controller->gamereport is automatically set to the game logic class described above. */ echo $html->h2('Game Verdict'); switch ($controller->gamereport->winner()) { case null: echo $html->p('Tie Game'); break; default: echo $html->p($controller->gamereport->getPlayerName($controller->gamereport->winner()) . ' Wins'); /* Get player name returns the moola username of the player. */ } echo $html->p($html->a('Play Again', array('href'=>$_SERVER['PHP_SELF']))); $p1 = $controller->gamereport->getPlayer(1); $p2 = $controller->gamereport->getPlayer(2); $c1 = isset($controller->gamereport->state[$p1]['guess']) ? $controller->gamereport->state[$p1]['guess'] : '?'; $c2 = isset($controller->gamereport->state[$p2]['guess']) ? $controller->gamereport->state[$p2]['guess'] : '?'; echo $html->p($controller->gamereport->getPlayerName($p1) . ' (' . $c1 . ') vs. ' . $controller->gamereport->getPlayerName($p2) . ' (' . $c2 . ')'); echo $html->p('Number Was: ' . $controller->gamereport->getGameState('number')); break; case MoolaGameController::GAMEREPORT_INPROGRESS: echo $html->h2('Game Verdict'); echo $html->p(MoolaGameController::GAMEREPORT_INPROGRESS); break; case MoolaGameController::GAMEREPORT_NOSTART: echo $html->h2('Game Verdict'); echo $html->p(MoolaGameController::GAMEREPORT_NOSTART); break; case MoolaGameController::WAIT_OPPONENT_JOIN: echo $html->p(MoolaGameController::WAIT_OPPONENT_JOIN); break; case MoolaGameController::WAIT_OPPONENT_TURN: echo $html->p(MoolaGameController::WAIT_OPPONENT_TURN); break; case MoolaGameController::GAME_TAKETURN: if ($guess = $_POST['guess']) { $currentgame->turnPlayer(array('guess'=>$guess)); if ($currentgame->hasBot()) {//Check if an autoplayer is one of the players. $currentgame->turnBot(); } if ($currentgame->complete() || $currentgame->completeForPlayer($currentgame->player)) { header('Location: ?gamereport=' . $currentgame->id); exit(); } } case MoolaGameController::GAME_INPROGRESS: echo $html->form(array('action'=>'', 'method'=>'post', 'style'=>'text-align: center;', 'id'=>'choiceform')); echo $html->p('Guess the number (0-9).'); echo $html->div( $html->input(array('type'=>'text', 'name'=>'guess')) . $html->input(array('type'=>'submit')) ); echo $html->form(null); include('timelimit.php'); // The countdown timer. countdown($currentgame); break; default: case MoolaGameController::NOGAME: // Game doesnt exist. This is optional and if controller->auto is true (which it is by default), it will automatically redirect you. break; } ?>