-
Notifications
You must be signed in to change notification settings - Fork 1
/
turnqueue.py
103 lines (83 loc) · 2.63 KB
/
turnqueue.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
from mushyutils import colorfy
class TurnQueue(object):
# queue is the initiative queue
# order is the in-progress ordering
__slots__ = ("queue", "order")
def __init__(self):
self.queue = []
self.order = []
def wipe(self):
"""Wipe everything"""
self.queue = []
self.order = []
def reset(self):
"""Reset the current ordering"""
self.order = []
def add(self, name, initiative):
"""Add a new entry to the queue"""
i = self._index(name.lower())
if i != -1:
self.queue[0] = (name, initiative)
else:
self.queue.append((name.lower(), initiative))
self.queue = sorted(self.queue, key=lambda x: x[1], reverse=True)
def remove(self, name):
i = self._index(name.lower())
if i == -1:
raise AttributeError
self.queue.pop(i)
def promote(self, name):
name = name.lower()
i = self._index(name)
if i == -1:
raise AttributeError
elif i == 0:
return False
newval = self.queue[i - 1][1] + 0.001
self.queue[i] = (name, newval)
self.queue = sorted(self.queue, key=lambda x: x[1], reverse=True)
return True
def demote(self, name):
name = name.lower()
i = self._index(name)
if i == -1:
raise AttributeError
elif i == len(self.queue) - 1:
return False
newval = self.queue[i + 1][1] - 0.001
self.queue[i] = (name, newval)
self.queue = sorted(self.queue, key=lambda x: x[1], reverse=True)
return True
def commit(self):
"""Commit the current queue to the ordering"""
self.order = []
for entry in self.queue:
self.order.append(entry[0])
def tick(self):
"""Take a turn"""
if len(self.order) < 2:
return False
elif len(self.order) == 1:
return True
temp = self.order.pop(0)
self.order.append(temp)
return True
def peek(self):
if len(self.order) == 0:
return AttributeError
return self.order[0]
def _index(self, name):
for i in range(len(self.queue)):
entry = self.queue[i]
if entry[0] == name:
return i
return -1
def __str__(self):
s = colorfy(" Turn Order \n", "green")
s += "------------------\n"
for entry in self.queue:
s += entry[0]
if entry[0] == self.peek():
s += colorfy(" <--- Current Turn", "bred")
s += "\n"
return s