Subspace Hockey/Football Zone Forums

Please login or register.

Login with username, password and session length

Author Topic: Doobie made... a timer!  (Read 416 times)

Doobie

  • Local Moderator
  • Registered Posting Whore
  • *****
  • Posts: 2011
Doobie made... a timer!
« on: January 11, 2017, 06:06:59 PM »

Posting here because I don't have access to Public Development.  I am assuming it will be moved later.

I have been poking around at the server, modules, and python lately.  So when RKDN mentioned a timer capability last night I figured it would be a good start, despite how unsexy.  The lack of mod-managed central public timer was surprisingly problematic in hosting events.

Next Steps: A Sysop has to insert/attach the module, the conf file has to be edited to enable the ?starttimer and ?stoptimer commands for mods and above, and the ?timeleft command for everyone.

Chances are this will never get heavy use as I am working on a general "game" module that will do the bare minimum for running a hockey game(voting, timing, scorekeeping, etc) which I hope to have "done" in time for the next 5v5 event.  The key feature of the "game" module being that it will run any number of games in an arena with minimal configuration, regardless of rink/team/settings.  I thought the events were good and can be made great/frequent with a little automation.

Without further suspense... the code!  Nothing sexy, still needs docs for the commands, but... it works!

Code: [Select]
# Made by Doobie yadda yadda

import asss


TICK_RATE_HUNDREDTHS = 100
MAX_TIME_MINUTES = 30
MIN_TIME_MINUTES = 1


# Get ASSS interfaces
ichat = asss.get_interface(asss.I_CHAT)


def c_start_timer(cmd, params, player, targ):
    """
    "Syntax:\n"
    "  ?starttimer <time_in_minute>\n"
    "Example:\n"
    "  ?starttimer 12\n"
    """

    if player.arena.doob_timer is not None:
        ichat.SendMessage(player, "Timer already exists, cancel before creating new timer")
        return

    try:
        minutes = int(params)
    except ValueError:
        ichat.SendMessage(player, "Please specify time in minutes (?starttimer 12)")
        return

    if minutes > MAX_TIME_MINUTES:
        ichat.SendMessage(player, "Maximum timer value is %d minutes" % MAX_TIME_MINUTES)
        return

    if minutes < MIN_TIME_MINUTES:
        ichat.SendMessage(player, "Minimum timer value is %d minute" % MIN_TIME_MINUTES)
        return

    start_timer(player.arena, minutes)


def start_timer(arena, minutes):
    hundredths = minutes * 60 * 100
    arena.doob_ticks_left = hundredths
    arena.doob_timer = make_second_tick_timer(arena)
    ichat.SendArenaMessage(arena, "Timer started. %s" % get_time_string(arena))


def c_time(cmd, params, player, targ):
    if player.arena.doob_timer is None:
        ichat.SendMessage(player, "No timer available")
    else:
        ichat.SendMessage(player, "Time left %s" % get_time_string(player.arena))


def c_stop_timer(cmd, params, player, targ):
    if player.arena.doob_timer is None:
        ichat.SendMessage(player, "No timer to stop")
        return

    ichat.SendArenaMessage(player.arena, "Timer stopped")
    player.arena.doob_timer = None
    player.arena.doob_time_left = None


def make_second_tick_timer(arena):
    def second_timer():
        if arena.doob_ticks_left == 0:
            ichat.SendArenaMessage(arena, "Time is up!")
            arena.doob_timer = None
            arena.doob_ticks_left = None
            return 0
        else:
            arena.doob_ticks_left -= TICK_RATE_HUNDREDTHS
            if arena.doob_ticks_left % 6000 == 0:
                ichat.SendArenaMessage(arena, "Time left %s" % get_time_string(arena))
            return 1

    return asss.set_timer(second_timer, TICK_RATE_HUNDREDTHS, TICK_RATE_HUNDREDTHS)


def get_time_string(arena):
    totalseconds = arena.doob_ticks_left / 100
    mins, secs = divmod(totalseconds, 60)
    timeformat = '{:02d}:{:02d}'.format(mins, secs)
    return timeformat


def mm_attach(a):
    a.doob_timer = None
    a.doob_ticks_left = None
    a.doob_start_timer_cmd = asss.add_command("starttimer", c_start_timer, a)
    a.doob_time_left_cmd = asss.add_command("timeleft", c_time, a)
    a.doob_stop_timer_cmd = asss.add_command("stoptimer", c_stop_timer, a)


def mm_detach(a):
    a.doob_timer = None
    a.doob_ticks_left = None
    for attr in ['start_timer_cmd', 'time_left_cmd', 'stop_timer_cmd']:
        try:
            delattr(a, 'doob_' + attr)
        except:
            pass

« Last Edit: January 11, 2017, 06:09:45 PM by Doobie »
Logged

Goldeye

  • Local Moderator
  • My post count proves I'm better than you
  • *****
  • Posts: 5363
Re: Doobie made... a timer!
« Reply #1 on: January 11, 2017, 06:31:27 PM »

Looks good, I'll put it up on the server.   I called it 'simpletimer' and did s/doob/simpletimer/
Dunno when I can log in so Arc or Dan can insmod it.  I forget the format for py; `?insmod <py> simpletimer` or something like that.
Logged
Shlazzer> dont you ppl realize once our sun goes supernova, NOTHING anyone or anything has EVER done or said on this planet will EVER matter?

Thrill> also i have a gr8 personality

I made $124.03 for a single season of HZ!
Nubby> U could b 3rd highest payed player
Nubby> Maybe 2nd

Lawn Dwarf

  • Registered Posting Whore
  • *
  • Posts: 1221
Re: Doobie made... a timer!
« Reply #2 on: January 11, 2017, 06:46:54 PM »

