Welcome to the Umamusume Wiki! If you want to contribute, please read the guidelines.

Module:Game

From Umamusume Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Game/doc

--[[
    !! THIS PAGE IS MANAGED BY GITLAB !!
    ANY EDITS TO PAGE CONTENT WILL BE OVERWRITTEN
    TO MAKE CHANGES, PLEASE SUBMIT A MERGE REQUEST AT https://gitlab.com/umamusume-wiki/lua-modules
]]

local Game = {}

---@class QueryConfig
---@field from string
---@field join_on string|nil
---@field where string|nil
---@field limit string|number|nil
---@field order_by string|nil
---@field data table|nil

local function _queryMaster(query)
    if query.join_on ~= nil then query['join on'] = query.join_on end
    if query.order_by ~= nil then query['order by'] = query.order_by end

    local data, error = mw.ext.externaldata.getDbData(query)
    if error ~= nil then
        mw.logObject(error, "Masterdata query error")
    end
    return data
end

---Query the Masterdata SQLite database for the JP server
---@param query QueryConfig the ExternalData query object. "db" is not required, it is set automatically.
---@return table data the response data in a list of tables
function Game.queryMaster(query)
    query['db'] = 'masterdata'
    return _queryMaster(query)
end

---Query the Masterdata SQLite database for the EN server
---@param query QueryConfig the ExternalData query object. "db" is not required, it is set automatically.
---@return table data the response data in a list of tables
function Game.queryMasterEN(query)
    query['db'] = 'masterdata_en'
    return _queryMaster(query)
end

---Query the JP masterdata text_data for the given category and indices
---@param category number
---@param indices (string|number)[]
---@param ignoreEscapedNewlines boolean|nil if true, don't replace "\\n" with newlines
---@return (string|nil)[] texts keyed by index
function Game.getJPText(category, indices, ignoreEscapedNewlines)
    local indicesSub = table.concat(indices, ",")
    local datas = Game.queryMaster {
        from = "text_data",
        where = string.format("category = %s AND `index` IN (%s)", category, indicesSub),
    }
    if #datas == 0 then return {} end

    local texts = {}
    for _, data in ipairs(datas) do
        if not ignoreEscapedNewlines then
            texts[data.index] = string.gsub(data.text, "\\n", "\n")
        else
            texts[data.index] = data.text
        end
    end
    return texts
end

---Query the EN masterdata text_data for the given category and indices
---@param category number
---@param indices (string|number)[]
---@param ignoreEscapedNewlines boolean|nil if true, don't replace "\\n" with newlines
---@return (string|nil)[] texts keyed by index
function Game.getENText(category, indices, ignoreEscapedNewlines)
    local indicesSub = table.concat(indices, ",")
    local datas = Game.queryMasterEN {
        from = "text_data",
        where = string.format("category = %s AND `index` IN (%s)", category, indicesSub),
    }
    if #datas == 0 then return {} end

    local texts = {}
    for _, data in ipairs(datas) do
        if not ignoreEscapedNewlines then
            texts[data.index] = string.gsub(data.text, "\\n", "\n")
        else
            texts[data.index] = data.text
        end
    end
    return texts
end

return Game