Exemplo n.º 1
0
 /**
  * read the object information from the db
  */
 private function load($id)
 {
     $db = new DB();
     $sql = "SELECT pa.idgame, pa.action, pa.params, pa.idtable, pa.idplayer, UNIX_TIMESTAMP(pa.timestamp) AS timestamp\n                  FROM poker_actions AS pa \n                 WHERE pa.idaction = '{$id}'";
     $result = $db->query($sql);
     if ($result->length() > 0) {
         // general information
         $this->info = array("action" => $result->action, 'params' => unserialize($result->params), 'player' => $result->idplayer != '' && $result->idplayer != 0 ? PokerPlayer::getInstance($result->idplayer) : false, 'game' => Poker::getInstance($result->idgame), 'table' => PokerTable::getInstance($result->idtable), 'time' => $result->timestamp);
         $this->id = $id;
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * read the object information from the db
  */
 private function load($id)
 {
     $db = new DB();
     $sql = "SELECT pt.idgame, pt.title, pp.idplayer, pt.d, pt.sb, pt.bb, pt.seats, pt.blind, pt.iduser, pt.idspot\n                  FROM poker_tables AS pt\n             LEFT JOIN poker_players AS pp ON (pp.idtable = pt.idtable AND pp.pactive = 1)\n                 WHERE pt.idtable = '{$id}'\n              ORDER BY pp.position";
     $result = $db->query($sql);
     if ($result->length() > 0) {
         $players = array();
         if ($result->idplayer != '') {
             $prev = false;
             do {
                 $player = PokerPlayer::getInstance($result->idplayer);
                 $player->prev = $prev;
                 if (is_object($prev)) {
                     $prev->next = $player;
                 }
                 $players[$player->position] = $player;
                 $prev = $player;
             } while ($result->next());
             $first = reset($players);
             $first->prev = $prev;
             $prev->next = $first;
         }
         // general information
         $this->info = array("game" => $result->idgame != 0 && $result->idgame != '' ? Poker::getInstance($result->idgame) : FALSE, "spot" => $result->idspot != 0 && $result->idspot != '' ? PokerSpot::getInstance($result->idspot) : FALSE, 'title' => $result->title, 'players' => $players, 'positions' => array('smallblind' => $result->sb, 'bigblind' => $result->bb, 'dealer' => $result->d), 'blinds' => array('big' => 2 * $result->blind, 'small' => $result->blind), 'seats' => $result->seats, 'user' => $result->iduser);
         $this->info['free'] = $this->info['seats'] - count($this->info['players']);
         $this->id = $id;
         return true;
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * Handle form action
  */
 protected function formAction()
 {
     $this->escapeFormVars();
     switch ($this->s->action) {
         case 'load':
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             $this->form['autocomplete'] = $this->getGameInfo($this->s->params[0], $player);
             return true;
             break;
         case 'bet':
         case 'raise':
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             // check, if valid action
             if ($this->validAction($player, $this->s->action, array('value' => $this->vars['value']))) {
                 // save new action
                 $actions = $player->table->game->getTurnActions();
                 $rel_value = $this->s->action == 'bet' ? $this->vars['value'] : $this->vars['value'] - $actions['bet']->params['value'];
                 $log_value = $this->s->action == 'bet' ? $rel_value : $this->vars['value'] - $player->bet;
                 if ($this->saveAction($player, $this->s->action, array('value' => $this->vars['value'], 'rel_value' => $rel_value, 'own_value' => $log_value))) {
                     return true;
                 }
             }
             break;
         case 'call':
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             // check, if valid action
             if ($this->validAction($player, $this->s->action, array('value' => $this->vars['value']))) {
                 // save new action
                 if ($this->saveAction($player, $this->s->action, array('value' => $this->vars['value']))) {
                     return true;
                 }
             }
             break;
         case 'fold':
         case 'check':
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             // check, if valid action
             if ($this->validAction($player, $this->s->action)) {
                 // save new action
                 if ($this->saveAction($player, $this->s->action)) {
                     return true;
                 }
             }
             break;
         case 'join':
             // player joins table
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             if (isset($this->s->params[0]) && $player == false) {
                 // table id
                 // create new player & mark the player to join the table for the next game
                 $player = new PokerPlayer($this->s->params[0], $this->vars['seat'], $this->vars['stack']);
                 if ($this->validAction($player, 'join', array('seat' => $this->vars['seat'], 'stack' => $this->vars['stack'])) && $player->save()) {
                     $this->saveAction($player, 'join', array('seat' => $this->vars['seat'], 'stack' => $this->vars['stack']));
                     $table = PokerTable::getInstance($this->s->params[0], TRUE);
                     // start new game if sufficient player count & no game running
                     if (count($table->players) >= 2 && $table->game == FALSE) {
                         $this->gameNew($table);
                     }
                     return true;
                 }
             }
             break;
         case 'leave':
             // player leaves table
             $player = PokerPlayer::getActivePlayer($this->s->params[0]);
             if (isset($this->s->params[0])) {
                 // table id
                 // mark the player to leave the table after the current game
                 $player->leave = true;
                 if ($player->join == true && $player->delete() || $player->save()) {
                     $this->saveAction($player, 'leave');
                     return true;
                 }
             }
             break;
         case 'poll':
             // Close the session prematurely to avoid usleep() from locking other requests
             session_write_close();
             // Automatically die after timeout (plus buffer)
             set_time_limit(MESSAGE_TIMEOUT_SECONDS + MESSAGE_TIMEOUT_SECONDS_BUFFER);
             // Counter to manually keep track of time elapsed (PHP's set_time_limit() is unrealiable while sleeping)
             $counter = MESSAGE_TIMEOUT_SECONDS;
             $new_actions = false;
             // Poll for messages and hang if nothing is found, until the timeout is exhausted
             while ($counter > 0) {
                 // Check for new data
                 if ($new_actions = PokerTable::getNewActions($this->vars['timestamp'], $this->s->params[0])) {
                     break;
                 } else {
                     // Otherwise, sleep for the specified time, after which the loop runs again
                     usleep(MESSAGE_POLL_MICROSECONDS);
                     // Decrement seconds from counter (the interval was set in μs, see above)
                     $counter -= MESSAGE_POLL_MICROSECONDS / 1000000;
                 }
             }
             // If we've made it this far, we've either timed out or have some data to deliver to the client
             if (is_array($new_actions)) {
                 $player = PokerPlayer::getActivePlayer($this->s->params[0]);
                 $this->form['autocomplete'] = $this->getGameInfo($this->s->params[0], $player, $new_actions);
             }
             return true;
             break;
         case 'chat':
             // Close the session prematurely to avoid usleep() from locking other requests
             session_write_close();
             // Automatically die after timeout (plus buffer)
             set_time_limit(MESSAGE_TIMEOUT_SECONDS + MESSAGE_TIMEOUT_SECONDS_BUFFER);
             // Counter to manually keep track of time elapsed (PHP's set_time_limit() is unrealiable while sleeping)
             $counter = MESSAGE_TIMEOUT_SECONDS;
             $new_actions = false;
             // Poll for messages and hang if nothing is found, until the timeout is exhausted
             while ($counter > 0) {
                 // Check for new data
                 if ($new_messages = PokerTable::getNewMessages($this->vars['timestamp'], $this->s->params[0])) {
                     break;
                 } else {
                     // Otherwise, sleep for the specified time, after which the loop runs again
                     usleep(MESSAGE_POLL_MICROSECONDS * 4);
                     // Decrement seconds from counter (the interval was set in μs, see above)
                     $counter -= MESSAGE_POLL_MICROSECONDS * 4 / 1000000;
                 }
             }
             // If we've made it this far, we've either timed out or have some data to deliver to the client
             if (is_array($new_messages)) {
                 $messages = array();
                 foreach ($new_messages as $key => $message) {
                     $messages[] = '[' . $message->sender->realname . ', ' . $message->created['time'] . '] ' . $message->text . "\r\n";
                 }
                 $this->form['autocomplete'] = array('timestamp' => time(), 'messages' => $messages);
             }
             return true;
             break;
     }
     return false;
 }