ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
(Generate patch)

Comparing kgsueme/kgsueme/game.pl (file contents):
Revision 1.36 by pcg, Tue Jun 3 07:35:08 2003 UTC vs.
Revision 1.42 by pcg, Tue Jun 3 18:17:05 2003 UTC

1use utf8; 1use utf8;
2 2
3package game::goclock; 3package game::goclock;
4
5# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
6# is completely borked.
4 7
5use Time::HiRes (); 8use Time::HiRes ();
6 9
7use KGS::Constants; 10use KGS::Constants;
8 11
18 $self->{format} = sub { "ERROR" }; 21 $self->{format} = sub { "ERROR" };
19 22
20 $self; 23 $self;
21} 24}
22 25
23sub format_time($) { 26sub configure {
24 my ($time) = @_;
25
26 $time > 60*60
27 ? sprintf "%d:%02d:%02d", $time / (60 * 60), $time / 60 % 60, $time % 60
28 : sprintf "%d:%02d", $time / 60 % 60, $time % 60;
29}
30
31sub set_rules {
32 my ($self, $timesys, $main, $interval, $count) = @_; 27 my ($self, $timesys, $main, $interval, $count) = @_;
33 28
34 if ($timesys == TIMESYS_ABSOLUTE) { 29 if ($timesys == TIMESYS_ABSOLUTE) {
35 $self->{set} = sub { $self->{time} = $_[0] }; 30 $self->{set} = sub { $self->{time} = $_[0] };
36 $self->{format} = sub { format_time $_[0] }; 31 $self->{format} = sub { util::format_time $_[0] };
37 32
38 } elsif ($timesys == TIMESYS_BYO_YOMI) { 33 } elsif ($timesys == TIMESYS_BYO_YOMI) {
39 my $low = $interval * $count; 34 my $low = $interval * $count;
40 35
41 $self->{set} = sub { $self->{time} = $_[0] }; 36 $self->{set} = sub { $self->{time} = $_[0] };
42 37
43 $self->{format} = sub { 38 $self->{format} = sub {
44 if ($_[0] > $low) { 39 if ($_[0] > $low) {
45 format_time $_[0] - $low; 40 util::format_time $_[0] - $low;
46 } else { 41 } else {
47 sprintf "%s (%d)", 42 sprintf "%s (%d)",
48 format_time int ($_[0] % $interval) || $interval, 43 util::format_time int (($_[0] - 1) % $interval + 1),
49 $_[0] / $interval; 44 $_[0] / $interval;
50 } 45 }
51 }; 46 };
52 47
53 } elsif ($timesys == TIMESYS_CANADIAN) { 48 } elsif ($timesys == TIMESYS_CANADIAN) {
54 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] }; 49 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
55 50
56 $self->{format} = sub { 51 $self->{format} = sub {
57 if (!$self->{moves}) { 52 if (!$self->{moves}) {
58 format_time $_[0] - $low; 53 util::format_time $_[0] - $low;
59 } else { 54 } else {
60 my $time = int($_[0] % $interval) || $interval; 55 my $time = int (($_[0] - 1) % $interval + 1);
56
61 sprintf "%s/%d {%d}", 57 sprintf "%s/%d {%d}",
62 format_time $time, 58 util::format_time $time,
63 $self->{moves}, 59 $self->{moves},
64 int ($time / ($self->{moves} || 1)); 60 $time / ($self->{moves} || 1);
65 61
66 } 62 }
67 }; 63 };
68 64
69 } else { 65 } else {
73 } 69 }
74} 70}
75 71
76sub refresh { 72sub refresh {
77 my ($self, $timestamp) = @_; 73 my ($self, $timestamp) = @_;
78 my $timer = $self->{time} + $self->{start} - $timestamp + 0.5; 74 my $timer = $self->{time} + $self->{start} - $timestamp;
79 75
80 # we round the timer value slightly... the protocol isn't exact anyways, 76 # we round the timer value slightly... the protocol isn't exact anyways,
81 # and this gives smoother timers ;) 77 # and this gives smoother timers ;)
78 my @format = $self->{format}->(int ($timer + 0.4));
82 $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4))); 79 $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4)));
83 80
84 $timer - int $timer; 81 $timer - int $timer;
85} 82}
86 83
147 $vbox->add ($self->{imagebox} = new Gtk2::VBox); 144 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
148 145
149 $self; 146 $self;
150} 147}
151 148
152sub set_rules { 149sub configure {
153 my ($self, $rules) = @_; 150 my ($self, $user, $rules) = @_;
154 151
155 if ($self->{name}->get_text ne $rules->{player}[$self->{colour}]) { 152 if ($self->{name}->get_text ne $user->as_string) {
156 $self->{name}->set_text ($rules->{player}[$self->{colour}]); 153 $self->{name}->set_text ($user->as_string);
157 154
158 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; 155 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
159 $self->{imagebox}->add (gtk::image_from_data undef); 156 $self->{imagebox}->add (gtk::image_from_data undef);
160 $self->{imagebox}->show_all; 157 $self->{imagebox}->show_all;
161 158
162 # the big picture... 159 # the big picture...
163 appwin::userpic ($rules->{player}[$self->{colour}], sub { 160 appwin::userpic ($user->{name}, sub {
164 return unless $self->{imagebox}; 161 return unless $self->{imagebox};
165 if ($_[0]) { 162 if ($_[0]) {
166 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children; 163 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
167 $self->{imagebox}->add (gtk::image_from_data $_[0]); 164 $self->{imagebox}->add (gtk::image_from_data $_[0]);
168 $self->{imagebox}->show_all; 165 $self->{imagebox}->show_all;
169 } 166 }
170 }); 167 });
171 } 168 }
172 169
173 $self->{clock}->set_rules (@{$rules->{rules}}{qw(timesys time interval count)}); 170 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
174} 171}
175 172
176sub set_state { 173sub set_state {
177 my ($self, $captures, $timer, $when) = @_; 174 my ($self, $captures, $timer, $when) = @_;
178 175
376 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height, 373 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
377 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192 374 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
378 ); 375 );
379 } 376 }
380 377
381 # first the big stones (handicap stones different for effect) 378 # first the big stones (handicap stones could be different)
382 for ([MARK_B, $mark & MARK_MOVE ? 255 : 192], 379 for ([MARK_B, $mark & MARK_MOVE ? 255 : 255],
383 [MARK_W, $mark & MARK_MOVE ? 255 : 192], 380 [MARK_W, $mark & MARK_MOVE ? 255 : 255],
384 [MARK_GRAY_B, 128], 381 [MARK_GRAY_B, 128],
385 [MARK_GRAY_W, 128]) { 382 [MARK_GRAY_W, 128]) {
386 my ($mask, $alpha) = @$_; 383 my ($mask, $alpha) = @$_;
387 if ($mark & $mask) { 384 if ($mark & $mask) {
388 $stone->composite ( 385 $stone->composite (
575 } 572 }
576 } 573 }
577 574
578 $self->{pixbuf} = $pixbuf; 575 $self->{pixbuf} = $pixbuf;
579 576
580 # hoshi-points(!)#d#
581 # caching of empty board gfx(!)#d#
582
583 for my $x (1 .. $size) { 577 for my $x (1 .. $size) {
584 for my $y (1 .. $size) { 578 for my $y (1 .. $size) {
585 my $rand = ($x ^ $y ^ 0x5555); 579 my $rand = ($x ^ $y ^ 0x5555);
586 580
587 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs); 581 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
678sub event_update_tree { 672sub event_update_tree {
679 my ($self) = @_; 673 my ($self) = @_;
680 674
681 $self->{path} = $self->get_path; 675 $self->{path} = $self->get_path;
682 676
683 $self->{userpanel}[WHITE]->set_rules ($self->{path}[0]); # should be onload only
684 $self->{userpanel}[BLACK]->set_rules ($self->{path}[0]); # should be onload only
685
686 if ($self->{moveadj}) { 677 if ($self->{moveadj}) {
687 my $upper = $self->{moveadj}->upper; 678 my $upper = $self->{moveadj}->upper;
688 my $pos = $self->{moveadj}->get_value; 679 my $pos = $self->{moveadj}->get_value;
689 680
690 $self->{moveadj}->upper (scalar @{$self->{path}}); 681 $self->{moveadj}->upper (scalar @{$self->{path}});
691 682
683 warn "UPDATE_TREE $pos,$upper";#d#
692 $self->{moveadj}->changed; 684 $self->{moveadj}->changed;
693 if ($pos == $upper) { 685 if ($pos == $upper) {
694 $self->{moveadj}->set_value (scalar @{$self->{path}}); 686 $self->{moveadj}->set_value (scalar @{$self->{path}});
695 } else { 687 } else {
696 $self->{moveadj}->value_changed; 688 $self->{moveadj}->value_changed;
707 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>" 699 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
708 if $newnode; 700 if $newnode;
709 701
710 for (split /\n/, $comment) { 702 for (split /\n/, $comment) {
711 $text .= "\n"; 703 $text .= "\n";
712 if ($_ =~ s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) { 704 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
713 $text .= "<user>" . (util::toxml $1) . "</user>:"; 705 $text .= "<user>" . (util::toxml $1) . "</user>:";
714 } 706 }
707
708 # coords only for 19x19 so far
709 $_ = util::toxml $_;
710 s{
711 (
712 \b
713 (?:[bw])?
714 [, ]{0,2}
715 [a-hj-t] # valid for upto 19x19
716 \s?
717 [1-9]?[0-9]
718 \b
719 )
720 }{
721 "<coord>$1</coord>";
722 }sgexi;
723
715 $text .= $_; 724 $text .= $_;
716 } 725 }
717 726
718 $self->{text}->append_text ($text); 727 $self->{text}->append_text ($text);
719} 728}
735 744
736sub event_update_game { 745sub event_update_game {
737 my ($self) = @_; 746 my ($self) = @_;
738 $self->SUPER::event_update_game; 747 $self->SUPER::event_update_game;
739 748
749 $self->{user}[BLACK] = $self->{user1};
750 $self->{user}[WHITE] = $self->{user2};
751
752 # show board
753
754 $self->{left}->remove ($_) for $self->{left}->get_children;
755 if ($self->is_valid) {
756 $self->{left}->add ($self->{boardbox});
757 (delete $self->{challenge})->destroy if $self->{challenge};
758 } else {
759 $self->{left}->add ($self->{challenge}->widget);
760 }
761 $self->{left}->show_all;
762
763 # view text
764
740 $text = "\n<header>Game Update</header>"; 765 $text = "\n<header>Game Update</header>";
741 766
742 $text .= "\nType: " . (util::toxml $gametype{$self->type}) 767 $text .= "\nType: " . (util::toxml $gametype{$self->type})
743 . " (" . (util::toxml $gameopt{$self->option}) . ")"; 768 . " (" . (util::toxml $gameopt{$self->option}) . ")";
744 $text .= "\nFlags:"; 769 $text .= "\nFlags:";
745 $text .= " valid" if $self->is_valid; 770 $text .= " valid" if $self->is_valid;
746 $text .= " adjourned" if $self->is_adjourned; 771 $text .= " adjourned" if $self->is_adjourned;
747 $text .= " scored" if $self->is_scored; 772 $text .= " scored" if $self->is_scored;
748 $text .= " saved" if $self->is_saved; 773 $text .= " saved" if $self->is_saved;
749 774
750 $text .= "\nWhite: <user>" . (util::toxml $self->{user1}->as_string) . "</user>"; 775 $text .= "\nWhite: <user>" . (util::toxml $self->{user2}->as_string) . "</user>";
751 $text .= "\nBlack: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"; 776 $text .= "\nBlack: <user>" . (util::toxml $self->{user1}->as_string) . "</user>";
752 $text .= "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid; 777 $text .= "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid;
753 778
754 if ($self->is_valid) { 779 if ($self->is_valid) {
755 $text .= "\nHandicap: " . $self->{handicap}; 780 $text .= "\nHandicap: " . $self->{handicap};
756 $text .= "\nKomi: " . $self->{komi}; 781 $text .= "\nKomi: " . $self->{komi};
757 $text .= "\nSize: " . $self->size_string; 782 $text .= "\nSize: " . $self->size_string;
758 } 783 }
759 784
760 $self->{text}->append_text ($text); 785 $self->{text}->append_text ("<infoblock>$text</infoblock>");
786}
761 787
762 $self->{left}->remove ($_) for $self->{left}->get_children; 788sub event_update_rules {
763 if ($self->is_valid) { 789 my ($self, $rules) = @_;
764 $self->{left}->add ($self->{boardbox}); 790
765 (delete $self->{challenge})->destroy if $self->{challenge}; 791 $self->{userpanel}[$_]->configure ($self->{user}[$_], $rules)
766 } else { 792 for BLACK, WHITE;
767 $self->{left}->add ($self->{challenge}->widget); 793
794 my $text = "\n<header>Game Rules</header>";
795
796 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
797
798 $text .= "\nTime: ";
799
800 if ($rules->{timesys} == TIMESYS_NONE) {
801 $text .= "infinite";
802 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
803 $text .= util::format_time $rules->{time};
804 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
805 $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count};
806 $text .= sprintf " + %s (%d)", util::format_time $rules->{interval}, $rules->{count};
807 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
808 $text .= util::format_time $rules->{time};
809 $text .= sprintf " + %s/%d", util::format_time $rules->{interval}, $rules->{count};
810 }
768 } 811
769 $self->{left}->show_all; 812 $self->{text}->append_text ("<infoblock>$text</infoblock>");
813}
814
815sub inject_resign_game {
816 my ($self, $msg) = @_;
817
818 $self->{text}->append_text ("\n<infoblock><header>Resign</header>"
819 . "\n<user>"
820 . (util::toxml $self->{user}[$msg->{player}]->as_string)
821 . "</user> resigned.</infoblock>");
822}
823
824sub inject_final_result {
825 my ($self, $msg) = @_;
826
827 $self->{text}->append_text ("<infoblock>\n<header>Game Over</header>"
828 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
829 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
830 . "</infoblock>"
831 );
770} 832}
771 833
772sub destroy { 834sub destroy {
773 my ($self) = @_; 835 my ($self) = @_;
774 (delete $self->{userpanel}[WHITE])->destroy if $self->{userpanel}[WHITE]; 836 $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy
775 (delete $self->{userpanel}[BLACK])->destroy if $self->{userpanel}[BLACK]; 837 for BLACK, WHITE;
776 $self->SUPER::destroy; 838 $self->SUPER::destroy;
777 delete $appwin::gamelist->{game}{$self->{channel}}; 839 delete $appwin::gamelist->{game}{$self->{channel}};
778} 840}
779 841
7801; 8421;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines