Storycraft: write completed story to disk via !storycraft export #

This commit is contained in:
Brian S. Stephan 2011-01-09 21:41:23 -06:00
parent e39d55f03d
commit e9c22d33f3
1 changed files with 64 additions and 0 deletions

View File

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import random
import re
import sqlite3
import time
from Module import Module
@ -79,6 +80,7 @@ class Storycraft(Module):
startgamepattern = rootcommand + '\s+start\s+game\s+(\d+)$'
showlinepattern = rootcommand + '\s+game\s+(\d+)\s+show\s+line$'
addlinepattern = rootcommand + '\s+game\s+(\d+)\s+add\s+line\s+(.*)$'
exportpattern = rootcommand + '\s+export\s+(\d+)$'
self.statusre = re.compile(statuspattern)
self.newgamere = re.compile(newgamepattern)
@ -87,6 +89,7 @@ class Storycraft(Module):
self.startgamere = re.compile(startgamepattern)
self.showlinere = re.compile(showlinepattern)
self.addlinere = re.compile(addlinepattern)
self.exportre = re.compile(exportpattern)
def db_init(self):
"""Set up the database tables, if they don't exist."""
@ -172,6 +175,8 @@ class Storycraft(Module):
return self.storycraft_showline(connection, event, nick, userhost, what, admin_unlocked)
elif self.addlinere.search(what):
return self.storycraft_addline(connection, event, nick, userhost, what, admin_unlocked)
elif self.exportre.search(what):
return self.storycraft_export(connection, event, nick, userhost, what, admin_unlocked)
def storycraft_status(self, connection, event, nick, userhost, what, admin_unlocked):
"""Print information about the storycraft games, or one specific game."""
@ -474,6 +479,34 @@ class Storycraft(Module):
else:
return 'Game #{0:d} does not exist.'.format(game_id)
def storycraft_export(self, connection, event, nick, userhost, what, admin_unlocked):
"""Provide the story for access outside of the bot."""
match = self.exportre.search(what)
if match:
game_id = int(match.group(1))
# retrieve the game
game = self._get_game_details(game_id)
if game:
# check that the game is completed
if 'COMPLETED' == game.status:
# get the most recent line
lines = self._get_lines_for_game(game_id)
if lines:
# write the story to disk
fn = self._export_game_to_disk(game, lines)
if fn:
return 'Story written as {0:s}.'.format(fn)
else:
return 'Error writing story to disk.'
else:
return 'Game #{0:d} has no lines - internal consistency error.'.format(game_id)
else:
return 'Game #{0:d} has not been completed.'.format(game_id)
else:
return 'Game #{0:d} does not exist.'.format(game_id)
def _get_game_details(self, game_id):
"""Get the details of one game."""
@ -987,6 +1020,37 @@ class Storycraft(Module):
print('sqlite error: ' + str(e))
raise
def _export_game_to_disk(self, game, lines):
"""Write a game to disk."""
filename = 'storycraft-{0:0000d}-{1:s}-{2:s}.txt'.format(game.id, game.owner_nick, time.strftime('%Y%m%d%H%M%S'))
f = open(filename, 'w')
lines_by_player = {}
line_count = 0
# the array is in LIFO, so reverse it
lines.reverse()
for line in lines:
# get player for this line
player = self._get_player_by_id(line.player_id)
line_count = line_count + 1
if not player.nick in lines_by_player:
lines_by_player[player.nick] = []
lines_by_player[player.nick].append(str(line_count))
f.write(line.line + '\n')
legend = ''
f.write('\n')
for player in lines_by_player.keys():
f.write(player + ':' + ','.join(lines_by_player[player]) + '\n')
f.close()
return filename
def _get_storycraft_settings(self):
"""Get the server settings."""