public function init() { parent::init(); $id = $_POST["id"]; // check if npc is valid if (!is_numeric($id) || $id <= 0) { $this->error('Invalid ' . $this->_type . '-ID'); } $this->npc = R::load($this->_type, $id); if ($this->npc->getID() != $id) { $this->error($this->_type . ' doesnt exist!'); } // check if npc has right type if ($this->npc->type != $this->myType) { $this->error("This " . $this->_type . " is not of " . $this->myType); } // check if user is nearby $userPos = R::findOne('map_position', ' user_id = ?', array($this->user->getID())); $dist = sqrt(pow($this->npc->x - $userPos->x, 2) + pow($this->npc->y - $userPos->y, 2)); if ($dist > 3 || $this->npc->map != $userPos->map) { $this->error('Du bist zu weit entfehrt!'); } // check if quest availible $this->myNPCQuest = R::relatedOne($this->user, 'quests_npc', ' complete_time = 0 AND (startnpc_id = ? OR stopnpc_id = ?)', array($this->npc->getID(), $this->npc->getID())); if ($this->myNPCQuest != null) { if ($this->myNPCQuest->startnpc_id == $this->npc->getID()) { if ($this->myNPCQuest->accepted == 0) { $this->myNPCQuestRole = 'startnpc'; $startnpc = $this->npc->name; $stopnpc = R::getCell('SELECT `name` FROM map_npc WHERE id = ?', array($this->myNPCQuest->stopnpc_id)); } } else { if ($this->myNPCQuest->accepted == 1) { $this->myNPCQuestRole = 'stopnpc'; $stopnpc = $this->npc->name; $startnpc = R::getCell('SELECT `name` FROM map_npc WHERE id = ?', array($this->myNPCQuest->startnpc_id)); } } if ($this->myNPCQuestRole != 'none') { $all = Config::getConfig('npc_quests'); $this->myNPCQuestData = $all[$this->myNPCQuest->quest_id][$this->myNPCQuestRole]; $this->myNPCQuestData["text"] = str_replace(array('{startnpc}', '{stopnpc}'), array($startnpc, $stopnpc), $this->myNPCQuestData["text"]); foreach ($this->myNPCQuestData["items"] as $k => $v) { $this->myNPCQuestData["items"][$k]["param"] = str_replace(array('{startnpc}', '{stopnpc}'), array($startnpc, $stopnpc), $v["param"]); } if ($this->_controllerFunc == 'Interact') { $this->output('quest', '<br /> <br /> <b>Quest:</b> ' . htmlspecialchars($this->myNPCQuestData["text"]) . ' <br /> <a href="#questing">' . ($this->myNPCQuestRole == 'startnpc' ? 'annehmen' : 'abschließen') . '</a>'); } } } }
public function showdown() { $allPlayers = R::related($this->bean, 'poker_player'); $pokerHands = array(10 => new PokerParser("[a>a>a>a>a"), 9 => new PokerParser("a>a>a>a>a"), 8 => new PokerParser("1{4}"), 7 => new PokerParser("1{3}2{2}"), 6 => new PokerParser("a{5}"), 5 => new PokerParser("?>?>?>?>?"), 4 => new PokerParser("1{3}"), 3 => new PokerParser("1{2}2{2}"), 2 => new PokerParser("1{2}"), 1 => new PokerParser("?")); $bestValue = -1; $winners = array(); $totalPot = $this->bean->global_pot; // new round to move players to $nextRound = R::findOne('poker_round', " state='pending'"); if ($nextRound == null) { $nextRound = R::dispense('poker_round'); $nextRound->state = 'pending'; $nextRound->step = 0; $nextRound->global_pot = 0; R::store($nextRound); } foreach ($allPlayers as $p) { $cards = array_merge(json_decode($p->cards, true), json_decode($this->bean->cards, true)); $totalPot += $p->bid; $val = 0; foreach ($pokerHands as $value => $parser) { if ($parser->check($cards)) { // player has this $val = $value * 100 + $parser->getHighestCardValue(); break; } } if ($val > $bestValue) { $bestValue = $val; $winners = array($p); } elseif ($val == $bestValue) { $winners[] = $p; } // kick from current round R::unassociate($this->bean, $p); // put into next round R::associate($nextRound, $p); } $winnerCount = count($winners); $winAmount = floor($totalPot / $winnerCount); $winnerNames = array(); foreach ($winners as $win) { $usr = R::relatedOne($win, 'user'); $usr->cash += $winAmount; $winnerNames[] = $usr->username; R::store($usr); } R::trash($this->bean); return array("winners" => $winnerNames, "amount" => $winAmount, "bestValue" => $bestValue); }
public function show_Cancel() { if (!$this->user->hasPremium()) { $this->error('Nur für Premiumaccounts.'); } if (!is_numeric($this->get(1)) || $this->get(1) < 1) { $this->error('Ungültige ID'); } $crule = R::relatedOne($this->myCompany, 'crule', 'id = ?', array($this->get(1))); if (!$crule) { $this->error('Die ID wurde nicht gefunden.'); } R::trash($crule); $this->output('load', 'show'); }
public function show_Main() { $poker_player = R::relatedOne($this->user, 'poker_player'); if ($poker_player == null) { $poker_player = R::dispense('poker_player'); $poker_player->status = 'view'; $poker_player->cards = ''; $poker_player->bid = 0; $poker_player->all_in = false; $poker_player->all_in_amount = 0; R::store($poker_player); R::associate($poker_player, $this->user); } if ($poker_player->status == 'view') { Framework::TPL()->assign('can_join', true); } }
public function cancel() { $cs = Config::getConfig("company_quests"); $details = $cs[$this->name]; //$company = R::relatedOne($this->bean, 'company'); $user = R::relatedOne($this->bean, 'user'); if ($user == null) { R::trash($this->bean); return; } $company = R::findOne('company', 'user_id = ?', array($user->id)); $company->balance -= floor($details["oncomplete"]["cash"] * 0.1); R::begin(); R::trash($this->bean); R::store($company); R::commit(); }
public function show_News() { $id = $this->get(1); if (!is_numeric($id)) { $this->error("Invalid ID"); } $post = R::load('homepage_posts', $id); if (!$post->id) { $this->error("Post not found"); } $author = R::relatedOne($post, 'user'); $this->output('date', date("d.m.Y", $post->time)); $this->output('title', $post->title); $this->output('text', nl2br(htmlspecialchars($post->content))); $this->output('link', $post->link); $this->output('author', $author->username); }
public function show_News() { header("Content-Type: application/rss+xml; charset=UTF-8"); $tpl = new Smarty(); $tpl->cache_lifetime = 3600 * 24; $tpl->setCaching(Smarty::CACHING_LIFETIME_CURRENT); $cacheID = 'rssFeed'; if (!$tpl->isCached('rss.xml', $cacheID)) { $items = array(); $dbItems = R::find('homepage_posts', ' 1=1 ORDER BY id DESC LIMIT 15'); foreach ($dbItems as $item) { $author = R::relatedOne($item, 'user'); $items[] = array('title' => $item->title, 'link' => $item->link == '' ? APP_WEBSITE . APP_DIR : $item->link, 'description' => $item->content, 'author' => $author->username, 'date' => date("D, d M Y H:i:s O", $item->time)); } $tpl->assign('items', $items); $tpl->assign('gentime', date("D, d M Y H:i:s O")); $tpl->assign('link', APP_WEBSITE . APP_DIR); } $tpl->display('rss.xml', $cacheID); }
public function cancel_Order() { // when deleting an order, cash and ress must be returned to company $company = R::relatedOne($this->bean, 'company'); if ($this->type == 'buy') { // buy order needs cash updated $company->balance += $this->r_amount * $this->price; R::store($company); R::trash($this->bean); } else { // sell order needs ress updated if ($this->r_type == 'resource') { $holder = R::findOne('company_ress', ' company_id = ?', array($company->id)); } else { $holder = R::findOne('company_products', ' company_id = ?', array($company->id)); } $holder->{$this->r_name} += $this->r_amount; R::store($holder); R::trash($this->bean); } }
public function show_Main() { if ($this->get(1) == "login") { Framework::TPL()->assign('first_login', true); } else { Framework::TPL()->assign('first_login', false); } $session_expired = false; if ($this->get(1) == "session_expired") { $session_expired = true; } Framework::TPL()->assign('session_expired', $session_expired); $count = R::getCell('select count(id) from user'); Framework::TPL()->assign("playercount", $count); // assign news $news = array(); $dbNews = R::find('homepage_posts', ' 1=1 ORDER BY id DESC LIMIT 3'); foreach ($dbNews as $n) { $author = R::relatedOne($n, 'user'); $news[] = array('id' => $n->id, 'title' => $n->title, 'author' => $author->username, 'time' => $n->time); } Framework::TPL()->assign("news", $news); }
$s = R::dispense('person'); $s2 = R::dispense('person'); $t->name = 'a'; $t->role = 'teacher'; $s->role = 'student'; $s2->role = 'student'; $s->name = 'a'; $s2->name = 'b'; R::associate($t, $s); R::associate($t, $s2); $students = R::related($t, 'person', ' "role" = ? ORDER BY "name" ', array("student")); $s = array_shift($students); $s2 = array_shift($students); asrt($s->name, 'a'); asrt($s2->name, 'b'); $s = R::relatedOne($t, 'person', ' role = ? ORDER BY "name" ', array("student")); asrt($s->name, 'a'); //empty classroom R::clearRelations($t, 'person', $s2); $students = R::related($t, 'person', ' role = ? ORDER BY "name" ', array("student")); asrt(count($students), 1); $s = reset($students); asrt($s->name, 'b'); function getList($beans, $property) { $items = array(); foreach ($beans as $bean) { $items[] = $bean->{$property}; } sort($items); return implode(",", $items);
/** * handle company rules */ protected function cronHandleCrule() { // delete old rules R::exec('DELETE FROM `crule` WHERE `until` < ?', array(time())); // get new rules $crules = R::find('crule'); // counter $counter = array('buy' => 0, 'sell' => 0); foreach ($crules as $crule) { $company = R::relatedOne($crule, 'company'); $amount = R::getCell('SELECT `' . $crule->r_name . '` FROM `company_' . ($crule->r_type == 'product' ? 'products' : 'ress') . '` WHERE company_id = ?', array($company->id)); // fix amount with existant orders $existant = R::related($company, 'order', 'r_name = ? AND type = ?', array($crule->r_name, $crule->action)); foreach ($existant as $e) { if ($e->type == 'buy') { $amount += $e->r_amount; } else { $amount -= $e->r_amount; } } $order = R::dispense('order'); $order->type = $crule->action; $order->r_type = $crule->r_type; $order->r_name = $crule->r_name; $order->price = $crule->r_price; $order->date = time(); $order->automatic = false; $order->a_expires = 0; $sold = 0; if ($crule->action == 'buy' && $amount < $crule->r_limit) { // create buy order $maxBuy = $crule->r_limit - $amount; $costs = $maxBuy * $crule->r_price; if ($costs > $company->balance) { $maxBuy = floor($company->balance / $crule->r_price); } if ($maxBuy == 0) { continue; } $company->balance -= $maxBuy * $crule->r_price; $order->r_amount = $maxBuy; $counter['buy']++; } else { if ($crule->action == 'sell' && $amount > $crule->r_limit) { // create sell order $order->r_amount = $amount - $crule->r_limit; $sold += $amount - $crule->r_limit; $counter['sell']++; } else { continue; } } R::begin(); if ($sold != 0) { R::exec('UPDATE `company_' . ($crule->r_type == 'product' ? 'products' : 'ress') . '` SET `' . $crule->r_name . '` = `' . $crule->r_name . '`-? WHERE company_id = ?', array($sold, $company->id)); } R::store($order); R::store($company); R::associate($order, $company); R::commit(); } $this->log('handleCrule', $counter['buy'] . ' new buy-orders, ' . $counter['sell'] . ' new sell-orders'); }
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')))); }
$s = R::dispense('person'); $s2 = R::dispense('person'); $t->name = 'a'; $t->role = 'teacher'; $s->role = 'student'; $s2->role = 'student'; $s->name = 'a'; $s2->name = 'b'; R::associate($t, $s); R::associate($t, $s2); $students = R::related($t, 'person', ' role = "student" ORDER BY `name` '); $s = array_shift($students); $s2 = array_shift($students); asrt($s->name, 'a'); asrt($s2->name, 'b'); $s = R::relatedOne($t, 'person', ' role = "student" ORDER BY `name` '); asrt($s->name, 'a'); //empty classroom R::clearRelations($t, 'person', $s2); $students = R::related($t, 'person', ' role = "student" ORDER BY `name` '); asrt(count($students), 1); $s = reset($students); asrt($s->name, 'b'); $students = R::related($t, 'person', ' role = ? ORDER BY `name` ', array("student")); asrt(count($students), 1); $s = reset($students); asrt($s->name, 'b'); function getList($beans, $property) { $items = array(); foreach ($beans as $bean) {
/** * Test basic and complex common usage scenarios for * relations and associations. * * @return void */ public function testScenarios() { list($q1, $q2) = R::dispense('quote', 2); list($pic1, $pic2) = R::dispense('picture', 2); list($book, $book2, $book3) = R::dispense('book', 4); list($topic1, $topic2, $topic3, $topic4, $topic5) = R::dispense('topic', 5); list($page1, $page2, $page3, $page4, $page5, $page6, $page7) = R::dispense('page', 7); $q1->text = 'lorem'; $q2->text = 'ipsum'; $book->title = 'abc'; $book2->title = 'def'; $book3->title = 'ghi'; $page1->title = 'pagina1'; $page2->title = 'pagina2'; $page3->title = 'pagina3'; $page4->title = 'pagina4'; $page5->title = 'pagina5'; $page6->title = 'cover1'; $page7->title = 'cover2'; $topic1->name = 'holiday'; $topic2->name = 'cooking'; $topic3->name = 'gardening'; $topic4->name = 'computing'; $topic5->name = 'christmas'; // Add one page to the book $book->ownPage[] = $page1; $id = R::store($book); asrt(count($book->ownPage), 1); asrt(reset($book->ownPage)->getMeta('type'), 'page'); $book = R::load('book', $id); asrt(count($book->ownPage), 1); asrt(reset($book->ownPage)->getMeta('type'), 'page'); // Performing an own addition $book->ownPage[] = $page2; $id = R::store($book); $book = R::load('book', $id); asrt(count($book->ownPage), 2); // Performing a deletion $book = R::load('book', $id); unset($book->ownPage[1]); $id = R::store($book); $book = R::load('book', $id); asrt(count($book->ownPage), 1); asrt(reset($book->ownPage)->getMeta('type'), 'page'); asrt(R::count('page'), 2); //still exists asrt(reset($book->ownPage)->id, '2'); // Doing a change in one of the owned items $book->ownPage[2]->title = 'page II'; $id = R::store($book); $book = R::load('book', $id); asrt(reset($book->ownPage)->title, 'page II'); // Change by reference now... don't copy! $refToPage2 = $book->ownPage[2]; $refToPage2->title = 'page II b'; $id = R::store($book); $book = R::load('book', $id); asrt(reset($book->ownPage)->title, 'page II b'); // Doing all actions combined $book->ownPage[] = $page3; R::store($book); $book = R::load('book', $id); unset($book->ownPage[2]); // And test custom key $book->ownPage['customkey'] = $page4; $book->ownPage[3]->title = "THIRD"; R::store($book); $book = R::load('book', $id); asrt(count($book->ownPage), 2); $p4 = $book->ownPage[4]; $p3 = $book->ownPage[3]; asrt($p4->title, 'pagina4'); asrt($p3->title, 'THIRD'); // Test replacing an element $book = R::load('book', $id); $book->ownPage[4] = $page5; R::store($book); $book = R::load('book', $id); asrt(count($book->ownPage), 2); $p5 = $book->ownPage[5]; asrt($p5->title, 'pagina5'); // Other way around - single bean asrt($p5->book->title, 'abc'); asrt(R::load('page', 5)->book->title, 'abc'); asrt(R::load('page', 3)->book->title, 'abc'); // Add the other way around - single bean $page1->id = 0; $page1->book = $book2; $page1 = R::load('page', R::store($page1)); asrt($page1->book->title, 'def'); $b2 = R::load('book', $id); asrt(count($b2->ownPage), 2); // Remove the other way around - single bean unset($page1->book); R::store($page1); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 0); // Re-add the page $b2->ownPage[] = $page1; R::store($b2); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 1); // Different, less elegant way to remove $page1 = reset($b2->ownPage); $page1->book_id = NULL; R::store($page1); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 0); // Re-add the page $b2->ownPage[] = $page1; R::store($b2); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 1); // Another less elegant way to remove $page1->book = NULL; R::store($page1); $cols = R::getColumns('page'); asrt(isset($cols['book']), FALSE); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 0); // Re-add the page $b2->ownPage[] = $page1; R::store($b2); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 1); // Another less elegant... just plain ugly... way to remove $page1->book = FALSE; R::store($page1); $cols = R::getColumns('page'); asrt(isset($cols['book']), FALSE); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 0); // Re-add the page $b2->ownPage[] = $page1; R::store($b2); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 1); // You are not allowed to re-use the field for something else foreach (array(1, -2.1, array(), TRUE, 'NULL', new stdClass(), 'just a string', array('a' => 1), 0) as $value) { try { $page1->book = $value; fail(); } catch (RedBean_Exception_Security $e) { pass(); } } // Test fk, not allowed to set to 0 $page1 = reset($b2->ownPage); $page1->book_id = 0; // Even uglier way, but still needs to work $page1 = reset($b2->ownPage); $page1->book_id = NULL; R::store($b2); $b2 = R::load('book', $book2->id); asrt(count($b2->ownPage), 0); // Test shared items $book = R::load('book', $id); $book->sharedTopic[] = $topic1; $id = R::store($book); // Add an item asrt(count($book->sharedTopic), 1); asrt(reset($book->sharedTopic)->name, 'holiday'); $book = R::load('book', $id); asrt(count($book->sharedTopic), 1); asrt(reset($book->sharedTopic)->name, 'holiday'); // Add another item $book->sharedTopic[] = $topic2; $id = R::store($book); $tidx = R::store(R::dispense('topic')); $book = R::load('book', $id); asrt(count($book->sharedTopic), 2); $t1 = $book->sharedTopic[1]; $t2 = $book->sharedTopic[2]; asrt($t1->name, 'holiday'); asrt($t2->name, 'cooking'); // Remove an item unset($book->sharedTopic[2]); asrt(count($book->sharedTopic), 1); $id = R::store($book); $book = R::load('book', $id); asrt(count($book->sharedTopic), 1); asrt(reset($book->sharedTopic)->name, 'holiday'); // Add and change $book->sharedTopic[] = $topic3; $book->sharedTopic[1]->name = 'tropics'; $id = R::store($book); $book = R::load('book', $id); asrt(count($book->sharedTopic), 2); asrt($book->sharedTopic[1]->name, 'tropics'); testids($book->sharedTopic); R::trash(R::load('topic', $tidx)); $id = R::store($book); $book = R::load('book', $id); // Delete without save unset($book->sharedTopic[1]); $book = R::load('book', $id); asrt(count($book->sharedTopic), 2); $book = R::load('book', $id); // Delete without init asrt(R::count('topic'), 3); unset($book->sharedTopic[1]); $id = R::store($book); asrt(R::count('topic'), 3); asrt(count($book->sharedTopic), 1); asrt(count($book2->sharedTopic), 0); // Add same topic to other book $book2->sharedTopic[] = $topic3; asrt(count($book2->sharedTopic), 1); $id2 = R::store($book2); asrt(count($book2->sharedTopic), 1); $book2 = R::load('book', $id2); asrt(count($book2->sharedTopic), 1); // Get books for topic asrt(count(R::related($topic3, 'book')), 2); asrt(R::relatedOne($topic3, 'book') instanceof RedBean_OODBBean, TRUE); $items = R::related($topic3, 'book'); $a = reset($items); asrt(R::relatedOne($topic3, 'book')->id, $a->id); $t3 = R::load('topic', $topic3->id); asrt(count($t3->sharedBook), 2); asrt(R::relatedOne($topic3, 'nothingness'), NULL); // Testing relatedLast $z = end($items); asrt(R::relatedLast($topic3, 'book')->id, $z->id); asrt(R::relatedLast($topic3, 'manuscript'), NULL); // Nuke an own-array, replace entire array at once without getting first $page2->id = 0; $page2->title = 'yet another page 2'; $page4->id = 0; $page4->title = 'yet another page 4'; $book = R::load('book', $id); $book->ownPage = array($page2, $page4); R::store($book); $book = R::load('book', $id); asrt(count($book->ownPage), 2); asrt(reset($book->ownPage)->title, 'yet another page 2'); asrt(end($book->ownPage)->title, 'yet another page 4'); testids($book->ownPage); // Test with alias format $book3->cover = $page6; $idb3 = R::store($book3); $book3 = R::load('book', $idb3); $justACover = $book3->fetchAs('page')->cover; asrt($book3->cover instanceof RedBean_OODBBean, TRUE); asrt($justACover->title, 'cover1'); // No page property asrt(isset($book3->page), FALSE); // Test doubling and other side effects ... should not occur.. $book3->sharedTopic = array($topic1, $topic2); $book3 = R::load('book', R::store($book3)); $book3->sharedTopic = array(); $book3 = R::load('book', R::store($book3)); asrt(count($book3->sharedTopic), 0); $book3->sharedTopic[] = $topic1; $book3 = R::load('book', R::store($book3)); // Added only one, not more? asrt(count($book3->sharedTopic), 1); asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1); // Add the same $book3->sharedTopic[] = $topic1; $book3 = R::load('book', R::store($book3)); asrt(count($book3->sharedTopic), 1); asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1); $book3->sharedTopic['differentkey'] = $topic1; $book3 = R::load('book', R::store($book3)); asrt(count($book3->sharedTopic), 1); asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1); // Ugly assign, auto array generation $book3->ownPage[] = $page1; $book3 = R::load('book', R::store($book3)); asrt(count($book3->ownPage), 1); asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 1); $book3 = R::load('book', $idb3); $book3->ownPage = array(); // No change until saved asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 1); $book3 = R::load('book', R::store($book3)); asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 0); asrt(count($book3->ownPage), 0); $book3 = R::load('book', $idb3); /** * Why do I need to do this ---> why does trash() not set id -> 0? * Because you unset() so trash is done on origin not bean */ $page1->id = 0; $page2->id = 0; $page3->id = 0; $book3->ownPage[] = $page1; $book3->ownPage[] = $page2; $book3->ownPage[] = $page3; $book3 = R::load('book', R::store($book3)); asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 3); asrt(count($book3->ownPage), 3); unset($book3->ownPage[$page2->id]); $book3->ownPage[] = $page3; $book3->ownPage['try_to_trick_ya'] = $page3; $book3 = R::load('book', R::store($book3)); asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 2); asrt(count($book3->ownPage), 2); // Delete and re-add $book3 = R::load('book', $idb3); unset($book3->ownPage[10]); $book3->ownPage[] = $page1; $book3 = R::load('book', R::store($book3)); asrt(count($book3->ownPage), 2); $book3 = R::load('book', $idb3); unset($book3->sharedTopic[1]); $book3->sharedTopic[] = $topic1; $book3 = R::load('book', R::store($book3)); asrt(count($book3->sharedTopic), 1); // Test performance $logger = RedBean_Plugin_QueryLogger::getInstanceAndAttach(R::$adapter); $book = R::load('book', 1); $book->sharedTopic = array(); R::store($book); // No more than 1 update asrt(count($logger->grep('UPDATE')), 1); $book = R::load('book', 1); $logger->clear(); print_r($book->sharedTopic, 1); // No more than 1 select asrt(count($logger->grep('SELECT')), 1); $logger->clear(); $book->sharedTopic[] = $topic1; $book->sharedTopic[] = $topic2; asrt(count($logger->grep('SELECT')), 0); R::store($book); $book->sharedTopic[] = $topic3; // Now do NOT clear all and then add one, just add the one $logger->clear(); R::store($book); $book = R::load('book', 1); asrt(count($book->sharedTopic), 3); // No deletes asrt(count($logger->grep("DELETE FROM")), 0); $book->sharedTopic['a'] = $topic3; unset($book->sharedTopic['a']); R::store($book); $book = R::load('book', 1); asrt(count($book->sharedTopic), 3); // No deletes asrt(count($logger->grep("DELETE FROM")), 0); $book->ownPage = array(); R::store($book); asrt(count($book->ownPage), 0); $book->ownPage[] = $page1; $book->ownPage['a'] = $page2; asrt(count($book->ownPage), 2); R::store($book); unset($book->ownPage['a']); asrt(count($book->ownPage), 2); unset($book->ownPage[11]); R::store($book); $book = R::load('book', 1); asrt(count($book->ownPage), 1); $aPage = $book->ownPage[10]; unset($book->ownPage[10]); $aPage->title .= ' changed '; $book->ownPage['anotherPage'] = $aPage; $logger->clear(); R::store($book); // if ($db=="mysql") asrt(count($logger->grep("SELECT")),0); $book = R::load('book', 1); asrt(count($book->ownPage), 1); $ap = reset($book->ownPage); asrt($ap->title, "pagina1 changed "); // Fix udiff instead of diff $book3->ownPage = array($page3, $page1); $i = R::store($book3); $book3 = R::load('book', $i); asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 2); asrt(count($book3->ownPage), 2); $pic1->name = 'aaa'; $pic2->name = 'bbb'; R::store($pic1); R::store($q1); $book3->ownPicture[] = $pic1; $book3->ownQuote[] = $q1; $book3 = R::load('book', R::store($book3)); // two own-arrays -->forgot array_merge asrt(count($book3->ownPicture), 1); asrt(count($book3->ownQuote), 1); asrt(count($book3->ownPage), 2); $book3 = R::load('book', R::store($book3)); unset($book3->ownPicture[1]); $book3 = R::load('book', R::store($book3)); asrt(count($book3->ownPicture), 0); asrt(count($book3->ownQuote), 1); asrt(count($book3->ownPage), 2); $book3 = R::load('book', R::store($book3)); $NOTE = 0; $quotes = R::dispense('quote', 10); foreach ($quotes as &$justSomeQuote) { $justSomeQuote->note = 'note' . ++$NOTE; } $pictures = R::dispense('picture', 10); foreach ($pictures as &$justSomePic) { $justSomePic->note = 'note' . ++$NOTE; } $topics = R::dispense('topic', 10); foreach ($topics as &$justSomeTopic) { $justSomeTopic->note = 'note' . ++$NOTE; } for ($j = 0; $j < 10; $j++) { // Do several mutations for ($x = 0; $x < rand(1, 20); $x++) { modgr($book3, $quotes, $pictures, $topics); } $qbefore = count($book3->ownQuote); $pbefore = count($book3->ownPicture); $tbefore = count($book3->sharedTopic); $qjson = json_encode($book->ownQuote); $pjson = json_encode($book->ownPicture); $tjson = json_encode($book->sharedTopic); $book3 = R::load('book', R::store($book3)); asrt(count($book3->ownQuote), $qbefore); asrt(count($book3->ownPicture), $pbefore); asrt(count($book3->sharedTopic), $tbefore); asrt(json_encode($book->ownQuote), $qjson); asrt(json_encode($book->ownPicture), $pjson); asrt(json_encode($book->sharedTopic), $tjson); testids($book->ownQuote); testids($book->ownPicture); testids($book->sharedTopic); } }
public function testGetBeanWhenThereIsNoneToGet() { $bean = R::dispense('a'); $bean2 = R::relatedOne($bean, 'b'); $this->assertTrue($bean2 === null); }
private function giveCards() { $deck = new PlayingCardDeck(); $deck->shuffle(); // cards for center $center_cards = array(); for ($i = 0; $i < 5; $i++) { $center_cards[] = array_merge($deck->drawCard(), array('turned' => $i <= 1 ? true : false)); } $this->_pokerRound->cards = json_encode($center_cards); // cards for players $first = null; $last = null; $players = R::related($this->_pokerRound, 'poker_player', ' 1=1 ORDER BY id'); foreach ($players as $p) { // if any player has less than 10 cash, kick him out! $usr = R::relatedOne($this->_pokerPlayer, 'user'); if ($usr->cash < 10) { $p->status = 'view'; R::store($p); R::unassociate($this->_pokerRound, $p); continue; } $cards = array(); $cards[] = $deck->drawCard(); $cards[] = $deck->drawCard(); $p->cards = json_encode($cards); $p->bid = 10; $p->status = 'play'; $p->game_state = 0; $p->all_in = false; $p->all_in_amount = 0; R::store($p); // substract bid $usr->cash -= 10; R::store($usr); $last = $p; } $this->_pokerRound->current_player = $last; $this->_pokerRound->ttl = time() + 60; R::store($this->_pokerRound); }