-
Notifications
You must be signed in to change notification settings - Fork 0
/
modes.py
205 lines (181 loc) · 7.64 KB
/
modes.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
205
from field import *
from constants import *
from helpers import *
from timer import *
import time
import asyncio
import websockets
import threading
import json
class MainMenu:
def __init__(self, screen, Input, animate=False):
self.screen = screen
self.Input = Input
self.options = [
('Normal', Basic, lambda x: Basic.__init__(x, x.screen, x.Input)),
('Multiplayer', Multiplayer, lambda x: Multiplayer.__init__(x, x.screen, x.Input)),
('20L Sprint', Sprint, lambda x: Sprint.__init__(x, 20, x.screen, x.Input)),
('40L Sprint', Sprint, lambda x: Sprint.__init__(x, 40, x.screen, x.Input)),
]
self.selected = 0
self.animate = animate
self.typedIndex = 0
self.typeTimer = Timer(7)
self.beginTimer = Timer(30,loop=False)
self.blinkTimer = Timer(60)
updateColors(screen)
def run(self, key, dt):
yOff = self.drawlogo() + 1
for i,o in enumerate(self.options):
attr = 3 if i == self.selected else 0
self.screen.print_at(o[0],
self.screen.width//2-len(o[0])//2,
yOff + i,
attr=attr)
if key == self.screen.KEY_DOWN:
self.selected = (self.selected + 1) % len(self.options)
elif key == self.screen.KEY_UP:
self.selected = (self.selected - 1) % len(self.options)
# enter
elif key in (10,13):
self.__class__ = self.options[self.selected][1]
self.options[self.selected][2](self)
def drawlogo(self):
size = self.screen.height // 2
sx = self.screen.width // 2 - size
sy = size // 4
corners = [(sx,sy),(sx+size*2,sy),(sx,sy+size),(sx+size*2,sy+size)]
corners = [tuple(map(int,i)) for i in corners]
fancyRect(self.screen,sx,sy,size*2,size)
self.screen.print_at('>', corners[0][0] + 3, corners[0][1] + 4)
text = 'term-tris'
if not self.animate:
self.screen.print_at(text, corners[0][0] + 5, corners[0][1] + 4)
else:
self.screen.print_at(text[:self.typedIndex], corners[0][0] + 5, corners[0][1] + 4)
if self.beginTimer.check(1) and self.typeTimer.check(1):
self.typedIndex += 1
if self.typedIndex == len(text):
self.animate = False
x = len(text) if not self.animate else self.typedIndex
c = 7 if self.blinkTimer.timer < self.blinkTimer.after/2 else 0
self.screen.print_at(' ', corners[0][0] + 5 + x, corners[0][1] + 4, bg=c)
self.blinkTimer.check(1)
return sy+size
class Basic:
def __init__(self, screen, Input):
self.screen = screen
self.Input = Input
self.field = Field(screen,Input)
def run(self, key, dt):
self.field.update(key, dt)
self.field.show()
class Multiplayer(Basic):
def __init__(self, screen, Input):
super().__init__(screen, Input)
self.response = None
self.last = None
self.timer = Timer(2)
self.loop = asyncio.get_event_loop()
self.thread = None
self.uuid = None
with open('config.json','r') as f:
a = {}
js = json.load(f)['client'].get('multiplayer',a)
self.host = js.get('host','localhost')
self.port = js.get('port',8000)
self.connect('login')
self.others = []
self.fields = []
self.prevLines = 0
self.message = None
def connect(self, path, *send):
if self.loop.is_running():
return
self.response = None
self.last = path
def blocking(loop):
async def connect():
async with websockets.connect(f'ws://{self.host}:{self.port}/{path}') as websocket:
for i in send:
await websocket.send(i)
msg = await websocket.recv()
self.response = msg
try:
loop.run_until_complete(connect())
except Exception:
self.response = Exception
self.thread = threading.Thread(target=blocking,args=(self.loop,))
self.thread.start()
def run(self, key, dt):
if self.message != None:
self.screen.print_at(self.message,int(self.screen.width / 2 - len(self.message) / 2), self.screen.height//2, bg=1, colour=7)
else:
super().run(key, dt)
self.screen.print_at(str(self.uuid),0,0)
for f in self.fields:
f.show()
if self.response == Exception:
self.message = 'An exception has occurred, press e to return.'
self.last = -1
elif self.response != None and self.last != None and not self.loop.is_running():
p = self.last
if self.last == 'login':
self.uuid = self.response
elif self.last == 'send':
self.connect('get', self.uuid)
elif self.last == 'get':
self.others = self.response.split(',')
if self.others[0] == '':
self.others = []
self.fields = []
self.field.x = 10 # 10 because its the average width of a held piece times 2 and plus 2 for spacing
xSpacing = (self.screen.width - self.field.width - 20) // (len(self.others) + 1)
for j,o in enumerate(self.others):
f = Field(self.screen,basic=True)
f.x = (j+1) * xSpacing + self.field.x + self.field.width + 10
f.grid = list(o)
f.grid = [None if i == ' ' else i for i in f.grid]
self.fields.append(f)
self.connect('garbage', self.uuid, str(self.field.lines - self.prevLines))
self.prevLines = self.field.lines
elif self.last == 'garbage':
garbage = self.response
if garbage != '':
garbage = [tuple(map(int,i.split(','))) for i in garbage.split(';')]
gblock = 'Z'
for g in garbage:
for _ in range(g[0]): self.field.move('up')
for y in range(g[0]):
for x in range(self.field.width):
if x == g[1]:
continue
self.field.setCell(x,(self.field.height + self.field.hidden - 1) - y, gblock)
if p == self.last:
self.last = None
self.response = None
elif self.last == None:
if self.timer.check(dt):
fieldstr = ''.join(i if i != None else ' ' for i in self.field.grid)
self.connect('send',self.uuid,fieldstr)
def quit(self):
if self.response != Exception:
async def connect():
async with websockets.connect(f'ws://{self.host}:{self.port}/logout') as websocket:
await websocket.send(self.uuid)
self.loop.run_until_complete(connect())
class Sprint(Basic):
def __init__(self, lines, *args):
super().__init__(*args)
self.lines = lines
self.start = time.perf_counter()
self.end = None
def run(self, key, dt):
if self.end == None:
self.field.update(key, dt)
self.field.show()
self.screen.print_at(f'{self.field.lines}/{self.lines}',0,0)
cur = time.perf_counter() if self.end == None else self.end
self.screen.print_at(str(cur-self.start),0,1)
if self.field.lines >= self.lines:
self.end = cur