built in websockets for auto-reload
This commit is contained in:
parent
38102ce778
commit
72e968274b
@ -14,7 +14,8 @@ def main():
|
||||
random.seed()
|
||||
wiz_game.the_game.start_game()
|
||||
|
||||
api.run(port=8001, address="127.0.0.1")
|
||||
#api.run(port=8001, address="127.0.0.1")
|
||||
api.run(port=8001, address="192.168.42.1")
|
||||
exit(0)
|
||||
|
||||
|
||||
|
@ -21,6 +21,34 @@
|
||||
<script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
||||
<script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
|
||||
<![endif]-->
|
||||
<script>
|
||||
let socket = new WebSocket("wss://cowiz.struppi.name/ws");
|
||||
|
||||
socket.onopen = function(e) {
|
||||
//alert("[open] Connection established");
|
||||
//alert("Sending to server");
|
||||
//socket.send("My name is John");
|
||||
};
|
||||
|
||||
socket.onmessage = function(event) {
|
||||
//alert(`[message] Data received from server: ${event.data}`);
|
||||
location.reload(true)
|
||||
};
|
||||
|
||||
socket.onclose = function(event) {
|
||||
if (event.wasClean) {
|
||||
//alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||
} else {
|
||||
// e.g. server process killed or network down
|
||||
// event.code is usually 1006 in this case
|
||||
//alert('[close] Connection died');
|
||||
}
|
||||
};
|
||||
|
||||
socket.onerror = function(error) {
|
||||
alert(`[error] ${error.message}`);
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -32,16 +32,27 @@ def index(req, resp):
|
||||
)
|
||||
|
||||
@api.route("/control/next_hand/")
|
||||
def start_next_hand(req, resp):
|
||||
async def start_next_hand(req, resp):
|
||||
the_game.next_hand()
|
||||
for ws in the_game.websockets:
|
||||
try:
|
||||
await ws.send_text("reload")
|
||||
except Exception as e:
|
||||
print("ws: got {}".format(e))
|
||||
api.redirect(resp, '/control/', status_code=303)
|
||||
|
||||
|
||||
@api.route("/control/next_trick/")
|
||||
def start_next_trick(req, resp):
|
||||
async def start_next_trick(req, resp):
|
||||
the_game.next_trick()
|
||||
for ws in the_game.websockets:
|
||||
try:
|
||||
await ws.send_text("reload")
|
||||
except Exception as e:
|
||||
print("ws: got {}".format(e))
|
||||
api.redirect(resp, '/control/', status_code=303)
|
||||
|
||||
@api.route("/control/deal/{cards}")
|
||||
def deal(req, resp, cards):
|
||||
the_game.deal_cards(int(cards))
|
||||
api.redirect(resp, '/control/', status_code=303)
|
||||
#@api.route("/control/deal/{cards}")
|
||||
#def deal(req, resp, cards):
|
||||
# the_game.deal_cards(int(cards))
|
||||
# api.redirect(resp, '/control/', status_code=303)
|
||||
|
@ -1,6 +1,7 @@
|
||||
from app_instance import api
|
||||
|
||||
from wiz_game import the_game
|
||||
from starlette.websockets import WebSocketDisconnect
|
||||
|
||||
|
||||
@api.route("/player/{player}")
|
||||
@ -29,16 +30,37 @@ def show(req, resp, player: str):
|
||||
|
||||
|
||||
@api.route("/player/{player}/play/{card}")
|
||||
def play(req, resp, player: str, card: str):
|
||||
async def play(req, resp, player: str, card: str):
|
||||
p = int(player)
|
||||
c = int(card)
|
||||
the_game.play_card(p, c)
|
||||
url = '/player/'+player
|
||||
for ws in the_game.websockets:
|
||||
try:
|
||||
await ws.send_text("reload")
|
||||
except Exception as e:
|
||||
print("ws: got {}".format(e))
|
||||
api.redirect(resp, url, status_code=303)
|
||||
|
||||
@api.route("/player/{player}/set_trump/{trump}")
|
||||
def set_trump_color(req, resp, player, trump):
|
||||
async def set_trump_color(req, resp, player, trump):
|
||||
p = int(player)
|
||||
the_game.set_trump_color(trump)
|
||||
url = '/player/'+player
|
||||
for ws in the_game.websockets:
|
||||
try:
|
||||
await ws.send_text("reload")
|
||||
except Exception as e:
|
||||
print("ws: got {}".format(e))
|
||||
api.redirect(resp, url, status_code=303)
|
||||
|
||||
|
||||
@api.route('/ws', websocket=True)
|
||||
async def websocket(ws):
|
||||
await ws.accept()
|
||||
the_game.websockets.append(ws)
|
||||
try:
|
||||
foo = await ws.receive_text()
|
||||
except WebSocketDisconnect:
|
||||
the_game.websockets.remove(ws)
|
||||
|
||||
|
@ -81,6 +81,7 @@ class WizGame:
|
||||
self.num_tricks_played = 0
|
||||
self.played_cards = list()
|
||||
self.players_ordered = self.players
|
||||
self.websockets = []
|
||||
|
||||
def start_game(self):
|
||||
self.create_deck()
|
||||
@ -90,6 +91,7 @@ class WizGame:
|
||||
self.deal_cards(1)
|
||||
self.played_cards = list()
|
||||
self.players_ordered = self.players
|
||||
self.websockets = []
|
||||
|
||||
def create_deck(self):
|
||||
self.card_deck = []
|
||||
|
Loading…
Reference in New Issue
Block a user