# lb-tablet

1. Navigate to the **lb-tablet/config/config.lua**
2. Find the `Config.HousingScript` option and customize:

```lua
CConfig.HousingScript = "vms_housing"
```

2. Navigate to the **lb-tablet/server/custom/housing**
3. Create a new lua file named `vms_housing.lua`
4. Paste all of the following code into the `vms_housing.lua` file

```lua
if Config.HousingScript ~= "vms_housing" then
    return
end

local resourceName = "vms_housing"

while GetResourceState(resourceName) ~= "started" do
    debugprint("Waiting for housing script to start...")
    Wait(1000)
end

local selectPropertyQuery = [[
    SELECT
        p.id,
        COALESCE(NULLIF(p.owner, ''), NULLIF(p.renter, '')) AS owner,
        CASE
            WHEN NULLIF(p.owner, '') IS NOT NULL THEN p.owner_name
            ELSE p.renter_name
        END AS `name`
    FROM houses p
]]

local searchPropertiesQuery = selectPropertyQuery .. [[
    WHERE
        COALESCE(NULLIF(p.owner, ''), NULLIF(p.renter, '')) IS NOT NULL

        AND (
            CASE
                WHEN NULLIF(p.owner, '') IS NOT NULL THEN p.owner_name
                ELSE p.renter_name
            END LIKE ?
            OR CAST(p.id AS CHAR) LIKE ?
        )

        {WHERE_FILTER}
    LIMIT ?, ?
]]


local function EncodePropertyId(owner, id)
    return "owner:" .. owner .. ",id:" .. id
end

local function DecodePropertyId(id)
    local owner, propertyId = string.match(id, "owner:(.+),id:([^,]+)$")
    return owner, propertyId and tonumber(propertyId)
end

local function FormatPropery(property)
    local propertyData = exports['vms_housing']:GetProperty(property.id)

    property.label = propertyData.name
    property.id = EncodePropertyId(property.owner, property.id)

    property.owner = {
        name = property.name,
        identifier = property.owner
    }

    property.name = nil
    property.propertyid = nil
    property.address = propertyData.address

    if propertyData.type == 'mlo' then
        property.location = {x = propertyData.metadata.menu.x, y = propertyData.metadata.menu.y}
    else
        if propertyData.object_id then
            local object = exports['vms_housing']:GetProperty(propertyData.object_id)
            if object.type == 'building' then
                property.location = {x = object.metadata.enter.x, y = object.metadata.enter.y}
                goto skip
            end
        end
        property.location = {x = propertyData.metadata.enter.x, y = propertyData.metadata.enter.y}
        ::skip::
    end

    return property
end

function SearchProperties(query, page, filter)
    local params = { "%" .. query .. "%", "%" .. query .. "%" }
    local where = ""
    local searchQuery = searchPropertiesQuery

    if filter.tags then
        for i = 1, #filter.tags do
            where = where .. " AND EXISTS (SELECT 1 FROM lbtablet_police_profile_tags ppt WHERE ppt.id COLLATE utf8mb4_unicode_ci = (CONCAT('house:owner:', p.owner, ',id:', p.id) COLLATE utf8mb4_unicode_ci) AND ppt.tag_id = ?)"
            params[#params+1] = filter.tags[i]
        end
    end

    searchQuery = searchQuery:gsub("{WHERE_FILTER}", where)

    params[#params+1] = (page or 0) * 10
    params[#params+1] = 10

    local properties = MySQL.query.await(
        searchQuery,
        params
    )

    for i = 1, #properties do
        properties[i] = FormatPropery(properties[i])
    end

    return properties
end

function GetProperty(identifier)
    local owner, id = DecodePropertyId(identifier)
    
    local property = exports['vms_housing']:GetProperty(id)

    if not property then
        debugprint("Failed to decode property id", id)
        return
    end

    return FormatPropery({
        id = id,
        owner = property.owner or property.renter,
        name = property.owner_name or property.renter_name,
    })
end

function GetPlayerProperties(identifier)
    local propertiesList = exports['vms_housing']:GetPlayerProperties(identifier)
    local properties = {}

    for k, v in pairs(propertiesList) do
        table.insert(properties, {
            id = v.id,
            name = v.name
        })
    end

    return properties
end
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.vames-store.com/assets/vms_housing/compatibility/tablet/lb-tablet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
