Skip to content

Commit

Permalink
Results and recovering available from Pygame window
Browse files Browse the repository at this point in the history
  • Loading branch information
sillypantscoder committed May 12, 2022
1 parent d908dc5 commit 58b79e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
11 changes: 1 addition & 10 deletions games.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
import datetime

def read_file(filename):
f = open(filename, "r")
Expand Down Expand Up @@ -28,7 +27,6 @@ def __init__(self):
"img": ["M 0 0 L 500 0 L 500 500 Z", "M 250 250 L 0 500 L 250 500 Z"]
}
]
self.drawingTime: datetime.datetime = datetime.datetime.now()
def get(self, path, gameno):
if path == "/": # / -> /wait
return {
Expand Down Expand Up @@ -70,7 +68,7 @@ def get(self, path, gameno):
\t\t<link rel="icon" type="image/x-icon" href="wait.ico">
\t</head>
\t<body>
\t\tWaiting for other players...""" + ('<br>\n\t\t<a href="recover">Recover</a>' if ((datetime.datetime.now() - self.drawingTime).total_seconds() >= 2) else '') + """
\t\tWaiting for other players...
\t</body>
</html>"""
}
Expand Down Expand Up @@ -264,13 +262,6 @@ def get(self, path, gameno):
},
"content": r
}
elif path == "/refresh": # /refresh
drawingTime = datetime.datetime.now()
return {
"status": 200,
"headers": {},
"content": ""
}
elif path == "/recover": # /recover
if self.drawingProgress == 1:
self.drawingProgress -= 1
Expand Down
51 changes: 29 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get(path):
"headers": {
"Content-Type": "text/html"
},
"content": """<!DOCTYPE html>
"content": ("""<!DOCTYPE html>
<html>
\t<head>
\t\t<title>Word Pictionary</title>
Expand All @@ -53,22 +53,14 @@ def get(path):
\t\t</style>
\t</head>
\t<body>
\t\t<h1>Word Pictionary</h1>
\t\t<iframe src="/0/"></iframe>
\t\t<iframe src="/1/"></iframe>
\t\t<iframe src="/2/"></iframe>
\t\t<iframe src="/3/"></iframe>
\t\t<iframe src="/4/"></iframe>
\t\t<h1>Word Pictionary - Results</h1>
\t\t<iframe src="/0/results"></iframe>
\t\t<iframe src="/1/results"></iframe>
\t\t<iframe src="/2/results"></iframe>
\t\t<iframe src="/3/results"></iframe>
\t\t<iframe src="/4/results"></iframe>
\t</body>
</html>"""
}
elif path == "/results":
return {
"status": 200,
"headers": {
"Content-Type": "text/html"
},
"content": """<!DOCTYPE html>
</html>""" if show_results else """<!DOCTYPE html>
<html>
\t<head>
\t\t<title>Word Pictionary</title>
Expand All @@ -83,13 +75,13 @@ def get(path):
\t</head>
\t<body>
\t\t<h1>Word Pictionary</h1>
\t\t<iframe src="/0/results"></iframe>
\t\t<iframe src="/1/results"></iframe>
\t\t<iframe src="/2/results"></iframe>
\t\t<iframe src="/3/results"></iframe>
\t\t<iframe src="/4/results"></iframe>
\t\t<iframe src="/0/"></iframe>
\t\t<iframe src="/1/"></iframe>
\t\t<iframe src="/2/"></iframe>
\t\t<iframe src="/3/"></iframe>
\t\t<iframe src="/4/"></iframe>
\t</body>
</html>"""
</html>""")
}
elif path.split("/")[1].isdigit():
gamepath = "/".join(path.split("/")[2:])
Expand Down Expand Up @@ -152,6 +144,7 @@ def log_message(self, format: str, *args) -> None:

def async_pygame():
global running
global show_results
pygame.font.init()
screensize = [500, 500]
screen = pygame.display.set_mode(screensize, pygame.RESIZABLE)
Expand All @@ -162,25 +155,39 @@ def async_pygame():
running = True
c = pygame.time.Clock()
while running:
clickpos = []
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
screensize = event.size
screen = pygame.display.set_mode(screensize, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONUP:
clickpos.append(event.pos)
# Drawing
screen.fill((255, 255, 255))
for i in range(len(activeGames)):
r = font.render(f"Game {i + 1} status: {activeGames[i].drawingProgress} ({['Waiting for word', 'Word', 'Waiting for draw', 'Drawing'][activeGames[i].drawingProgress]})", True, (0, 0, 0))
screen.blit(r, (0, i * fontheight))
resultsrect = r.get_rect().move(0, i * fontheight)
for p in clickpos:
if resultsrect.collidepoint(*p):
activeGames[i].drawingProgress -= 1
r = font.render(f"Close this window to stop the server", True, (0, 0, 0))
screen.blit(r, (0, (i + 2) * fontheight))
r = font.render(f"Show results: {'Yes' if show_results else 'No'}", True, (0, 0, 0))
screen.blit(r, (0, (i + 3) * fontheight))
resultsrect = r.get_rect().move(0, (i + 3) * fontheight)
for p in clickpos:
if resultsrect.collidepoint(*p):
show_results = not show_results
# Flip
pygame.display.flip()
c.tick(60)

if __name__ == "__main__":
running = True
show_results = False
webServer = HTTPServer((hostName, serverPort), MyServer)
webServer.timeout = 1
print("Server started http://%s:%s" % (hostName, serverPort))
Expand Down

0 comments on commit 58b79e7

Please sign in to comment.