mirror of
https://github.com/RussellChamp/tilde-projects.git
synced 2026-10-28 00:22:34 +00:00
Just some recent changes
This commit is contained in:
1 parent
ce32bc223b
commit
9247e2f2e2
20 files changed
+243
-88
No files matched your search
@@ -40,7 +40,8 @@ and continue asking for word replacement until it runs out of words to replace.
|
||||
* Allow story writers the ability to specify that a single answer should be used multiple times in a story
|
||||
* Maybe specified like {{#a noun#}} or {{#a noun#4}}
|
||||
* Create word-munging options that can be specified for a word to modify given user input
|
||||
* Capital - convert "foo" to "Foo"
|
||||
* AllCaps - convert "bar" to "BAR"
|
||||
* Numeric - convert "3" to "three"
|
||||
* Ordinal - convert "3" to "third"
|
||||
* title - convert "foo" to "Foo"
|
||||
* upper - convert "bar" to "BAR"
|
||||
* lower - convert "bAz" to "baz"
|
||||
* numeric - convert "3" to "three"
|
||||
* ordinal - convert "3" to "third"
|
||||
@@ -1,12 +1,16 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import collections
|
||||
import glob
|
||||
import inflect
|
||||
import random
|
||||
import re
|
||||
|
||||
p = inflect.engine()
|
||||
file_pattern = "/home/*/madlibs/*.madlib"
|
||||
file_regex = re.compile(r'^/home/(.*?)/.*/([^/]*)\.madlib$')
|
||||
word_regex = re.compile(r'{{(.*?)}}')
|
||||
word_regex = re.compile(r'{{(.*?)(#.*?)?(\|.*)?}}')
|
||||
word_replace_regex = re.compile(r'^(.*)#(\d*)$')
|
||||
|
||||
# Take a file path and return a tuple with the title and original path
|
||||
def munge_story(file_name):
|
||||
@@ -25,7 +29,16 @@ def find_stories(limit=999, shuffle=False):
|
||||
|
||||
# Count the number of replacable words in the story
|
||||
def count_words(story):
|
||||
return len(word_regex.findall(story))
|
||||
# words that have a '#' part and match should only be counted once
|
||||
count = 0
|
||||
repeats = []
|
||||
for match in re.finditer(word_regex, story):
|
||||
if match.group(2) is None: #the '#' part
|
||||
count += 1
|
||||
elif match.group(1) + match.group(2) not in repeats:
|
||||
count += 1
|
||||
repeats.append(match.group(1) + match.group(2))
|
||||
return count
|
||||
|
||||
# Count the number of replacable words in the story when given a file path
|
||||
def count_words_file(storyPath):
|
||||
@@ -45,10 +58,38 @@ def find_next_word(story, rand=False):
|
||||
|
||||
return (match.group(0), match.group(1))
|
||||
|
||||
# Replace a word and return the entire story body
|
||||
# Using a query phrase, replace a word and return the entire story body
|
||||
def replace_word(story, query, word):
|
||||
return story.replace(query, word, 1)
|
||||
#return re.sub(query, word, story, 1)
|
||||
rquery = word_regex.search(query)
|
||||
# '{{foo#bar|baz|bang}}' => ('foo', '#bar', '|baz|bang')
|
||||
pipes = [p.lower() for p in (rquery.group(3) if rquery.group(3) is not None else '').strip('|').split('|')]
|
||||
munged_word = process_pipes(word, pipes)
|
||||
story = story.replace(query, munged_word, 1)
|
||||
|
||||
if rquery.group(2) is not None: #if there is a '#' part we replace all instances
|
||||
print "Looking for {{" + rquery.group(1) + rquery.group(2) + ".*?}}"
|
||||
others = re.findall(r'{{' + rquery.group(1) + rquery.group(2) + '.*?}}', story)
|
||||
if len(others) > 0:
|
||||
story = replace_word(story, others[0], word) #iteratively replace the next instance
|
||||
|
||||
return story
|
||||
|
||||
|
||||
# Modify user input based on certain modifying pipes
|
||||
def process_pipes(word, pipes):
|
||||
for pipe in pipes:
|
||||
try:
|
||||
word = {
|
||||
'upper': lambda word: word.upper(),
|
||||
'lower': lambda word: word.lower(),
|
||||
'title': lambda word: word.title(),
|
||||
'numeric': lambda word: p.number_to_words(word),
|
||||
'ordinal': lambda word: p.ordinal(word),
|
||||
collections.defaultdict: lambda word: word
|
||||
}[pipe](word)
|
||||
except Exception:
|
||||
pass # just keep going if an error occurs processing a pipe
|
||||
return word
|
||||
|
||||
# A helper function that will split the story up into chat-printable lengths
|
||||
def yield_lines(line, max_size):
|
||||
|
||||
Binary file not shown.
@@ -59,7 +59,7 @@ def resetGlobals(channel=""):
|
||||
story[channel] = ""
|
||||
nextword[channel] = {}
|
||||
|
||||
def get_stories(channel):
|
||||
def get_stories(channel, botnick):
|
||||
global state
|
||||
global stories
|
||||
state[channel] = State.thinking
|
||||
@@ -73,11 +73,11 @@ def get_stories(channel):
|
||||
for idx, story in enumerate(stories[channel]):
|
||||
sendmsg(channel, "[{}] {} ({} words)".format(idx, story[0], story[2]))
|
||||
time.sleep(0.5 * THROTTLE_FACTOR)
|
||||
sendmsg(channel, "Please select a story by index:")
|
||||
sendmsg(channel, "Please select a story by index by saying '{}: <number>':".format(botnick))
|
||||
state[channel] = State.story_selection
|
||||
|
||||
# Handle user input when the bot is directly addressed
|
||||
def handle_bot_msg(channel, msg):
|
||||
def handle_bot_msg(channel, msg, botnick):
|
||||
global state
|
||||
global stories
|
||||
|
||||
@@ -92,9 +92,9 @@ def handle_bot_msg(channel, msg):
|
||||
if saved_state != State.idle and msg == "!quit":
|
||||
quit_game(channel)
|
||||
elif saved_state == State.idle and msg == "startgame":
|
||||
get_stories(channel)
|
||||
get_stories(channel, botnick)
|
||||
elif saved_state == State.story_selection:
|
||||
handle_story_selection(channel, msg)
|
||||
handle_story_selection(channel, msg, botnick)
|
||||
elif saved_state == State.word_input:
|
||||
handle_story_step(channel, msg)
|
||||
else:
|
||||
@@ -106,7 +106,7 @@ def quit_game(channel):
|
||||
sendmsg(channel, "Ok, quitting the current game.")
|
||||
|
||||
# Handle user input when we are in story selection mode
|
||||
def handle_story_selection(channel, msg):
|
||||
def handle_story_selection(channel, msg, botnick):
|
||||
global stories
|
||||
global state
|
||||
try:
|
||||
@@ -120,19 +120,19 @@ def handle_story_selection(channel, msg):
|
||||
with open(stories[channel][imsg][1], "r") as storyFile:
|
||||
story[channel] = storyFile.read()
|
||||
stories[channel] = {} # Clear out the saved selectable stories in memory
|
||||
story_start(channel)
|
||||
story_start(channel, botnick)
|
||||
except ValueError:
|
||||
sendmsg(channel, "Invalid selection. Try again!")
|
||||
state[channel] = State.story_selection
|
||||
|
||||
# Handle when a story is being started
|
||||
def story_start(channel):
|
||||
def story_start(channel, botnick):
|
||||
global story
|
||||
global state
|
||||
global nextword
|
||||
|
||||
state[channel] = State.thinking
|
||||
sendmsg(channel, "Alright! Let's get started!")
|
||||
sendmsg(channel, "Alright! Let's get started! Say '{}: <word>' to give me words.".format(botnick))
|
||||
nextword[channel] = madlib.find_next_word(story[channel], True)
|
||||
time.sleep(0.5 * THROTTLE_FACTOR)
|
||||
sendmsg(channel, "Give me {}:".format(nextword[channel][1]))
|
||||
@@ -169,7 +169,7 @@ def finish_story(channel):
|
||||
sendmsg(channel, '=' * MAX_LINE)
|
||||
for line in story[channel].splitlines():
|
||||
for part in madlib.yield_lines(line, MAX_LINE):
|
||||
time.sleep(0.4 * THROTTLE_FACTOR)
|
||||
time.sleep(0.6 * THROTTLE_FACTOR)
|
||||
sendmsg(channel, part)
|
||||
padlen = (MAX_LINE - 9)/2
|
||||
mod = (MAX_LINE - 9) % 2
|
||||
@@ -186,10 +186,19 @@ def sendmsg(chan , msg):
|
||||
ircsock.send("PRIVMSG {} :{}\n".format(chan, msg))
|
||||
|
||||
def joinchan(chan):
|
||||
global state
|
||||
state[chan] = State.idle
|
||||
ircsock.send("JOIN {}\n".format(chan))
|
||||
|
||||
def rollcall(channel, botnick):
|
||||
sendmsg(channel, "Do you like MadLibs? Start a collaborative story by saying '{}: startgame'".format(botnick))
|
||||
global state
|
||||
if channel not in state:
|
||||
state[channel] = State.idle
|
||||
|
||||
if state[channel] == State.idle:
|
||||
sendmsg(channel, "Do you like MadLibs? Start a collaborative story by saying '{}: startgame'".format(botnick))
|
||||
else:
|
||||
sendmsg(channel, "A game is already in progress. Say '{}: <word>' to provide me with the next word or '{}: !quit' to stop the current game".format(botnick, botnick))
|
||||
|
||||
def connect(server, channel, botnick):
|
||||
ircsock.connect((server, 6667))
|
||||
@@ -222,7 +231,7 @@ def listen(botnick):
|
||||
rollcall(channel, botnick)
|
||||
elif message.startswith(botnick) == True:
|
||||
botmsg = botmsgre.match(message).group(1)
|
||||
handle_bot_msg(channel, botmsg)
|
||||
handle_bot_msg(channel, botmsg, botnick)
|
||||
|
||||
sys.stdout.flush()
|
||||
time.sleep(1 * THROTTLE_FACTOR)
|
||||
|
||||
Reference in new issue
Block a user