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