-
Notifications
You must be signed in to change notification settings - Fork 16
/
game.py
108 lines (85 loc) · 4.36 KB
/
game.py
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
import json
import sys
from chat import Chat
from config import readFromConfig
from dataclasses import Data
from solver import Solver
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol
from shutil import get_terminal_size
from twisted.internet.protocol import ReconnectingClientFactory
debug = readFromConfig("General", "debug_mode")
class GameProtocol(WebSocketClientProtocol):
def onOpen(self):
if debug:
print("[Connection] Connection established!")
self.block_chat = False # It will block chat when question are shown
self.chat = Chat()
self.solver = Solver()
""" Game Summary """
self.gs_enable = readFromConfig("GameSummary", "enable")
self.gs_prize = readFromConfig("GameSummary", "show_prize")
self.gs_userids = readFromConfig("GameSummary", "show_userids")
self.gs_usernames = readFromConfig("GameSummary", "show_usernames")
def onMessage(self, payload, isBinary):
if not isBinary:
message = json.loads(payload.decode())
if message["type"] == "question":
self.block_chat = True
self.solver.solve(message)
elif message["type"] == "questionClosed":
self.block_chat = False
elif message["type"] == "questionSummary":
self.block_chat = True
self.solver.solve(message)
elif message["type"] == "questionFinished":
self.block_chat = False
if not self.block_chat:
if (message["type"] == "interaction" and message["itemId"] == "chat") or message["type"] == "kicked":
self.chat.showMessage(message)
elif message["type"] == "broadcastStats":
connected = str(message["viewerCounts"]["connected"])
playing = str(message["viewerCounts"]["playing"])
watching = str(message["viewerCounts"]["watching"])
print(" Broadcast Stats ".center(get_terminal_size()[0], "="))
print(("Connected Players: " + connected).center(get_terminal_size()[0]))
print(("Playing Players: " + playing).center(get_terminal_size()[0]))
print(("Watching Players: " + watching).center(get_terminal_size()[0]))
print("".center(get_terminal_size()[0], "="))
if message["type"] == "gameSummary":
if self.gs_enable:
self.block_chat = True
winnerCount = str(message["numWinners"])
winnerList = message["winners"]
print(" Game Summary ".center(get_terminal_size()[0], "="))
print((winnerCount + " Winners!").center(get_terminal_size()[0]))
for winner in range(len(winnerList)):
userInfo = winnerList[winner]
toPrint = ""
if self.gs_usernames or self.gs_userids:
if self.gs_usernames:
toPrint += str(userInfo["name"]) + ' '
if self.gs_usernames and self.gs_userids:
toPrint += '(' + str(userInfo["id"]) + ') '
elif self.gs_userids:
toPrint += str(userInfo["id"]) + ' '
if self.gs_prize:
toPrint += "just won " + str(userInfo["prize"] + "!")
print("".center(get_terminal_size()[0], "="))
elif message["type"] == "postGame":
self.block_chat = False
if message["type"] == "broadcastEnded":
Data.allowReconnecting = False
self.transport.loseConnection()
class GameFactory(WebSocketClientFactory, ReconnectingClientFactory):
protocol = GameProtocol
def clientConnectionFailed(self, connector, reason):
if debug:
print("[Connection] Connection failed! Retrying...")
self.retry(connector)
def clientConnectionLost(self, connector, reason):
if Data.allowReconnecting:
if debug:
print("[Connection] Connection has been lost! Retrying...")
self.retry(connector)
else:
print(" Game Ended! ".center(get_terminal_size()[0], "*"))