Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

local p = {}
local cargo = mw.ext.cargo

local function clean(s)
    if not s then return nil end
    local res = s:gsub("^%s*(.-)%s*$", "%1")
    return res == "" and nil or res
end

-- Helper to parse the key=value string line (e.g. "team=GodLike, rank=1, elims=5")
local function parseLine(line)
    local data = {}
    -- Split by comma
    for pair in string.gmatch(line, "[^,]+") do
        -- Split by equals sign
        local key, val = string.match(pair, "^%s*([^=]+)%s*=%s*(.-)%s*$")
        if key and val then
            data[string.lower(key)] = val
        end
    end
    return data
end

function p.store(frame)
    local args = frame:getParent().args
    
    local statType = clean(args.type) or "team"
    statType = string.lower(statType)
    
    -- Global Metadata (Common to all rows in this match)
    local meta = {
        tournament       = clean(args.tournament),
        tournament_type  = clean(args.tournament_type),
        stage            = clean(args.stage),
        group_name       = clean(args.group),
        map              = clean(args.map),
        match_type       = clean(args.match_type),
        tournament_day   = clean(args.tournament_day),
        date             = clean(args.date),
        time             = clean(args.time),
        number_of_teams  = clean(args.number_of_teams),
        number_of_players= clean(args.number_of_players)
    }

    local tableName = (statType == "player") and "MatchStats_Player" or "MatchStats_Team"
    
    -- Process each numbered line
    for i = 1, 100 do
        local line = args[tostring(i)]
        if line and clean(line) then
            local rowData = parseLine(line)
            
            local storeData = {
                tournament       = meta.tournament,
                tournament_type  = meta.tournament_type,
                stage            = meta.stage,
                group_name       = meta.group_name,
                map              = meta.map,
                match_type       = meta.match_type,
                tournament_day   = meta.tournament_day,
                match_date       = meta.date,
                match_time       = meta.time
            }
            
            if statType == "team" then
                storeData.number_of_teams = meta.number_of_teams
                storeData.team            = rowData.team
                storeData.short_name      = rowData.short
                storeData.match_played    = tonumber(rowData.mp) or 1
                storeData.rank            = tonumber(rowData.rank)
                storeData.wwcd            = tonumber(rowData.wwcd) or 0
                storeData.place_pts       = tonumber(rowData.place) or 0
                storeData.elim_pts        = tonumber(rowData.elims) or 0
                storeData.bonus_pts       = tonumber(rowData.bonus) or 0
                
                -- Auto calculate total if not provided
                if rowData.total then
                    storeData.total_pts = tonumber(rowData.total)
                else
                    storeData.total_pts = storeData.place_pts + storeData.elim_pts + storeData.bonus_pts
                end
                
            elseif statType == "player" then
                storeData.number_of_players = meta.number_of_players
                storeData.player          = rowData.player
                storeData.team            = rowData.team
                storeData.short_name      = rowData.short
                storeData.match_played    = tonumber(rowData.mp) or 1
                storeData.player_elims    = tonumber(rowData.player_elims) or 0
                storeData.mvp             = tonumber(rowData.mvp) or 0     
                storeData.team_rank       = tonumber(rowData.team_rank)
                storeData.team_wwcd       = tonumber(rowData.team_wwcd) or 0
                storeData.team_place      = tonumber(rowData.team_place) or 0
                storeData.team_elims      = tonumber(rowData.team_elims) or 0
                
                if rowData.team_total then
                    storeData.team_total = tonumber(rowData.team_total)
                else
                    storeData.team_total = storeData.team_place + storeData.team_elims
                end
            end
            
            -- Optional Shared Stats (Combat/Utility)
            storeData.damage       = tonumber(rowData.damage)
            storeData.survival     = rowData.survival
            storeData.healing      = tonumber(rowData.healing)
            storeData.rdamage      = tonumber(rowData.rdamage)
            storeData.headshots    = tonumber(rowData.headshots)
            storeData.assists      = tonumber(rowData.assists)
            storeData.knockouts    = tonumber(rowData.knockouts)
            storeData.long_elim    = tonumber(rowData.long_elim)
            storeData.vehicle_elim = tonumber(rowData.vehicle_elim)
            storeData.grenade_elim = tonumber(rowData.grenade_elim)
            storeData.smokes       = tonumber(rowData.smokes)
            storeData.grenades     = tonumber(rowData.grenades)
            storeData.molotovs     = tonumber(rowData.molotovs)
            storeData.flash        = tonumber(rowData.flash)
            storeData.utilities    = tonumber(rowData.utilities)
            storeData.airdrops     = tonumber(rowData.airdrops)
            storeData.rescues      = tonumber(rowData.rescues)
            storeData.dist_drove   = tonumber(rowData.dist_drove)
            storeData.dist_walk    = tonumber(rowData.dist_walk)
            storeData.total_dist   = tonumber(rowData.total_dist)
            
            -- Store to Cargo
            if cargo and cargo.store then
                cargo.store(tableName, storeData)
            end
        end
    end
    
    return "<div style='display:none;'>Match Stats Stored Successfully.</div>"
end

return p