Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Commit

Permalink
Teams stored in database
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando "Brujo" Benavides committed Dec 15, 2011
1 parent fcb23bf commit 7e0d1fe
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 255 deletions.
Binary file added dump.rdb
Binary file not shown.
6 changes: 3 additions & 3 deletions include/match_stream.hrl
Expand Up @@ -8,13 +8,13 @@
-record(match_stream_user, {user_id :: match_stream:user_id(),
visit_count = 1 :: pos_integer()}).

-record(match_stream_team, {id :: match_stream:team_id(),
-record(match_stream_team, {team_id :: match_stream:team_id(),
name :: binary(),
division :: match_stream:division()}).

-record(match_stream_match, {match_id :: match_stream:match_id(),
home :: match_stream:team_id(),
visit :: match_stream:team_id(),
home :: match_stream:team(),
visit :: match_stream:team(),
start_time :: match_stream:date(),
home_players :: [match_stream:player()],
visit_players :: [match_stream:player()],
Expand Down
93 changes: 0 additions & 93 deletions priv/elp-tig-2011-09-10.config

This file was deleted.

182 changes: 93 additions & 89 deletions priv/gdc-boc-2011-09-10.config
@@ -1,93 +1,97 @@
{home, <<"gdc">>}.
{visit, <<"boc">>}.
{home, [{id, <<"gdc">>},
{name, <<"Godoy Cruz">>},
{division, <<"Primera A">>}]}.
{visit, [{id, <<"boc">>},
{name, <<"Boca Juniors">>},
{division, <<"Primera A">>}]}.
{start_date, {2011,09,10}}.
{stadium, <<"Estadio Mundialista de Mendoza">>}.
{steps, [
{sleep, 3000},
{start, [{home_players,
[{25, <<"Albil">>},
{14, <<"Mercado">>},
{ 2, <<"Desabato">>},
{ 3, <<"Cellay">>},
{27, <<"Dominguez">>},
{11, <<"Veron">>},
{10, <<"Fernandez">>},
{ 5, <<"Sanchez">>},
{19, <<"Gonzalez">>},
{18, <<"Fernandez G.">>},
{ 9, <<"Boselli">>}
]},
{visit_players,
[{12, <<"Garcia">>},
{13, <<"Casteglione">>},
{21, <<"Echeverria">>},
{ 3, <<"Blengio">>},
{15, <<"Diaz">>},
{ 5, <<"Castano">>},
{ 6, <<"Martinez">>},
{11, <<"Leone">>},
{10, <<"Morales">>},
{ 7, <<"Luna">>},
{ 9, <<"Maggiolo">>}
]}
]},
{sleep, 5000},
{goal, [{team, <<"boc">>}, {player, 7}]}, %% 5'
{sleep, 12000},
{foul, [{team, <<"gdc">>}, {player, 25}]},
{penalty, [{team, <<"boc">>}, {player, 6}]}, %% 17'
{sleep, 1000},
{card, [{card, red}, {team, <<"gdc">>}, {player, 25}]}, %% 18'
{sleep, 1000},
{substitution, [{team, <<"gdc">>}, {player_in, {21, <<"Silva">>}}, {player_out, 18}]}, %% 19'
{sleep, 2000},
{card, [{card, yellow}, {team, <<"gdc">>}, {player, 11}]}, %% 21'
{card, [{card, yellow}, {team, <<"gdc">>}, {player, 2}]}, %% 21'
{sleep, 1000},
{goal, [{team, <<"boc">>}, {player, 10}]}, %% 22'
{sleep, 3000},
{goal, [{team, <<"boc">>}, {player, 6}]}, %% 25'
{sleep, 10000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{save, [{team, <<"gdc">>}, {player, 1}]},
{sleep, 1000},
{halftime, []}, %% 45'
{sleep, 15000},
{continue, []}, %% 60'
{sleep, 15000},
{goal, [{team, <<"gdc">>}, {player, 7}, {player_team, <<"boc">>}]}, %% 75'
{sleep, 4000},
{substitution, [{team, <<"gdc">>}, {player_in, {22, <<"Galvan">>}}, {player_out, 19}]}, %% 79'
{sleep, 3000},
{card, [{card, yellow}, {team, <<"boc">>}, {player, 3}]}, %% 82'
{sleep, 1000},
{substitution, [{team, <<"boc">>}, {player_in, {22, <<"Pio">>}}, {player_out, 6}]}, %% 83'
{sleep, 1000},
{substitution, [{team, <<"gdc">>}, {player_in, {23, <<"Benitez">>}}, {player_out, 11}]}, %% 84'
{sleep, 4000},
{substitution, [{team, <<"boc">>}, {player_in, {23, <<"Rodales">>}}, {player_out, 15}]}, %% 88'
{sleep, 5000},
{substitution, [{team, <<"boc">>}, {player_in, {24, <<"Carrasco">>}}, {player_out, 9}]}, %% 93'
{sleep, 1000},
{card, [{card, yellow}, {team, <<"boc">>}, {player, 10}]}, %% 94'
{sleep, 16000},
{stop, []}
{sleep, 3000},
{start, [{home_players,
[{25, <<"Albil">>},
{14, <<"Mercado">>},
{ 2, <<"Desabato">>},
{ 3, <<"Cellay">>},
{27, <<"Dominguez">>},
{11, <<"Veron">>},
{10, <<"Fernandez">>},
{ 5, <<"Sanchez">>},
{19, <<"Gonzalez">>},
{18, <<"Fernandez G.">>},
{ 9, <<"Boselli">>}
]},
{visit_players,
[{12, <<"Garcia">>},
{13, <<"Casteglione">>},
{21, <<"Echeverria">>},
{ 3, <<"Blengio">>},
{15, <<"Diaz">>},
{ 5, <<"Castano">>},
{ 6, <<"Martinez">>},
{11, <<"Leone">>},
{10, <<"Morales">>},
{ 7, <<"Luna">>},
{ 9, <<"Maggiolo">>}
]}
]},
{sleep, 5000},
{goal, [{team, <<"boc">>}, {player, 7}]}, %% 5'
{sleep, 12000},
{foul, [{team, <<"gdc">>}, {player, 25}]},
{penalty, [{team, <<"boc">>}, {player, 6}]}, %% 17'
{sleep, 1000},
{card, [{card, red}, {team, <<"gdc">>}, {player, 25}]}, %% 18'
{sleep, 1000},
{substitution, [{team, <<"gdc">>}, {player_in, {21, <<"Silva">>}}, {player_out, 18}]}, %% 19'
{sleep, 2000},
{card, [{card, yellow}, {team, <<"gdc">>}, {player, 11}]}, %% 21'
{card, [{card, yellow}, {team, <<"gdc">>}, {player, 2}]}, %% 21'
{sleep, 1000},
{goal, [{team, <<"boc">>}, {player, 10}]}, %% 22'
{sleep, 3000},
{goal, [{team, <<"boc">>}, {player, 6}]}, %% 25'
{sleep, 10000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 5}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 3}]}, %% 35'
{sleep, 1000},
{shot, [{team, <<"boc">>}, {player, 9}]}, %% 35'
{save, [{team, <<"gdc">>}, {player, 1}]},
{sleep, 1000},
{halftime, []}, %% 45'
{sleep, 15000},
{continue, []}, %% 60'
{sleep, 15000},
{goal, [{team, <<"gdc">>}, {player, 7}, {player_team, <<"boc">>}]}, %% 75'
{sleep, 4000},
{substitution, [{team, <<"gdc">>}, {player_in, {22, <<"Galvan">>}}, {player_out, 19}]}, %% 79'
{sleep, 3000},
{card, [{card, yellow}, {team, <<"boc">>}, {player, 3}]}, %% 82'
{sleep, 1000},
{substitution, [{team, <<"boc">>}, {player_in, {22, <<"Pio">>}}, {player_out, 6}]}, %% 83'
{sleep, 1000},
{substitution, [{team, <<"gdc">>}, {player_in, {23, <<"Benitez">>}}, {player_out, 11}]}, %% 84'
{sleep, 4000},
{substitution, [{team, <<"boc">>}, {player_in, {23, <<"Rodales">>}}, {player_out, 15}]}, %% 88'
{sleep, 5000},
{substitution, [{team, <<"boc">>}, {player_in, {24, <<"Carrasco">>}}, {player_out, 9}]}, %% 93'
{sleep, 1000},
{card, [{card, yellow}, {team, <<"boc">>}, {player, 10}]}, %% 94'
{sleep, 16000},
{stop, []}
]}.
53 changes: 36 additions & 17 deletions src/match_stream.erl
Expand Up @@ -28,7 +28,7 @@
-export([start/0, stop/0]).
-export([start/2, stop/1]).
-export([new_match/4, cancel_match/3, register_event/5, cancel_match/1, register_event/3,
matches/0, history/1, history/3, match/1]).
matches/0, history/1, history/3, match/1, set_team/1, team/1]).
-export([timestamp/0]).

-type date() :: {2000..3000, 1..12, 1..31}.
Expand All @@ -49,10 +49,10 @@
-type user() :: #match_stream_user{}.
-type period() :: not_started | first | halftime | last | penalties | ended.

-type data() :: {home, team_id()} |
-type data() :: {home, team()} |
{home_players, [player()]} |
{home_score, non_neg_integer()} |
{visit, team_id()} |
{visit, team()} |
{visit_players, [player()]} |
{visit_score, non_neg_integer()} |
{period, period()} |
Expand Down Expand Up @@ -87,21 +87,40 @@ stop() -> application:stop(?MODULE).
%%-------------------------------------------------------------------
%% SERVER API
%%-------------------------------------------------------------------
%% @doc Registers a team.
%% It's created if it doesn't exist, updated otherwise.
-spec set_team(team()) -> ok.
set_team(Team) ->
case match_stream_db:team(Team#match_stream_team.team_id) of
not_found -> match_stream_db:create(Team);
_Team -> match_stream_db:team_update(Team#match_stream_team.team_id, fun(_) -> Team end)
end.

%% @doc Returns a team.
-spec team(team_id()) -> not_found | team().
team(TeamId) -> match_stream_db:team(TeamId).

%% @doc Registers a match.
%% StartDate is expected to be an UTC datetime
-spec new_match(team_id(), team_id(), date(), binary()) -> {ok, match_id()} | {error, {duplicated, match_id()}}.
new_match(Home, Visit, StartDate, Stadium) ->
MatchId = build_id(Home, Visit, StartDate),
try match_stream_db:create(
#match_stream_match{match_id = MatchId,
home = Home,
visit = Visit,
stadium = Stadium,
start_time= StartDate}) of
ok -> {ok, MatchId}
catch
_:duplicated ->
{error, {duplicated, MatchId}}
-spec new_match(team_id(), team_id(), date(), binary()) -> {ok, match_id()} | {error, {duplicated, match_id()} | {invalid_team, team_id()}}.
new_match(HomeId, VisitId, StartDate, Stadium) ->
case {match_stream_db:team(HomeId), match_stream_db:team(VisitId)} of
{not_found, _} -> {error, {invalid_team, HomeId}};
{_, not_found} -> {error, {invalid_team, VisitId}};
{Home, Visit} ->
MatchId = build_id(HomeId, VisitId, StartDate),
case match_stream_db:match(MatchId) of
not_found ->
ok = match_stream_db:create(
#match_stream_match{match_id = MatchId,
home = Home,
visit = Visit,
stadium = Stadium,
start_time= StartDate}),
{ok, MatchId};
_Match ->
{error, {duplicated, MatchId}}
end
end.

%% @doc Cancels a match
Expand Down Expand Up @@ -134,7 +153,7 @@ register_event(MatchId, Kind, Data) ->
matches() ->
match_stream_db:match_all().

%% @doc List of available matches
%% @doc Returns a match
-spec match(match_id()) -> not_found | match_stream:match().
match(MatchId) ->
match_stream_db:match(MatchId).
Expand Down
4 changes: 4 additions & 0 deletions src/match_stream_client.erl
Expand Up @@ -122,6 +122,10 @@ to_json(#match_stream_event{timestamp = TS, kind = Kind, data = Data}) ->
[{<<"timestamp">>, TS},
{<<"kind">>, atom_to_binary(Kind, utf8)} |
lists:map(fun to_json/1, Data)];
to_json({Key, Team}) when is_record(Team, match_stream_team) ->
{atom_to_binary(Key, utf8), to_json(Team)};
to_json(#match_stream_team{team_id = I, name = N, division = D}) ->
[{<<"team_id">>, I}, {<<"name">>, N}, {<<"division">>, D}];
to_json({Key, Players}) when Key == home_players;
Key == visit_players ->
case Players of
Expand Down

0 comments on commit 7e0d1fe

Please sign in to comment.