fvtt-rolemaster-frp/module/criticals_data/process_critical_json.lua

188 lines
4.8 KiB
Lua
Raw Normal View History

2024-10-20 18:21:19 +02:00
local lfs = require "lfs"
local json = require "dkjson"
local _CRIT_KEYS = {"A", "B", "C", "D", "E"}
local function process_values(rest, condition)
-- Get the +XH (with X being a number)
2025-01-04 20:42:37 +01:00
if not rest then
return {}
end
2024-10-20 18:21:19 +02:00
local damage = rest:match("%+(%d*)H")
-- π = mustparry
local mustparry = false
if rest:match(" π") then
mustparry = 1
end
if rest:match(" (%d)π") then
mustparry = rest:match(" (%d*)π")
end
local mustparry_duration = false
local mustparry_value = false
local duration, value = rest:match(" (%d?)%((π%-?%d+)%)")
if duration or value then
mustparry_duration = tonumber(duration) or 1
mustparry_value = tonumber(value)
end
-- ∑ = stunned
local stunned = false
local match = rest:match(" (%d*)∑")
if match then
stunned = tonumber(match) or 1
end
-- ∏ = cannot parry
local cannot_parry = false
match = rest:match(" (%d*)∑?∏")
if match then
cannot_parry = tonumber(match) or 1
end
match = rest:match(" (%d*)∏")
if match then
cannot_parry = tonumber(match) or 1
end
-- ∫ = wounds per rounds
local wounds_per_round = false
match = rest:match(" (%d?)∫")
if match then
wounds_per_round = tonumber(match) or 1
end
-- Round penalty
local round_penalty_duration = false
local nbRounds, round_penalty_value = rest:match(" (%d?)%((%-?%d+)%)")
if nbRounds or round_penalty_value then
round_penalty_duration = tonumber(nbRounds) or 1
round_penalty_value = tonumber(round_penalty_value)
end
-- Round bonus
local round_bonus_duration = false
local nbRounds, round_bonus_value = rest:match(" (%d?)%((%+?%d+)%)")
if nbRounds or round_bonus_value then
round_bonus_duration = tonumber(nbRounds) or 1
round_bonus_value = tonumber(round_bonus_value)
end
print(rest, damage, mustparry, stunned, cannot_parry, wounds_per_round, round_penalty_duration, round_penalty_value,
round_bonus_duration, round_bonus_value)
return {
condition = condition or "none",
damage = damage,
mustparry = mustparry,
stunned = stunned,
cannot_parry = cannot_parry,
wounds_per_round = wounds_per_round,
round_penalty_duration = round_penalty_duration,
round_penalty_value = round_penalty_value,
round_bonus_duration = round_bonus_duration,
2025-01-04 20:42:37 +01:00
round_bonus_value = round_bonus_value,
2024-10-20 18:21:19 +02:00
mustparry_duration = mustparry_duration,
mustparry_value = mustparry_value
}
end
local all_criticals = {}
2025-01-04 20:42:37 +01:00
local function process_line(line)
local effects = {}
-- Get description (ie all text befire the first \n)
local desc = line:match("([^\n]+)")
-- Get the rest of the text
local rest = line:match("\n(.+)")
if rest then
if rest:match("with ") then
local reasonwith, with, reasonwithout, without = rest:match("(with[^:]*): ([^w]+)(w/o[^:]*): (.+)")
print("SPLITTEW WITH", desc)
effects[1] = process_values(with, reasonwith)
effects[2] = process_values(without, reasonwithout)
else
effects[1] = process_values(rest)
end
else
print("NO REST", desc)
end
return desc, effects
end
2024-10-20 18:21:19 +02:00
-- Loop thru all JSON files in the criticals_data directory
for file in lfs.dir("./") do
if file:match("json$") then
local json_file = io.open("./" .. file, "r")
local json_data = json_file:read("*a")
json_file:close()
local crit = {
2025-01-04 20:42:37 +01:00
name = string.match(file, "([%w_]+).json"),
2024-10-20 18:21:19 +02:00
criticals = {}
}
local rows = json.decode(json_data)
2025-01-04 20:42:37 +01:00
if not rows then
print("Error decoding JSON file: " .. file)
return
end
2024-10-20 18:21:19 +02:00
for _, c in ipairs(rows) do
local new_crit = {
score = c.Score,
2025-01-04 20:42:37 +01:00
isCreature = false,
2024-10-20 18:21:19 +02:00
levels = {}
}
2025-01-04 20:42:37 +01:00
-- Process tables with A-E criticals
local isWeapon = true
2024-10-20 18:21:19 +02:00
for _, key in ipairs(_CRIT_KEYS) do
local line = c[key]
2024-11-16 09:31:15 +01:00
if not line then
2025-01-04 20:42:37 +01:00
isWeapon = false
2024-11-16 09:31:15 +01:00
break
end
2025-01-04 20:42:37 +01:00
local desc, effects = process_line(line)
2024-10-20 18:21:19 +02:00
new_crit.levels[key] = {
key = key,
description = desc,
effects = effects
}
end
2025-01-04 20:42:37 +01:00
-- Creatures table
if not isWeapon then
new_crit.isCreature = true
for key, line in pairs(c) do
if key ~= "Score" then
local desc, effects = process_line(line)
new_crit.levels[key] = {
key = key,
description = desc,
effects = effects
}
end
end
end
2024-10-20 18:21:19 +02:00
table.insert(crit.criticals, new_crit)
end
2025-01-04 20:42:37 +01:00
2024-10-20 18:21:19 +02:00
all_criticals[crit.name] = crit
end
end
-- Convert to JSON
2025-01-04 20:42:37 +01:00
local json_data = json.encode(all_criticals, {
indent = true
})
2024-10-20 18:21:19 +02:00
print(json_data)
2025-01-04 20:42:37 +01:00
-- Write the whole json_data to a single file
local json_file = io.open("criticals.json", "w+")
json_file:write(json_data)
json_file:close()
2024-10-20 18:21:19 +02:00