PynPong/pynpong/networking/gui.py

134 lines
4.1 KiB
Python
Raw Normal View History

2022-12-20 18:17:47 -05:00
"""
Starting launcher of the game. This is where you host new games or join other ones.
"""
import tkinter as tk
import concurrent.futures
import pypong.networking.client as client
2022-12-20 18:17:47 -05:00
global running
global listbox
2022-12-20 18:17:47 -05:00
def main():
executor = concurrent.futures.ThreadPoolExecutor(max_workers=512)
2022-12-20 18:17:47 -05:00
"""
Runs the game launcher.
"""
main_window = tk.Tk()
2022-12-22 09:08:06 -05:00
main_window.title('PynPong Launcher')
2022-12-20 18:17:47 -05:00
main_window.resizable(width=False, height=False)
main_window.geometry('500x300')
2022-12-20 18:17:47 -05:00
def quit_launcher():
"""
Quits the main menu.
"""
global running
running = False
executor.shutdown(wait=False, cancel_futures=True)
2022-12-20 18:17:47 -05:00
def check_game(*game_ip):
game_ip = ''.join(game_ip)
result = client.check_ip(game_ip)
return result
def get_games(network):
2022-12-20 18:17:47 -05:00
"""
Refresh the games list.
"""
results = []
listbox.delete(0, tk.END)
listbox.insert(0, ' Building IP range...')
main_window.update()
possible_ips = client.get_ips_from_network(network.get().split(' ')[0])
2022-12-20 18:17:47 -05:00
listbox.delete(0, tk.END)
listbox.insert(0, 'this might take some time...')
listbox.insert(0, 'Refreshing server listing,')
main_window.update()
# possible_ips = ['192.168.12.19']
futures = []
for ip in possible_ips:
if ip == '192.168.12.19':
print(ip)
future = executor.submit(check_game, ip)
futures.append(future)
game_found = False
for future in futures:
result = future.result()
main_window.update()
if 'PYPONGRST;SVRINFO' in result:
print(result)
raw_response = result.split(';')
response = []
for block in raw_response:
print(block)
chunks = block.split(':')
for chunk in chunks:
response.append(chunk)
print(response)
if 'NAME' in response:
session_name = response[response.index('NAME') + 1]
if not game_found:
listbox.delete(0, tk.END)
listbox.insert(0, session_name)
game_found = True
main_window.update()
if not game_found:
listbox.delete(0, tk.END)
listbox.insert(0, 'No games found!')
def get_addresses():
interface_info = client.get_interface_info()
shortened_info = []
for interface in interface_info:
shortened_info.append('{0} [{1}]'.format(interface.network, interface.interface_name))
return shortened_info
2022-12-20 18:17:47 -05:00
# Create the top title label
2022-12-22 09:08:06 -05:00
title_label = tk.Label(text='PynPong Launcher', fg='white', bg='#000040', padx=1, pady=20)
2022-12-20 18:17:47 -05:00
title_label.pack(side=tk.TOP, fill=tk.BOTH)
# Create the list of games
global listbox
2022-12-20 18:17:47 -05:00
listbox = tk.Listbox(main_window)
listbox.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
# Add scrollbar
scrollbar = tk.Scrollbar(main_window)
scrollbar.pack(side=tk.LEFT, fill=tk.Y)
# Link scrollbar to list of games
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# Create buttons
button_frame = tk.Frame(main_window)
tk.Button(button_frame, text='Host a Game', height=3, width=30).pack()
tk.Button(button_frame, text='Join Selected Game', height=3, width=30).pack()
tk.Button(button_frame, text='Refresh Game List', height=2, width=20,
command=lambda: get_games(selected_network)).pack()
networks = get_addresses()
selected_network = tk.StringVar()
selected_network.set(networks[0])
interface_menu = tk.OptionMenu(button_frame, selected_network, *networks)
interface_menu.pack()
2022-12-20 18:17:47 -05:00
button_frame.pack(side=tk.RIGHT)
# Set it so that if the X is pressed the application quits
main_window.protocol('WM_DELETE_WINDOW', quit_launcher)
global running
running = True
while running:
main_window.update()
if __name__ == '__main__':
main()