pyircbot/pyircbot/modules/DuckHunt.py

171 lines
5.9 KiB
Python
Raw Normal View History

2014-09-09 21:07:37 -07:00
#!/usr/bin/env python
2014-10-02 18:14:42 -07:00
"""
.. module:: DuckHunt
:synopsis: An animal hunting IRC game
2014-10-02 18:14:42 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-01-01 14:59:01 -08:00
from pyircbot.modulebase import ModuleBase, ModuleHook
2014-09-09 21:07:37 -07:00
import time
import json
2014-09-09 21:07:37 -07:00
import random
from threading import Timer
import os
2017-01-01 14:59:01 -08:00
2014-09-09 21:07:37 -07:00
class DuckHunt(ModuleBase):
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
self.hooks = [ModuleHook("PRIVMSG", self.hunt)]
self.jsonPath = self.getFilePath("scores.json")
2017-01-01 14:59:01 -08:00
self.timer = None
self.isDuckOut = False
self.outStart = 0
self.misses = {}
2017-01-01 14:59:01 -08:00
self.startHunt()
2017-01-01 14:59:01 -08:00
def hunt(self, args, prefix, trailing):
prefixObj = self.bot.decodePrefix(prefix)
fromWho = prefixObj.nick
2017-01-01 14:59:01 -08:00
cmd = self.bot.messageHasCommand("!huntscore", trailing, False)
if cmd:
scores = self.loadScores()
2017-01-01 14:59:01 -08:00
if fromWho not in scores:
self.bot.act_PRIVMSG(fromWho, "You have no points :(")
else:
scores = scores[fromWho]
kills = 0
runts = 0
prime = 0
weight = 0.0
shots = 0
misses = 0
for kill in scores:
if kill["prime"]:
2017-01-01 14:59:01 -08:00
prime += 1
if kill["runt"]:
2017-01-01 14:59:01 -08:00
runts += 1
kills += 1
weight += kill["weight"]
shots += 1
shots += kill["misses"]
misses += kill["misses"]
self.bot.act_PRIVMSG(fromWho, "You've shot %s %s for a total weight of %s lbs." %
(kills, self.config["animalSpeciesPlural"], weight))
self.bot.act_PRIVMSG(fromWho, "%s prime catches, %s runts, %s bullets used and %s misses." %
(prime, runts, shots, misses))
# self.bot.act_PRIVMSG(fromWho, "More info & highscores: http://duckhunt.xmopx.net/")
self.log.info("DuckHunt: %s used !huntscore" % fromWho)
return
2017-01-01 14:59:01 -08:00
# Channel only
2017-01-01 14:59:01 -08:00
if not args[0][0] == "#":
return
2017-01-01 14:59:01 -08:00
cmd = self.bot.messageHasCommand("!shoot", trailing, False)
if cmd:
if self.isDuckOut:
2017-01-01 14:59:01 -08:00
if fromWho not in self.misses:
self.misses[fromWho] = 0
shotIn = round(time.time() - self.outStart, 2)
2017-01-01 14:59:01 -08:00
if random.randint(0, 100) <= self.config["missChance"]:
2017-01-01 14:59:01 -08:00
self.bot.act_PRIVMSG(self.config["activeChannel"], "%s fires after %s seconds and misses!" %
(fromWho, shotIn))
self.misses[fromWho] += 1
return
2017-01-01 14:59:01 -08:00
self.isDuckOut = False
2017-01-01 14:59:01 -08:00
bagged = {
2017-01-01 14:59:01 -08:00
"species": self.config["animalSpecies"],
"gender": "M" if random.randint(0, 1) == 1 else "F",
"time": shotIn,
"prime": False,
"runt": False,
"weight": 0.0,
"date": time.time(),
"misses": self.misses[fromWho]
}
2017-01-01 14:59:01 -08:00
message = "%s %s " % (fromWho, "bags")
2017-01-01 14:59:01 -08:00
if random.randint(0, 100) <= self.config["primeChance"]:
2017-01-01 14:59:01 -08:00
bagged["prime"] = True
bagged["weight"] = self.getRandWeight(self.config["weightMax"], self.config["weightFat"])
message += "a prime catch, a "
elif random.randint(0, 100) <= self.config["runtChance"]:
2017-01-01 14:59:01 -08:00
bagged["runt"] = True
bagged["weight"] = self.getRandWeight(self.config["weightRunt"], self.config["weightMin"])
message += "a runt of a catch, a "
else:
2017-01-01 14:59:01 -08:00
bagged["weight"] = self.getRandWeight(self.config["weightMin"], self.config["weightMax"])
message += "a "
2017-01-01 14:59:01 -08:00
message += "%s lb " % (bagged["weight"])
2017-01-01 14:59:01 -08:00
if bagged["gender"] == "M":
message += self.config["animalNameMale"] + " "
else:
2017-01-01 14:59:01 -08:00
message += self.config["animalNameFemale"] + " "
message += "in %s seconds!" % shotIn
self.bot.act_PRIVMSG(self.config["activeChannel"], message)
2017-01-01 14:59:01 -08:00
self.addKillFor(fromWho, bagged)
2017-01-01 14:59:01 -08:00
self.misses = {}
2017-01-01 14:59:01 -08:00
self.startHunt()
def startHunt(self):
" Creates a timer that waits a certain amount of time then sends out a bird \\_o< quack"
2017-01-01 14:59:01 -08:00
delay = self.config["delayMin"] + random.randint(0, self.config["delayMax"] - self.config["delayMin"])
self.timer = Timer(delay, self.duckOut)
self.timer.start()
print("DuckHunt: Sending out animal in %s seconds" % delay)
2017-01-01 14:59:01 -08:00
def duckOut(self):
self.isDuckOut = True
self.bot.act_PRIVMSG(self.config["activeChannel"], self.config["animal"])
self.outStart = time.time()
2017-01-01 14:59:01 -08:00
def getRandWeight(self, minW, maxW):
2017-01-01 14:59:01 -08:00
weight = maxW - minW
weight = float(weight) * random.random()
return round(weight + minW, 2)
def getScoreFor(self, playername):
return 1
2017-01-01 14:59:01 -08:00
def getScoresFor(self, playername):
return 1
2017-01-01 14:59:01 -08:00
def addKillFor(self, playername, kill):
scores = self.loadScores()
2017-01-01 14:59:01 -08:00
if scores is None:
scores = {}
2017-01-01 14:59:01 -08:00
if playername not in scores:
scores[playername] = []
scores[playername].append(kill)
self.saveScores(scores)
2017-01-01 14:59:01 -08:00
def loadScores(self):
if not os.path.exists(self.jsonPath):
json.dump({}, open(self.jsonPath, 'w'))
return json.load(open(self.jsonPath, 'r'))
2017-01-01 14:59:01 -08:00
def saveScores(self, scores):
json.dump(scores, open(self.jsonPath, 'w'))
2017-01-01 14:59:01 -08:00
def ondisable(self):
self.timer.cancel()