Skip to content

Instantly share code, notes, and snippets.

@hukl
Last active December 10, 2015 11:13
Show Gist options
  • Save hukl/57372bebd66cb37cd5d4 to your computer and use it in GitHub Desktop.
Save hukl/57372bebd66cb37cd5d4 to your computer and use it in GitHub Desktop.
Blog post example for truth table in erlang
% Day 14 and after last match of season: 100
% State Resetted: 010
% Finished processing StateLevel > MaxLevel: 001
% Flag | Current | Is State | Finished | Meaning
% | Season Over? | Reset? | Processing? |
% -----|--------------|----------|-------------|------------------------------------------
% 100 | 1 | 0 | 0 | resume progress current season
% 101 | 1 | 0 | 1 | noop day 14
% 110 | 1 | 1 | 0 | start progression
% 111 | 1 | 1 | 1 | impossible
% 000 | 0 | 0 | 0 | broken and unfinished
% 001 | 0 | 0 | 1 | reset state
% 010 | 0 | 1 | 0 | noop mid season
% 011 | 0 | 1 | 1 | impossible
-define(CURRENT_SEASON_OVER, 4).
-define(NOT_CURRENT_SEASON_OVER, 0).
-define(STATE_RESET, 2).
-define(STATE_NOT_RESET, 0).
-define(FINISHED, 1).
-define(NOT_FINISHED, 0).
% Processing the season did not finish (crash etc)
% and we resume without resetting state or view
-define(RESUME_PROCESSING, 4).
% On day fourteen, processing was successful,
% noop until day 1 of next season
-define(NOOP_DAY_14, 5).
% Start processing previous season
-define(START_PROCESSING, 6).
% We're in the next season but processing
% the previous season did not finish
-define(BROKEN_STATE, 0).
% We're in the next season, processing of
% prev season finished, reset the state
-define(RESET_STATE, 1).
% We're in day 1…13, everything processed
% smoothly, state table is already RESET
-define(NOOP_MID_SEASON, 2).
is_current_season_over() ->
DayOfTheSeason = fc_season:day_of_season(),
IsSeasonOver = fc_season:is_season_over(),
case (14 == DayOfTheSeason) and IsSeasonOver of
true -> ?CURRENT_SEASON_OVER;
_ -> ?NOT_CURRENT_SEASON_OVER
end.
is_finished_processing() ->
MaxLevel = fc_league_server:get_max_level(),
{ok, StateLevel, _} = fc_league_state:get_state(),
case StateLevel > MaxLevel of
true -> ?FINISHED;
_ -> ?NOT_FINISHED
end.
is_state_reset() ->
State = fc_league_state:get_state(),
case State of
{ok, 0, _} -> ?STATE_RESET;
_ -> ?STATE_NOT_RESET
end.
check_for_ended_seasons() ->
Result = is_current_season_over() bor is_finished_processing() bor is_state_reset(),
case Result of
?NOOP_MID_SEASON -> noop;
?NOOP_DAY_14 -> noop;
?START_PROCESSING -> start_processing();
?RESUME_PROCESSING -> resume_processing();
?RESET_STATE -> fc_league_state:reset_state();
Unexpected -> throw({broken_state, Unexpected})
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment