31:72639365f60a
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 22:41:13 +0900
Dedicated service: rps_nickserv.

next change 32:e528c326cf10
previous change 23:af20828828e5

src/rps.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps).
-export([
go/0,
lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
go() ->
register(nickserv, spawn_link(rps_nickserv, nickserv, [])),
register(lobby, spawn_link(?MODULE, lobby, [])),
register(counter, spawn_link(?MODULE, counter, [])),
register(printer, spawn_link(?MODULE, printer, [])),
[spawn_link(rps_bots, Bot, []) || Bot <- ?BOTS].
lobby() ->
lobby([]).
lobby(Players) when length(Players) =:= length(?BOTS) ->
Room = spawn_link(rps_room, room, [Players]),
lists:foreach(fun(Player) -> Player ! {room, Room} end, Players),
lobby([]);
lobby(Players) ->
receive
{join, Who} ->
lobby([Who|Players])
end.
counter() ->
counter(0, 0, dict:new(), dict:new()).
counter(Plays, Draws, Wins, Hands) ->
receive
{draw, _} ->
counter(Plays + 1, Draws + 1, Wins, Hands);
{winner, {Hand, Winner}} ->
counter(Plays + 1, Draws,
dict:update_counter(Winner, 1, Wins),
dict:update_counter(Hand, 1, Hands));
{sendstats, Pid} ->
Pid ! {stats, {
plays, Plays,
draws, Draws,
wins, dict:to_list(Wins),
hands, dict:to_list(Hands)}},
counter(Plays, Draws, Wins, Hands)
end.
printer() ->
receive
{stats, Stats} ->
io:format("Stats: ~w.~n", [Stats])
after 1000 ->
counter ! {sendstats, self()}
end,
printer().