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

Comparing kgsueme/kgsueme/room.pl (file contents):
Revision 1.14 by pcg, Tue Jul 22 21:24:50 2003 UTC vs.
Revision 1.32 by elmex, Tue Jun 8 17:58:48 2004 UTC

1package room; 1package room;
2 2
3use KGS::Constants; 3use KGS::Constants;
4 4
5use base KGS::Listener::Room; 5use base KGS::Listener::Room;
6use base gtk::widget; 6
7use Glib::Object::Subclass
8 Gtk2::Frame;
7 9
8sub new { 10sub new {
9 my $self = shift; 11 my ($self, %arg) = @_;
10 $self = $self->SUPER::new(@_);
11 12
12 $self->listen($self->{conn}, qw(msg_room:)); 13 $self = $self->Glib::Object::new;
14 $self->{$_} = delete $arg{$_} for keys %arg;
13 15
14 $self->{window} = new Gtk2::Window 'toplevel';
15 $self->{window}->set_title("KGS Room $self->{name}");
16 gtk::state $self->{window}, "room::window", $self->{name}, window_size => [600, 400];
17
18 $self->{window}->signal_connect(delete_event => sub { $self->part; 1 });
19
20 $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
21 $self->{hpane}->set(position_set => 1);
22 gtk::state $self->{hpane}, "room::hpane", $self->{name}, position => 200;
23
24 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
25
26 $vbox->add(($self->{text} = new gtk::text)->widget);
27
28 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
29 $self->{entry}->signal_connect(activate => sub { 16 $self->signal_connect (destroy => sub {
30 my $text = $self->{entry}->get_text; 17 delete $::config->{rooms}{$self->{channel}};
31 $self->say($text) if $text =~ /\S/; 18 delete $self->{app}{room}{$self->{channel}};
32 $self->{entry}->set_text(""); 19 (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate};
20 $self->unlisten;
21 %{$_[0]} = ();
33 }); 22 });
34 23
35 $self->{hpane}->pack2((my $sw = new Gtk2::ScrolledWindow), 0, 1); 24 $self->listen ($self->{conn}, qw(msg_room:));
36 $sw->set_policy("automatic", "always");
37 25
26 $self->signal_connect (delete_event => sub { $self->part; 1 });
27
28 $self->add (my $vbox = new Gtk2::VBox);
29
30 $vbox->pack_start((my $hbox = new Gtk2::HBox), 0, 1, 0);
31
32 $hbox->pack_start ((my $button = new_with_label Gtk2::Button "Leave"), 0, 1, 0);
33 $button->signal_connect (clicked => sub { $self->part });
34
35 $hbox->pack_start ((my $button = new_with_label Gtk2::Button "New Game"), 0, 1, 0);
36 $button->signal_connect (clicked => sub { $self->new_game });
37
38 $vbox->pack_start ((my $hpane = new Gtk2::HPaned), 1, 1, 0);
39 gtk::state $hpane, "room::hpane", undef, position => 500;
40
41 $hpane->pack1 (($self->{chat} = new chat app => $self->{app}), 1, 0);
42
43 $self->{chat}->signal_connect (command => sub {
44 my ($chat, $cmd, $arg) = @_;
45 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, room => $self);
46 });
47
48 $hpane->pack2 ((my $sw = new Gtk2::ScrolledWindow), 1, 0);
49
50 $sw->set_policy ("automatic", "always");
51
38 $sw->add(($self->{userlist} = new userlist)->widget); 52 $sw->add ($self->{userlist} = new userlist);
39 53
40 $self; 54 $self;
41} 55}
42 56
43sub join { 57sub FINALIZE_INSTANCE { print "FIN room\n" } # never called MEMLEAK #d#TODO#
44 my ($self) = @_;
45 $self->SUPER::join;
46
47 $self->{window}->show_all;
48}
49 58
50sub part { 59sub part {
51 my ($self) = @_; 60 my ($self) = @_;
61
62 $self->hide;
52 $self->SUPER::part; 63 $self->SUPER::part;
53
54 $self->destroy; # yeaha
55} 64}
56 65
57sub inject_msg_room { 66sub inject_msg_room {
58 my ($self, $msg) = @_; 67 my ($self, $msg) = @_;
59 68
60 # secret typoe ;-) 69 # secret typoe ;-)
61 $self->{text}->append_text("\n<header><user>" . (util::toxml $msg->{name}) 70 $self->{chat}->append_text ("\n<user>" . (util::toxml $msg->{name})
62 . "</user>: </header>" . (util::toxml $msg->{message})); 71 . "</user>: " . (util::toxml $msg->{message}));
63} 72}
64 73
65sub event_update_users { 74sub event_update_users {
66 my ($self, $add, $update, $remove) = @_; 75 my ($self, $add, $update, $remove) = @_;
67 76
70 79
71sub event_update_games { 80sub event_update_games {
72 my ($self, $add, $update, $remove) = @_; 81 my ($self, $add, $update, $remove) = @_;
73 82
74 $self->{app}{gamelist}->update ($self, $add, $update, $remove); 83 $self->{app}{gamelist}->update ($self, $add, $update, $remove);
84
85 # try to identify any new games assigned to us. stupid protocol
86 # first updates the game, joins you and THEN tells you that
87 # which of the games you asked for this is.
88
89 for (@$add) {
90 if (($_->{black}{name} eq $self->{conn}{name}
91 || $_->{white}{name} eq $self->{conn}{name}
92 || $_->{owner}{name} eq $self->{conn}{name})
93 && (my $game = shift @{$self->{new_game}})) {
94 $game->inject_upd_game ({ game => $_ });
95 $game->set_channel ($game->{channel});
96 }
97 }
75} 98}
76 99
77sub event_join { 100sub event_join {
78 my ($self) = @_; 101 my ($self) = @_;
79 $self->SUPER::event_join; 102 $self->SUPER::event_join;
83 # mysteriously enough, we have to request game updates manually 106 # mysteriously enough, we have to request game updates manually
84 $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub { 107 $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub {
85 $self->req_games; 108 $self->req_games;
86 1; 109 1;
87 }; 110 };
111
112 $self->show_all;
88} 113}
89 114
90sub event_part { 115sub event_part {
91 my ($self) = @_; 116 my ($self) = @_;
92 117
93 delete $::config->{rooms}{$self->{channel}}; 118 $self->SUPER::event_part;
94 delete $self->{app}{roomlist}{room}{$self->{channel}}; 119 $self->destroy;
95 (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; 120}
96 $self->unlisten;
97 121
122sub event_quit {
123 my ($self) = @_;
124
98 $self->SUPER::event_part; 125 $self->SUPER::event_quit;
126 $self->destroy;
99} 127}
100 128
101sub event_update_roominfo { 129sub event_update_roominfo {
102 my ($self) = @_; 130 my ($self) = @_;
103 131
104 $self->{text}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n" 132 $self->{chat}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n"
105 . "<description>" . (util::toxml $self->{description}) . "</description>\n"); 133 . "<description>" . (util::toxml $self->{description}) . "</description>\n");
106} 134}
107 135
108sub destroy { 136sub new_game {
109 my ($self) = @_; 137 my ($self) = @_;
110 138
111 $self->event_part; 139 my $game = new game conn => $self->{conn}, app => $self->{app}, roomid => $self->{channel};
140 $game->new_game_challenge;
141 $game->show_all;
112 142
113 $self->SUPER::destroy; 143 push @{$self->{new_game}}, $game;
114} 144}
115 145
1161; 1461;
117 147

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines