Skip to content
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
4 changes: 3 additions & 1 deletion src/lua/asobi_lua_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ game.terrain.preload(coords_list) -- preload chunks async
```
""".

-include_lib("kernel/include/logger.hrl").

-export([install/2]).
-export([deep_decode/1, decode_to_map/2]).

Expand Down Expand Up @@ -734,7 +736,7 @@ decode_to_map(Term, LuaSt) ->
[] ->
#{};
L when is_list(L) ->
logger:warning(#{
?LOG_WARNING(#{
msg => ~"asobi_lua decode_to_map: expected map, got list — coercing to #{}",
length => length(L)
}),
Expand Down
20 changes: 11 additions & 9 deletions src/lua/asobi_lua_match.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function vote_resolved(template, result, state) -- return updated state

-behaviour(asobi_match).

-include_lib("kernel/include/logger.hrl").

-export([init/1, join/2, leave/2, handle_input/3, tick/1, get_state/2]).
-export([vote_requested/1, vote_resolved/3]).

Expand All @@ -57,7 +59,7 @@ init(Config) ->
P when is_binary(P); is_list(P) ->
P;
undefined ->
logger:error(#{msg => ~"asobi_lua_match init: missing lua_script", config => Config}),
?LOG_ERROR(#{msg => ~"asobi_lua_match init: missing lua_script", config => Config}),
erlang:error({missing_lua_script, Config})
end,
GameConfig = maps:get(game_config, Config, #{}),
Expand All @@ -80,21 +82,21 @@ init(Config) ->
{ok, [], _} ->
%% asobi_match:init/1 doesn't allow an error return; log and
%% crash so the supervisor handles it with full context.
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_match init: lua init() returned no value",
script => ScriptPath
}),
erlang:error({lua_error, ~"init() must return a table"});
{error, Reason} ->
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_match init: lua init() failed",
script => ScriptPath,
reason => Reason
}),
erlang:error({lua_init_failed, Reason})
end;
{error, Reason} ->
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_match init: lua_loader:new/1 failed",
script => ScriptPath,
reason => Reason
Expand All @@ -108,7 +110,7 @@ join(PlayerId, #{lua_state := LuaSt, game_state := GS} = State) ->
{ok, [GS1 | _], LuaSt1} ->
{ok, State#{lua_state => LuaSt1, game_state => GS1}};
{error, Reason} ->
logger:warning(#{msg => ~"lua join error", player_id => PlayerId, reason => Reason}),
?LOG_WARNING(#{msg => ~"lua join error", player_id => PlayerId, reason => Reason}),
{error, Reason}
end.

Expand All @@ -118,7 +120,7 @@ leave(PlayerId, #{lua_state := LuaSt, game_state := GS} = State) ->
{ok, [GS1 | _], LuaSt1} ->
{ok, State#{lua_state => LuaSt1, game_state => GS1}};
{error, Reason} ->
logger:warning(#{msg => ~"lua leave error", player_id => PlayerId, reason => Reason}),
?LOG_WARNING(#{msg => ~"lua leave error", player_id => PlayerId, reason => Reason}),
{ok, State}
end.

Expand All @@ -131,7 +133,7 @@ handle_input(PlayerId, Input, #{lua_state := LuaSt, game_state := GS} = State) -
{ok, [GS1 | _], LuaSt2} ->
{ok, State#{lua_state => LuaSt2, game_state => GS1}};
{error, Reason} ->
logger:warning(#{
?LOG_WARNING(#{
msg => ~"lua input error", player_id => PlayerId, reason => Reason
}),
{ok, State}
Expand All @@ -149,10 +151,10 @@ tick(State0) ->
{ok, State#{lua_state => LuaSt1, game_state => GS1}}
end;
{error, timeout} ->
logger:error(#{msg => ~"lua tick timeout", script => maps:get(script, State)}),
?LOG_ERROR(#{msg => ~"lua tick timeout", script => maps:get(script, State)}),
{ok, State};
{error, Reason} ->
logger:error(#{msg => ~"lua tick error", reason => Reason}),
?LOG_ERROR(#{msg => ~"lua tick error", reason => Reason}),
{ok, State}
end.

Expand Down
6 changes: 4 additions & 2 deletions src/lua/asobi_lua_reload.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ per-tick stat overhead can set `asobi_lua.reload_mode` (or the
changes are container restarts. The per-tick `stat()` is skipped.
""".

-include_lib("kernel/include/logger.hrl").

-export([maybe_hot_reload/1]).

%% Reload runs script-author code under a wall-clock budget. A `while true do
Expand All @@ -78,12 +80,12 @@ do_maybe_reload(#{script := Path, script_mtime := OldMtime, lua_state := LuaSt}
NewMtime ->
case reload_script(Path, LuaSt) of
{ok, NewLuaSt} ->
logger:notice(#{
?LOG_NOTICE(#{
msg => ~"lua hot reload", script => Path, mtime => NewMtime
}),
State#{lua_state => NewLuaSt, script_mtime => NewMtime};
{error, Reason} ->
logger:warning(#{
?LOG_WARNING(#{
msg => ~"lua hot reload failed",
script => Path,
reason => Reason
Expand Down
18 changes: 10 additions & 8 deletions src/lua/asobi_lua_world.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function on_zone_unloaded(cx, cy, state) -- return state

-behaviour(asobi_world).

-include_lib("kernel/include/logger.hrl").

-export([init/1, join/2, leave/2, spawn_position/2]).
-export([zone_tick/2, handle_input/3, post_tick/2]).
-export([generate_world/2, get_state/2]).
Expand Down Expand Up @@ -59,7 +61,7 @@ init(Config) ->
P when is_binary(P); is_list(P) ->
P;
undefined ->
logger:error(#{msg => ~"asobi_lua_world init: missing lua_script", config => Config}),
?LOG_ERROR(#{msg => ~"asobi_lua_world init: missing lua_script", config => Config}),
erlang:error({missing_lua_script, Config})
end,
GameConfig = maps:get(game_config, Config, #{}),
Expand All @@ -76,21 +78,21 @@ init(Config) ->
script_mtime => filelib:last_modified(ScriptPath)
}};
{ok, [], _} ->
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_world init: lua init() returned no value",
script => ScriptPath
}),
erlang:error({lua_error, ~"init() must return a table"});
{error, Reason} ->
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_world init: lua init() failed",
script => ScriptPath,
reason => Reason
}),
erlang:error({lua_init_failed, Reason})
end;
{error, Reason} ->
logger:error(#{
?LOG_ERROR(#{
msg => ~"asobi_lua_world init: lua_loader:new/1 failed",
script => ScriptPath,
reason => Reason
Expand Down Expand Up @@ -251,7 +253,7 @@ generate_world(Seed, Config) when is_map(Config) ->
{ok, ZoneStates} = generate_world(Seed, #{lua_state => LuaSt}),
{ok, inject_per_zone_lua(ZoneStates, ScriptPath, PreInstall)};
{error, Reason} ->
logger:error(#{
?LOG_ERROR(#{
msg =>
~"asobi_lua_world generate_world: lua_loader:new failed; world will spawn with empty zones",
script => ScriptPath,
Expand Down Expand Up @@ -425,7 +427,7 @@ decode_terrain_provider(Result, LuaSt) ->
end,
{Mod, ProvArgs};
not_allowed ->
logger:warning(#{
?LOG_WARNING(#{
msg => ~"terrain_provider_not_allowed",
requested => ModBin
}),
Expand Down Expand Up @@ -471,7 +473,7 @@ log_lua_error(Callback, Reason, StateOrZoneState) ->
timeout -> ~"timeout";
_ -> ~"error"
end,
logger:warning(#{
?LOG_WARNING(#{
msg => ~"lua callback failed",
callback => Callback,
severity => Severity,
Expand Down Expand Up @@ -559,7 +561,7 @@ decode_phases(PhasesRef, LuaSt) ->
%% type helps the developer notice it; without this, decode silently
%% returned [], the world server treated it as "no phases", and the
%% mismatch only surfaced as runtime weirdness much later.
logger:warning(#{
?LOG_WARNING(#{
msg => ~"asobi_lua_world: phases() returned non-list, ignoring",
got_type => type_of(Other)
}),
Expand Down
Loading