This commit is contained in:
Ben Harris committed 2018-10-09 02:50:10 -04:00
1 parent a8a5de36f2
commit 58753c7234
38 files changed
+4549 -2523

No files matched your search

+237 -102
View File
@@ -20,12 +20,30 @@ import inflect
parser = OptionParser()
parser.add_option("-s", "--server", dest="server", default='127.0.0.1',
help="the server to connect to", metavar="SERVER")
parser.add_option("-c", "--channel", dest="channel", default='#bot_test',
help="the channel to join", metavar="CHANNEL")
parser.add_option("-n", "--nick", dest="nick", default='numberwang_bot',
help="the nick to use", metavar="NICK")
parser.add_option(
"-s",
"--server",
dest="server",
default="127.0.0.1",
help="the server to connect to",
metavar="SERVER",
)
parser.add_option(
"-c",
"--channel",
dest="channel",
default="#bot_test",
help="the channel to join",
metavar="CHANNEL",
)
parser.add_option(
"-n",
"--nick",
dest="nick",
default="numberwang_bot",
help="the nick to use",
metavar="NICK",
)
(options, args) = parser.parse_args()
@@ -43,116 +61,194 @@ guesses = 0
lastGuesser = ""
currentScores = {}
def resetGlobals():
global roundsLeft
global bonusRound
global guesses
global lastGuesser
global currentScores
roundsLeft = 0
bonusRound = 0
guesses = 0
lastGuesser = ""
currentScores.clear()
global roundsLeft
global bonusRound
global guesses
global lastGuesser
global currentScores
roundsLeft = 0
bonusRound = 0
guesses = 0
lastGuesser = ""
currentScores.clear()
def ping(pong):
ircsock.send("PONG {}\n".format(pong))
ircsock.send("PONG {}\n".format(pong))
def sendmsg(chan, msg):
ircsock.send("PRIVMSG " + chan + " :" + msg + "\n")
def sendmsg(chan , msg):
ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(chan):
ircsock.send("JOIN "+ chan +"\n")
ircsock.send("JOIN " + chan + "\n")
def hello():
ircsock.send("PRIVMSG "+ channel +" :Hello!\n")
ircsock.send("PRIVMSG " + channel + " :Hello!\n")
def start_numberwang(channel, user):
if(channel != "#bots"):
ircsock.send("PRIVMSG " + channel + " :Numberwang has been disabled for " + channel + " due to spamminess. Please join " + GOOD_CHAN + " to start a game.\n")
if channel != "#bots":
ircsock.send(
"PRIVMSG "
+ channel
+ " :Numberwang has been disabled for "
+ channel
+ " due to spamminess. Please join "
+ GOOD_CHAN
+ " to start a game.\n"
)
return
print user + " started a game"
print(user + " started a game")
resetGlobals()
ircsock.send("PRIVMSG " + channel + " :It's time for Numberwang!\n")
time.sleep(1)
ircsock.send("PRIVMSG " + channel + " :Here's how to play:\n")
ircsock.send("PRIVMSG " + channel + " :1. There are 10 rounds\n")
ircsock.send("PRIVMSG " + channel + " :2. Each round lasts 10 seconds. You're up against the clock!\n")
ircsock.send("PRIVMSG " + channel + " :3. Play your numbers, as long as they're between 0 and 99.\n")
ircsock.send(
"PRIVMSG "
+ channel
+ " :2. Each round lasts 10 seconds. You're up against the clock!\n"
)
ircsock.send(
"PRIVMSG "
+ channel
+ " :3. Play your numbers, as long as they're between 0 and 99.\n"
)
ircsock.send("PRIVMSG " + channel + " :4. That's Numberwang!\n")
time.sleep(2)
ircsock.send("PRIVMSG " + channel + " :Let's get started!\n")
global roundsLeft
global bonusRound
roundsLeft = random.randint(MIN_ROUNDS,MAX_ROUNDS)
bonusRound = random.randint(2,roundsLeft-1)
print "There will be " + str(roundsLeft) + " rounds with the bonus on round " + str(roundsLeft - bonusRound + 1)
roundsLeft = random.randint(MIN_ROUNDS, MAX_ROUNDS)
bonusRound = random.randint(2, roundsLeft - 1)
print(
"There will be "
+ str(roundsLeft)
+ " rounds with the bonus on round "
+ str(roundsLeft - bonusRound + 1)
)
def print_scores(channel):
scoreStrs = []
first = True
for name in currentScores:
scoreStrs.append(name + " is " + ("also " if not first and random.randint(1,3) == 3 else "") + "on " + str(currentScores[name]))
scoreStrs.append(
name
+ " is "
+ ("also " if not first and random.randint(1, 3) == 3 else "")
+ "on "
+ str(currentScores[name])
)
first = False
ircsock.send("PRIVMSG " + channel + " :" + p.join(scoreStrs) + "!\n")
def guess_numberwang(channel, user, messageText):
global guesses
global lastGuesser
global currentScores
global roundsLeft
print user + " guessed '" + messageText + "'"
guess = re.sub('[^0-9]','',messageText.split()[0]) #must have a number in the first 'word'
if(guess):
if(LIMIT_GUESSING and user == lastGuesser):
ircsock.send("PRIVMSG " + channel + " :" + user + ", you just guessed! Give another player a try!\n")
print(user + " guessed '" + messageText + "'")
guess = re.sub(
"[^0-9]", "", messageText.split()[0]
) # must have a number in the first 'word'
if guess:
if LIMIT_GUESSING and user == lastGuesser:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ", you just guessed! Give another player a try!\n"
)
else:
guesses += 1
lastGuesser = user
###CORRECT GUESS###
if(random.randint(0,10) > 10 - guesses): #the more guesses, the higher the probability
if (
random.randint(0, 10) > 10 - guesses
): # the more guesses, the higher the probability
guesses = 0
lastGuesser = ""
ircsock.send("PRIVMSG " + channel + " :" + user + ": THAT'S NUMBERWANG!\n")
points = random.randint(2,10) * (random.randint(2,4) if roundsLeft == bonusRound else 1)
ircsock.send(
"PRIVMSG " + channel + " :" + user + ": THAT'S NUMBERWANG!\n"
)
points = random.randint(2, 10) * (
random.randint(2, 4) if roundsLeft == bonusRound else 1
)
if user in currentScores.keys():
currentScores[user] += points
else:
currentScores[user] = points
roundsLeft -= 1
time.sleep(2)
if(roundsLeft == 0):
ircsock.send("PRIVMSG " + channel + " :Numberwang is now over. Thank you for playing!\n")
if roundsLeft == 0:
ircsock.send(
"PRIVMSG "
+ channel
+ " :Numberwang is now over. Thank you for playing!\n"
)
ircsock.send("PRIVMSG " + channel + " :Final scores:\n")
print_scores(channel)
save_scores()
else:
print_scores(channel)
newRoundStr = ""
if(roundsLeft == 1):
if roundsLeft == 1:
newRoundStr += "The last round is Wangernumb!"
elif(roundsLeft == bonusRound):
elif roundsLeft == bonusRound:
newRoundStr += "**Bonus Round!**"
else:
newRoundStr += "New Round!"
if(random.randint(1,10) > 8):
if random.randint(1, 10) > 8:
newRoundStr += " Let's rotate the board!"
ircsock.send("PRIVMSG " + channel + " :" + newRoundStr + " Start guessing!\n")
ircsock.send(
"PRIVMSG " + channel + " :" + newRoundStr + " Start guessing!\n"
)
###INCORRECT GUESS###
else:
ircsock.send("PRIVMSG " + channel + " :" + random.choice(["Sorry", "I'm sorry", "No", "Nope"]) + ", " + user + ", " \
+ random.choice(["that's not", "that is not", "that isn't", "that is not", "that won't make", "that will not make"])\
+ " Numberwang!\n")
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ random.choice(["Sorry", "I'm sorry", "No", "Nope"])
+ ", "
+ user
+ ", "
+ random.choice(
[
"that's not",
"that is not",
"that isn't",
"that is not",
"that won't make",
"that will not make",
]
)
+ " Numberwang!\n"
)
def stop_numberwang(channel, user):
print user + " stopped a game"
print(user + " stopped a game")
resetGlobals()
ircsock.send("PRIVMSG " + channel + " :Numberwang has been stopped. No points have been awarded. " + user + " is such a party pooper!\n")
ircsock.send(
"PRIVMSG "
+ channel
+ " :Numberwang has been stopped. No points have been awarded. "
+ user
+ " is such a party pooper!\n"
)
def save_scores():
with open(SCORE_FILE, "r+") as scorefile:
@@ -162,16 +258,22 @@ def save_scores():
for line in scores:
for name in currentScores:
score = line.strip("\n").split("&^%")
if(score[0] == name):
line = score[0] + "&^%" + str(int(score[1]) + currentScores[name]) + "\n"
if score[0] == name:
line = (
score[0]
+ "&^%"
+ str(int(score[1]) + currentScores[name])
+ "\n"
)
del currentScores[name]
break
scorefile.write(line)
for name in currentScores: #new wangers
for name in currentScores: # new wangers
line = name + "&^%" + str(currentScores[name]) + "\n"
scorefile.write(line)
def show_highscores(channel):
with open(SCORE_FILE, "r") as scorefile:
scores = []
@@ -182,83 +284,116 @@ def show_highscores(channel):
ircsock.send("PRIVMSG " + channel + " : ====TOP WANGERS====\n")
for score in scores:
ircsock.send("PRIVMSG " + channel + " :== ~" + score[1] + " (" + str(score[0]) + " points!) ==\n")
ircsock.send(
"PRIVMSG "
+ channel
+ " :== ~"
+ score[1]
+ " ("
+ str(score[0])
+ " points!) ==\n"
)
def show_user_score(channel, user):
with open(SCORE_FILE, "r") as scorefile:
for line in scorefile.readlines():
score = line.strip("\n").split("&^%")
if(user == score[0]):
ircsock.send("PRIVMSG " + channel + " :" + user + ": Your global numberwang score is " + str(score[1]) + "!\n")
if user == score[0]:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Your global numberwang score is "
+ str(score[1])
+ "!\n"
)
return
#if we don't find a score line
ircsock.send("PRIVMSG " + channel + " :" + user + ": You haven't scored any points yet!\n")
# if we don't find a score line
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": You haven't scored any points yet!\n"
)
def rollcall(channel):
ircsock.send("PRIVMSG "+ channel +" :Is it time for Numberwang? It might be! Start a new game with !numberwang or stop a current game with !wangernumb Get your score with !myscore and the list of top wangers with !topwangers\n")
ircsock.send(
"PRIVMSG "
+ channel
+ " :Is it time for Numberwang? It might be! Start a new game with !numberwang or stop a current game with !wangernumb Get your score with !myscore and the list of top wangers with !topwangers\n"
)
def connect(server, channel, botnick):
ircsock.connect((server, 6667))
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :krowbar\n") # user authentication
ircsock.send("NICK "+ botnick +"\n")
ircsock.connect((server, 6667))
ircsock.send(
"USER " + botnick + " " + botnick + " " + botnick + " :krowbar\n"
) # user authentication
ircsock.send("NICK " + botnick + "\n")
joinchan(channel)
joinchan(GOOD_CHAN)
joinchan(channel)
joinchan(GOOD_CHAN)
def get_user_from_message(msg):
try:
i1 = msg.index(':') + 1
i2 = msg.index('!')
return msg[i1:i2]
except ValueError:
return ""
try:
i1 = msg.index(":") + 1
i2 = msg.index("!")
return msg[i1:i2]
except ValueError:
return ""
def listen():
while 1:
while 1:
ircmsg = ircsock.recv(2048)
ircmsg = ircmsg.strip('\n\r')
ircmsg = ircsock.recv(2048)
ircmsg = ircmsg.strip("\n\r")
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
formatted = formatter.format_message(ircmsg)
formatted = formatter.format_message(ircmsg)
if "" == formatted:
continue
if "" == formatted:
continue
# print formatted
# print formatted
split = formatted.split("\t")
time = split[0]
user = split[1]
command = split[2]
channel = split[3]
messageText = split[4]
split = formatted.split("\t")
time = split[0]
user = split[1]
command = split[2]
channel = split[3]
messageText = split[4]
if ircmsg.find(":!numberwang") != -1 and roundsLeft == 0:
start_numberwang(channel, user)
if ircmsg.find(":!numberwang") != -1 and roundsLeft == 0:
start_numberwang(channel, user)
if channel == GOOD_CHAN:
if ircmsg.find(":!wangernumb") != -1 and roundsLeft > 0:
stop_numberwang(channel, user)
if roundsLeft > 0:
guess_numberwang(channel, user, messageText)
if channel == GOOD_CHAN:
if ircmsg.find(":!wangernumb") != -1 and roundsLeft > 0:
stop_numberwang(channel, user)
if roundsLeft > 0:
guess_numberwang(channel, user, messageText)
if ircmsg.find(":!topwangers") != -1:
show_highscores(channel)
if ircmsg.find(":!myscore") != -1:
show_user_score(channel, user)
if ircmsg.find(":!topwangers") != -1:
show_highscores(channel)
if ircmsg.find(":!myscore") != -1:
show_user_score(channel, user)
if ircmsg.find(":!rollcall") != -1:
rollcall(channel)
if ircmsg.find(":!rollcall") != -1:
rollcall(channel)
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
sys.stdout.flush()
time.sleep(1)
sys.stdout.flush()
time.sleep(1)
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(options.server, options.channel, options.nick)