ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/room.pl
Revision: 1.22
Committed: Sun May 30 05:04:09 2004 UTC (20 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +1 -1 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 $self = $self->Glib::Object::new;
13 $self->{$_} = delete $arg{$_} for keys %arg;
14
15 $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
23 $self->listen ($self->{conn}, qw(msg_room:));
24
25 $self->signal_connect (delete_event => sub { $self->part; 1 });
26
27 $self->add (my $hbox = new Gtk2::HBox);
28
29 $hbox->pack_start ((my $vbox = new Gtk2::VBox), 1, 1, 0);
30
31 $vbox->add ($self->{chat} = new chat);
32
33 $self->{chat}->signal_connect(command => sub {
34 my ($chat, $cmd, $arg) = @_;
35 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, room => $self);
36 });
37
38 $hbox->pack_start ((my $vbox = new Gtk2::VBox), 0, 1, 0);
39
40 $vbox->pack_start ((my $button = new_with_label Gtk2::Button "Close"), 0, 1, 0);
41 $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 $sw->set_policy("automatic", "always");
48
49 $sw->add ($self->{userlist} = new userlist);
50
51 $self;
52 }
53
54 sub FINALIZE_INSTANCE { print "FIN room\n" } # never called MEMLEAK #d#TODO#
55
56 sub part {
57 my ($self) = @_;
58 $self->SUPER::part;
59
60 $self->hide_all;
61 }
62
63 sub inject_msg_room {
64 my ($self, $msg) = @_;
65
66 # secret typoe ;-)
67 $self->{chat}->append_text ("\n<header><user>" . (util::toxml $msg->{name})
68 . "</user>: </header>" . (util::toxml $msg->{message}));
69 }
70
71 sub event_update_users {
72 my ($self, $add, $update, $remove) = @_;
73
74 $self->{userlist}->update ($add, $update, $remove);
75 }
76
77 sub event_update_games {
78 my ($self, $add, $update, $remove) = @_;
79
80 $self->{app}{gamelist}->update ($self, $add, $update, $remove);
81
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 if ($_->player_colour ($self->{conn}{name})
88 and my $game = shift @{$self->{new_game}}) {
89 $game->inject_upd_game ({ game => $_ });
90 $game->set_channel ($game->{channel});
91 }
92 }
93 }
94
95 sub event_join {
96 my ($self) = @_;
97 $self->SUPER::event_join;
98
99 $::config->{rooms}{$self->{channel}} = { channel => $self->{channel}, name => $self->{name} };
100
101 # mysteriously enough, we have to request game updates manually
102 $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub {
103 $self->req_games;
104 1;
105 };
106
107 $self->show_all;
108 }
109
110 sub event_part {
111 my ($self) = @_;
112
113 $self->SUPER::event_part;
114 $self->destroy;
115 }
116
117 sub event_update_roominfo {
118 my ($self) = @_;
119
120 $self->{chat}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n"
121 . "<description>" . (util::toxml $self->{description}) . "</description>\n");
122 }
123
124 sub new_game {
125 my ($self) = @_;
126
127 my $d = $self->{app}{defaults};
128
129 my $game = new game conn => $self->{conn}, app => $self->{app}, roomid => $self->{channel};
130 $game->{challenge}{""} = {
131 gametype => $d->{gametype},
132 flags => 0,
133 notes => $d->{stones},
134 rules => {
135 ruleset => $d->{ruleset},
136 size => $d->{size},
137 timesys => $d->{timesys},
138 time => $d->{time},
139 interval => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_time} : $d->{can_time},
140 count => $d->{timesys} == TIMESYS_BYO_YOMI ? $d->{byo_periods} : $d->{can_stones},
141 },
142
143 inlay => $game->{chat}->new_inlay,
144 };
145 $game->draw_challenge ("");
146 $game->show_all;
147
148 push @{$self->{new_game}}, $game;
149 }
150
151 1;
152