/** * 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; }