/** * check if user has given right * @param string $type * @return boolean */ public function hasRight($type) { $right = RCached::findOne('user_right', ' type = ?', array($type), date("d.m.Y - H")); if (!$right) { return false; } return R::areRelated($this->bean, $right); }
/** * Test areRelated(). * * @return void */ public function testAreRelatedAndVariations() { $sheep = R::dispense('sheep'); $sheep->name = 'Shawn'; $sheep2 = R::dispense('sheep'); $sheep2->name = 'Billy'; $sheep3 = R::dispense('sheep'); $sheep3->name = 'Moo'; R::store($sheep3); R::associate($sheep, $sheep2); asrt(R::areRelated($sheep, $sheep2), TRUE); R::exec('DELETE FROM sheep_sheep WHERE sheep2_id = ? -- keep-cache', array($sheep2->id)); asrt(R::areRelated($sheep, $sheep2), FALSE); // Use cache? R::associate($sheep, $sheep2); R::$writer->setUseCache(TRUE); asrt(R::areRelated($sheep, $sheep2), TRUE); R::exec('DELETE FROM sheep_sheep WHERE sheep2_id = ? -- keep-cache', array($sheep2->id)); asrt(R::areRelated($sheep, $sheep2), TRUE); // Use cache? R::associate($sheep, $sheep2); asrt(R::areRelated($sheep, $sheep3), FALSE); $pig = R::dispense('pig'); asrt(R::areRelated($sheep, $pig), FALSE); R::freeze(TRUE); asrt(R::areRelated($sheep, $pig), FALSE); R::freeze(FALSE); $foo = R::dispense('foo'); $bar = R::dispense('bar'); $foo->id = 1; $bar->id = 2; asrt(R::areRelated($foo, $bar), FALSE); }
protected function actionM($action, $name, $details, $type, $id) { if (!is_numeric($id) || $id < 0) { $this->output('maintext', 'Ungültige Order!'); return; } $order = R::findOne('order', ' id = ? AND type = ? AND r_name = ?', array($id, $action == 'sell' ? 'buy' : 'sell', $name)); if (!$order) { $this->output('maintext', 'Die angegebene Order konnte nicht gefunden werden!'); return; } if (R::areRelated($order, $this->myCompany)) { $this->output('maintext', 'Die angegebene Order konnte nicht gefunden werden!'); return; } $orderCompany = R::relatedOne($order, 'company'); if (isset($_POST['amount']) && is_numeric($_POST['amount']) && $_POST['amount'] > 0) { $amount = $_POST['amount']; if ($action == 'sell') { if ($amount > ($type == 'r' ? $this->myRess->{$name} : $this->myProducts->{$name})) { $this->output('maintext', 'Deine Firma lagert nicht genügend Ressourcen für diesen Verkauf!'); return; } if ($amount > $order->r_amount) { $this->output('maintext', 'Diese Firma Ordert {' . $type . '_' . $name . '} maximal ' . formatCash($order->r_amount) . ' mal!'); return; } // checks done $this->myCompany->balance += $amount * $order->price; if ($type == 'r') { $this->myRess->{$name} -= $amount; $order->r_amount -= $amount; $targetComp = R::findOne('company_ress', ' company_id = ?', array($orderCompany->id)); $targetComp->{$name} += $amount; R::begin(); R::store($this->myRess); if ($order->r_amount <= 0) { R::trash($order); } else { R::store($order); } R::store($targetComp); R::store($this->myCompany); R::commit(); } else { $this->myProducts->{$name} -= $amount; $order->r_amount -= $amount; $targetComp = R::findOne('company_products', ' company_id = ?', array($orderCompany->id)); $targetComp->{$name} += $amount; R::begin(); R::store($this->myProducts); if ($order->r_amount <= 0) { R::trash($order); } else { R::store($order); } R::store($targetComp); R::store($this->myCompany); R::commit(); } $this->output('maintext', 'Der Verkauf war erfolgreich!'); return; } else { $totalPrice = $amount * $order->price; if ($totalPrice > $this->myCompany->balance) { $this->output('maintext', 'Deine Firma hat nicht genügend Geld für diesen Kauf!'); return; } if ($amount > $order->r_amount) { $this->output('maintext', 'Es werden maximal ' . formatCash($order->r_amount) . ' Verkaufseinheiten verkaufen!'); return; } // buy $this->myCompany->balance -= $totalPrice; if ($type == 'r') { $this->myRess->{$name} += $amount; } else { $this->myProducts->{$name} += $amount; } $order->r_amount -= $amount; R::begin(); R::store($this->myCompany); R::store($this->myRess); R::store($this->myProducts); if ($order->r_amount <= 0) { R::trash($order); } else { R::store($order); } R::commit(); $this->output('maintext', 'Der Kauf war erfolgreich!'); return; } } $this->output('maintext', '<h3>Fremde ' . ($order->type == 'sell' ? 'Verkauf' : 'Kauf') . 'order</h3> <p>{' . $type . '_' . $name . '}</p> <p>Firma: <b>' . htmlspecialchars($orderCompany->name) . '</b><br /> Preis pro VE: <b>' . formatCash($order->price) . ' {money}</b> <br /> Maximal Verfügbare VE\'s: <b>' . formatCash($order->r_amount) . '</b> </p> <h4>' . ($action == 'buy' ? 'Kaufen' : 'Verkaufen') . '</h4>'); $this->output('form', array('target' => 'action/' . $action . '/' . $name . '/m/' . $order->id, 'elements' => array(array('desc' => 'Menge', 'name' => 'amount', 'type' => 'text')))); }
private function posthookHomepage_posts(&$bean) { if (!R::areRelated($bean, $this->user)) { R::associate($bean, $this->user); } }
/** * Fast track link block code should not affect self-referential N-M relations. * * @return void */ public function testFastTrackRelations() { testpack('Test fast-track linkBlock exceptions'); list($donald, $mickey, $goofy, $pluto) = R::dispense('friend', 4); $donald->name = 'D'; $mickey->name = 'M'; $goofy->name = 'G'; $pluto->name = 'P'; $donald->sharedFriend = array($mickey, $goofy); $mickey->sharedFriend = array($pluto, $goofy); $mickey->sharedBook = array(R::dispense('book')); R::storeAll(array($mickey, $donald, $goofy, $pluto)); $donald = R::load('friend', $donald->id); $mickey = R::load('friend', $mickey->id); $goofy = R::load('friend', $goofy->id); $pluto = R::load('friend', $pluto->id); $names = implode(',', R::gatherLabels($donald->sharedFriend)); asrt($names, 'G,M'); $names = implode(',', R::gatherLabels($goofy->sharedFriend)); asrt($names, 'D,M'); $names = implode(',', R::gatherLabels($mickey->sharedFriend)); asrt($names, 'D,G,P'); $names = implode(',', R::gatherLabels($pluto->sharedFriend)); asrt($names, 'M'); // Now in combination with with() conditions... $donald = R::load('friend', $donald->id); $names = implode(',', R::gatherLabels($donald->withCondition(' name = ? ', array('M'))->sharedFriend)); asrt($names, 'M'); // Now in combination with with() conditions... $donald = R::load('friend', $donald->id); $names = implode(',', R::gatherLabels($donald->with(' ORDER BY name ')->sharedFriend)); asrt($names, 'G,M'); // Now counting $goofy = R::load('friend', $goofy->id); asrt((int) $goofy->countShared('friend'), 2); asrt((int) $donald->countShared('friend'), 2); asrt((int) $mickey->countShared('friend'), 3); asrt((int) $pluto->countShared('friend'), 1); R::unassociate($donald, $mickey); asrt((int) $donald->countShared('friend'), 1); asrt(R::areRelated($donald, $mickey), FALSE); asrt(R::areRelated($mickey, $donald), FALSE); asrt(R::areRelated($mickey, $goofy), TRUE); asrt(R::areRelated($goofy, $mickey), TRUE); R::getWriter()->setUseCache(TRUE); $mickeysFriends = R::$associationManager->related($mickey, 'friend', TRUE); asrt(count($mickeysFriends), 2); $mickeysFriends = R::$associationManager->related($mickey, 'friend', TRUE); asrt(count($mickeysFriends), 2); $plutosFriends = R::$associationManager->related($pluto, 'friend', TRUE); asrt(count($plutosFriends), 1); $mickeysBooks = R::$associationManager->related($mickey, 'book', TRUE); asrt(count($mickeysBooks), 1); R::getWriter()->setUseCache(FALSE); }
public function show_Play() { // return chat state if (isset($_POST['chatID']) && is_numeric($_POST['chatID'])) { $messages = R::getAll('SELECT id, time, message, MAX(id) as mid FROM poker_message WHERE time > ? AND id > ?', array(time() - 3600, $_POST['chatID'])); if ($messages[0]['id'] == null) { $messages = array(); $this->output('lastID', $_POST['chatID']); } else { $this->output('lastID', $messages[0]['mid']); foreach ($messages as $k => $v) { unset($messages[$k]['mid']); $messages[$k]['time'] = date('H:i:s', $v['time']); } } $this->output('msg', $messages); } // if no round is in running mode, set pending to running $count = R::getCell('SELECT count(id) FROM poker_round WHERE state = "running"'); if ($count == 0) { // only start a round if enough players are there $pending = R::getCell("SELECT\n\t\t\tcount(pp.id)\n\t\tFROM\n\t\t\tpoker_player AS pp, poker_round AS pr,\n\t\t\tpoker_player_poker_round As conn\n\t\tWHERE\n\t\t\t(pr.state = 'pending') AND\n\t\t\tconn.poker_player_id = pp.id AND\n\t\t\tconn.poker_round_id = pr.id"); if ($pending >= 2) { R::exec("UPDATE poker_round SET state = 'running' WHERE state = 'pending'"); } else { $this->output('waiting_for_players', true); return; } } // now get running round $this->_pokerRound = R::findOne('poker_round', " state = 'running'"); // if no player is playing, reset round if ($this->_pokerRound->player_count() == 0) { R::trash($this->_pokerRound); return; } if ($this->_pokerRound->player_count() == 1) { $this->chatShowDown($this->_pokerRound->showdown()); return; } // both $r = R::getRow('SELECT MAX(pp.bid) AS max_bid, MIN(pp.bid) AS min_bid, SUM(pp.bid) + pr.global_pot AS pot, MIN(pp.game_state) AS gstate FROM poker_player AS pp, poker_round AS pr, poker_player_poker_round AS conn WHERE pr.id = ? AND conn.poker_player_id = pp.id AND conn.poker_round_id = pr.id', array($this->_pokerRound->getID())); $this->_maxBid = $r['max_bid']; $this->_minBid = $r['min_bid']; $this->_pot = $r['pot']; $this->_minGameState = $r['gstate']; // game is handled locked if (!file_exists(PATH . "/cache/pokerlock.txt") || true) { $fp = fopen(PATH . "/cache/pokerlock.txt", "w"); fwrite($fp, 0); fclose($fp); $this->handle(); if (file_exists(PATH . "/cache/pokerlock.txt")) { unlink(PATH . "/cache/pokerlock.txt"); } $this->output('locked', false); } else { $this->output('locked', true); } // time to live $ttl = $this->_pokerRound->ttl - time(); $this->output('ttl', $ttl); if ($ttl < 0) { $usr = R::findOne('user', ' id = ?', array($this->_pokerRound->current_player_id)); // kick out specific player $this->_pokerRound->kick(R::load('poker_player', $this->_pokerRound->current_player_id)); $this->_pokerRound->next_player(); if ($usr != null) { $this->chatMessage(htmlspecialchars($usr->username) . ' verlässt das Spiel.'); } } // output game state $nC = array(); $cC = json_decode($this->_pokerRound->cards, true); foreach ($cC as $k => $v) { if ($v['turned']) { $nC[] = $v; } else { $nC[] = array('card' => '?', 'color' => '?', 'turned' => 'true'); } } $this->output('maxbid', $this->_maxBid); $this->output('pot', $this->_pot); // my turn? if ($this->_pokerRound->current_player_id == $this->_pokerPlayer->getID()) { $this->output('myturn', true); $opts = array(); if ($this->_pokerPlayer->bid < $this->_maxBid) { $opts = array('fold'); $diff = $this->_maxBid - $this->_pokerPlayer->bid; if ($diff <= $this->user->cash) { $opts[] = 'call'; $opts[] = 'raise'; } } else { $opts = array('fold', 'check', 'raise'); } $this->output('options', $opts); // handle player actions if ($this->get(1) != "" && in_array($this->get(1), $opts)) { switch ($this->get(1)) { case "fold": $this->_pokerRound->kick($this->_pokerPlayer); $this->_pokerRound->next_player(); $this->chatMessage('{me}: FOLD'); break; case "call": $diff = $this->_maxBid - $this->_pokerPlayer->bid; $this->user->cash -= $diff; $this->_pokerPlayer->bid += $diff; R::store($this->user); R::store($this->_pokerPlayer); $this->_pokerRound->next_player(); $this->chatMessage('{me}: CALL'); break; case "raise": $diff = $this->_maxBid - $this->_pokerPlayer->bid; if ($diff < 0) { $diff = 0; } if (is_numeric($this->get(2))) { if ($this->user->cash - $diff - $this->get(2) < 0) { return; } $this->user->cash -= $diff + $this->get(2); $this->_pokerPlayer->bid += $diff + $this->get(2); $this->_pokerRound->next_player(); $this->chatMessage('{me}: RAISE ' . formatCash($this->get(2)) . ' {money}'); } else { return; // invalid input } break; case "check": $this->_pokerRound->next_player(); $this->chatMessage('{me}: CHECK'); break; } $this->_pokerPlayer->game_state = $this->_pokerRound->step; R::store($this->_pokerPlayer); return; } } else { $this->output('myturn', false); } // own cards? if (R::areRelated($this->_pokerPlayer, $this->_pokerRound)) { $this->output('my_bid', $this->_pokerPlayer->bid); $this->output('my_cards', json_decode($this->_pokerPlayer->cards, true)); } $this->output('center_cards', $nC); // now output opp cards/status $opps = array(); $q = R::getAll("SELECT\n\t\t\tpp.bid AS bid,\n\t\t\tpp.cards AS cards,\n\t\t\tpp.all_in AS all_in,\n\t\t\tpp.all_in_amount AS all_in_amount,\n\t\t\tu.username AS username\n\t\tFROM\n\t\t\tpoker_player AS pp, poker_round AS pr,\n\t\t\tpoker_player_poker_round As conn,\n\t\t\tpoker_player_user AS uconn,\n\t\t\tuser AS u\n\t\tWHERE\n\t\t\tpr.id = ? AND\n\t\t\tconn.poker_round_id = pr.id AND\n\t\t\tconn.poker_player_id = pp.id AND\n\t\t\tuconn.poker_player_id = pp.id AND\n\t\t\tuconn.user_id = u.id AND\n\t\t\tu.id != ?", array($this->_pokerRound->getID(), $this->user->getID())); foreach ($q as $o) { $o['cards'] = json_decode($o['cards'], true); foreach ($o['cards'] as $k => $card) { $o['cards'][$k]['card'] = "?"; $o['cards'][$k]['color'] = "?"; } $opps[] = $o; } $this->output('players', $opps); }