-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptControl.py
45 lines (39 loc) · 1.53 KB
/
ScriptControl.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
from random import randint
class ChoiceScriptComponent:
LOCK_LENGTH = 5
def __init__(self,options):
self._options = options
self._locks = [0] * len(options)
if len(self._locks) < ChoiceScriptComponent.LOCK_LENGTH:
self.LOCK_LENGTH = len(self._locks)-2
def _reduceLock(self):
self._locks[:] = [(lockLength-1) if lockLength > 0 else 0 for lockLength in self._locks]
def _lockChoice(self,index):
self._locks[index] = self.LOCK_LENGTH
def getRandomSegment(self):
options = [(option,index) for index,option in enumerate(self._options) if self._locks[index] == 0]
if len(options) == 1:
segmentChoice = 0
else:
segmentChoice = randint(0,len(options)-1)
self._reduceLock()
self._lockChoice(options[segmentChoice][1])
return options[segmentChoice][0]
class ScriptItem:
def __init__(self,script):
self._script = []
for item in script:
if isinstance(item,str):
self._script.append(item)
elif isinstance(item,tuple):
self._script.append(ChoiceScriptComponent(item))
else:
raise Exception("Invalid type in script datastructure")
def generateScriptLine(self):
line = ""
for segment in self._script:
if isinstance(segment,str):
line += segment
elif isinstance(segment,ChoiceScriptComponent):
line += segment.getRandomSegment()
return line