package LasTres::Redis;

use v5.36.0;

use strict;
use warnings;

use feature 'signatures';

use parent 'Mojo::Redis';

use LasTres::Schema;
our $VERSION = $LasTres::Schema::VERSION;

{
    my $self;

    sub new {
        my $class = shift;
        if ( !defined $self ) {
            my $redis_file = '/var/run/redis/redis.sock';
            if (-e $redis_file) {
                $self = $class->SUPER::new(
                    Mojo::URL->new->host($redis_file) );
            } else {
                $self = $class->SUPER::new(@_);
            }
        }
        return $self;
    }
}

sub prefix {
    return "LasTres::Redis::$VERSION";
}

sub subscribe ( $self, $topic, $callback ) {
    $self->pubsub->listen(
        $topic,
        sub ( $self, $message, $topic ) {
            $callback->( $message, $topic, [$topic] );
        }
    );
}

sub publish ( $self, $topic, $message ) {
    $self->pubsub->notify( $topic, $message );
}

sub pj_subscription ( $class, $pj ) {
    return prefix() . '::Subscriptions::PJ::' . $pj->uuid;
}

sub battle_key ( $class, $battle_uuid ) {
    return prefix() . '::Key::Battle' . $battle_uuid;
}

sub executing_frame_key ($class) {
    return prefix() . '::Key::Frame';
}
1;
