Debian Codegraph @

python/CFCampfire.py

raw
1 # CFCampfire.py - Talking campfire-related classes
2 #
3 # Copyright (C) 2006 Nicolas Weeger
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19
20 import Crossfire
21 import random
22 import os
23
24 key_status = 'campfire_status'
25 key_line = 'campfire_line'
26 key_story = 'campfire_story'
27 status_read = 'read'
28 status_pause = 'pause'
29
30 class CFCampfire:
31 '''Main handling class.'''
32
33 def say(self):
34 '''Called when the player talks to the firecamp.'''
35 w = Crossfire.WhoAmI()
36
37 if ( Crossfire.WhatIsMessage() == 'story' ):
38 if w.ReadKey(key_status) == '':
39 w.Say('So you want a story, he... All right.')
40 if w.ReadKey(key_story) == '':
41 w.WriteKey(key_story,self.get_story(), 1)
42 w.WriteKey(key_status, status_read, 1)
43 w.WriteKey(key_line, '0', 1)
44 w.CreateTimer(2,1)
45 elif ( Crossfire.WhatIsMessage() == 'pause' ):
46 if w.ReadKey(key_status) == status_read:
47 w.Say('Sure, let\'s rest some.')
48 w.WriteKey(key_status, status_pause, 1)
49 elif ( Crossfire.WhatIsMessage() == 'resume' ):
50 if w.ReadKey(key_status) == status_pause:
51 w.Say('Ok, let\'s continue.')
52 w.WriteKey(key_status, status_read, 1)
53 elif ( Crossfire.WhatIsMessage() == 'stop' ):
54 if w.ReadKey(key_status) != '':
55 w.WriteKey(key_status, '', 1)
56 w.WriteKey(key_story,'', 1)
57 w.Say('Hump, very well, I\'ll keep quiet from now on.')
58
59 def get_story(self):
60 '''Returns a random story from the stories directory, internal use.'''
61 story_dir = os.path.join( Crossfire.DataDirectory(), 'stories' )
62 stories = os.listdir(story_dir)
63 return stories[random.randint(0, len(stories) - 1)]
64
65 def timer(self):
66 '''Called when the firecamp's timer expire. Sends more of the story.'''
67 w = Crossfire.WhoAmI()
68 if w.ReadKey(key_status) == '':
69 # Been stopped in talking, probably.
70 return
71
72 line = int('0' + w.ReadKey(key_line))
73
74 if w.ReadKey(key_status) == status_pause:
75 w.CreateTimer(1,1)
76 return
77
78 f = open(os.path.join( Crossfire.DataDirectory(), 'stories', w.ReadKey(key_story)), 'r')
79 c = f.read().split('\n')
80 if line >= len(c):
81 w.Say('End of story.')
82 w.WriteKey(key_status, '', 1)
83 w.WriteKey(key_line, '0', 1)
84 w.WriteKey(key_story, '', 1)
85 else:
86 w.Say(c[line])
87 w.CreateTimer(len(c[line]) / 10,1)
88 line = line + 1
89 w.WriteKey(key_line, str(line), 1)