Add network interactions to lobby
This commit is contained in:
97
lobby.py
97
lobby.py
@@ -2,6 +2,8 @@ import tkinter as tk
|
|||||||
import color
|
import color
|
||||||
import main as quadraLudi
|
import main as quadraLudi
|
||||||
import importlib
|
import importlib
|
||||||
|
from socket import socket, AF_INET, SOCK_STREAM
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
class Lobby:
|
class Lobby:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -20,6 +22,9 @@ class Lobby:
|
|||||||
self.pseudo = tk.StringVar(win, "")
|
self.pseudo = tk.StringVar(win, "")
|
||||||
self.message = tk.StringVar(win, "")
|
self.message = tk.StringVar(win, "")
|
||||||
|
|
||||||
|
# Network
|
||||||
|
self.networkManager = NetworkManager()
|
||||||
|
|
||||||
# First
|
# First
|
||||||
self.nameDisplay()
|
self.nameDisplay()
|
||||||
|
|
||||||
@@ -60,15 +65,20 @@ class Lobby:
|
|||||||
scoreFrame = tk.LabelFrame(mainFrame, text = 'Score', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
scoreFrame = tk.LabelFrame(mainFrame, text = 'Score', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
||||||
scoreFrame.grid(column = 0, row = 0, sticky = 'NSEW', padx = 5)
|
scoreFrame.grid(column = 0, row = 0, sticky = 'NSEW', padx = 5)
|
||||||
self.scoreFrame = scoreFrame
|
self.scoreFrame = scoreFrame
|
||||||
self.addScoreEntry(1, 'Eveldee', 2354)
|
rank = 1
|
||||||
self.addScoreEntry(2, 'Fetyrix', 42)
|
for score in self.networkManager.requestScores():
|
||||||
|
pseudo, score = score.split(';')
|
||||||
|
self.addScoreEntry(rank, pseudo, score)
|
||||||
|
rank += 1
|
||||||
|
|
||||||
# Chat
|
# Chat
|
||||||
chatFrame = tk.LabelFrame(mainFrame, text = 'Chat', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
chatFrame = tk.LabelFrame(mainFrame, text = 'Chat', bg = color.LEVEL_1, fg = color.PURPLE, font = (None, 12))
|
||||||
chatFrame.grid(column = 1, row = 0, sticky = 'NSEW', padx = 5)
|
chatFrame.grid(column = 1, row = 0, sticky = 'NSEW', padx = 5)
|
||||||
self.chatFrame = chatFrame
|
self.chatFrame = chatFrame
|
||||||
self.addChatEntry('16:45', 'Eveldee', 'Hello')
|
for messsage in self.networkManager.requestMessages():
|
||||||
self.addChatEntry('16:46', 'Fetyrix', 'Je suis un très long text text text text text text text text text text text text text text text text')
|
split = messsage.split(';')
|
||||||
|
time, pseudo, content = split[0], split[1], ';'.join(split[2:])
|
||||||
|
self.addChatEntry(time, pseudo, content)
|
||||||
|
|
||||||
bottomFrame = tk.Frame(chatFrame, bg = color.LEVEL_5)
|
bottomFrame = tk.Frame(chatFrame, bg = color.LEVEL_5)
|
||||||
bottomFrame.pack(side = tk.BOTTOM, fill = tk.X)
|
bottomFrame.pack(side = tk.BOTTOM, fill = tk.X)
|
||||||
@@ -89,7 +99,7 @@ class Lobby:
|
|||||||
).grid(column = 0, row = 1, columnspan = 2, sticky = 'NSEW', padx = 5, pady = 5)
|
).grid(column = 0, row = 1, columnspan = 2, sticky = 'NSEW', padx = 5, pady = 5)
|
||||||
|
|
||||||
def nameConfirm(self):
|
def nameConfirm(self):
|
||||||
pseudo = self.pseudo.get()
|
pseudo = self.pseudo.get().replace('@', ' ').replace(';', ' ')
|
||||||
|
|
||||||
if pseudo == '':
|
if pseudo == '':
|
||||||
return
|
return
|
||||||
@@ -99,17 +109,22 @@ class Lobby:
|
|||||||
def sendMessage(self):
|
def sendMessage(self):
|
||||||
# Check empty
|
# Check empty
|
||||||
message = self.message.get()
|
message = self.message.get()
|
||||||
|
pseudo = self.pseudo.get()
|
||||||
if message == '':
|
if message == '':
|
||||||
return
|
return
|
||||||
|
|
||||||
# Send
|
# Send
|
||||||
|
message = message.replace('@', ' ')
|
||||||
|
self.networkManager.sendMessage(pseudo, message)
|
||||||
|
|
||||||
# Clear
|
# Clear
|
||||||
|
now = datetime.now().time()
|
||||||
|
time = f"{now.hour}:{now.minute}"
|
||||||
|
self.addChatEntry(time, pseudo, message)
|
||||||
self.message.set('')
|
self.message.set('')
|
||||||
|
|
||||||
def sendScore(self, score):
|
def sendScore(self, score):
|
||||||
pass
|
self.networkManager.sendScore(self.pseudo.get(), score)
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
# Reload
|
# Reload
|
||||||
@@ -144,14 +159,74 @@ class Lobby:
|
|||||||
frame = tk.Frame(self.chatFrame, bg = color.LEVEL_1)
|
frame = tk.Frame(self.chatFrame, bg = color.LEVEL_1)
|
||||||
frame.pack(anchor = tk.W)
|
frame.pack(anchor = tk.W)
|
||||||
font = (None, 10)
|
font = (None, 10)
|
||||||
tk.Label(frame, text = f'[{time}]', bg = color.LEVEL_1, fg = color.PURPLE, font = font).pack(side = tk.LEFT)
|
tk.Label(frame, text = f'[{time}]', bg = color.LEVEL_1, fg = color.PURPLE, font = font).pack(side = tk.LEFT, anchor = tk.N)
|
||||||
tk.Label(frame, text = f'({pseudo})', bg = color.LEVEL_1, fg = color.GREEN, font = font).pack(side = tk.LEFT)
|
tk.Label(frame, text = f'({pseudo})', bg = color.LEVEL_1, fg = color.GREEN, font = font).pack(side = tk.LEFT, anchor = tk.N)
|
||||||
tk.Label(frame, text = f':', bg = color.LEVEL_1, fg = color.YELLOW, font = font).pack(side = tk.LEFT)
|
tk.Label(frame, text = f':', bg = color.LEVEL_1, fg = color.YELLOW, font = font, bd = 0).pack(side = tk.LEFT, anchor = tk.N)
|
||||||
tk.Label(frame, text = f'{message}', bg = color.LEVEL_1, fg = color.PINK, font = font, wraplength = 450, justify = tk.LEFT).pack(side = tk.LEFT)
|
tk.Label(frame, text = f'{message}', bg = color.LEVEL_1, fg = color.PINK, font = font, wraplength = 450, justify = tk.LEFT).pack(side = tk.LEFT, anchor = tk.N)
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
self.win.mainloop()
|
self.win.mainloop()
|
||||||
|
|
||||||
|
class NetworkManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.address = ('127.0.0.1', 1409)
|
||||||
|
|
||||||
|
def _createSocket(self):
|
||||||
|
# Create TCP socket
|
||||||
|
sock = socket(AF_INET, SOCK_STREAM)
|
||||||
|
sock.connect(self.address)
|
||||||
|
|
||||||
|
return sock
|
||||||
|
|
||||||
|
def _sendPacket(self, command, content):
|
||||||
|
packet = f"{command}\n{content}".encode('utf-8')
|
||||||
|
length = len(packet).to_bytes(4, 'little')
|
||||||
|
|
||||||
|
sock = self._createSocket()
|
||||||
|
sock.sendall(length)
|
||||||
|
sock.sendall(packet)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
def _receivePacket(self, command):
|
||||||
|
sock = self._createSocket()
|
||||||
|
|
||||||
|
# Send command
|
||||||
|
packet = f"{command}\n".encode('utf-8')
|
||||||
|
length = len(packet).to_bytes(4, 'little')
|
||||||
|
|
||||||
|
sock.sendall(length)
|
||||||
|
sock.sendall(packet)
|
||||||
|
|
||||||
|
# Receive
|
||||||
|
# Length
|
||||||
|
length = sock.recv(4)
|
||||||
|
length = int.from_bytes(length, 'little')
|
||||||
|
# Data
|
||||||
|
response = sock.recv(length)
|
||||||
|
response = response.decode('utf-8')
|
||||||
|
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
split = response.split('\n')
|
||||||
|
return (split[0], '\n'.join(split[1:]))
|
||||||
|
|
||||||
|
def sendMessage(self, pseudo, message):
|
||||||
|
now = datetime.now().time()
|
||||||
|
time = f"{now.hour}:{now.minute}"
|
||||||
|
|
||||||
|
self._sendPacket('ChatAdd', f"{time};{pseudo};{message}")
|
||||||
|
|
||||||
|
def sendScore(self, pseudo, score):
|
||||||
|
self._sendPacket('ScoreAdd', f"{pseudo};{score}")
|
||||||
|
|
||||||
|
def requestMessages(self):
|
||||||
|
_, response = self._receivePacket('ChatRequest')
|
||||||
|
return response.split('@')
|
||||||
|
|
||||||
|
def requestScores(self):
|
||||||
|
_, response = self._receivePacket('ScoreRequest')
|
||||||
|
return response.split('@')
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
lobby = Lobby()
|
lobby = Lobby()
|
||||||
lobby.show()
|
lobby.show()
|
||||||
|
|||||||
Reference in New Issue
Block a user