Skip to content

Commit

Permalink
Merge pull request witeman#4 from ClarenceAu/master
Browse files Browse the repository at this point in the history
merge the branch
  • Loading branch information
witeman committed Mar 5, 2012
2 parents f55202b + 453f18a commit 654ba71
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 53 deletions.
8 changes: 6 additions & 2 deletions include/common.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
trace
}).

-define(PLAYER_TIMEOUT, 15000).
-define(SLOW_PLAYER_TIMEOUT, 18000).
-define(FAST_PLAYER_TIMEOUT, 9000).
-define(START_DELAY, 14000).

%%% Error codes
Expand All @@ -40,6 +41,7 @@

%%% Game stage

-define(GS_CANCEL, 254).
-define(GS_PREFLOP, 0).
-define(GS_FLOP, 1).
-define(GS_TURN, 2).
Expand All @@ -62,7 +64,9 @@
-record(limit, {
type,
low,
high
high,
min,
max
}).

%%% Query operator
Expand Down
3 changes: 2 additions & 1 deletion include/game.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
limit,
low,
high,
min, max,
ante,
%% card deck
deck,
Expand All @@ -38,7 +39,7 @@
observers = [],
%% time given to players
%% to make a move
timeout = ?PLAYER_TIMEOUT,
timeout = ?SLOW_PLAYER_TIMEOUT,
%% number of raises so far
raise_count = 0,
%% players required to start a game
Expand Down
37 changes: 33 additions & 4 deletions include/pp.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
game_type,
limit_type,
expected, % query op
joined, % query op
waiting
min,
timeout
}).

-define(CMD_SEAT_QUERY, 14).
Expand Down Expand Up @@ -135,7 +135,7 @@
seat_count,
required = 2,
start_delay = ?START_DELAY,
player_timeout = ?PLAYER_TIMEOUT,
player_timeout = ?SLOW_PLAYER_TIMEOUT,
rigged_deck = [],
barrier
}).
Expand All @@ -150,7 +150,8 @@
seat_count,
required,
joined,
waiting
waiting,
timeout
}).

-define(CMD_PLAYER_INFO, 19).
Expand Down Expand Up @@ -444,3 +445,31 @@
nick,
pass
}).

-define(NOTIFY_GAME_DETAIL, 55).

-record(notify_game_detail, {
game,
pot,
players,
seats,
stage,
min, max,
low, high
}).

-define(NOTIFY_SEAT_DETAIL, 56).

-record(notify_seat_detail, {
game,
seat,
state,
player,
inplay,
nick
}).

-define(NOTIFY_UNWATCH, 57).
-record(notify_unwatch, {
game
}).
32 changes: 28 additions & 4 deletions src/exch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

-include_lib("eunit/include/eunit.hrl").

-include("common.hrl").
-include("test.hrl").
-include("pp.hrl").

-record(exch, {
parent,
Expand All @@ -53,7 +55,8 @@ behaviour_info(callbacks) ->
{start, 1},
{stop, 1},
{call, 3},
{dispatch, 2}].
{dispatch, 2},
{cast, 3}].

