File Coverage

blib/lib/Bot/Cobalt/Plugin/Games.pm
Criterion Covered Total %
statement 19 73 26.0
branch 0 8 0.0
condition 0 6 0.0
subroutine 7 12 58.3
pod 0 3 0.0
total 26 102 25.4


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Plugin::Games;
2             $Bot::Cobalt::Plugin::Games::VERSION = '0.021002';
3 1     1   1017 use strictures 2;
  1         7  
  1         34  
4              
5 1     1   167 use List::Objects::WithUtils;
  1         1  
  1         8  
6              
7 1     1   930 use Bot::Cobalt;
  1         1  
  1         5  
8 1     1   935 use Bot::Cobalt::Core::Loader;
  1         1  
  1         25  
9              
10 1     1   4 use Object::Pluggable::Constants ':ALL';
  1         1  
  1         570  
11              
12             sub MODULES () { 0 }
13             sub CMDS () { 1 }
14             sub OBJS () { 2 }
15              
16              
17             sub new {
18 1     1 0 354 bless [
19             [], # MODULES
20             +{}, # CMDS
21             +{}, # OBJS
22             ], shift
23             }
24              
25             sub Cobalt_register {
26 0     0 0   my ($self, $core) = splice @_, 0, 2;
27              
28 0           my $count = $self->_load_games();
29 0           $core->log->info("Loaded - $count games");
30              
31 0           PLUGIN_EAT_NONE
32             }
33              
34             sub Cobalt_unregister {
35 0     0 0   my ($self, $core) = splice @_, 0, 2;
36              
37 0           $core->log->debug("Cleaning up our games...");
38 0           for my $module (@{ $self->[MODULES] }) {
  0            
39 0           Bot::Cobalt::Core::Loader->unload($module);
40             }
41 0           $core->log->info("Unloaded");
42              
43 0           PLUGIN_EAT_NONE
44             }
45              
46             sub _handle_auto {
47             ## Handler for autoviv'd methods. See _load_games
48 0     0     my ($self, $core) = splice @_, 0, 2;
49 0           my $msg = ${ $_[0] };
  0            
50              
51 0           my $context = $msg->context;
52              
53 0           my $cmd = $msg->cmd;
54              
55 0   0       my $game = $self->[CMDS]->{$cmd} // return PLUGIN_EAT_NONE;
56 0           my $obj = $self->[OBJS]->{$game};
57              
58 0           my $msgarr = $msg->message_array;
59 0           my $str = join ' ', @$msgarr;
60              
61 0           my $resp = '';
62 0 0         $resp = $obj->execute($msg, $str) if $obj->can('execute');
63              
64 0 0         broadcast( message =>
65             $context,
66             $msg->target,
67             $resp
68             ) if $resp;
69              
70 0           PLUGIN_EAT_NONE
71             }
72              
73             sub _load_games {
74 0     0     my ($self) = @_;
75              
76 0           my $pcfg = core->get_plugin_cfg( $self );
77 0   0       my $games = $pcfg->{Games} // {};
78              
79 0           logger->debug("Loading games");
80              
81 0           my $count = 0;
82 0           for my $game (keys %$games) {
83 0   0       my $module = $games->{$game}->{Module} // next;
84 0 0         next unless ref $games->{$game}->{Cmds} eq 'ARRAY';
85              
86             ## attempt to load module
87             ## FIXME convert to Loader.pm interface
88             {
89 0           local $@;
  0            
90 0           eval "require $module";
91 0 0         if ($@) {
92 0           logger->warn("Failed to load $module - $@");
93             next
94 0           } else {
95 0           logger->debug("Found: $module");
96             }
97             }
98              
99 0           push @{ $self->[MODULES] }, $module;
  0            
100              
101 0           my $obj = $self->[OBJS]->{$game} = $module->new;
102              
103 0           for my $cmd (@{ $games->{$game}->{Cmds} }) {
  0            
104 0           $self->[CMDS]->{$cmd} = $game;
105             ## install a cmd handler and register for it
106 0     0     my $handler = sub { $_[0]->_handle_auto(@_[1 .. $#_]) };
  0            
107 1     1   4 { no strict 'refs';
  1         1  
  1         115  
  0            
108 0           *{ __PACKAGE__.'::Bot_public_cmd_'.$cmd } = $handler;
  0            
109             }
110              
111 0           register( $self, SERVER => [ 'public_cmd_'.$cmd ] );
112             }
113              
114 0           ++$count;
115 0           logger->debug("Game loaded: $game");
116             }
117              
118             $count
119 0           }
120              
121              
122             1;
123             __END__