Beispiel #1
0
/**
 * drops users from users.json by timeout
 * @param int $timeout
 * @return execute record to file
 *
 */
function dropUsersBySession($timeout)
{
    $users = getJsonFromFile('users.json');
    foreach ($users as $id => $user) {
        if ($id[0] == 'g') {
            if (time() - $user->visited > $timeout && $user->online == false) {
                dropUser($users, $id);
            }
        }
    }
    return setJsonToFile('users.json', $users);
}
Beispiel #2
0
function search()
{
    $users = getJsonFromFile('active_users.json');
    if (empty($users->count)) {
        return false;
    }
    foreach ($users as $id => $u) {
        if ($id[0] == 'g') {
            findOpponent($users, $id);
        }
    }
    setJsonToFile('active_users.json', $users);
    return true;
}
Beispiel #3
0
/**
 * add in search users in active_users.json
 * @return bool true if added, false if not 
 *
 */
function addInSearch()
{
    if ($_SESSION['status'] == (1 || 2)) {
        return false;
    }
    $id = $_SESSION['user'];
    $activeUsers = getJsonFromFile('active_users.json');
    $users = getJsonFromFile('users.json');
    $activeUsers->lastId = (int) substr($id, 6);
    $activeUsers->count += 1;
    $activeUsers->{$id} = $users->{$id}->mmr;
    dropUser($users, $id);
    setJsonToFile('active_users.json', $activeUsers);
    setJsonToFile('users.json', $users);
    $GLOBALS['cantSearch'] = false;
    $_SESSION['status'] = 1;
    return $id;
}
Beispiel #4
0
/**
 * send message to chat.json
 * @param string $who
 * @param string $message
 * @return history
 *
 */
function sendChat($who, $message)
{
    $chats = getJsonFromFile('chat.json');
    $id = $_SESSION['chat'];
    if ($id === null || $chats->{$id} === null) {
        return false;
    }
    if (empty($chats->{$id}->history)) {
        $chats->{$id}->history = array();
    }
    $formattedMessage = $who . ': ' . $message;
    array_push($chats->{$id}->history, $formattedMessage);
    setJsonToFile('chat.json', $chats);
    return getChatHistory($id, $chats);
}
Beispiel #5
0
function garbageCollector($params)
{
    if (isset($_SESSION['user'])) {
        $users = getJsonFromFile('users.json');
        if (isset($users->{$_SESSION}['user'])) {
            if (isset($_REQUEST['quit'])) {
                $users->{$_SESSION}['user']->online = false;
            } else {
                $users->{$_SESSION}['user']->online = true;
                $users->{$_SESSION}['user']->visited = time();
            }
            setJsonToFile('users.json', $users);
        } else {
            $activeUsers = getJsonFromFile('active_users.json');
            if (isset($activeUsers->{$_SESSION}['user'])) {
                if (isset($_REQUEST['quit'])) {
                    $_SESSION['status'] = 0;
                    dropFromSearch($activeUsers, array($_SESSION['user']));
                    setJsonToFile('active_users.json', $activeUsers);
                    garbageCollector();
                }
            } else {
                $_SESSION['user'] = null;
                garbageCollector();
            }
        }
        return $_SESSION['user'];
    } else {
        return addUser();
    }
}