From afe4638b0aa93cf81cbfc9006f65c0a6849ab642 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 11 Feb 2020 21:49:43 -0800 Subject: [PATCH] No longer directly access HashObject internals --- PBot/Capabilities.pm | 50 +++++++------------ PBot/Channels.pm | 71 ++++++++------------------- PBot/Commands.pm | 35 ++++---------- PBot/HashObject.pm | 38 ++++++++++----- PBot/PBot.pm | 7 ++- Plugins/Spinach.pm | 112 ++++++++++++++++++++----------------------- 6 files changed, 130 insertions(+), 183 deletions(-) diff --git a/PBot/Capabilities.pm b/PBot/Capabilities.pm index 2a6bf0d2..219048e1 100644 --- a/PBot/Capabilities.pm +++ b/PBot/Capabilities.pm @@ -44,8 +44,7 @@ sub has { return 0; } - foreach my $c (keys %{$cap_data}) { - next if $c eq '_name'; + foreach my $c ($self->{caps}->get_keys($cap)) { return 1 if $c eq $subcap and $cap_data->{$c}; return 1 if $self->has($c, $subcap, $depth); } @@ -56,7 +55,7 @@ sub userhas { my ($self, $user, $cap) = @_; return 0 if not defined $user; return 1 if $user->{$cap}; - foreach my $key (keys %{$user}) { + foreach my $key (keys %$user) { next if $key eq '_name'; next if not $user->{$key}; return 1 if $self->has($key, $cap); @@ -67,10 +66,9 @@ sub userhas { sub exists { my ($self, $cap) = @_; $cap = lc $cap; - foreach my $c (keys %{$self->{caps}->{hash}}) { - next if $c eq '_name'; + foreach my $c ($self->{caps}->get_keys) { return 1 if $c eq $cap; - foreach my $sub_cap (keys %{$self->{caps}->{hash}->{$c}}) { + foreach my $sub_cap ($self->{caps}->get_keys($c)) { return 1 if $sub_cap eq $cap; } } @@ -95,43 +93,30 @@ sub add { sub remove { my ($self, $cap, $subcap) = @_; $cap = lc $cap; - if (not defined $subcap) { - foreach my $c (keys %{$self->{caps}->{hash}}) { - next if $c eq '_name'; - foreach my $sub_cap (keys %{$self->{caps}->{hash}->{$c}}) { - delete $self->{caps}->{hash}->{$c}->{$sub_cap} if $sub_cap eq $cap; - } - if ($c eq $cap) { - delete $self->{caps}->{hash}->{$c}; + foreach my $c ($self->{caps}->get_keys) { + foreach my $sub_cap ($self->{caps}->get_keys($c)) { + $self->{caps}->remove($c, $sub_cap, 1) if $sub_cap eq $cap; } + $self->{caps}->remove($c, undef, 1) if $c eq $cap; } } else { - $subcap = lc $subcap; - if (exists $self->{caps}->{hash}->{$cap}) { - delete $self->{caps}->{hash}->{$cap}->{$subcap}; - } - - if (keys %{$self->{caps}->{hash}->{$cap}} == 1) { - delete $self->{caps}->{hash}->{$cap}; - } + $self->{caps}->remove($cap, $subcap, 1) if $self->{caps}->exists($cap); } $self->{caps}->save; } sub rebuild_botowner_capabilities { my ($self) = @_; - $self->{caps}->remove('botowner'); - foreach my $cap (keys %{$self->{caps}->{hash}}) { - next if $cap eq '_name'; + $self->{caps}->remove('botowner', undef, 1); + foreach my $cap ($self->{caps}->get_keys) { $self->add('botowner', $cap, 1); } } sub list { my ($self, $capability) = @_; - $capability = lc $capability if defined $capability; - return "No such capability $capability." if defined $capability and not exists $self->{caps}->{hash}->{$capability}; + return "No such capability $capability." if defined $capability and not $self->{caps}->exists($capability); my @caps; my @groups; @@ -139,19 +124,18 @@ sub list { my $result; if (not defined $capability) { - @caps = sort keys %{$self->{caps}->{hash}}; + @caps = sort $self->{caps}->get_keys; $result = 'Capabilities: '; } else { - @caps = sort keys %{$self->{caps}->{hash}->{$capability}}; - return "Capability $capability has no grouped capabilities." if not @caps or @caps == 1; + @caps = sort $self->{caps}->get_keys($capability); + return "Capability $capability has no grouped capabilities." if not @caps; $result = "Grouped capabilities for $capability: "; } # first list all capabilities that have sub-capabilities (i.e. grouped capabilities) # then list stand-alone capabilities foreach my $cap (@caps) { - next if $cap eq '_name'; - my $count = keys(%{$self->{caps}->{hash}->{$cap}}) - 1; + my $count = $self->{caps}->get_keys($cap); if ($count > 0) { push @groups, "$cap ($count cap" . ($count == 1 ? '' : 's') . ")" if $count; } else { @@ -225,7 +209,7 @@ sub capcmd { foreach my $key (sort keys %{$u}) { next if $key eq '_name'; next if not $self->exists($key); - my $count = keys (%{$self->{caps}->{hash}->{$key}}) - 1; + my $count = $self->{caps}->get_keys; if ($count > 0) { push @groups, "$key ($count cap" . ($count == 1 ? '' : 's') . ")"; } else { diff --git a/PBot/Channels.pm b/PBot/Channels.pm index 52d30b95..deb5d2ea 100644 --- a/PBot/Channels.pm +++ b/PBot/Channels.pm @@ -16,7 +16,7 @@ use feature 'unicode_strings'; sub initialize { my ($self, %conf) = @_; $self->{channels} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Channels', filename => $conf{filename}); - $self->load_channels; + $self->{channels}->load; $self->{pbot}->{commands}->register(sub { $self->join(@_) }, "join", 1); $self->{pbot}->{commands}->register(sub { $self->part(@_) }, "part", 1); @@ -33,25 +33,20 @@ sub initialize { sub join { my ($self, $from, $nick, $user, $host, $arguments) = @_; - foreach my $channel (split /[\s+,]/, $arguments) { $self->{pbot}->{logger}->log("$nick!$user\@$host made me join $channel\n"); $self->{pbot}->{chanops}->join_channel($channel); } - return "/msg $nick Joining $arguments"; } sub part { my ($self, $from, $nick, $user, $host, $arguments) = @_; - $arguments = $from if not $arguments; - foreach my $channel (split /[\s+,]/, $arguments) { $self->{pbot}->{logger}->log("$nick!$user\@$host made me part $channel\n"); $self->{pbot}->{chanops}->part_channel($channel); } - return "/msg $nick Parting $arguments"; } @@ -71,10 +66,7 @@ sub unset { sub add { my ($self, $from, $nick, $user, $host, $arguments) = @_; - - if (not defined $arguments or not length $arguments) { - return "Usage: chanadd "; - } + return "Usage: chanadd " if not defined $arguments or not length $arguments; my $data = { enabled => 1, @@ -87,39 +79,30 @@ sub add { sub remove { my ($self, $from, $nick, $user, $host, $arguments) = @_; - - if (not defined $arguments or not length $arguments) { - return "Usage: chanrem "; - } - - $arguments = lc $arguments; + return "Usage: chanrem " if not defined $arguments or not length $arguments; # clear unban timeouts - if (exists $self->{pbot}->{chanops}->{unban_timeout}->{hash}->{$arguments}) { - delete $self->{pbot}->{chanops}->{unban_timeout}->{hash}->{$arguments}; - $self->{pbot}->{chanops}->{unban_timeout}->save; + if ($self->{pbot}->{chanops}->{unban_timeout}->exists($arguments)) { + $self->{pbot}->{chanops}->{unban_timeout}->remove($arguments); } # clear unmute timeouts - if (exists $self->{pbot}->{chanops}->{unmute_timeout}->{hash}->{$arguments}) { - delete $self->{pbot}->{chanops}->{unmute_timeout}->{hash}->{$arguments}; - $self->{pbot}->{chanops}->{unmute_timeout}->save; + if ($self->{pbot}->{chanops}->{unmute_timeout}->exists($arguments)) { + $self->{pbot}->{chanops}->{unmute_timeout}->remove($arguments); } # TODO: ignores, etc? - return $self->{channels}->remove($arguments); } sub list { my ($self, $from, $nick, $user, $host, $arguments) = @_; my $result; - - foreach my $index (sort keys %{ $self->{channels}->{hash} }) { - $result .= "$self->{channels}->{hash}->{$index}->{_name}: {"; + foreach my $channel (sort $self->{channels}->get_keys) { + $result .= $self->{channels}->get_data($channel, '_name') . ': {'; my $comma = ' '; - foreach my $key (sort keys %{ $self->{channels}->{hash}->{$index} }) { - $result .= "$comma$key => $self->{channels}->{hash}->{$index}->{$key}"; + foreach my $key (sort $self->{channels}->get_keys($channel)) { + $result .= "$comma$key => " . $self->{channels}->get_data($channel, $key); $comma = ', '; } $result .= " }\n"; @@ -130,43 +113,31 @@ sub list { sub autojoin { my ($self) = @_; return if $self->{pbot}->{joined_channels}; - my $chans; - foreach my $chan (keys %{ $self->{channels}->{hash} }) { - if ($self->{channels}->{hash}->{$chan}->{enabled}) { - $chans .= "$self->{channels}->{hash}->{$chan}->{_name},"; + my $channels; + foreach my $channel ($self->{channels}->get_keys) { + if ($self->{channels}->get_data($channel, 'enabled')) { + $channels .= $self->{channels}->get_data($channel, '_name') . ','; } } - $self->{pbot}->{logger}->log("Joining channels: $chans\n"); - $self->{pbot}->{chanops}->join_channel($chans); + $self->{pbot}->{logger}->log("Joining channels: $channels\n"); + $self->{pbot}->{chanops}->join_channel($channels); $self->{pbot}->{joined_channels} = 1; } sub is_active { my ($self, $channel) = @_; - my $lc_channel = lc $channel; - return exists $self->{channels}->{hash}->{$lc_channel} && $self->{channels}->{hash}->{$lc_channel}->{enabled}; + # returns undef if channel doesn't exist; otherwise, the value of 'enabled' + return $self->{channels}->get_data($channel, 'enabled'); } sub is_active_op { my ($self, $channel) = @_; - return $self->is_active($channel) && $self->{channels}->{hash}->{lc $channel}->{chanop}; + return $self->is_active($channel) && $self->{channels}->get_data($channel, 'chanop'); } sub get_meta { my ($self, $channel, $key) = @_; - $channel = lc $channel; - return undef if not exists $self->{channels}->{hash}->{$channel}; - return $self->{channels}->{hash}->{$channel}->{$key}; -} - -sub load_channels { - my ($self) = @_; - $self->{channels}->load; -} - -sub save_channels { - my ($self) = @_; - $self->{channels}->save; + return $self->{channels}->get_data($channel, $key); } 1; diff --git a/PBot/Commands.pm b/PBot/Commands.pm index 7e29a830..4544a62e 100644 --- a/PBot/Commands.pm +++ b/PBot/Commands.pm @@ -22,7 +22,7 @@ sub initialize { $self->PBot::Registerable::initialize(%conf); $self->{metadata} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Commands', filename => $conf{filename}); - $self->load_metadata; + $self->{metadata}->load; $self->register(sub { $self->cmdset(@_) }, "cmdset", 1); $self->register(sub { $self->cmdunset(@_) }, "cmdunset", 1); @@ -131,28 +131,14 @@ sub interpreter { sub set_meta { my ($self, $command, $key, $value, $save) = @_; - $command = lc $command; - return undef if not exists $self->{metadata}->{hash}->{$command}; - $self->{metadata}->{hash}->{$command}->{$key} = $value; - $self->save_metadata if $save; + return undef if not $self->{metadata}->exists($command); + $self->{metadata}->set($command, $key, $value, !$save); return 1; } sub get_meta { my ($self, $command, $key) = @_; - $command = lc $command; - return undef if not exists $self->{metadata}->{hash}->{$command}; - return $self->{metadata}->{hash}->{$command}->{$key}; -} - -sub load_metadata { - my ($self) = @_; - $self->{metadata}->load; -} - -sub save_metadata { - my ($self) = @_; - $self->{metadata}->save; + return $self->{metadata}->get_data($command, $key); } sub cmdset { @@ -180,15 +166,12 @@ sub help { # check built-in commands first if ($self->exists($keyword)) { - if (exists $self->{metadata}->{hash}->{$keyword}) { - my $name = $self->{metadata}->{hash}->{$keyword}->{_name}; - my $requires_cap = $self->{metadata}->{hash}->{$keyword}->{requires_cap}; - my $help = $self->{metadata}->{hash}->{$keyword}->{help}; + if ($self->{metadata}->exists($keyword)) { + my $name = $self->{metadata}->get_data($keyword, '_name'); + my $requires_cap = $self->{metadata}->get_data($keyword, 'requires_cap'); + my $help = $self->{metadata}->get_data($keyword, 'help'); my $result = "/say $name: "; - - if ($requires_cap) { - $result .= "[Requires can-$keyword] "; - } + $result .= "[Requires can-$keyword] " if $requires_cap; if (not defined $help or not length $help) { $result .= "I have no help for this command yet."; diff --git a/PBot/HashObject.pm b/PBot/HashObject.pm index c4494716..572c5856 100644 --- a/PBot/HashObject.pm +++ b/PBot/HashObject.pm @@ -31,6 +31,8 @@ sub load { my $filename; if (@_) { $filename = shift; } else { $filename = $self->{filename}; } + $self->clear; + if (not defined $filename) { Carp::carp "No $self->{name} filename specified -- skipping loading from file"; return; @@ -53,7 +55,7 @@ sub load { # update existing entries to use _name to preserve case # and lowercase any non-lowercased entries - foreach my $index (keys %{ $self->{hash} }) { + foreach my $index (keys %{$self->{hash}}) { if (not exists $self->{hash}->{$index}->{_name}) { if (lc $index eq $index) { $self->{hash}->{$index}->{_name} = $index; @@ -100,7 +102,7 @@ sub levenshtein_matches { my $comma = ''; my $result = ""; - foreach my $index (sort keys %{ $self->{hash} }) { + foreach my $index (sort keys %{$self->{hash}}) { my $distance = fastdistance($keyword, $index); my $length = (length $keyword > length $index) ? length $keyword : length $index; @@ -128,7 +130,7 @@ sub set { if (not defined $key) { my $result = "[$self->{name}] $self->{hash}->{$lc_index}->{_name} keys: "; my $comma = ''; - foreach my $k (sort keys %{ $self->{hash}->{$lc_index} }) { + foreach my $k (sort keys %{$self->{hash}->{$lc_index}}) { next if $k eq '_name'; $result .= $comma . "$k => " . $self->{hash}->{$lc_index}->{$k}; $comma = "; "; @@ -143,7 +145,6 @@ sub set { $self->{hash}->{$lc_index}->{$key} = $value; $self->save unless $dont_save; } - return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: $key " . (defined $value ? "set to $value" : "is not set."); } @@ -159,18 +160,27 @@ sub unset { delete $self->{hash}->{$lc_index}->{$key}; $self->save; - return "[$self->{name}] $self->{hash}->{$lc_index}->{_name}: $key unset."; } sub exists { + my ($self, $index, $data_index) = @_; + return exists $self->{hash}->{lc $index} if not defined $data_index; + return exists $self->{hash}->{lc $index}->{$data_index}; +} + +sub get_keys { my ($self, $index) = @_; - return exists $self->{hash}->{lc $index}; + return keys %{$self->{hash}} if not defined $index; + return grep { $_ ne '_name' } keys %{$self->{hash}->{lc $index}}; } sub get_data { - my ($self, $index) = @_; - return $self->{hash}->{lc $index}; + my ($self, $index, $data_index) = @_; + my $lc_index = lc $index; + return undef if not exists $self->{hash}->{$lc_index}; + return $self->{hash}->{$lc_index} if not defined $data_index; + return $self->{hash}->{$lc_index}->{$data_index}; } sub add { @@ -188,7 +198,7 @@ sub add { } sub remove { - my ($self, $index) = @_; + my ($self, $index, $data_index, $dont_save) = @_; my $lc_index = lc $index; if (not exists $self->{hash}->{$lc_index}) { @@ -197,9 +207,15 @@ sub remove { return $result; } - my $data = delete $self->{hash}->{$lc_index}; - $self->save; + if (defined $data_index) { + delete $self->{hash}->{$lc_index}->{$data_index}; + delete $self->{hash}->{$lc_index} if keys(%{$self->{hash}->{$lc_index}}) == 1; + $self->save unless $dont_save; + return "$self->{hash}->{$lc_index}->{_name}.$data_index removed from $self->{name}"; + } + my $data = delete $self->{hash}->{$lc_index}; + $self->save unless $dont_save; return "$data->{_name} removed from $self->{name}."; } diff --git a/PBot/PBot.pm b/PBot/PBot.pm index f01baf82..cd603ce9 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -416,12 +416,12 @@ sub reload { my %reloadables = ( 'capabilities' => sub { - $self->{capabilities}->load; + $self->{capabilities}->{caps}->load; return "Capabilities reloaded."; }, 'commands' => sub { - $self->{commands}->load_metadata; + $self->{commands}->{metadata}->load; return "Commands metadata reloaded."; }, @@ -449,8 +449,7 @@ sub reload { }, 'channels' => sub { - $self->{channels}->{channels}->clear; - $self->{channels}->load_channels; + $self->{channels}->{channels}->load; return "Channels reloaded."; }, diff --git a/Plugins/Spinach.pm b/Plugins/Spinach.pm index 733a73a3..fa3ee1a8 100644 --- a/Plugins/Spinach.pm +++ b/Plugins/Spinach.pm @@ -55,14 +55,15 @@ sub initialize { $self->{stats_filename} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spinach/stats.sqlite'; $self->{metadata} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Spinach Metadata', filename => $self->{metadata_filename}); - $self->load_metadata(); + $self->{metadata}->load; + $self->set_metadata_defaults; $self->{stats} = Plugins::Spinach::Stats->new(pbot => $self->{pbot}, filename => $self->{stats_filename}); $self->{rankcmd} = Plugins::Spinach::Rank->new(pbot => $self->{pbot}, channel => $self->{channel}, filename => $self->{stats_filename}); - $self->create_states(); - $self->load_questions(); - $self->load_stopwords(); + $self->create_states; + $self->load_questions; + $self->load_stopwords; $self->{choosecategory_max_count} = 4; $self->{picktruth_max_count} = 4; @@ -182,10 +183,8 @@ sub load_stopwords { close $fh; } -sub load_metadata { - my $self = shift; - $self->{metadata}->load; - +sub set_metadata_defaults { + my ($self) = @_; my $defaults = { category_choices => 7, category_autopick => 0, @@ -198,22 +197,17 @@ sub load_metadata { debug_state => 0, }; - if (not exists $self->{metadata}->{hash}->{settings}) { - $self->{metadata}->{hash}->{settings} = $defaults; + if ($self->{metadata}->exists('settings')) { + $self->{metadata}->add('settings', $defaults, 1); } else { foreach my $key (keys %$defaults) { - if (not exists $self->{metadata}->{hash}->{settings}->{$key}) { - $self->{metadata}->{hash}->{settings}->{$key} = $defaults->{$key}; + if (not $self->{metadata}->exists('settings', $key)) { + $self->{metadata}->set('settings', $key, $defaults->{$key}, 1); } } } } -sub save_metadata { - my $self = shift; - $self->{metadata}->save; -} - my %color = ( white => "\x0300", black => "\x0301", @@ -880,33 +874,31 @@ sub spinach_cmd { return "Bad filter: No categories match. Try again."; } - $self->{metadata}->{hash}->{filter}->{"category_" . $_ . "_filter"} = $args; - $self->save_metadata; + $self->{metadata}->set('filter', "category_" . $_ . "_filter", $args); return "Spinach $_ filter set."; } when ('clear') { - delete $self->{metadata}->{hash}->{filter}; - $self->save_metadata; + $self->{metadata}->remove('filter'); return "Spinach filter cleared."; } when ('show') { - if (not exists $self->{metadata}->{hash}->{filter}->{category_include_filter} - and not exists $self->{metadata}->{hash}->{filter}->{category_exclude_filter}) { + if (not $self->{metadata}->exists('filter', 'category_include_filter') + and not $self->{metadata}->exists('filter', 'category_exclude_filter')) { return "There is no Spinach filter set."; } my $text = "Spinach "; my $comma = ""; - if (exists $self->{metadata}->{hash}->{filter}->{category_include_filter}) { - $text .= "include filter set to: " . $self->{metadata}->{hash}->{filter}->{category_include_filter}; + if ($self->{metadata}->exists('filter', 'category_include_filter')) { + $text .= "include filter set to: " . $self->{metadata}->get_data('filter', 'category_include_filter'); $comma = "; "; } - if (exists $self->{metadata}->{hash}->{filter}->{category_exclude_filter}) { - $text .= $comma . "exclude filter set to: " . $self->{metadata}->{hash}->{filter}->{category_exclude_filter}; + if ($self->{metadata}->exists('filter', 'category_exclude_filter')) { + $text .= $comma . "exclude filter set to: " . $self->{metadata}->get_data('filter', 'category_exclude_filter'); } return $text; @@ -1083,7 +1075,7 @@ sub run_one_state { if ($self->{current_state} =~ /r\dq\d/) { my $removed = 0; for (my $i = 0; $i < @{$self->{state_data}->{players}}; $i++) { - if ($self->{state_data}->{players}->[$i]->{missedinputs} >= $self->{metadata}->{hash}->{settings}->{max_missed_inputs}) { + if ($self->{state_data}->{players}->[$i]->{missedinputs} >= $self->{metadata}->get_data('settings', 'max_missed_inputs')) { $self->send_message($self->{channel}, "$color{red}$self->{state_data}->{players}->[$i]->{name} has missed too many prompts and has been ejected from the game!$color{reset}"); splice @{$self->{state_data}->{players}}, $i--, 1; $removed = 1; @@ -1127,7 +1119,7 @@ sub run_one_state { } # dump new state data for logging/debugging - if ($state_data->{newstate} and $self->{metadata}->{hash}->{settings}->{debug_state}) { + if ($state_data->{newstate} and $self->{metadata}->get_data('settings', 'debug_state')) { $self->{pbot}->{logger}->log("Spinach: New state: $self->{previous_state} ($state_data->{previous_result}) --> $self->{current_state}\n" . Dumper $state_data); } @@ -1645,14 +1637,16 @@ sub choosecategory { my @choices; my @categories; - if (exists $self->{metadata}->{hash}->{filter}->{category_include_filter} and length $self->{metadata}->{hash}->{filter}->{category_include_filter}) { - @categories = grep { /$self->{metadata}->{hash}->{filter}->{category_include_filter}/i } keys %{$self->{categories}}; + if ($self->{metadata}->exists('filter', 'category_include_filter') and length $self->{metadata}->get_data('filter', 'category_include_filter')) { + my $filter = $self->{metadata}->get_data('filter', 'category_include_filter'); + @categories = grep { /$filter/i } keys %{$self->{categories}}; } else { @categories = keys %{$self->{categories}}; } - if (exists $self->{metadata}->{hash}->{filter}->{category_exclude_filter} and length $self->{metadata}->{hash}->{filter}->{category_exclude_filter}) { - @categories = grep { $_ !~ /$self->{metadata}->{hash}->{filter}->{category_exclude_filter}/i } @categories; + if ($self->{metadata}->exists('filter', 'category_exclude_filter') and length $self->{metadata}->get_data('filter', 'category_exclude_filter')) { + my $filter = $self->{metadata}->get_data('filter', 'category_exclude_filter'); + @categories = grep { $_ !~ /$filter/i } @categories; } my $no_infinite_loops = 0; @@ -1667,17 +1661,17 @@ sub choosecategory { next; } - if (exists $self->{metadata}->{hash}->{settings}->{min_difficulty}) { - @questions = grep { $self->{categories}{$cat}{$_}->{value} >= $self->{metadata}->{hash}->{settings}->{min_difficulty} } @questions; + if ($self->{metadata}->exists('settings', 'min_difficulty')) { + @questions = grep { $self->{categories}{$cat}{$_}->{value} >= $self->{metadata}->get_data('settings', 'min_difficulty') } @questions; } - if (exists $self->{metadata}->{hash}->{settings}->{max_difficulty}) { - @questions = grep { $self->{categories}{$cat}{$_}->{value} <= $self->{metadata}->{hash}->{settings}->{max_difficulty} } @questions; + if ($self->{metadata}->exists('settings', 'max_difficulty')) { + @questions = grep { $self->{categories}{$cat}{$_}->{value} <= $self->{metadata}->get_data('settings', 'max_difficulty') } @questions; } - if (exists $self->{metadata}->{hash}->{settings}->{seen_expiry}) { + if ($self->{metadata}->exists('settings', 'seen_expiry')) { my $now = time; - @questions = grep { $now - $self->{categories}{$cat}{$_}->{seen_timestamp} >= $self->{metadata}->{hash}->{settings}->{seen_expiry} } @questions; + @questions = grep { $now - $self->{categories}{$cat}{$_}->{seen_timestamp} >= $self->{metadata}->get_data('settings', 'seen_expiry') } @questions; } next if not @questions; @@ -1686,7 +1680,7 @@ sub choosecategory { push @choices, $cat; } - last if @choices == $self->{metadata}->{hash}->{settings}->{category_choices} or @categories < $self->{metadata}->{hash}->{settings}->{category_choices};; + last if @choices == $self->{metadata}->get_data('settings', 'category_choices') or @categories < $self->{metadata}->get_data('settings', 'category_choices'); } if (not @choices) { @@ -1706,7 +1700,7 @@ sub choosecategory { $comma = "; "; } - if ($state->{reroll_category} and not $self->{metadata}->{hash}->{settings}->{category_autopick}) { + if ($state->{reroll_category} and not $self->{metadata}->get_data('settings', 'category_autopick')) { $self->send_message($self->{channel}, "$state->{categories_text}"); } @@ -1730,7 +1724,7 @@ sub choosecategory { if ($state->{ticks} % $tock == 0) { $state->{tocked} = 1; - if (exists $state->{random_category} or $self->{metadata}->{hash}->{settings}->{category_autopick}) { + if (exists $state->{random_category} or $self->{metadata}->get_data('settings', 'category_autopick')) { delete $state->{random_category}; my $category = $state->{category_options}->[rand (@{$state->{category_options}} - 2)]; my $questions = scalar keys %{ $self->{categories}{$category} }; @@ -1788,14 +1782,14 @@ sub getnewquestion { @questions = sort { $self->{categories}{$state->{current_category}}{$a}->{seen_timestamp} <=> $self->{categories}{$state->{current_category}}{$b}->{seen_timestamp} } @questions; my $now = time; - @questions = grep { $now - $self->{categories}{$state->{current_category}}{$_}->{seen_timestamp} >= $self->{metadata}->{hash}->{settings}->{seen_expiry} } @questions; + @questions = grep { $now - $self->{categories}{$state->{current_category}}{$_}->{seen_timestamp} >= $self->{metadata}->get_data('settings', 'seen_expiry') } @questions; - if (exists $self->{metadata}->{hash}->{settings}->{min_difficulty}) { - @questions = grep { $self->{categories}{$state->{current_category}}{$_}->{value} >= $self->{metadata}->{hash}->{settings}->{min_difficulty} } @questions; + if ($self->{metadata}->exists('settings', 'min_difficulty')) { + @questions = grep { $self->{categories}{$state->{current_category}}{$_}->{value} >= $self->{metadata}->get_data('settings', 'min_difficulty') } @questions; } - if (exists $self->{metadata}->{hash}->{settings}->{max_difficulty}) { - @questions = grep { $self->{categories}{$state->{current_category}}{$_}->{value} <= $self->{metadata}->{hash}->{settings}->{max_difficulty} } @questions; + if ($self->{metadata}->exists('settings', 'max_difficulty')) { + @questions = grep { $self->{categories}{$state->{current_category}}{$_}->{value} <= $self->{metadata}->get_data('settings', 'max_difficulty') } @questions; } if (not @questions) { @@ -2116,7 +2110,7 @@ sub showlies { last if @liars; if ($player->{truth} ne $state->{correct_answer}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { my $player_id = $self->{stats}->get_player_id($player->{name}, $self->{channel}); my $player_data = $self->{stats}->get_player_data($player_id); $player_data->{bad_guesses}++; @@ -2143,7 +2137,7 @@ sub showlies { my $comma = ''; foreach my $liar (@liars) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { my $player_id = $self->{stats}->get_player_id($liar->{name}, $self->{channel}); my $player_data = $self->{stats}->get_player_data($player_id); $player_data->{players_deceived}++; @@ -2157,7 +2151,7 @@ sub showlies { $liar->{good_lie} = 1; } - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { my $player_id = $self->{stats}->get_player_id($player->{name}, $self->{channel}); my $player_data = $self->{stats}->get_player_data($player_id); $player_data->{bad_guesses}++; @@ -2193,7 +2187,7 @@ sub showtruth { my $comma = ''; my $count = 0; foreach my $player (@{$state->{players}}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $player_id = $self->{stats}->get_player_id($player->{name}, $self->{channel}); $player_data = $self->{stats}->get_player_data($player_id); @@ -2202,14 +2196,14 @@ sub showtruth { } if (exists $player->{deceived}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $self->{stats}->update_player_data($player_id, $player_data); } next; } if (exists $player->{truth} and $player->{truth} eq $state->{correct_answer}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $player_data->{good_guesses}++; $self->{stats}->update_player_data($player_id, $player_data); } @@ -2246,7 +2240,7 @@ sub reveallies { $comma = '; '; if ($player->{good_lie}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { my $player_id = $self->{stats}->get_player_id($player->{name}, $self->{channel}); my $player_data = $self->{stats}->get_player_data($player_id); $player_data->{good_lies}++; @@ -2295,7 +2289,7 @@ sub showfinalscore { my $i = @{$state->{players}}; $state->{finalscores} = []; foreach my $player (sort { $a->{score} <=> $b->{score} } @{$state->{players}}) { - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $player_id = $self->{stats}->get_player_id($player->{name}, $self->{channel}); $player_data = $self->{stats}->get_player_data($player_id); @@ -2319,7 +2313,7 @@ sub showfinalscore { $mentions = "Honorable mentions: $mentions"; } - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $self->{stats}->update_player_data($player_id, $player_data); } @@ -2336,7 +2330,7 @@ sub showfinalscore { $text = sprintf("%15s%-13s%7s", "WINNER: ", $player->{name}, $self->commify($player->{score})); } - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $self->{stats}->update_player_data($player_id, $player_data); } @@ -2408,7 +2402,7 @@ sub getplayers { } } - my $min_players = $self->{metadata}->{hash}->{settings}->{min_players} // 2; + my $min_players = $self->{metadata}->get_data('settings', 'min_players') // 2; if (@$players >= $min_players and not $unready) { $self->send_message($self->{channel}, "All players ready!"); @@ -2460,7 +2454,7 @@ sub getplayers { sub round1 { my ($self, $state) = @_; - if ($self->{metadata}->{hash}->{settings}->{stats}) { + if ($self->{metadata}->get_data('settings', 'stats')) { $self->{stats}->begin; $self->{stats_running} = 1; }