Add lobby auto-refresh
This commit is contained in:
50
lobby.py
50
lobby.py
@@ -24,6 +24,8 @@ class Lobby:
|
|||||||
# Variables
|
# Variables
|
||||||
self.pseudo = tk.StringVar(win, "")
|
self.pseudo = tk.StringVar(win, "")
|
||||||
self.message = tk.StringVar(win, "")
|
self.message = tk.StringVar(win, "")
|
||||||
|
self.scoreLen = 0
|
||||||
|
self.chatLen = 0
|
||||||
|
|
||||||
# Network
|
# Network
|
||||||
self.networkManager = NetworkManager()
|
self.networkManager = NetworkManager()
|
||||||
@@ -68,22 +70,13 @@ 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
|
||||||
rank = 1
|
|
||||||
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
|
||||||
for messsage in self.networkManager.requestMessages():
|
|
||||||
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, name = 'bottomFrame')
|
||||||
bottomFrame.pack(side = tk.BOTTOM, fill = tk.X)
|
bottomFrame.pack(side = tk.BOTTOM, fill = tk.X)
|
||||||
entry = tk.Entry(bottomFrame, textvariable = self.message, bg = color.LEVEL_1, fg = color.GHOST, font = (None, 11))
|
entry = tk.Entry(bottomFrame, textvariable = self.message, bg = color.LEVEL_1, fg = color.GHOST, font = (None, 11))
|
||||||
entry.bind('<Return>', lambda e: self.sendMessage())
|
entry.bind('<Return>', lambda e: self.sendMessage())
|
||||||
@@ -101,10 +94,44 @@ class Lobby:
|
|||||||
command = lambda: self.play()
|
command = lambda: self.play()
|
||||||
).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)
|
||||||
|
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
messages = self.networkManager.requestMessages()
|
||||||
|
scores = self.networkManager.requestScores()
|
||||||
|
|
||||||
|
# Score
|
||||||
|
if len(scores) > self.scoreLen:
|
||||||
|
for child in self.scoreFrame.children.values():
|
||||||
|
child.pack_forget()
|
||||||
|
|
||||||
|
self.scoreLen = len(scores)
|
||||||
|
|
||||||
|
rank = 1
|
||||||
|
for score in self.networkManager.requestScores():
|
||||||
|
pseudo, score = score.split(';')
|
||||||
|
self.addScoreEntry(rank, pseudo, score)
|
||||||
|
rank += 1
|
||||||
|
|
||||||
|
# Chat
|
||||||
|
if len(messages) > self.chatLen:
|
||||||
|
for child in self.chatFrame.children.values():
|
||||||
|
if child._name != 'bottomFrame':
|
||||||
|
child.pack_forget()
|
||||||
|
|
||||||
|
self.chatLen = len(messages)
|
||||||
|
|
||||||
|
for messsage in self.networkManager.requestMessages():
|
||||||
|
split = messsage.split(';')
|
||||||
|
time, pseudo, content = split[0], split[1], ';'.join(split[2:])
|
||||||
|
self.addChatEntry(time, pseudo, content)
|
||||||
|
|
||||||
|
self.win.after(1000, lambda: self.update())
|
||||||
|
|
||||||
def nameConfirm(self):
|
def nameConfirm(self):
|
||||||
pseudo = self.pseudo.get().replace('@', ' ').replace(';', ' ')
|
pseudo = self.pseudo.get().replace('@', ' ').replace(';', ' ')
|
||||||
|
|
||||||
if pseudo == '':
|
if pseudo == '' or pseudo.isspace():
|
||||||
return
|
return
|
||||||
|
|
||||||
self.lobbyDisplay()
|
self.lobbyDisplay()
|
||||||
@@ -124,6 +151,7 @@ class Lobby:
|
|||||||
now = datetime.now().time()
|
now = datetime.now().time()
|
||||||
time = f"{now.hour}:{str(now.minute).zfill(2)}"
|
time = f"{now.hour}:{str(now.minute).zfill(2)}"
|
||||||
self.addChatEntry(time, pseudo, message)
|
self.addChatEntry(time, pseudo, message)
|
||||||
|
self.chatLen += 1
|
||||||
self.message.set('')
|
self.message.set('')
|
||||||
|
|
||||||
def sendScore(self, score):
|
def sendScore(self, score):
|
||||||
|
|||||||
Reference in New Issue
Block a user