File Coverage

blib/lib/Hoppy/Room/Memory.pm
Criterion Covered Total %
statement 70 75 93.3
branch 6 16 37.5
condition 2 2 100.0
subroutine 12 12 100.0
pod 8 8 100.0
total 98 113 86.7


line stmt bran cond sub pod time code
1             package Hoppy::Room::Memory;
2 5     5   732 use strict;
  5         10  
  5         189  
3 5     5   29 use warnings;
  5         9  
  5         144  
4 5     5   2936 use Hoppy::User;
  5         14  
  5         50  
5 5     5   200 use base qw(Hoppy::Base);
  5         12  
  5         4824  
6              
7             sub new {
8 7     7 1 33 my $class = shift;
9 7         73 my $self = $class->SUPER::new(@_);
10 7         33 $self->create_room('global');
11 7         39 return $self;
12             }
13              
14             sub create_room {
15 11     11 1 70 my $self = shift;
16 11         23 my $room_id = shift;
17 11 50       121 unless ( $self->{rooms}->{$room_id} ) {
18 11         47 $self->{rooms}->{$room_id} = {};
19             }
20             }
21              
22             sub delete_room {
23 1     1 1 9 my $self = shift;
24 1         4 my $room_id = shift;
25 1 50       6 if ( $room_id ne 'global' ) {
26 1         4 my $room = $self->{rooms}->{$room_id};
27 1         5 for my $user_id ( keys %$room ) {
28 0         0 $self->delete_user($user_id);
29             }
30 1         5 delete $self->{rooms}->{$room_id};
31             }
32             }
33              
34             sub login {
35 5     5 1 53 my $self = shift;
36 5         9 my $args = shift;
37 5         7 my $poe = shift;
38 5         30 my $c = $self->context;
39 5         30 my $result = 1;
40 5 50       19 if ( $c->service->{auth_login} ) {
41 0         0 $result = $c->service->{auth_login}->work( $args, $poe );
42 0 0       0 return 0 unless $result;
43             }
44 5         39 my $user_id = $args->{user_id};
45 5         8 my $password = $args->{password};
46 5         11 my $session_id = $args->{session_id};
47 5   100     22 my $room_id = $args->{room_id} || 'global';
48 5         37 delete $c->{not_authorized}->{$session_id};
49 5         41 my $user = Hoppy::User->new(
50             user_id => $user_id,
51             session_id => $session_id
52             );
53 5         21 $self->{rooms}->{$room_id}->{$user_id} = $user;
54 5         13 $self->{where_in}->{$user_id} = $room_id;
55 5         14 $self->{sessions}->{$session_id} = $user_id;
56 5         14 return $result;
57             }
58              
59             sub logout {
60 2     2 1 52 my $self = shift;
61 2         6 my $args = shift;
62 2         5 my $poe = shift;
63 2         13 my $c = $self->context;
64 2         13 my $result = 1;
65 2 50       9 if ( $c->service->{auth_logout} ) {
66 0         0 $result = $c->service->{auth_logout}->work( $args, $poe );
67 0 0       0 return 0 unless $result;
68             }
69 2         22 my $user_id = $args->{user_id};
70 2         16 my $user = $self->fetch_user_from_user_id($user_id);
71 2         81 delete $self->{sessions}->{ $user->session_id };
72 2         15 my $room_id = delete $self->{where_in}->{$user_id};
73 2         6 delete $self->{rooms}->{$room_id}->{$user_id};
74 2         9 $self->context->{not_authorized}->{ $user->session_id } = 1;
75 2         23 return $result;
76             }
77              
78             sub fetch_user_from_user_id {
79 6     6 1 24 my $self = shift;
80 6         13 my $user_id = shift;
81 6 50       29 return unless ($user_id);
82 6         16 my $room_id = $self->{where_in}->{$user_id};
83 6         41 return $self->{rooms}->{$room_id}->{$user_id};
84             }
85              
86             sub fetch_user_from_session_id {
87 3     3 1 24 my $self = shift;
88 3         7 my $session_id = shift;
89 3 50       13 return unless ($session_id);
90 3         10 my $user_id = $self->{sessions}->{$session_id};
91 3         14 return $self->fetch_user_from_user_id($user_id);
92             }
93              
94             sub fetch_users_from_room_id {
95 1     1 1 7 my $self = shift;
96 1         3 my $room_id = shift;
97 1         3 my @users = values %{ $self->{rooms}->{$room_id} };
  1         6  
98 1         13 return \@users;
99             }
100              
101             1;
102             __END__