Exemplo n.º 1
0
    }
    if (isset($_REQUEST['room'])) {
        $room = $_REQUEST['room'];
        $message['room'] = $room;
    } else {
        throw new Exception('ERROR NO ROOM SPECIFIED.');
    }
    // Store the UTC date with millisecond precision.
    $t = microtime(true);
    $micro = sprintf("%03d", ($t - floor($t)) * 1000);
    $message['timestamp'] = gmdate('Y-m-d H:i:s.', $t) . $micro;
    $key = 'chat_room_' . $room;
    // Retrieve the messages
    $message_list = data_store_get($key);
    array_push($message_list, $message);
    //var_dump($message_list);
    // Ensure the list is sorted by their timestamps before being stored.
    usort($message_list, function ($a, $b) {
        return strcmp($a['timestamp'], $b['timestamp']);
    });
    // Ensure that we don't have too many messages stored.
    while (count($message_list) > __MAX_CHAT_ROOM_MESSAGE_COUNT__) {
        array_shift($message_list);
    }
    // Save the message list.
    data_store_set($key, $message_list);
    $result['data']['message'] = $message;
} catch (Exception $e) {
    $result['error'] = $e->getMessage();
}
echo json_encode($result);
Exemplo n.º 2
0
        $key = $_REQUEST['key'];
    } else {
        throw new Exception('ERROR NO KEY SPECIFIED.');
    }
    if (isset($_REQUEST['player_data'])) {
        $str_player_data = $_REQUEST['player_data'];
        $new_player_data = json_decode($str_player_data, true);
    } else {
        throw new Exception('ERROR NO PLAYER DATA SPECIFIED.');
    }
    $data_key = 'player_data_' . $group;
    // Retrieve the data
    $data = data_store_get($data_key);
    if (isset($data[$key])) {
        $old_player_data = $data[$key];
    } else {
        $old_player_data = array();
    }
    // Merge the new player data into the old player data.
    $merged_player_data = array_merge($old_player_data, $new_player_data);
    // Store the server timestamp.
    $dateNow = new DateTime();
    $merged_player_data['last_update_time'] = $dateNow->format(__DATE_FORMAT__);
    $data[$key] = $merged_player_data;
    $result['data']['player_data'] = $merged_player_data;
    // Save the player data.
    data_store_set($data_key, $data);
} catch (Exception $e) {
    $result['error'] = $e->getMessage();
}
echo json_encode($result);