Permalink
Browse files

Initial game log presentation

  • Loading branch information...
1 parent 8cb94a0 commit 2f0e089908d3fa71dc16021ac6a24807c46529ad @hippich committed May 31, 2011
@@ -1,7 +1,6 @@
package Room::Controller::User::Hand;
use Moose;
use namespace::autoclean;
-use JSON::XS;
BEGIN {extends 'Catalyst::Controller'; }
@@ -17,7 +16,6 @@ Catalyst Controller.
=cut
-our @poker_cards_string = ( '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ax', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As' );
sub base : Chained PathPart('user/hands') CaptureArgs(0) {
my ($self, $c) = @_;
@@ -28,68 +26,41 @@ sub base : Chained PathPart('user/hands') CaptureArgs(0) {
);
}
- use Data::Dumper;
- $c->stash->{hands} = $c->user->hands;
- $c->stash->{dumper} = \&Dumper;
- $c->stash->{parser} = sub {$self->__parse_hands(shift); };
}
=head2 index
=cut
sub index : Chained('base') PathPart('') Args(0) {
- my ( $self, $c ) = @_;
+ my ( $self, $c ) = @_;
+ my $page = $c->req->params->{'page'};
+ $page = 1 if $page < 1;
+
+ $c->stash->{hands} = $c->user->hands->search(undef, {
+ rows => 50,
+ page => $page,
+ order_by => {
+ -desc => 'serial',
+ },
+ });
}
-
sub view_hand : Chained('base') PathPart('') Args(1) {
my ($self, $c, $id) = @_;
+ $c->stash->{hands} = $c->user->hands;
my $hand = $c->stash->{hands}->search({serial => $id})->first;
if (! $hand) {
$c->detach('/default');
}
- my $history_raw = $hand->description;
-
use Data::Dumper;
- $c->stash->{hand} = Dumper($self->__parse_hands($history_raw));
+ $c->stash->{hand} = $hand->get_parsed_history;
+ $c->stash->{hand_dump} = Dumper($hand->get_parsed_history);
}
-sub __parse_hands {
- my ($self, $history) = @_;
-
- $history =~ s/PokerCards\(\[([^\]]*)\]\)/$self->__parse_cards($1)/ge;
- $history =~ s/Decimal\('([^\']+)'\)/$1/g;
-
- $history =~ s/None/null/g;
- $history =~ s/True/1/g;
- $history =~ s/False/0/g;
-
- $history =~ s/(\d+)L/$1/g;
-
- $history =~ s/^\[\(/[[/;
- $history =~ s/\)\]/]]/;
- $history =~ s/\), \(/], [/g;
- $history =~ s/'/"/g;
-
- $history =~ s/(\d+): /"$1": /g;
-
- return decode_json $history;
-}
-
-sub __parse_cards {
- my ($self, $cards_str) = @_;
- my @cards = split /, /, $cards_str;
-
- foreach my $card (@cards) {
- $card = '"'. $poker_cards_string[$card & 0x3F] . '"';
- }
-
- return '['. (join ', ', @cards) .']';
-}
=head1 AUTHOR
@@ -44,6 +44,10 @@ __PACKAGE__->set_primary_key("serial");
# Created by DBIx::Class::Schema::Loader v0.04006 @ 2010-09-27 11:47:31
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1ToKcdFljUDL2gZ5bKoYSg
+use JSON::XS;
+use Switch;
+
+our @poker_cards_string = ( '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ax', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As' );
__PACKAGE__->has_many(
'userhands' => 'Room::Schema::PokerNetwork::Result::User2hand',
@@ -53,6 +57,102 @@ __PACKAGE__->many_to_many(
users => 'userhands', 'user'
);
+sub get_parsed_history {
+ my $self = shift;
+ my $parsed_history;
+ my @players;
+ my %players_by_id;
+
+ my $h = $self->__parse_hands();
+ $parsed_history->{game_history} = $h;
+ $parsed_history->{self} = $self;
+
+ foreach my $uid (@{$h->[0]->[7]}) {
+ my $player = $self->result_source->schema->resultset("Users")->find($uid);
+ push @players, $player;
+ $players_by_id{$player->serial} = $player;
+ }
+ $parsed_history->{players} = \@players;
+ $parsed_history->{players_by_id} = \%players_by_id;
+
+
+ return $parsed_history;
+}
+
+sub __parse_hands {
+ my $self = shift;
+ my $history = $self->description;
+
+ $history =~ s/PokerCards\(\[([^\]]*)\]\)/$self->__parse_cards($1)/ge;
+ $history =~ s/Decimal\('([^\']+)'\)/$1/g;
+
+ $history =~ s/None/null/g;
+ $history =~ s/True/1/g;
+ $history =~ s/False/0/g;
+
+ $history =~ s/(\d+)L/$1/g;
+
+ $history =~ s/^\[\(/[[/;
+ $history =~ s/\)\]/]]/;
+ $history =~ s/\), \(/], [/g;
+ $history =~ s/'/"/g;
+
+ $history =~ s/(\d+): /"$1": /g;
+
+ return decode_json $history;
+}
+
+sub __parse_cards {
+ my ($self, $cards_str) = @_;
+ my @cards = split /, /, $cards_str;
+
+ foreach my $card (@cards) {
+ $card = '"'. $poker_cards_string[$card & 0x3F] . '"';
+ }
+
+ return '['. (join ', ', @cards) .']';
+}
+
+
+sub format_hi_hand {
+ my $self = shift;
+ my $hand = shift;
+
+ if ($hand) {
+ my $best = shift @{$hand};
+ foreach my $card (@{$hand}) {
+ $card = $poker_cards_string[$card & 0x3F];
+ }
+
+ switch ($best) {
+ case 'NoPair' { $best = 'High card'; }
+ case 'TwoPair' { $best = 'Two pairs'; }
+ case 'Trips' { $best = 'Three of a kind'; }
+ case 'Straight' { $best = 'Straight'; }
+ case 'Flush' { $best = 'Flush'; }
+ case 'FlHouse' { $best = 'Full House'; }
+ case 'Quads' { $best = 'Four of a kind'; }
+ case 'StFlush' { $best = 'Straight flush'; }
+ }
+
+ return { best => $best, cards => $hand }
+ }
+ else {
+ return;
+ }
+}
+
+
+sub get_all_players {
+ my $self = shift;
+ my @players;
+ my $users = $self->users;
+ while (my $user = $users->next) {
+ push @players, $user->name;
+ }
+ return \@players;
+}
+
=head1 AUTHOR
Pavel Karoukin
View
@@ -1,3 +1,21 @@
-[% WHILE (hand = hands.next) %]
-[% dumper(parser(hand.description)) %]
+[% META title = 'Games history' %]
+
+<table>
+ <tr><th>Hand #</th><th>Players</th><th>Date</th><th>Action</th></tr>
+ [% WHILE (hand = hands.next) %]
+ <tr>
+ <td>[% hand.serial %]</td>
+ <td>[% hand.get_all_players.join(', ') %]</td>
+ <td>[% hand.created %]</td>
+ <td><a href='[% c.uri_for('/user/hands/' _ hand.serial) %]'>View</a></td>
+ </tr>
+ [% END %]
+</table>
+
+[% IF hands.pager.next_page %]
+<span style="float: right"><a href="[% c.uri_for('/user/hands', undef, { page => hands.pager.next_page}) %]">Next &gt;&gt;</a></span>
+[% END %]
+
+[% IF hands.pager.previous_page %]
+<span style="float: left"><a href="[% c.uri_for('/user/hands', undef, { page => hand.pager.previous_page}) %]">&lt;&lt; Prev</a></span>
[% END %]
@@ -1 +1,116 @@
-[% hand %]
+[% META title = 'Game history' %]
+
+<a href='javascript:history.go(-1);'> &lt;&lt; Back </a>
+<pre>
+[% blind = 'small' -%]
+[% board = '' -%]
+[% FOR a IN hand.game_history -%]
+[% SWITCH a.0 -%]
+[%# -%]
+[%# Game beginning -%]
+[%# -%]
+[% CASE 'game' -%]
+Betco.in Game #[% a.2 %]: [% a.5 %] [% a.6 %] [% hand.self.created %]
+Table 'N/A' 10-max Seat #[% a.8 + 1 %] is the button
+[% FOR player IN hand.players -%]
+[% player_serial = player.serial -%]
+Seat [% loop.count %]: [% player.name %] ($[% a.9.$player_serial / 100 | format('%.2f') %] in chips)
+[% END -%]
+[%# -%]
+[%# Blinds -%]
+[%# -%]
+[% CASE 'blind' -%]
+[% player_id = a.1 -%]
+[% hand.players_by_id.$player_id.name %]: posts [% blind %] blind $[% a.2 / 100 | format('%.2f') %]
+[% blind = 'big' -%]
+[%# -%]
+[%# Rounds -%]
+[%# -%]
+[% CASE ['round'] -%]
+[% SWITCH a.1 -%]
+[% CASE 'pre-flop' -%]
+[% current_player_id = c.user.serial -%]
+*** HOLE CARDS ***
+Dealt to [% hand.players_by_id.$current_player_id.name %] [[% a.3.$current_player_id.join(' ') %]]
+[% CASE 'flop' -%]
+*** FLOP *** [[% a.2.join(' ') %]]
+[% CASE 'turn' -%]
+*** TURN *** [[% a.2.join(' ') %]]
+[% CASE 'river' -%]
+*** RIVER *** [[% a.2.join(' ') %]]
+[% board = a.2.join(' ') -%]
+[% END -%]
+[%# -%]
+[%# Check action -%]
+[%# -%]
+[% CASE 'check' -%]
+[% player_id = a.1 -%]
+[% hand.players_by_id.$player_id.name %]: checks
+[%# -%]
+[%# Fold action -%]
+[%# -%]
+[% CASE 'fold' -%]
+[% player_id = a.1 -%]
+[% hand.players_by_id.$player_id.name %]: folds
+[%# -%]
+[%# Call action -%]
+[%# -%]
+[% CASE 'call' -%]
+[% player_id = a.1 -%]
+[% hand.players_by_id.$player_id.name %]: calls $[% a.2 / 100 | format('%.2f') %]
+[%# -%]
+[%# Raise action -%]
+[%# -%]
+[% CASE 'raise' -%]
+[% player_id = a.1 -%]
+[% hand.players_by_id.$player_id.name %]: bets $[% a.2 / 100 | format('%.2f') %]
+[%# -%]
+[%# Showdown action -%]
+[%# -%]
+[% CASE 'showdown' -%]
+*** SHOW DOWN ***
+[% FOREACH h IN a.2 -%]
+[% player_id = h.key -%]
+[% hand.players_by_id.$player_id.name %]: shows [[% h.value.join(' ') %]]
+[% END -%]
+[%# -%]
+[%# End action -%]
+[%# -%]
+[% CASE 'end' -%]
+[% rake = 0 -%]
+[% pot = 0 -%]
+[% FOREACH p IN a.2.0.serial2share -%]
+[% player_id = p.key -%]
+[% hand.players_by_id.$player_id.name %] collected [% p.value / 100 | format('%.2f') %] from pot
+[% rake = rake + a.2.0.serial2rake.$player_id -%]
+[% pot = pot + p.value -%]
+[% END -%]
+*** SUMMARY ***
+Total pot $[% (rake + pot) / 100 | format('%.2f') %] | Rake $[% rake / 100 | format('%.2f') %]
+[% IF board != '' -%]
+Board [[% board %]]
+[% END -%]
+[% FOREACH player_id IN a.2.0.player_list -%]
+Seat [% loop.count %]: [% hand.players_by_id.$player_id.name %]
+[%- IF a.2.0.foldwin == 1 -%]
+[%- IF a.2.0.serial2share.$player_id > 0 -%]
+ won ($[% a.2.0.serial2share.$player_id / 100 | format('%.2f') %]) (Everybody else folded)
+[% ELSE -%]
+ folded
+[% END -%]
+[%- ELSE -%]
+[%- IF a.2.0.serial2best.$player_id -%]
+[%- decoded = hand.self.format_hi_hand(a.2.0.serial2best.$player_id.hi.1) -%]
+ showed [[% decoded.cards.join(' ') %]]
+[%- IF a.2.0.serial2share.$player_id > 0 -%]
+ and won ($[% a.2.0.serial2share.$player_id / 100 | format('%.2f') %]) with [% decoded.best %]
+[%- END %]
+[% ELSE -%]
+ folded
+[% END -%]
+[%- END -%]
+[% END -%]
+[% END -%]
+[% END -%]
+[%# hand_dump %]
+</pre>
View
@@ -1,5 +1,11 @@
[% META title = 'User account' %]
+<div class="secondary-menu">
+ <ul>
+ <li><a href='[% c.uri_for('/user/hands') %]'>Hands history</a></li>
+ </ul>
+</div>
+
<div class="profile-avatar">
<img src="[% c.uri_for("/AVATAR/" _ c.user.serial) %]" alt="[% c.user.name %] picture" />
<br />
View
@@ -5,11 +5,4 @@ use Test::More;
BEGIN { use_ok 'Catalyst::Test', 'Room' }
BEGIN { use_ok 'Room::Controller::User::Hand' }
-my $controller = Room::Controller::User::Hand->new;
-my $history = "[('game', 0, 22, 0, 0.0, 'holdem', '.02-.04-no-limit', [6, 7], 7, {6: 200, 7: 94L}), ('position', 0), ('blind', 6, 2, 0), ('position', 1), ('blind', 7, 4, 0), ('position', -1), ('round', 'pre-flop', PokerCards([]), {6: PokerCards([197, 193]), 7: PokerCards([215, 221])}), ('position', 0), ('call', 6, 2), ('position', 1), ('check', 7), ('position', -1), ('round', 'flop', PokerCards([47, 40, 50]), None), ('position', 0), ('raise', 6, 4), ('position', 1), ('call', 7, 4), ('position', -1), ('round', 'turn', PokerCards([47, 40, 50, 30]), None), ('position', 0), ('check', 6), ('position', 1), ('check', 7), ('position', -1), ('round', 'river', PokerCards([47, 40, 50, 30, 25]), None), ('position', 0), ('check', 6), ('position', 1), ('check', 7), ('position', -1), ('showdown', None, {6: PokerCards([5, 1]), 7: PokerCards([215, 221])}), ('end', [6], [{'serial2delta': {6: 8, 7: -8}, 'player_list': [6, 7], 'serial2rake': {6: 0}, 'serial2share': {6: 16}, 'pot': 16, 'serial2best': {6: {'hi': [16894848, ['OnePair', 40, 1, 25, 50, 47]]}, 7: {'hi': [834180, ['NoPair', 25, 50, 23, 47, 30]]}}, 'type': 'game_state', 'side_pots': {'building': 0, 'pots': [[16, 16]], 'last_round': 3, 'contributions': {0: {0: {6: 4, 7: 4}}, 1: {0: {6: 4, 7: 4}}, 2: {}, 'total': {6: 8, 7: 8}, 3: {}}}}, {'serials': [6, 7], 'pot': 16, 'hi': [6], 'chips_left': 0, 'type': 'resolve', 'serial2share': {6: 16}}])]";
-
-my $tree = $controller->__parse_hands($history);
-
-ok( $tree->[0]->[0] eq 'game', 'History should parse correctly' );
-
done_testing();

0 comments on commit 2f0e089

Please sign in to comment.