32:e528c326cf10
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 15:09:55 +0900
Make rps_nickserv a gen_server.

next change 33:654a2f3e9ccf
previous change 31:72639365f60a

src/rps_bots.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps_bots).
-import(rps3, [get_rules/0]).
-export([fool/0, copycat/0, gambler/0]).
fool() ->
rps_nickserv:nick(self(), "fool"),
lobby ! {join, self()},
fool(joined).
fool(joined) ->
receive
{room, Room} ->
random:seed(now()),
fool(Room)
end;
fool(Room) ->
receive
start ->
Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
Room ! {Hand, self()},
fool(Room);
{winner, _} ->
fool(Room)
end.
copycat() ->
rps_nickserv:nick(self(), "copycat"),
lobby ! {join, self()},
copycat(joined).
copycat(joined) ->
receive
{room, Room} ->
copycat(Room, hd(get_rules()))
end.
copycat(Room, Hand) ->
receive
start ->
Room ! {Hand, self()},
copycat(Room, Hand);
{winner, {NewHand, _}} ->
copycat(Room, NewHand)
end.
gambler() ->
rps_nickserv:nick(self(), "gambler"),
lobby ! {join, self()},
gambler(joined).
gambler(joined) ->
receive
{room, Room} ->
Wins = dict:from_list([{C, 0} || C <- get_rules()]),
gambler(Room, Wins)
end.
gambler(Room, Wins) ->
receive
start ->
F = fun({_, A}, {_, B}) -> A =< B end,
{Hand, _} = hd(lists:sort(F, dict:to_list(Wins))),
Room ! {Hand, self()},
gambler(Room, Wins);
{winner, {Hand, _}} ->
gambler(Room, dict:update_counter(Hand, 1, Wins))
end.