%%%
%%% API
Expand Down Expand Up @@ -128,13 +131,34 @@ process_call(Event, Exch) ->
Cbk:call(Event, Exch#exch.data).

process_cast(Event, Exch) ->
%io:format("EVENT: ~p~n", [Event]),
{Mod, _} = hd(Exch#exch.stack),
State = Exch#exch.state,
Data = Exch#exch.data,
Ctx = Exch#exch.ctx,
Result = Mod:State(Data, Ctx, Event),
advance(Exch, Event, Result).

if
is_record(Event, watch) ->
io:format("EVENT: ~p~n", [Event]);
true ->
ok
end,
% Result = Mod:State(Data, Ctx, Event),
% advance(Exch, Event, Result).
%io:format("STATE: ~p~nMOD: ~p~nEVENT: ~p~n", [State, Mod, Event]),
case Cbk:cast(Event, Ctx, Data) of
skip ->
Result = Mod:State(Data, Ctx, Event),
case Result of
{stop, _, _} ->
io:format("STATE: ~p~nMOD: ~p~nEVENT: ~p~n", [State, Mod, Event]);
_ -> ok
end,
%io:format("RESULT: ~p~n", [Result]),
advance(Exch, Event, Result);
{NewGame, NewCtx} ->
%io:format("NEWGAME: ~p~nNEWCTX: ~p~n", [NewGame, NewCtx]),
{noreply, Exch#exch{data = NewGame, ctx = NewCtx}}
end.

init(Exch = #exch{ stack = [{Mod, Params}|_] }, Event) ->
Ctx = Exch#exch.ctx,
Expand Down
15 changes: 14 additions & 1 deletion src/game.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-module(game).
-behaviour(exch).

-export([id/0, start/1, stop/1, dispatch/2, call/2]).
-export([id/0, start/1, stop/1, dispatch/2, call/2, cast/3]).

-include_lib("eunit/include/eunit.hrl").

Expand All @@ -48,6 +48,8 @@ start([GID, R = #start_game{}]) ->
end,
low = (R#start_game.limit)#limit.low,
high = (R#start_game.limit)#limit.high,
min = (R#start_game.limit)#limit.min,
max = (R#start_game.limit)#limit.max,
deck = deck:new(R#start_game.rigged_deck),
pot = pot:new(),
seats = g:create_seats(R#start_game.seat_count),
Expand Down Expand Up @@ -112,6 +114,17 @@ call({'INPLAY', Player}, Game) ->
{_, Seat} = g:get_seat(Game, Player),
Seat#seat.inplay.

cast(R = #watch{}, Ctx, Game) ->
io:format("WATCH: ~p~n", [R]),
NewGame = g:watch(Game, Ctx, R),
{NewGame, Ctx};

cast(R = #unwatch{}, Ctx, Game) ->
NewGame = g:unwatch(Game, R),
{NewGame, Ctx};

cast(_, _, _) ->
skip.
%%%
%%% Utility
%%%
Expand Down
3 changes: 3 additions & 0 deletions src/player.erl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ handle_cast(R, Data)
is_record(R, show_cards);
is_record(R, notify_button);
is_record(R, notify_sb);
is_record(R, notify_game_detail);
is_record(R, notify_seat_detail);
is_record(R, notify_unwatch);
is_record(R, notify_bb) ->
forward_to_client(R, Data),
{noreply, Data};
Expand Down
52 changes: 47 additions & 5 deletions src/pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ limit() ->
record(limit, {
limit_type(),
price(),
price(),
price(),
price()
}).

query_op() ->
record(query_op, {
byte(),
byte()
int()
}).

game_to_id(G)
Expand Down Expand Up @@ -428,7 +430,8 @@ game_info() ->
seat_count(),
required_players(),
joined_players(),
waiting_players()
waiting_players(),
player_timeout()
}).

player_info() ->
Expand Down Expand Up @@ -637,6 +640,33 @@ sign_in() ->
pass()
}).

notify_game_detail() ->
record(notify_game_detail, {
game(),
amount(),
seat_count(),
byte(),
stage(),
price(),
price(),
price(),
price()
}).

notify_seat_detail() ->
record(notify_seat_detail, {
game(),
seat(),
state(),
player(),
amount(),
nick()
}).

notify_unwatch() ->
record(notify_unwatch, {
game()
}).
%%% Pickle

write(R) when is_record(R, bad) ->
Expand Down Expand Up @@ -803,7 +833,13 @@ write(R) when is_record(R, pong) ->

%% 新增的注册协议的序列化写函数
write(R) when is_record(R, sign_in) ->
[?CMD_SIGN_IN|pickle(sign_in(), R)].
[?CMD_SIGN_IN|pickle(sign_in(), R)];
write(R) when is_record(R, notify_seat_detail) ->
[?NOTIFY_SEAT_DETAIL | pickle(notify_seat_detail(), R)];
write(R) when is_record(R, notify_game_detail) ->
[?NOTIFY_GAME_DETAIL | pickle(notify_game_detail(), R)];
write(R) when is_record(R, notify_unwatch) ->
[?NOTIFY_UNWATCH | pickle(notify_unwatch(), R)].

%%% Unpickle

Expand Down Expand Up @@ -971,11 +1007,17 @@ read(<<?CMD_PONG, Bin/binary>>) ->

%% 新增的注册协议的读函数
read(<<?CMD_SIGN_IN, Bin/binary>>) ->
unpickle(sign_in(), Bin).
unpickle(sign_in(), Bin);
read(<<?NOTIFY_UNWATCH, Bin/binary>>) ->
unpickle(notify_unwatch(), Bin);
read(<<?NOTIFY_GAME_DETAIL, Bin/binary>>) ->
unpickle(notify_game_detail(), Bin);
read(<<?NOTIFY_SEAT_DETAIL, Bin/binary>>) ->
unpickle(notify_seat_detail(), Bin).

send(Socket, Data, Ping) ->
Bin = list_to_binary(write(Data)),
io:format("SND ~p~n", [Bin]),
io:format("SND ~p~n", [Data]),
case catch gen_tcp:send(Socket, Bin) of
ok ->
ok;
Expand Down
52 changes: 36 additions & 16 deletions src/schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,42 @@ install_counter(Nodes) ->

populate() ->
%% 创建tab_game_config table 的一条记录
g:setup(?GT_IRC_TEXAS, 20,
#limit{ type = ?LT_FIXED_LIMIT, low = 10, high = 20},
?START_DELAY, ?PLAYER_TIMEOUT,
10),
g:setup(?GT_TEXAS_HOLDEM, 10,
#limit{ type = ?LT_FIXED_LIMIT, low = 10, high = 20},
?START_DELAY, ?PLAYER_TIMEOUT,
50),
g:setup(?GT_TEXAS_HOLDEM, 10,
#limit{ type = ?LT_NO_LIMIT, low = 10, high = 20},
?START_DELAY, ?PLAYER_TIMEOUT,
50),
g:setup(?GT_TEXAS_HOLDEM, 10,
#limit{ type = ?LT_POT_LIMIT, low = 10, high = 20},
?START_DELAY, ?PLAYER_TIMEOUT,
50).
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 1, high = 2, min = 20, max = 200},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 1, high = 2, min = 20, max = 200},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 1, high = 2, min = 20, max = 200},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 1, high = 2, min = 20, max = 200},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 2, high = 4, min = 40, max = 400},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 2, high = 4, min = 40, max = 400},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 2, high = 4, min = 40, max = 400},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 2, high = 4, min = 40, max = 400},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 5, high = 10, min = 50, max = 1000},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 9,
#limit{ type = ?LT_FIXED_LIMIT, low = 5, high = 10, min = 50, max = 1000},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 5, high = 10, min = 50, max = 1000},
?START_DELAY, ?SLOW_PLAYER_TIMEOUT, 50),
g:setup(?GT_TEXAS_HOLDEM, 5,
#limit{ type = ?LT_FIXED_LIMIT, low = 5, high = 10, min = 50, max = 1000},
?START_DELAY, ?FAST_PLAYER_TIMEOUT, 50).

reset_counters()->
counter:reset(game),
Expand Down
Loading

0 comments on commit 654ba71

Please sign in to comment.