ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/room.pl
Revision: 1.30
Committed: Wed Jun 2 09:32:32 2004 UTC (19 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.29: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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