ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/room.pl
Revision: 1.26
Committed: Mon May 31 18:18:26 2004 UTC (19 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.25: +7 -0 lines
Log Message:
*** empty log message ***

File Contents

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