/** * Set the active item for this user. * * @param <type> $id_operation * @param <type> $gift If the activation is the result of a gift, the gift * details as a structuted array: <code> * $gift = array( * 'item' => 'name of the item', * 'sender' => uid of the user sending the gift, * ); * </code> */ public function ActivateItem($id_operation, $gift = NULL) { $sql = "INSERT INTO `{poker_user_ext}` (`uid`, `id_operation`) VALUES (%d, %d)\n\t\t\t\tON DUPLICATE KEY UPDATE `id_operation`= %d"; $res = db_query($sql, $this->_user->uid, $id_operation, $id_operation); $this->_activeItem = $id_operation; if ($gift == NULL) { $sql = 'SELECT pi.name FROM {poker_item} as pi LEFT JOIN {poker_operation} as po ON (pi.id_item = po.id_item) WHERE po.id_operation = %d'; $rs = db_query($sql, $id_operation); if ($rs) { $gift = array('item' => db_result($rs), 'sender' => $this->_user->uid); } } if ($gift) { $gift['receiver'] = $this->_user->uid; //Enqueue a 'live' gift event to all players sitting at the same table(s) as the receiver foreach ($this->Tables() as $table) { foreach (CPoker::UsersAtTable($table->serial) as $notified_uid) { CScheduler::instance()->RegisterTask(new CGiftNotificationMessage(), $notified_uid, array('live'), "-1 day", $gift); } } } }
public static function GiveItem($item_id, $targets, $debug = FALSE) { require_once drupal_get_path('module', 'os_poker') . "/scheduler.class.php"; require_once drupal_get_path('module', 'os_poker') . "/user.class.php"; $user = CUserManager::instance()->CurrentUser(); try { if (!is_array($targets) || count($targets) == 0) { throw new Exception(t('Bad parameter: !cause', array('!cause' => is_array($targets) ? t('$target is empty') : t('$targer is not an array')))); } $rawTargets = CUserManager::instance()->UserList($targets); $targets = array_filter($rawTargets, "_os_poker_user_accepts_gifts"); $ntargets = count($targets); if ($ntargets == 0) { throw new Exception(t('No one of targets accepts gifts')); } $item = new CItem($item_id); //check if the item really exists /* ** User must pay ! */ $nchips = $user->Chips(); $price = $item->price * $ntargets; if (bccomp($nchips, $price) < 0) { throw new Exception(t('User doesn\'t have enough money (!uc vs !cn needed).', array('!uc' => $nchips, '!cn' => $price))); } $sql = "INSERT INTO `{poker_operation}`\n\t\t\t\t (`id_item`, `uid`, `source_uid`, `tstamp`)\n\t\t\t\t VALUES "; foreach ($targets as $target) { $fields[] = "(%d, %d, %d, %s)"; $values[] = $item->id_item; $values[] = $target->uid; $values[] = $user->uid; $values[] = "NOW()"; } $sql .= implode(", ", $fields); $res = db_query($sql, $values); if ($res == FALSE) { throw new Exception(t('DB error : !message', array("!message" => db_error()))); } /* ** Expiry */ $operation_id = db_last_insert_id("{poker_operation}", "id_operation"); $ttl = $item->ttl; if ($operation_id && !empty($ttl)) { //$operation_id -= (count($targets) - 1); foreach ($targets as $target) { $gift = array('item' => $item->name, 'receiver' => $target->uid, 'sender' => $user->uid); if ($target->ActiveItem() <= 0) { $target->ActivateItem($operation_id, $gift); } else { //Send gift notification, even if the item is not activated foreach ($target->Tables() as $table) { foreach (CPoker::UsersAtTable($table->serial) as $notified_uid) { CScheduler::instance()->RegisterTask(new CGiftNotificationMessage(), $notified_uid, array('live'), "-1 day", $gift); } } } CScheduler::instance()->RegisterTask(new CItemExpiry(), $target->uid, 'live', $ttl, array("id_operation" => $operation_id)); ++$operation_id; $args["symbol"] = $item->picture; $args["text"] = t("You just receive a !gift from !user", array("!gift" => substr($item->name, 0, 30), "!user" => $user->profile_nickname)); if (_os_poker_user_accepts_gifts($user)) { $args["links"] = l(t("Send a gift in return"), "poker/shop/shop/1/buddy/" . $user->uid); } CMessageSpool::instance()->SendMessage($target->uid, $args); } } /* ** User must pay ! */ $user->SubChips($price); $user->Save(); } catch (Exception $e) { if ($debug == TRUE) { throw $e; } return FALSE; } return TRUE; }
function os_poker_shop_page($tab, $category = NULL, $target_type = NULL, $target_id = NULL, $subtarget_id = NULL) { $content = ""; if ($tab == NULL || $tab == "shop") { require_once drupal_get_path('module', 'os_poker') . "/user.class.php"; require_once drupal_get_path('module', 'os_poker') . "/shop.class.php"; require_once drupal_get_path('module', 'os_poker') . "/poker.class.php"; $subtarget = NULL; $buddies = NULL; $cats = CShop::ListCategories(); if ($target_type == NULL) { $target_type = "self"; } if ($category == NULL) { $vcats = array_keys($cats); if (count($vcats) > 0) { $category = $vcats[0]; } } $prods = CShop::ListItems($category); if (isset($_GET["list"]) && $_GET["list"] == "items" && !empty($_GET["ajax"])) { return print theme('os_poker_item_list', $prods); } $current_user = CUserManager::instance()->CurrentUser(); $buddies = array_filter($current_user->Buddies(TRUE), "_os_poker_user_accepts_gifts"); switch ($target_type) { case "table": $target = array_filter(CPoker::UsersAtTable($target_id, TRUE), "_os_poker_user_accepts_gifts"); if ($subtarget_id) { $subtarget = CUserManager::instance()->User($subtarget_id); } $merge = array_merge(array(), $target, $buddies); $special = array(); foreach ($merge as $u) { $special[$u->uid] = $u->uid; } $special = array_unique(array_keys($special)); break; case "buddy": $target = $buddies; $subtarget = CUserManager::instance()->User($target_id); break; case "self": $target = $buddies; $subtarget = $current_user; break; } if (!empty($_POST["shop_action"]) && !empty($_POST["shop_item"])) { $action = $_POST["shop_action"]; $success = TRUE; switch ($action) { case "subtarget": if ($subtarget->uid == $current_user->uid) { $success = CShop::BuyItem($_POST["shop_item"], empty($_POST["shop_item_activate"]) ? FALSE : !!$_POST["shop_item_activate"]); watchdog('os_poker', ' buy: ' . $success); } else { $success = CShop::GiveItem($_POST["shop_item"], array($subtarget)); watchdog('os_poker', ' giveItem: ' . $success); } break; case "target": $success = CShop::GiveItem($_POST["shop_item"], $target); watchdog('os_poker', ' target: ' . $success); break; case "special": $success = CShop::GiveItem($_POST["shop_item"], $special); watchdog('os_poker', ' spezial: ' . $success); break; } if ($success == FALSE) { $error = theme('poker_error_message', "<h1>" . t("Sorry !") . "</h1>" . t("You don't have enough Chips.")); } else { if ($target_type == 'table') { watchdog('os_poker', ' goto'); drupal_goto('poker/closebox'); } } } $params = array("categories" => $cats, "current_category" => $category, "items" => $prods, "current_user" => $current_user, "target_type" => $target_type, "target" => $target, "subtarget" => $subtarget, "target_id" => $target_id, "subtarget_id" => $subtarget_id, "buddies" => $buddies, "special" => $special, "error" => $error); $content = theme('os_poker_shop', $params); } else { if ($tab == "get_chips") { $content = theme('os_poker_shop_get_chips', drupal_get_form('chips_paypal_form'), $params); } } return theme('os_poker_shop_tabs', $tab, $content); }