예제 #1
0
 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;
 }
예제 #2
0
 public static function TriggerHelper($name, $user, &$tasks)
 {
     $runTasks = 0;
     if (isset($tasks[$name])) {
         $toDetroy = array();
         foreach ($tasks[$name] as $key => $value) {
             $keep_task = FALSE;
             $task = new $value->type();
             if ($task instanceof ITask) {
                 if ($value->active) {
                     CScheduler::instance()->_ignore[] = $value->id_task;
                     $task->Run($user, json_decode($value->arguments, TRUE));
                     $toDetroy[] = $value->id_task;
                     ++$runTasks;
                 } else {
                     $keep_task = TRUE;
                 }
             }
             if ($keep_task == FALSE) {
                 foreach ($value->trigger as $t) {
                     unset($tasks[$t][$key]);
                 }
             }
         }
         CScheduler::instance()->_ignore = array();
         CScheduler::instance()->DestroyTask($toDetroy);
     }
     return $runTasks;
 }
예제 #3
0
 /**
  * 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);
             }
         }
     }
 }
예제 #4
0
function os_poker_messagebox_page()
{
    require_once drupal_get_path('module', 'os_poker') . "/scheduler.class.php";
    $mbox = CScheduler::instance()->GetTasks("inbox");
    if (isset($_GET["list"]) && $_GET["list"] == "messages" && !empty($_GET["ajax"])) {
        return theme('os_poker_message_list', $mbox);
    }
    // Mark the messages as read - we consider all messages to be read once the user opens the messagebox
    CScheduler::instance()->MarkTasksAsRead();
    // Reset the unread message count in the navbar
    drupal_add_js(drupal_get_path('module', 'os_poker') . '/js/os_poker.messageboxreset.js', 'module');
    return theme('os_poker_messagebox', $mbox);
}
예제 #5
0
 public function SendInstantMessage($args, $targetUid = NULL)
 {
     $current_user = CUserManager::instance()->CurrentUser();
     if ($current_user && $current_user->uid != 0 || $targetUid) {
         if (isset($args["text"])) {
             if ($targetUid == NULL) {
                 $targetUid = $current_user->uid;
             }
             CScheduler::instance()->RegisterTask(new CMessage(), $targetUid, array('live'), "-1 day", array('type' => 'os_poker_imsg', 'body' => array('text' => $args['text'], 'title' => isset($args['title']) ? $args['title'] : t('Notification'))));
         }
     }
 }
예제 #6
0
function _os_poker_process_message_unsafe()
{
    static $poker_server_events = array('NONE', 'HAND', 'TOURNEY', 'BUY_IN', 'REFILL', 'PRIZE', 'REGISTER', 'UNREGISTER', 'LEAVE', 'SEAT', 'TOURNEY_START');
    $current_user = CUserManager::instance()->CurrentUser();
    $resp = array("errorMsg" => NULL, "error" => FALSE, "messages" => array());
    $message_type = trim(isset($_GET["type"]) ? trim($_GET["type"]) : "noop");
    if ($message_type == "os_poker_reward") {
        /* Reward message is a special message which comes without a current user context 
        		   args[0] = reward number
        		   args[1] = player serial
        		   args[2] = time_stamp
        		*/
        $all_rewards = CPoker::GetRewards();
        $params = json_decode($_GET['args'], TRUE);
        CPoker::ShowReward("reward" . $params[0], $params[1], $all_rewards);
        $resp["messages"][] = array("type" => "noop", "body" => NULL);
    }
    if ($current_user && $current_user->uid != 0) {
        switch ($message_type) {
            case "os_poker_sit_down":
                CPoker::CheckRewards("sit", $current_user->uid, json_decode($_GET["players"], TRUE));
                $resp["messages"][] = array("type" => "noop", "body" => NULL);
                break;
            case "os_poker_daily_gift":
                if ($current_user->DailyGift()) {
                    $resp["messages"][] = array('type' => 'os_poker_gift_sent', 'body' => array('text' => t('You have sent !amount free chips to !count buddies', array('!count' => count($current_user->Buddies()), '!amount' => 100))));
                } else {
                    $resp["messages"][] = array("type" => "noop", "body" => NULL);
                }
                break;
            case "os_poker_load_messagebox":
                CScheduler::instance()->Trigger("inbox");
                $mbox = CScheduler::instance()->GetUnreadTasks("inbox");
                $mboxsize = count($mbox);
                if ($_GET["msgcount"] != $mboxsize) {
                    $resp["messages"][] = array("type" => "os_poker_messagebox", "body" => array("inbox" => $mboxsize, "picture" => $base_path . drupal_get_path('module', 'os_poker') . "/images/mailbox.png"));
                } else {
                    $resp["messages"][] = array("type" => "noop", "body" => NULL);
                }
                break;
            case "os_poker_challenge_user":
                //TODO: limit challenge to 1 per sender/receiver
                if (isset($_GET["challengetarget"])) {
                    $target_user = CUserManager::instance()->User($_GET["challengetarget"]);
                    if ($target_user && $target_user->uid != 0 && $current_user->uid != $target_user->uid) {
                        //Wait for symbol, text, link
                        $args["symbol"] = 'chips';
                        $args["text"] = t("You just receive a headsup challenge from !user", array("!user" => $current_user->profile_nickname));
                        $args["links"] = "<a class='noreplace' href='javascript:void(0);' onclick='javascript:parent.os_poker_start_challenge(" . $current_user->uid . ", " . $target_user->uid . ");'>" . t("Accept") . "</a>/<a href='javascript:void(0);' >" . t("Refuse") . "</a>";
                        CMessageSpool::instance()->SendMessage($target_user->uid, $args);
                        $notification = array('text' => t("You just challenged !user", array("!user" => $target_user->profile_nickname ? $target_user->profile_nickname : variable_get('anonymous', t('Anonymous')))), 'title' => t('Challenge'));
                        CMessageSpool::instance()->SendInstantMessage($notification);
                        $resp["messages"][] = array("type" => "os_poker_notify", "body" => $notification);
                    }
                }
                break;
            case "os_poker_activate_item":
                if (isset($_GET["id_item"]) && is_numeric($_GET["id_item"])) {
                    $current_user->ActivateItem($_GET["id_item"]);
                } else {
                    trigger_error(t('Invalid item ID: %item_id', array('%item_id' => isset($_GET["id_item"]) ? $_GET["id_item"] : 'undefined')), E_USER_ERROR);
                }
                break;
            case "os_poker_invite_user":
                if (isset($_GET["target"])) {
                    $target_user = CUserManager::instance()->User($_GET["target"]);
                    if ($target_user && $target_user->uid != 0 && $current_user->uid != $target_user->uid) {
                        $tables = $current_user->Tables();
                        if (count($tables) > 0) {
                            $args["symbol"] = 'chips';
                            $args["text"] = t("!user is playing at table !table come and join", array("!user" => $current_user->profile_nickname, "!table" => $tables[0]->name));
                            //TODO : Check $_GET["online"] to send mail
                            CMessageSpool::instance()->SendMessage($target_user->uid, $args);
                            $notification = array('text' => t("You just invited !user to play", array("!user" => $target_user->profile_nickname ? $target_user->profile_nickname : variable_get('anonymous', t('Anonymous')))), 'title' => t('Invitation'));
                            CMessageSpool::instance()->SendInstantMessage($notification);
                            $resp["messages"][] = array("type" => "os_poker_notify", "body" => $notification);
                        }
                    }
                }
                break;
            case 'os_poker_trigger_error':
                trigger_error("Message triggered error.", E_USER_ERROR);
                break;
            case 'os_poker_trigger_exception':
                throw new Exception('Message triggered exception.');
                break;
            default:
                $resp["messages"][] = array("type" => "noop", "body" => NULL);
                break;
        }
    }
    if (in_array(drupal_strtoupper($message_type), $poker_server_events)) {
        if (preg_match('/\\[([^,]+),([^,]+),([^,]+)\\]/', $_GET['args'], $args)) {
            array_shift($args);
        }
        //Event setn from the poker server
        //TODO Authenticate the request... how ?
        switch ($message_type) {
            case 'BUY_IN':
                list($uid, $game_id, $amount) = $args;
                if (user_load($uid)) {
                    CScheduler::instance()->RegisterTask(new CUpdateUserChipsCount(), $uid, array('live'));
                    $resp["messages"][] = array("type" => "debug", "body" => t('Recevied BUY_IN for user !uid', array('!uid' => $uid)));
                } else {
                    trigger_error(t('Invalid user ID: %uid', array('%uid' => $uid)), E_USER_ERROR);
                }
                break;
            case 'TOURNEY_START':
                list($tourney_serial, $dummy1, $dummy2) = $args;
                $tourney_users = CPoker::TourneyRegisteredUsers($tourney_serial);
                foreach ($tourney_users as $tourney_user) {
                    if (user_load($tourney_user->user_serial)) {
                        CScheduler::instance()->RegisterTask(new CTourneyNotificationMessage(), $tourney_user->user_serial, array('live'), "-1 day", array($tourney_serial, $tourney_user->description_short, $tourney_user->table_serial));
                        $resp["messages"][] = array("type" => "debug", "body" => t('Recevied tourney start notification for user !uid', array('!uid' => $tourney_user->user_serial)));
                    }
                }
                break;
            case 'HAND':
            case 'TOURNEY':
            case 'REFILL':
            case 'PRIZE':
            case 'REGISTER':
            case 'UNREGISTER':
            case 'LEAVE':
            case 'SEAT':
            case 'NONE':
                $resp["messages"][] = array("type" => "noop", "body" => NULL);
                break;
        }
    } else {
        if (!$resp["messages"]) {
            $resp["messages"][] = array("type" => "noop", "body" => NULL);
        }
    }
    _os_poker_process_message_set_header();
    print json_encode($resp);
}
예제 #7
0
function os_poker_first_profile_form_submit($form, &$form_state)
{
    require_once drupal_get_path('module', 'os_poker') . "/scheduler.class.php";
    $cuser = CUserManager::instance()->CurrentUser(TRUE);
    $edit =& $form_state['values'];
    $profileComplete = TRUE;
    if (empty($edit["profile_nickname"])) {
        $cuser->profile_nickname = _os_poker_rand_player();
        $profileComplete &= FALSE;
    } else {
        $cuser->profile_nickname = $edit["profile_nickname"];
    }
    $cuser->name = $cuser->profile_nickname;
    if (variable_get('user_email_verification', TRUE)) {
        $raw_password = $cuser->pass2;
        $cuser->pass = $raw_password;
        $cuser->pass2 = NULL;
    }
    if (!empty($edit["profile_dob"])) {
        $cuser->profile_dob = $edit["profile_dob"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_gender"])) {
        $cuser->profile_gender = $edit["profile_gender"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_city"])) {
        $cuser->profile_city = $edit["profile_city"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["profile_country"])) {
        $cuser->profile_country = $edit["profile_country"];
    } else {
        $profileComplete &= FALSE;
    }
    if (!empty($edit["picture"])) {
        $cuser->picture = $edit["picture"];
    } else {
        $profileComplete &= FALSE;
    }
    //Check Profile complete
    if ($profileComplete && $cuser->CompleteProfile() == FALSE) {
        $cuser->AddChips(2000);
        $cuser->SetProfileComplete();
        if (isset($edit["first_profile"])) {
            CScheduler::instance()->RegisterTask(new CDelayMessage(), $cuser->uid, 'login', "-1 Day", array("type" => "os_poker_jump", "body" => array("lightbox" => TRUE, "url" => url("poker/buddies/invite", array("query" => array("height" => 442, "width" => 603), "absolute" => TRUE)))));
        }
    } else {
        if (isset($edit["first_profile"])) {
            CScheduler::instance()->RegisterTask(new CDelayMessage(), $cuser->uid, 'login', "-1 Day", array("type" => "os_poker_jump", "body" => array("lightbox" => TRUE, "url" => url("poker/profile/update", array("query" => array("height" => 442, "width" => 603), "absolute" => TRUE)))));
        }
    }
    $cuser->Save();
    //Trigger the invitation bonus
    CScheduler::instance()->Trigger('first_login');
    CScheduler::instance()->RegisterTask(new CDailyChips(), $cuser->uid, array('login', "live"), "+1 Day 00:00");
    //Send mail
    $account = $cuser->DrupalUser();
    if (variable_get('user_email_verification', TRUE) && !isset($account->fbu)) {
        drupal_mail('os_poker', 'profile', $account->mail, user_preferred_language($account), array('account' => $account, 'raw_password' => $raw_password));
    }
}