Thanks Doober!
Logged

BlueGoku

  • Choke Artist
  • Ambassador
  • Out of Control
  • *****
  • Posts: 8386
    • Center Ice
Re: Doobie made... a timer!
« Reply #3 on: January 11, 2017, 07:27:18 PM »

Good stuff.
Logged
image

Doobie

  • Local Moderator
  • Registered Posting Whore
  • *****
  • Posts: 2011
Re: Doobie made... a timer!
« Reply #4 on: January 11, 2017, 08:38:19 PM »

?insmod <py> name_of_mod

keeping the <py> and name of mod is the file name minus the .py

I think cmd_starttimer and cmd_stoptimer will be there in the group.dir from hz_common but ?timeleft for default will have to be added.  Unless there is a way to add .conf within the zone?
Logged

Goldeye

  • Local Moderator
  • My post count proves I'm better than you
  • *****
  • Posts: 5363
Re: Doobie made... a timer!
« Reply #5 on: January 11, 2017, 08:41:34 PM »

I did the conf.
Logged
Shlazzer> dont you ppl realize once our sun goes supernova, NOTHING anyone or anything has EVER done or said on this planet will EVER matter?

Thrill> also i have a gr8 personality

I made $124.03 for a single season of HZ!
Nubby> U could b 3rd highest payed player
Nubby> Maybe 2nd

Doobie

  • Local Moderator
  • Registered Posting Whore
  • *****
  • Posts: 2011
Re: Doobie made... a timer!
« Reply #6 on: January 15, 2017, 08:47:30 PM »

Tried having RKDN run ?insmod <py> simpletimer and it failed.

Not sure if the simpletimer is in the bin directory?
Logged

Doobie

  • Local Moderator
  • Registered Posting Whore
  • *****
  • Posts: 2011
Re: Doobie made... a timer!
« Reply #7 on: January 16, 2017, 03:01:25 PM »

Had a small bug(that isn't the reason why attempting to add it failed *cough*

arena.doob_time_left didn't exist anymore, changed it to arena.doob_ticks_left in every location but one

Code: [Select]
# Made by Doobie yadda yadda

import asss


TICK_RATE_HUNDREDTHS = 100
MAX_TIME_MINUTES = 30
MIN_TIME_MINUTES = 1


# Get ASSS interfaces
ichat = asss.get_interface(asss.I_CHAT)


def c_start_timer(cmd, params, player, targ):
    """
    "Syntax:\n"
    "  ?starttimer <time_in_minute>\n"
    "Example:\n"
    "  ?starttimer 12\n"
    """

    if player.arena.doob_timer is not None:
        ichat.SendMessage(player, "Timer already exists, cancel before creating new timer")
        return

    try:
        minutes = int(params)
    except ValueError:
        ichat.SendMessage(player, "Please specify time in minutes (?starttimer 12)")
        return

    if minutes > MAX_TIME_MINUTES:
        ichat.SendMessage(player, "Maximum timer value is %d minutes" % MAX_TIME_MINUTES)
        return

    if minutes < MIN_TIME_MINUTES:
        ichat.SendMessage(player, "Minimum timer value is %d minute" % MIN_TIME_MINUTES)
        return

    start_timer(player.arena, minutes)


def start_timer(arena, minutes):
    hundredths = minutes * 60 * 100
    arena.doob_ticks_left = hundredths
    arena.doob_timer = make_second_tick_timer(arena)
    ichat.SendArenaMessage(arena, "Timer started. %s" % get_time_string(arena))


def c_time(cmd, params, player, targ):
    if player.arena.doob_timer is None:
        ichat.SendMessage(player, "No timer available")
    else:
        ichat.SendMessage(player, "Time left %s" % get_time_string(player.arena))


def c_stop_timer(cmd, params, player, targ):
    if player.arena.doob_timer is None:
        ichat.SendMessage(player, "No timer to stop")
        return

    ichat.SendArenaMessage(player.arena, "Timer stopped")
    player.arena.doob_timer = None
    player.arena.doob_ticks_left = None


def make_second_tick_timer(arena):
    def second_timer():
        if arena.doob_ticks_left is None:#cancelled
            return 0
        if arena.doob_ticks_left == 0:
            ichat.SendArenaMessage(arena, "Time is up!")
            arena.doob_timer = None
            arena.doob_ticks_left = None
            return 0
        else:
            print "Left", arena.doob_ticks_left
            arena.doob_ticks_left -= TICK_RATE_HUNDREDTHS
            if arena.doob_ticks_left % 6000 == 0:
                ichat.SendArenaMessage(arena, "Time left %s" % get_time_string(arena))
            return 1

    return asss.set_timer(second_timer, TICK_RATE_HUNDREDTHS, TICK_RATE_HUNDREDTHS)


def get_time_string(arena):
    totalseconds = arena.doob_ticks_left / 100
    mins, secs = divmod(totalseconds, 60)
    timeformat = '{:02d}:{:02d}'.format(mins, secs)
    return timeformat


def mm_attach(a):
    a.doob_timer = None
    a.doob_ticks_left = None
    a.doob_start_timer_cmd = asss.add_command("starttimer", c_start_timer, a)
    a.doob_time_left_cmd = asss.add_command("timeleft", c_time, a)
    a.doob_stop_timer_cmd = asss.add_command("stoptimer", c_stop_timer, a)


def mm_detach(a):
    a.doob_timer = None
    a.doob_ticks_left = None
    for attr in ['start_timer_cmd', 'time_left_cmd', 'stop_timer_cmd']:
        try:
            delattr(a, 'doob_' + attr)
        except:
            pass
Logged
 

Page created in 0.061 seconds with 19 queries.