pixi integration is improving

This commit is contained in:
Stefan Rupp 2020-12-14 23:07:03 +01:00
parent c194164e3c
commit 965b7a5ad7
4 changed files with 76 additions and 35 deletions

View File

@ -27,6 +27,7 @@
let t = JSON.stringify(msg); let t = JSON.stringify(msg);
window.game_socket.send(t); window.game_socket.send(t);
} }
</script> </script>
<div style="font-family:Wizzta; font-size:100px"></div> <div style="font-family:Wizzta; font-size:100px"></div>
@ -37,12 +38,12 @@
<script src="/js-cardgame/js/player.js"></script> <script src="/js-cardgame/js/player.js"></script>
<script src="/js-cardgame/js/game.js"></script> <script src="/js-cardgame/js/game.js"></script>
<script> <script>
//let game = null;
let game = null; //window.game = null;
//window.onload = function() //window.onload = function()
function setupGame(names) function setupGame(names, hand)
{ {
PIXI.AbstractRenderer.autoDensity = true; PIXI.AbstractRenderer.autoDensity = true;
@ -91,7 +92,8 @@ function setupGame(names)
game = new Game(app.stage, resources); game = new Game(app.stage, resources);
//game.init(['Hubert', 'Struppi', 'Patrice', 'Steffi', "Max", "Renate"]); //game.init(['Hubert', 'Struppi', 'Patrice', 'Steffi', "Max", "Renate"]);
game.init(names) game.init(names)
game.give_round(["z3", "b04", "r08", "g12", "y13", "r02", "b05", "b02", "b03", "g03", "g05"]); game.give_round(hand)
//game.give_round(["z3", "b04", "r08", "g12", "y13", "r02", "b05", "b02", "b03", "g03", "g05"]);
//game.play_card(0, 0, "z3"); //game.play_card(0, 0, "z3");
//game.play_card(1, 0, "y13"); //game.play_card(1, 0, "y13");
//game.play_card(2, 0, "b02"); //game.play_card(2, 0, "b02");

View File

@ -22,6 +22,7 @@
<script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]--> <![endif]-->
<script> <script>
window.the_game = null
// let socket = new WebSocket("wss://cowiz.struppi.name/ws"); // let socket = new WebSocket("wss://cowiz.struppi.name/ws");
if (window.location.hostname == '127.0.0.1') { if (window.location.hostname == '127.0.0.1') {
ws_schema = 'ws://'; ws_schema = 'ws://';
@ -36,9 +37,13 @@
//alert("[open] Connection established"); //alert("[open] Connection established");
//alert("Sending to server"); //alert("Sending to server");
//socket.send("My name is John"); //socket.send("My name is John");
let player = "{{player}}"
var msg = { var msg = {
//type: "message",
//fct: "requestInit",
type: "message", type: "message",
fct: "requestInit", fct: "getHand",
player: player,
}; };
let t = JSON.stringify(msg); let t = JSON.stringify(msg);
window.game_socket.send(t); window.game_socket.send(t);
@ -50,10 +55,19 @@
if (fct == "reload") { if (fct == "reload") {
location.reload(true); location.reload(true);
} }
else if (fct == "getHand") {
window.hand = msg.cards;
var msg_out = {
type: "message",
fct: "requestInit",
};
let t = JSON.stringify(msg_out);
window.game_socket.send(t);
}
else if (fct == "startGame") { else if (fct == "startGame") {
let names = msg.names let names = msg.names
setupGame(names) setupGame(names, window.hand)
alert('game is set up') //alert('game is set up')
} }
else { else {
alert('unknown command: '+event.data); alert('unknown command: '+event.data);

View File

@ -80,28 +80,44 @@ async def initGame(ws):
await ws.send_json(msg) await ws.send_json(msg)
@api.route('/ws', websocket=True) async def getHand(player, ws):
async def websocket(ws): p = int(player)
c = the_game.players[p].cards
s = [x.json_serialise() for x in c]
msg = {"type": "message", "fct": "getHand", "cards": s}
await ws.send_json(msg)
async def ws_handler(ws):
await ws.accept() await ws.accept()
the_game.websockets.append(ws) the_game.websockets.append(ws)
try: while True:
x = await ws.receive_json() try:
fct = x['fct'] x = await ws.receive_json()
if fct == 'playcard': fct = x['fct']
p = int(x['player']) if fct == 'playcard':
c = int(x['card']) p = int(x['player'])
await play_card(p, c) c = int(x['card'])
elif fct == 'setTrumpColor': await play_card(p, c)
player = x['player'] elif fct == 'setTrumpColor':
color = x['color'] player = x['player']
await set_trump_color(player, color) color = x['color']
elif fct == "requestInit": await set_trump_color(player, color)
print("before init") elif fct == "requestInit":
await initGame(ws); await initGame(ws);
print("after init") elif fct == "getHand":
else: player = x['player']
print("confused :(") await getHand(player, ws)
else:
print("confused :(")
except WebSocketDisconnect:
the_game.websockets.remove(ws)
break;
except WebSocketDisconnect:
the_game.websockets.remove(ws) @api.route('/ws', websocket=True)
async def websocket(ws):
task = asyncio.create_task(ws_handler(ws))
the_game.ws_tasks.append(task)
await task;

View File

@ -9,9 +9,10 @@ def chunks(lst, n):
class Card: class Card:
def __init__(self, color, value): def __init__(self, color, value, idx=None):
self.color = color self.color = color
self.value = value self.value = value
self.idx = idx
def __str__(self): def __str__(self):
sc = self.color if self.color else ' ' sc = self.color if self.color else ' '
@ -21,6 +22,13 @@ class Card:
def __repr__(self): def __repr__(self):
return str(self) return str(self)
def json_serialise(self):
if self.color and self.color != '-':
s = "{}{:02d}".format(self.color, self.value)
else:
s = "{}{}".format(self.value, self.idx)
return s
def get_higher_card(card1: Card, card2: Card, trump_color: str): def get_higher_card(card1: Card, card2: Card, trump_color: str):
""" """
@ -92,6 +100,7 @@ class WizGame:
self.players_ordered = self.players self.players_ordered = self.players
self.last_trick = None self.last_trick = None
self.websockets = [] self.websockets = []
self.ws_tasks = []
def start_game(self): def start_game(self):
self.create_deck() self.create_deck()
@ -109,10 +118,10 @@ class WizGame:
for color in ["b", "r", "g", "y"]: for color in ["b", "r", "g", "y"]:
for val in range(1, 14): for val in range(1, 14):
self.card_deck.append(Card(color, val)) self.card_deck.append(Card(color, val))
for _ in range(1, 5): for idx in range(1, 5):
self.card_deck.append(Card('-', 'Z')) self.card_deck.append(Card('-', 'Z', idx))
for _ in range(1, 5): for idx in range(1, 5):
self.card_deck.append(Card('-', 'N')) self.card_deck.append(Card('-', 'N', idx))
def get_trump_color(self): def get_trump_color(self):
card = self.trump_card card = self.trump_card
@ -220,6 +229,6 @@ class WizGame:
pass pass
p = ["egg", "spam", "ham", "pups", "kekse"] p = ["spam", "egg", "ham", "tomato"]
the_game = WizGame(p) the_game = WizGame(p)