Skip to content

feat: print with [Sandbox.candidate_name] banner for error log #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ All other information should be explored by reading the source code!
- [2023/3/28 09:11] fix: ban nonhero units for hero choosing.([#31](https://github.com/Escapingbug/dotaxctf/pull/31),reporter: 114@x1ct34m)
- [2023/3/28 09:16] feat: clean up items when round ends.([#30](https://github.com/Escapingbug/dotaxctf/pull/30))
- [2023/3/29 08:46] fix: place history.scores updates to correct loc.([#32](https://github.com/Escapingbug/dotaxctf/pull/32), reporter: AAA剑圣)
- [2023/3/29 11:18] feat: print with [Sandbox.candidate_name] banner to support checking specified hero's log.([#33](https://github.com/Escapingbug/dotaxctf/pull/33))
- [2023/3/29 11:18] feat: print with `[Sandbox.candidate_name]` banner to support checking specified hero's log.([#33](https://github.com/Escapingbug/dotaxctf/pull/33))
- [2023/3/29 15:26] feat: print with `[Sandbox.candidate_name.script]` banner when script got an error.([#35](https://github.com/Escapingbug/dotaxctf/pull/35))
3 changes: 2 additions & 1 deletion bot_script_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ function BotScript:Init(script_content, candidate_name)
-- print("bot script: " .. script_content)
self.run_bot = Sandbox:LoadActionScript(script_content, candidate_name)
self.ctx = {}
self.candidate_name = candidate_name
end

function BotScript:OnThink(entity)
self.ctx = Sandbox:RunAction(self.run_bot, entity, self.ctx)
self.ctx = Sandbox:RunAction(self.run_bot, entity, self.ctx, self.candidate_name)
return 0.1
end

Expand Down
2 changes: 1 addition & 1 deletion rounds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function Rounds:ChooseHeros(chooser_scripts, attributes, bot_scripts)

for _, candidate_id in ipairs(cur_candidates) do
local chooser = Sandbox:LoadChooseHeroScript(chooser_scripts[candidate_id], Candidates[candidate_id])
local hero_name = Sandbox:RunChooseHero(chooser)
local hero_name = Sandbox:RunChooseHero(chooser, Candidates[candidate_id])
local player_id = self.candidate_to_player[candidate_id]
local player_owner = PlayerResource:GetPlayer(player_id)

Expand Down
12 changes: 6 additions & 6 deletions sandbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ function Sandbox:LoadScript(user_script, quota, env, candidate_name)
return results[2]
end

function Sandbox:RunFunctionWrap(func, ...)
function Sandbox:RunFunctionWrap(func, candidate_name, ...)
if not func then
return nil
end
local results = {pcall(func, ...)}
if not results[1] then
print("run script error: " .. results[2])
print("[Sandbox.".. candidate_name ..".script]", results[2])
return nil
end
return results[2]
Expand All @@ -64,17 +64,17 @@ function Sandbox:LoadActionScript(user_script, candidate_name)
return self:LoadScript(user_script, 500000, {}, candidate_name)
end

function Sandbox:RunChooseHero(choose_func)
local hero_name = self:RunFunctionWrap(choose_func)
function Sandbox:RunChooseHero(choose_func, candidate_name)
local hero_name = self:RunFunctionWrap(choose_func, candidate_name)
if type(hero_name) ~= "string" then
hero_name = self.default_hero
end
return hero_name
end

function Sandbox:RunAction(act_func, entity, ctx)
function Sandbox:RunAction(act_func, entity, ctx, candidate_name)
local sandboxed_entity = self:SandboxHero(entity, false)
local new_ctx = self:RunFunctionWrap(act_func, sandboxed_entity, ctx)
local new_ctx = self:RunFunctionWrap(act_func, candidate_name, sandboxed_entity, ctx)
return new_ctx
end

Expand Down