Browse files

Send table_view event to new person at table.

  • Loading branch information...
1 parent 26ff768 commit 6cd5bcdfc2c9a9e15546b6377d46a3dad5ecfdd3 @KenMacD committed Jan 22, 2012
View
7 api/tarabish.thrift
@@ -2,7 +2,7 @@
namespace py tarabish.thrift
-const i32 PROTOCOL_VERSION = 2
+const i32 PROTOCOL_VERSION = 3
exception InvalidOperation {
1: string why
@@ -67,6 +67,7 @@ enum BetterType {
# Thrift doesn't allow different types, so only some fields populated.
enum EventType {
+ TABLEVIEW, # table, table_view
JOIN, # table, name
PART, # table, name
SIT, # table, name, seat
@@ -124,6 +125,8 @@ struct Event {
15: byte high_value,
16: bool is_trump,
17: byte other_seat,
+
+ 18: TableView table_view,
}
service Tarabish
@@ -146,7 +149,7 @@ service Tarabish
list<TableView> getTables(1: i64 client_id)
throws (1:InvalidOperation invalid)
- TableView sit(1: i64 client_id, 2: i32 table_id, 3: byte seat)
+ void sit(1: i64 client_id, 2: i32 table_id, 3: byte seat)
throws (1:InvalidOperation invalid)
void stand(1: i64 client_id, 2: i32 table_id)
View
2 client/qt/src/tarabish/qt/connection.py
@@ -13,7 +13,7 @@
from PySide.QtCore import (QObject, QThread, QTimer, Signal)
-CLIENT_PROTO_VERSION = 2
+CLIENT_PROTO_VERSION = 3
class ServerEvents(QThread):
eventSignal = Signal(Event)
View
6 client/qt/src/tarabish/qt/lobby.py
@@ -200,9 +200,9 @@ def handleSit(self, tableSeatCell):
seat_num = tableSeatCell.seat
try:
- table_view = self.server.sit(table_id, seat_num)
- table = Table(table_id, seat_num, table_view, self.server,
- self.logger, self.resource_path)
+ self.server.sit(table_id, seat_num)
+ table = Table(table_id, seat_num, self.server, self.logger,
+ self.resource_path)
self.tables.append(table)
table.show()
self.tablesTable.updating()
View
14 client/qt/src/tarabish/qt/table.py
@@ -376,7 +376,7 @@ def get_seat_display(self, seat):
return self.seats[self._seat_to_position(seat)]
# TODO: logger no longer needed?
- def __init__(self, table_id, seat_num, table_view, server, logger,
+ def __init__(self, table_id, seat_num, server, logger,
resource_path, parent=None):
super(Table, self).__init__(parent)
@@ -397,9 +397,6 @@ def __init__(self, table_id, seat_num, table_view, server, logger,
self.seats.append(
self.SeatDisplay(Qt.AlignRight | Qt.AlignVCenter, 1, 0))
- for (num, seat) in enumerate(table_view.seats):
- self.get_seat_display(num).update(seat)
-
self.start_game_button = QPushButton("Start Game")
self.start_game_button.setEnabled(self.is_full())
self.start_game_button.clicked.connect(self._start_game)
@@ -444,6 +441,8 @@ def __init__(self, table_id, seat_num, table_view, server, logger,
main.setLayout(vbox)
self.setCentralWidget(main)
+ server.eventDispatcher.connect(EventType.TABLEVIEW,
+ self.handle_tableview_event, table_id)
server.eventDispatcher.connect(EventType.SIT, self.handle_sit_event, table_id)
server.eventDispatcher.connect(EventType.STAND, self.handle_stand_event, table_id)
server.eventDispatcher.connect(EventType.DEAL, self.handle_deal,
@@ -533,6 +532,13 @@ def play_bella(self):
except InvalidOperation as exc:
self.logger.append("Can not play bella because: %s" % (str(exc),))
+ def handle_tableview_event(self, table_view):
+ self.logger.append("Table update event")
+ for (num, seat) in enumerate(table_view.seats):
+ self.get_seat_display(num).update(seat)
+ # TODO: handle already started, esp when join with sit enabled
+ self.start_game_button.setEnabled(self.is_full())
+
def handle_sit_event(self, name, table, seat):
self.logger.append("User %s sat" % (name,))
self.get_seat_display(seat).sit(name)
View
4 server/src/client.erl
@@ -159,10 +159,10 @@ handle_call({sit, TableId, Seat}, _From, State) ->
case tarabish_server:get_table(TableId) of
{ok, Table} ->
case table:sit(Table, State#state.id, self(), Seat) of
- {ok, TableView} ->
+ ok ->
% If we were already watching the table is should overwrite
NewTables = orddict:store(TableId, Table, State#state.tables),
- {reply, {ok, TableView}, State#state{tables=NewTables}};
+ {reply, ok, State#state{tables=NewTables}};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
View
15 server/src/table.erl
@@ -91,6 +91,8 @@ handle_call({join, ClientName, Client}, _From, State) ->
Observers = [ClientName|State#state.observers],
NewState = State#state{members=NewMembers, observers=Observers},
update_server(NewState),
+ send_event_one(#event{type=?tarabish_EventType_TABLEVIEW,
+ table_view=make_table_view(NewState)}, NewState, Client),
{reply, ok, NewState}
end;
@@ -131,14 +133,13 @@ handle_call({sit, ClientName, Client, SeatNum}, _From, State)
seat=SeatNum},
case orddict:find(ClientName, State#state.members) of
{ok, #person{seat=none} = Person} ->
-
send_event_all(Event, State),
NewPerson = Person#person{seat=SeatNum},
NewMembers = orddict:store(ClientName, NewPerson, State#state.members),
NewObservers = lists:delete(ClientName, State#state.observers),
NewState = State#state{members=NewMembers, seats=NewSeats, observers=NewObservers},
update_server(NewState),
- {reply, {ok, make_table_view(NewState)}, NewState};
+ {reply, ok, NewState};
{ok, _Person} ->
{reply, {error, already_seated}, State};
error -> % Not at table, join
@@ -147,7 +148,11 @@ handle_call({sit, ClientName, Client, SeatNum}, _From, State)
NewMembers = orddict:store(ClientName, NewPerson, State#state.members),
NewState = State#state{members=NewMembers, seats=NewSeats},
update_server(NewState),
- {reply, {ok, make_table_view(NewState)}, NewState}
+
+ % Send a new client a table view
+ send_event_one(#event{type=?tarabish_EventType_TABLEVIEW,
+ table_view=make_table_view(NewState)}, NewState, Client),
+ {reply, ok, NewState}
end;
true ->
{reply, {error, seat_taken}, State}
@@ -329,6 +334,10 @@ cancel_game(#state{game=Game} = State) ->
send_event_all(Event, State) ->
send_event(State#state.id, Event, State#state.members).
+send_event_one(Event, State, Client) ->
+ TableEvent = Event#event{table=State#state.id},
+ client:recv_event(Client, TableEvent).
+
send_event(TableId, Event, MemberDict) ->
{_Ids, Members} = lists:unzip(orddict:to_list(MemberDict)),
TableEvent = Event#event{table=TableId},
View
4 server/src/web.erl
@@ -81,14 +81,14 @@ handle_request(Req, "/sit/", Client) ->
{TableIdNum, []} = string:to_integer(TableId),
SeatId = get_parameter("seat", QS),
{SeatIdNum, []} = string:to_integer(SeatId),
- TableView = client:sit(Client, TableIdNum, SeatIdNum), % TODO: handle error
+ ok = client:sit(Client, TableIdNum, SeatIdNum), % TODO: handle error
Location = ["/table/", TableId, "/", SeatId, "/"],
Req:respond({302, [{"Location", lists:concat(Location)}], <<>>});
handle_request(Req, Other, Client) ->
handle_token_request(Req, Client, string:tokens(Other, "/")).
-handle_token_request(Req, Client, ["table", TableId, SeatId]) ->
+handle_token_request(Req, _Client, ["table", TableId, SeatId]) ->
io:format("Start of table ~w seat ~w~n", [TableId, SeatId]),
Req:serve_file("table.html", "docroot").

0 comments on commit 6cd5bcd

Please sign in to comment.