-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartmultiplayerscreen.py
More file actions
550 lines (518 loc) · 31.3 KB
/
startmultiplayerscreen.py
File metadata and controls
550 lines (518 loc) · 31.3 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import AliasProperty, ObjectProperty, StringProperty, ListProperty, BooleanProperty, NumericProperty, DictProperty
from generalelements import ConfirmPopupContent, NormalPopup, ChatLabel, generate_seed, NormalDropDown, MenuButton, IPInput, IntegerInput
from globals import multiplayer_port
from startscreens import GameStartScreen
from kivy.lang.builder import Builder
Builder.load_string("""
<AddressPreset>:
orientation: 'horizontal'
padding: app.button_scale / 2, 0
WideButton:
disabled: True if app.mp_connection is None else False
text: root.ip
on_release: app.mp_connection.remote_ip = root.ip
on_release: root.parent.screen_manager.current = 'main'
<MultiplayerScreen>:
first_selected: first_select
disabled: self.starting_game
ScreenLayout:
pos: root.pos
size: root.size
size_hint: None, None
orientation: 'vertical'
Header:
NormalButton:
text: ' Back '
on_release: root.back()
HeaderLabel:
text: 'Multiplayer'
SettingsButton:
on_release: app.show_settings('multiplayer')
BoxLayout:
padding: app.screen_border
ScreenManager:
transition: SlideTransition(direction='up')
id: screen_manager
Screen:
name: 'main'
BoxLayout:
orientation: 'vertical'
WideButton:
font_size: app.text_scale * 1.5
size_hint_y: None
height: app.button_scale * 1.5
text: 'Start Game'
disabled: not root.connected
on_release: root.start_game()
LayoutSplitter:
size_hint_y: 1
owner: root
GridLayout:
size_hint_y: None
height: self.parent.height if self.parent.wider else self.minimum_height
cols: 1
padding: app.button_scale / 2, 0
WideMenuStarter:
disabled: app.mp_thinking
size_hint_y: None
height: app.button_scale
helptext: ['Connection Modes:', '-', 'Local: Connect to a computer on the same network (either via WiFi or wired connection.', '', 'Port Forwarding: Ask your router to forward a port via UPnP to easily connect to a computer elsewhere.', '', 'Direct Connection: Connect to another computer via IP address. You will most likely need to manually forward a port.', '', 'Bluetooth: Directly connect to another device over a bluetooth connection.']
text: 'Connection Mode: '+app.connection_friendly(root.multiplayer_mode)
on_release: root.multiplayer_mode_menu.open(self)
ScreenManager:
canvas.before:
Color:
rgba: app.theme.indented
BorderImage:
display_border: [app.display_border, app.display_border, app.display_border, app.display_border]
border: 16, 16, 16, 16
size: self.size
pos: self.pos
source: 'data/inverted.png'
size_hint_y: None
height: (app.button_scale * 3) + (app.display_border * 2)
id: connection_mode
current: root.multiplayer_mode
Screen:
name: 'local'
BoxLayout:
padding: [app.display_border, app.display_border, app.display_border, app.display_border]
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
text: "Your Address: "
NormalInput:
helptext: ['Your Address:', '-', 'This is your IP address that other apps should connect to. This field can be copied, but not edited. ']
readonly: True
text: app.local_ip
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
size_hint_y: None
height: app.button_scale
text: "Connect To: "
IPInput:
helptext: ['Connect To:', '-', "The IP address of the other app to connect to. This should be the same value shown in the 'Your Address' field of the other app. "]
text: app.remote_ip
on_text: root.set_remote_ip(self.text)
disabled: app.connecting or app.connected
NormalButton:
text: 'Find...'
helptext: ['Local Connection Finder:', '-', 'This button will open a list of other devices on the network that are actively seeking a connection.', 'Any other instances of this app currently on the multiplayer screen should show up in the list.']
disabled: app.connecting or app.connected
on_release: screen_manager.current = 'select'
on_release: root.current_selected = None
on_release: app.clear_selected_overlay()
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
LeftNormalLabel:
text: app.connect_status
WideButton:
id: first_select
text: app.connect_action
disabled: True if app.mp_connection is None else False
on_release: app.mp_connection.connect_action_call()
Screen:
name: 'port_forward'
BoxLayout:
padding: [app.display_border, app.display_border, app.display_border, app.display_border]
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
text: "Your Address: "
NormalInput:
helptext: ['Your Address:', '-', 'This is your IP address that other apps should connect to. Please note that this address may not be detected correctly on all devices, please double-check this with a website that shows your external IP address if someone is not able to connect to you. This field can be copied, but not edited. ']
readonly: True
text: app.external_local_ip
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
size_hint_y: None
height: app.button_scale
text: "Connect To: "
IPInput:
helptext: ['Connect To:', '-', "The IP address of the other app to connect to. This should be the same value shown in the 'Your Address' field of the other app. "]
text: app.remote_ip
on_text: root.set_remote_ip(self.text)
disabled: app.connecting or app.connected
ShortLabel:
size_hint_y: None
height: app.button_scale
text: ":"
IntegerInput:
helptext: ['Connect To:', '-', 'The port number to connect to. This must be set to the same number for both instances of the app, and this port must not be in use by another program or computer on your network. ']
size_hint_x: 0.5
text: str(app.port)
on_focus: root.set_port(self, self.text)
disabled: app.connecting or app.connected
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
LeftNormalLabel:
text: app.connect_status
WideButton:
id: first_select
text: app.connect_action
disabled: True if app.mp_connection is None or app.mp_thinking else False
on_release: app.mp_connection.connect_action_call()
Screen:
name: 'direct'
BoxLayout:
padding: [app.display_border, app.display_border, app.display_border, app.display_border]
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
text: "Your Address: "
NormalInput:
helptext: ['Your Address:', '-', 'This is your IP address that other apps should connect to. Please note that this address may not be detected correctly on all devices, please double-check this with a website that shows your external IP address if someone is not able to connect to you. This field can be copied, but not edited. ']
readonly: True
text: app.external_local_ip
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
ShortLabel:
size_hint_y: None
height: app.button_scale
text: "Connect To: "
IPInput:
helptext: ['Connect To:', '-', "The IP address of the other app to connect to. This should be the same value shown in the 'Your Address' field of the other app. "]
text: app.remote_ip
on_text: root.set_remote_ip(self.text)
disabled: app.connecting or app.connected
ShortLabel:
size_hint_y: None
height: app.button_scale
text: ":"
IntegerInput:
helptext: ['Connect To:', '-', 'The port number to connect to. This must be set to the same number for both instances of the app, and this port must not be in use by another program or computer on your network. ']
size_hint_x: 0.5
text: str(app.port)
on_focus: root.set_port(self, self.text)
disabled: app.connecting or app.connected
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: app.button_scale
LeftNormalLabel:
text: app.connect_status
WideButton:
id: first_select
text: app.connect_action
disabled: True if app.mp_connection is None or app.mp_thinking else False
on_release: app.mp_connection.connect_action_call()
Screen:
name: 'bluetooth'
BoxLayout:
padding: [app.display_border, app.display_border, app.display_border, app.display_border]
orientation: 'vertical'
NormalLabel:
text: 'Not implemented yet'
BoxLayout:
orientation: 'horizontal'
height: app.button_scale if self.parent.parent.wider else (app.button_scale / 2)
size_hint_y: None
LeftNormalLabel:
text: "Game Speed:"
LeftNormalLabel:
text: 'Opponent: '+str(root.other_speed)
SmoothSetting:
size_hint_y: None
height: app.button_scale * .75
item_width: app.button_scale
control_width: app.button_scale * 0.75
start_on: root.speed
on_active: root.speed = self.active
helptext: ['Game Speed:', '-', 'This defines the starting speed level for your game. Lines completed will increase your level, but your speed will not increase until you surpass this starting level. ', '', 'When connected to an opponent, the game speed selected by your opponent will be visible above this slider. ']
content: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
BoxLayout:
orientation: 'horizontal'
height: app.button_scale if self.parent.parent.wider else (app.button_scale / 2)
size_hint_y: None
LeftNormalLabel:
text: "Filled Lines:"
LeftNormalLabel:
text: 'Opponent: '+str(root.other_filled)
SmoothSetting:
size_hint_y: None
height: app.button_scale * .75
item_width: app.button_scale
helptext: ['Filled Lines:', '-', 'This sets a number of lines to be filled with garbage blocks at the start of the game. For each level of filled lines, two garbage lines will be added to the bottom of the field. ', '', 'When connected to an opponent, the filled lines selected by your opponent will be visible above this slider. ']
control_width: app.button_scale * 0.75
start_on: root.filled
on_active: root.filled = self.active
content: ['0', '1', '2', '3', '4', '5']
BoxLayout:
orientation: 'vertical'
padding: app.button_scale / 2, 0
LeftNormalLabel:
size_hint_y: None
height: (app.button_scale if self.parent.parent.wider else (app.button_scale / 2)) if app.music_support else 0
opacity: 1 if app.music_support else 0
text: "Music:"
Scroller:
size_hint: 1, 1
do_scroll_x: False
do_scroll_y: True
BoxLayout:
id: music
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height if app.music_support else 0
opacity: 1 if app.music_support else 0
FloatLayout:
size_hint_y: None
disabled: False if app.connected else True
height: app.button_scale * 2 if not self.disabled else 1
opacity: 0 if self.disabled else 1
NormalRecycleView:
canvas.before:
Color:
rgba: app.theme.indented
BorderImage:
display_border: [app.display_border, app.display_border, app.display_border, app.display_border]
border: 16, 16, 16, 16
size: self.size
pos: self.pos
source: 'data/inverted.png'
scroll_y: 0
do_scroll_x: False
size_hint: None, None
pos: self.parent.pos
size: self.parent.size
id: chatArea
data: app.multiplayer_text
viewclass: 'ChatLabel'
RecycleBoxLayout:
padding: app.button_scale / 4, app.button_scale / 10
default_size_hint: 1, None
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
Image:
size_hint: None, None
size: app.button_scale / 2, app.button_scale / 6.666
color: app.theme.button_text if app.multiplayer_typing else (1, 1, 1, 0)
anim_delay: 0.0333
pos: self.parent.pos[0] + self.parent.size[0] - self.size[0], self.parent.pos[1]
source: 'data/elipsis.zip'
BoxLayout:
disabled: not app.connected
size_hint_y: None
height: 1 if self.disabled else app.button_scale
opacity: 0 if self.disabled else 1
NormalInputChat:
multiline: False
hint_text: 'Type A Message To Your Opponent'
text: app.multiplayer_text_current
on_text: app.multiplayer_text_current = self.text
NormalButton:
text: 'Send'
on_release: app.multiplayer_send_message()
Screen:
name: 'select'
BoxLayout:
orientation: 'vertical'
WideButton:
text: 'Cancel'
on_release: screen_manager.current = 'main'
on_release: root.current_selected = None
on_release: app.clear_selected_overlay()
size_hint_y: None
height: app.button_scale
NormalRecycleView:
data: app.available_addresses
viewclass: 'AddressPreset'
RecycleBoxLayout:
screen_manager: screen_manager
default_size: None, app.button_scale
default_size_hint: 1, None
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
""")
class AddressPreset(BoxLayout):
"""Viewclass to display addresses to connect to in the multiplayer screen."""
ip = StringProperty()
refresh_time = NumericProperty()
class MultiplayerScreen(GameStartScreen):
"""Screen for settings for a multiplayer game"""
starting_game = BooleanProperty(False)
game_seed = 0
speed = NumericProperty(0)
filled = NumericProperty(0)
other_speed = NumericProperty(0)
other_filled = NumericProperty(0)
multiplayer_mode_menu = ObjectProperty()
popup_dismisser = ObjectProperty(allownone=True)
popup = ObjectProperty(allownone=True)
multiplayer_mode = StringProperty('')
def __init__(self, **kwargs):
self.register_event_type('on_filled')
self.register_event_type('on_speed')
super().__init__(**kwargs)
def back(self):
app = App.get_running_app()
app.multiplayer_stop()
app.main()
def dismiss(self):
app = App.get_running_app()
return app.dismiss_popup()
def set_remote_ip(self, ip):
app = App.get_running_app()
if app.mp_connection is not None:
app.mp_connection.remote_ip = ip
def set_port(self, textinput, port):
if textinput.focus:
return
app = App.get_running_app()
if app.mp_connection is not None:
try:
port = int(float(port))
except Exception as e:
port = multiplayer_port
app.port = port
if port == 0:
port = multiplayer_port
app.port = port
app.mp_connection.port = port
if port != app.mp_connection.forwarded_port:
app.mp_connection.restart('scanning')
def stop_game_start(self, *_):
self.starting_game = False
def connected(self, *_):
self.on_filled()
self.on_speed()
def ask_connect(self, *_):
Clock.schedule_once(self.ask_connect_delay)
def ask_connect_delay(self, *_):
app = App.get_running_app()
if app.popup:
return
content = ConfirmPopupContent(text=app.mp_connection.connect_ip+' wants to connect for multiplayer, accept?', yes_text='Connect', no_text="Don't Connect")
content.bind(on_answer=self.accept_connection_answer)
app.popup = NormalPopup(title="Accept Connection?", content=content, size_hint=(1, None), size=(1000, app.button_scale * 3), auto_dismiss=False)
app.popup.bind(on_dismiss=app.dismiss_popup)
app.popup.open()
self.popup_timeout_cancel()
self.popup_dismisser = Clock.schedule_once(self.popup_timeout, 30)
def popup_timeout_cancel(self):
if self.popup_dismisser:
self.popup_dismisser.cancel()
self.popup_dismisser = None
def popup_timeout(self, *_):
app = App.get_running_app()
app.dismiss_popup()
self.popup_dismisser = None
app.mp_connection.refuse_connect(block=False)
def accept_connection_answer(self, instance, answer):
app = App.get_running_app()
app.dismiss_popup()
self.popup_timeout_cancel()
if answer == 'yes':
app.mp_connection.connect_to()
else:
app.mp_connection.refuse_connect()
def set_multiplayer_mode(self, mode):
self.multiplayer_mode_menu.dismiss()
if mode in ['local', 'port_forward', 'direct', 'bluetooth']:
self.multiplayer_mode = mode
else:
self.multiplayer_mode = 'local'
app = App.get_running_app()
app.multiplayer_stop()
app.multiplayer_start()
app.mp_connection.remote_ip = app.config.get('Settings', 'last_multiplayer')
if mode != 'local':
try:
port = int(float(app.config.get('Settings', 'last_multiplayer_port')))
except Exception as e:
port = multiplayer_port
app.port = port
if port == 0:
port = multiplayer_port
app.port = port
app.mp_connection.port = port
else:
app.mp_connection.port = multiplayer_port
app.mp_ready_start = False
if app.mp_connection is None:
app.main()
app.message('No Internet\nPermissions!')
return
app.mp_connection.connect_mode = self.multiplayer_mode
app.mp_connection.restart('scanning')
def on_enter(self):
app = App.get_running_app()
self.multiplayer_mode_menu = NormalDropDown()
menu_button = MenuButton(text='Local Network')
menu_button.bind(on_release=lambda x: self.set_multiplayer_mode('local'))
self.multiplayer_mode_menu.add_widget(menu_button)
menu_button = MenuButton(text='Port Forward Remote')
menu_button.bind(on_release=lambda x: self.set_multiplayer_mode('port_forward'))
self.multiplayer_mode_menu.add_widget(menu_button)
menu_button = MenuButton(text='Direct Remote')
menu_button.bind(on_release=lambda x: self.set_multiplayer_mode('direct'))
self.multiplayer_mode_menu.add_widget(menu_button)
#menu_button = MenuButton(text='Bluetooth Connection')
#menu_button.bind(on_release=lambda x: self.set_multiplayer_mode('bluetooth'))
#self.multiplayer_mode_menu.add_widget(menu_button)
if not app.connected:
app.multiplayer_text = []
self.set_multiplayer_mode(app.config.get('Settings', 'last_multiplayer_mode'))
app.clear_selected_overlay()
self.starting_game = False
self.populate_areas()
self.ids.screen_manager.current = 'main'
def on_leave(self):
app = App.get_running_app()
app.config.set('Settings', 'last_multiplayer_mode', self.multiplayer_mode)
app.save_multiplayer_ip()
app.save_port()
self.ids.screen_manager.current = 'main'
app.stop_music()
def on_filled(self, *_):
app = App.get_running_app()
app.multiplayer_send('filled set', [self.filled])
def on_speed(self, *_):
app = App.get_running_app()
app.multiplayer_send('speed set', [self.speed])
def populate_areas(self):
self.populate_music()
def start_game(self):
app = App.get_running_app()
if not app.mp_connection.connected:
return
self.game_seed = generate_seed()
app.multiplayer_send('game start', [self.game_seed])
self.starting_game = True
def start_game_finish(self, seed=None):
app = App.get_running_app()
if seed is None:
seed = self.game_seed
app.game()
game_screen = app.game_screen
game_screen.start_speed = self.speed
game_screen.start_filled = self.filled
game_screen.periodic_add_lines = 0
game_screen.game_mode = 'C'
game_screen.start_game(seed=seed)