dr.botzo, the IRC bot with Django integration.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

209 lines
8.3 KiB

"""
Seen - track races and those racing them
Copyright (C) 2014 Brian S. Stephan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import re
from races.models import Race, Racer, RaceUpdate
from Module import Module
class Races(Module):
"""Track races and allow for logging updates."""
def register_handlers(self):
"""Hook handler functions into the IRC library."""
self.irc.add_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+join$',
self.join)
self.irc.add_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+leave$',
self.leave)
self.irc.add_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+start$',
self.start)
self.irc.add_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+finish$',
self.finish)
self.irc.add_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+update\s+(.*)$',
self.update)
def unregister_handlers(self):
"""Unhook handler functions into the IRC library."""
self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+join$',
self.join)
self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+leave$',
self.leave)
self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+start$',
self.start)
self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+finish$',
self.finish)
self.irc.remove_global_regex_handler(['pubmsg', 'privmsg'],
r'^!race\s+(\S+)\s+update\s+(.*)$',
self.update)
def join(self, nick, userhost, event, from_admin, groups):
"""Join a race."""
race_id, = groups
try:
race = Race.objects.get(key=race_id)
# is the nick already in the race
try:
racer = Racer.objects.get(race=race, nick=nick)
# ideally they're not in the db, but if they aren't flagged as joined,
# then let them join again i guess
if racer.joined:
return self.irc.reply(event, "you are already in {0:s}.".format(race.name))
except Racer.DoesNotExist:
racer = Racer()
racer.race = race
racer.nick = nick
racer.joined = True
racer.started = False
racer.finished = False
racer.save()
return self.irc.reply(event,
"you have joined {0:s}.".format(race.name))
except Race.DoesNotExist:
return self.irc.reply(event, "race {0:s} not found.".format(race_id))
def leave(self, nick, userhost, event, from_admin, groups):
"""Leave a race."""
race_id, = groups
try:
race = Race.objects.get(key=race_id)
try:
racer = Racer.objects.get(race=race, nick=nick)
if racer.finished:
return self.irc.reply(event, "you can't leave {0:s} since you finished it."
"".format(race.name))
if racer.joined:
racer.joined = False
racer.save()
return self.irc.reply(event, "you have left {0:s}.".format(race.name))
else:
return self.irc.reply(event, "you weren't in {0:s}.".format(race.name))
except Racer.DoesNotExist:
return self.irc.reply(event, "you weren't in {0:s}.".format(race.name))
except Race.DoesNotExist:
return self.irc.reply(event, "race {0:s} not found.".format(race_id))
def start(self, nick, userhost, event, from_admin, groups):
"""Start racing."""
race_id, = groups
try:
race = Race.objects.get(key=race_id)
try:
racer = Racer.objects.get(race=race, nick=nick)
if racer.started:
return self.irc.reply(event, "you have already started {0:s}.".format(race.name))
if racer.finished:
return self.irc.reply(event, "you can't start {0:s} since you finished it."
"".format(race.name))
racer.started = True
racer.save()
# create an event for this too
RaceUpdate.objects.create(race=race, racer=racer, update="Started the race.")
return self.irc.reply(event, "you have started {0:s}.".format(race.name))
except Racer.DoesNotExist:
return self.irc.reply(event, "you have not joined {0:s}.".format(race.name))
except Race.DoesNotExist:
return self.irc.reply(event, "race {0:s} not found.".format(race_id))
def finish(self, nick, userhost, event, from_admin, groups):
"""Finish racing."""
race_id, = groups
try:
race = Race.objects.get(key=race_id)
try:
racer = Racer.objects.get(race=race, nick=nick)
if not racer.started:
return self.irc.reply(event, "you have not started {0:s}.".format(race.name))
if racer.finished:
return self.irc.reply(event, "you have already finished {0:s}."
"".format(race.name))
racer.finished = True
racer.save()
# create an event for this too
RaceUpdate.objects.create(race=race, racer=racer, update="Finished the race.")
return self.irc.reply(event, "you have finished {0:s}.".format(race.name))
except Racer.DoesNotExist:
return self.irc.reply(event, "you have not joined {0:s}.".format(race.name))
except Race.DoesNotExist:
return self.irc.reply(event, "race {0:s} not found.".format(race_id))
def update(self, nick, userhost, event, from_admin, groups):
"""Add an update to an ongoing race."""
race_id, update = groups
try:
race = Race.objects.get(key=race_id)
try:
racer = Racer.objects.get(race=race, nick=nick)
if not racer.started:
return self.irc.reply(event, "you have not started {0:s}.".format(race.name))
if racer.finished:
return self.irc.reply(event, "you have already finished {0:s}."
"".format(race.name))
# create an event
RaceUpdate.objects.create(race=race, racer=racer, update=update)
return self.irc.reply(event, "update logged.")
except Racer.DoesNotExist:
return self.irc.reply(event, "you have not joined {0:s}.".format(race.name))
except Race.DoesNotExist:
return self.irc.reply(event, "race {0:s} not found.".format(race_id))
# vi:tabstop=4:expandtab:autoindent