/
mob.py
executable file
·204 lines (177 loc) · 6.26 KB
/
mob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
import sys
import traceback
import unittest
import pickle
from pymud.chainedmap import ChainedMap
from pymud.coroutine import step
from pymud.interpreter import interpret
from pymud.message import Channel,RepeaterMixin
from pymud.formatter import ColorTextFormatter
from pymud.container import SlottedContainer
from pymud.sim import Sim
from pymud.scriptable import Scriptable
from pymud.persist import P,getP
from pymud.exceptions import *
from commands import *
class Mob(RepeaterMixin,Channel,SlottedContainer,Scriptable,Sim):
commands = ChainedMap(map={ 'say':say,
'look': look,
'map': lookMap,
'chat': chat,
'help': help,
'commands': commands,
'do': do,
'go': go,
'wander': wander,
'script': script,
'trigger': trigger,
'get': get,
'drop': drop,
'use': use,
'inventory': inventory,
'equipment': equipment,
'wear': wear,
'remove': remove,
'quit': quit,
'EOF': quit,
'description': description,
'break': breakCommand,
'wait': wait,
'stop': stop,
'set':setVariable})
scripts = ChainedMap(map={'hi':'say hi\n'})
triggers = ChainedMap()
conditions = ChainedMap(map={'alive':True})
location = P.null
description = "an ugly son of a mob"
detail = "a really ugly son of a mob"
attributes = ['mob']
name = 'mob'
interface = None
ticksPerTurn = 5
def __init__( self,
variables=None,
commands=None,
id=None):
Channel.__init__(self)
SlottedContainer.__init__(self)
Scriptable.__init__(self)
Sim.__init__(self)
self.id = id
self.location = P.null
self.interface = None
if variables:
self.variables = variables
if commands:
self.commands = commands
def default(self,args):
say(*[self] + args)
def getOtherCommands(self):
commands = []
for item in self.slots.values():
commands.append(item().commands)
return commands
def __setstate__(self,state):
self.__dict__ = state.copy()
self.commands.parent = self.__class__.commands
self.commands.mapsFunc = self.getOtherCommands
self.scripts.parent = self.__class__.scripts
self.triggers.parent = self.__class__.triggers
self.conditions.parent = self.__class__.conditions
self.commandScript = None
self.scriptsQueue = []
self.interface = None
def __getstate__(self):
state = self.__dict__.copy()
del state['commandScript']
del state['scriptsQueue']
del state['interface']
return state
def applyCommand(self,command,arguments=[]):
func = self.commands[command]
if hasattr(func,'im_self') and func.im_self:
func(*arguments)
else:
func(*[self] + arguments)
def seen(self,o):
o.sendMessage("look",description=self.detail)
def receiveMessage(self,message):
RepeaterMixin.receiveMessage(self,message)
Scriptable.receiveMessage(self,message)
class SlowMob(Mob):
def __init__(self,id=None):
Mob.__init__(self,id)
self.ticksPerTurn = 100
class ReallySlowMob(Mob):
def __init__(self,id=None):
Mob.__init__(self,id)
self.ticksPerTurn = 1000
class Test(unittest.TestCase, ColorTextFormatter):
def setUp(self):
self.id = None
def receiveMessage(self,message):
sys.stdout.write("\n")
sys.stdout.write(self.formatMessage(message))
sys.stdout.write("\n")
sys.stdout.flush()
def testSayHi(self):
from pymud.persist import P
amob = Mob()
amob.addListener(P(self))
say(amob,"hi")
amob.applyCommand("say",["hi"])
def testSet(self):
amob = Mob()
setVariable(amob,'a','5')
self.assertEquals(amob.variables['a'],'5')
del amob.variables['a']
amob.applyCommand("set",["a","5"])
self.assertEquals(amob.variables['a'],'5')
def testPickleEmpty(self):
mob1 = Mob()
out = pickle.dumps(mob1,pickle.HIGHEST_PROTOCOL)
mob2 = pickle.loads(out)
self.assertNotEquals(mob1,mob2)
def testPickleVariables(self):
from pymud.persist import P
mob1 = Mob()
mob1.addListener(P(self))
setVariable(mob1,'a','5')
say(mob1,'hi1')
self.assertEquals(mob1.variables['a'],'5')
out = pickle.dumps(mob1,pickle.HIGHEST_PROTOCOL)
mob2 = pickle.loads(out)
mob2.addListener(P(self))
self.assertEquals(mob2.variables['a'],'5')
say(mob2,'hi2')
def testPickleCommands(self):
from pymud.persist import P
mob1 = Mob()
mob1.addListener(P(self))
mob1.commands['uber'] = uber
mob1.applyCommand('say',['hi'])
mob1.applyCommand('uber')
out = pickle.dumps(mob1,pickle.HIGHEST_PROTOCOL)
#print out
mob2 = pickle.loads(out)
mob2.addListener(P(self))
mob2.applyCommand('say',['hi'])
mob2.applyCommand('uber')
def testRunEmpty(self):
amob = Mob()
amob.run(0)
def testRun(self):
from pymud.persist import P
amob = Mob()
amob.addListener(P(self))
amob.appendCommand("say hi")
amob.appendCommand("say hi")
amob.appendCommand("say hi")
self.assertFalse(amob.commandScript)
self.assertEquals(len(amob.commandQueue),3)
amob.run(0)
self.assertEquals(len(amob.scriptsQueue),0)
self.assertFalse(amob.commandScript)
if __name__ == "__main__":
unittest.main()