Skip to content

Instantly share code, notes, and snippets.

@Pondidum
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save Pondidum/81760e9966050d5f0dda to your computer and use it in GitHub Desktop.

Select an option

Save Pondidum/81760e9966050d5f0dda to your computer and use it in GitHub Desktop.
DSL for class+spec loading
local environment = {
create = function()
local env = {}
env.case = function(condition, resultTable)
return function()
local result = condition()
print("case:", result)
local action = resultTable[result]
if action then
action()
end
end
end
env.group = function(...)
local actions = {...}
return function()
print("group:")
for i, action in ipairs(actions) do
action()
end
end
end
env.talent = function(name)
return function()
print("talent:", name)
if env.player.talents[name] then
env.ability(name)()
end
end
end
env.ability = function(name)
return function()
print("ability:", name)
env.results[env.currentDisplay] = env.results[env.currentDisplay] or {}
table.insert(env.results[env.currentDisplay], name)
end
end
env.display = function(displayName, ...)
local actions = {...}
return function()
print("display", displayName)
env.currentDisplay = displayName
for i,action in ipairs(actions) do
action()
end
end
end
env.class = function(options)
return function()
local action = options[env.player.class()]
if action then
action()
end
end
end
env.spec = function(options)
return function()
local action = options[env.player.spec()]
if action then
action()
end
end
end
return env
end
}
local config = function()
return class({
hunter = spec({
suvival = group(
display("cooldowns",
ability("stampede"),
talent("dire beast")
)
),
}),
shaman = spec({
enhancement = group(
display("rotation",
ability("lava lash"),
ability("storm strike")
),
display("cooldowns",
ability("ascendance"),
ability("feral spirits"),
ability("fire elemental totem")
)
),
elemental = group(),
restoration = group()
}),
})
end
local buildConfig = function()
local printResults = function(displays)
print("----------------------")
for name,spells in pairs(displays) do
print(name .. ":")
for i,v in ipairs(spells) do
print(i,v)
end
end
end
local env = environment.create()
env.player = {
class = function() return "hunter" end,
spec = function() return "suvival" end,
talents = {
["dire beast"] = true
}
}
env.results = {}
setmetatable(env, { __index = _G })
local parse = function()
print("config: get start")
local output = config()
print("config: get finish")
print("config: exec start")
output()
print("config: exec finish")
printResults(env.results)
env.results = {}
env.player.class = function() return "shaman" end
env.player.spec = function() return "enhancement" end
print("config: exec start")
output()
print("config: exec finish")
printResults(env.results)
env.results = {}
end
setfenv(parse, env)
setfenv(config, env)
parse()
end
buildConfig()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment