mirror of
https://github.com/simon987/bingo.git
synced 2025-04-10 13:56:42 +00:00
restart game & fixes
This commit is contained in:
parent
c1814d883d
commit
704ef006a8
68
app.py
68
app.py
@ -7,7 +7,8 @@ from common import config, db
|
|||||||
from models import BingoGame, GameState, GameMode, User
|
from models import BingoGame, GameState, GameMode, User
|
||||||
from util import is_valid_id
|
from util import is_valid_id
|
||||||
|
|
||||||
INVALID_ID_ERR = "Invalid identifier"
|
ERR_INVALID_ID = "Invalid identifier"
|
||||||
|
ERR_BAD_REQUEST = "Bad request"
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = config["FLASK_SECRET"]
|
app.config['SECRET_KEY'] = config["FLASK_SECRET"]
|
||||||
@ -39,11 +40,27 @@ class BingoNamespace(Namespace):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__("/socket")
|
super().__init__("/socket")
|
||||||
|
|
||||||
def on_get_end_message(self):
|
def on_get_end_message(self, message):
|
||||||
log("get_end_message", {})
|
room = message["room"] if "room" in message else None
|
||||||
emit("end_message", {
|
log("get_end_message", message, room=room)
|
||||||
"text": "Game has ended, replay?"
|
|
||||||
})
|
if room is None or "oid" not in message:
|
||||||
|
emit("create_game_rsp", {
|
||||||
|
"error": ERR_BAD_REQUEST
|
||||||
|
})
|
||||||
|
return
|
||||||
|
|
||||||
|
game = db.get_game(room)
|
||||||
|
if game.admin == message["oid"]:
|
||||||
|
emit("end_message", {
|
||||||
|
"text": "Game has ended, replay?",
|
||||||
|
"replay": True,
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
emit("end_message", {
|
||||||
|
"text": "Waiting for %s to restart game..." % (db.get_user(game.admin).name, ),
|
||||||
|
"replay": False,
|
||||||
|
})
|
||||||
|
|
||||||
def on_cell_click(self, message):
|
def on_cell_click(self, message):
|
||||||
room = message["room"]
|
room = message["room"]
|
||||||
@ -52,6 +69,9 @@ class BingoNamespace(Namespace):
|
|||||||
user = db.get_user(message["oid"])
|
user = db.get_user(message["oid"])
|
||||||
card = db.get_card(message["card"])
|
card = db.get_card(message["card"])
|
||||||
cell = card.cells[message["cidx"]]
|
cell = card.cells[message["cidx"]]
|
||||||
|
game = db.get_game(room)
|
||||||
|
|
||||||
|
# if game.mode == GameMode.FREE or game.mode:
|
||||||
|
|
||||||
if not cell.checked or card.last_cell == message["cidx"]:
|
if not cell.checked or card.last_cell == message["cidx"]:
|
||||||
card.check_cell(message["cidx"])
|
card.check_cell(message["cidx"])
|
||||||
@ -84,16 +104,16 @@ class BingoNamespace(Namespace):
|
|||||||
db.save_card(card)
|
db.save_card(card)
|
||||||
db.save_user(user)
|
db.save_user(user)
|
||||||
|
|
||||||
emit("card_state", {
|
|
||||||
"card": card.serialize(),
|
|
||||||
"parent": user.name
|
|
||||||
}, room=room)
|
|
||||||
|
|
||||||
emit("get_card_rsp", {
|
emit("get_card_rsp", {
|
||||||
"card": card.serialize(),
|
"card": card.serialize(),
|
||||||
"parent": user.name
|
"parent": user.name
|
||||||
})
|
})
|
||||||
|
|
||||||
|
emit("card_state", {
|
||||||
|
"card": card.serialize(),
|
||||||
|
"parent": user.name
|
||||||
|
}, room=room)
|
||||||
|
|
||||||
for player in game.players:
|
for player in game.players:
|
||||||
if player != user.oid:
|
if player != user.oid:
|
||||||
other_user = db.get_user(player)
|
other_user = db.get_user(player)
|
||||||
@ -102,21 +122,35 @@ class BingoNamespace(Namespace):
|
|||||||
emit("card_state", {"card": other_card.serialize(), "parent": other_user.name})
|
emit("card_state", {"card": other_card.serialize(), "parent": other_user.name})
|
||||||
|
|
||||||
def on_create_game(self, message):
|
def on_create_game(self, message):
|
||||||
room = message["room"]
|
room = message["room"] if "room" in message else None
|
||||||
log("create_game", message, room)
|
log("create_game", message, room)
|
||||||
|
|
||||||
|
if "oid" not in message or room is None or "mode" not in message or "pool" not in message:
|
||||||
|
emit("create_game_rsp", {
|
||||||
|
"created": False,
|
||||||
|
"error": ERR_BAD_REQUEST
|
||||||
|
})
|
||||||
|
return
|
||||||
|
|
||||||
if not is_valid_id(room):
|
if not is_valid_id(room):
|
||||||
emit("create_game_rsp", {
|
emit("create_game_rsp", {
|
||||||
"created": False,
|
"created": False,
|
||||||
"error": INVALID_ID_ERR
|
"error": ERR_INVALID_ID
|
||||||
}, room=room)
|
})
|
||||||
return
|
return
|
||||||
|
|
||||||
game = db.get_game(room)
|
game = db.get_game(room)
|
||||||
if game.state is GameState.CREATING:
|
if game.state is GameState.CREATING or (game.state is GameState.ENDED and game.admin == message["oid"]):
|
||||||
|
for oid in game.players:
|
||||||
|
player = db.get_user(oid)
|
||||||
|
if room in player.cards:
|
||||||
|
del player.cards[room]
|
||||||
|
db.save_user(player)
|
||||||
|
|
||||||
game.state = GameState.PLAYING
|
game.state = GameState.PLAYING
|
||||||
game.mode = GameMode[message["mode"]]
|
game.mode = GameMode[message["mode"]]
|
||||||
game.pool = message["pool"]
|
game.pool = message["pool"]
|
||||||
|
game.admin = message["oid"]
|
||||||
db.save_game(game)
|
db.save_game(game)
|
||||||
|
|
||||||
emit("game_state", {"state": game.state.name, }, room=room)
|
emit("game_state", {"state": game.state.name, }, room=room)
|
||||||
@ -129,11 +163,11 @@ class BingoNamespace(Namespace):
|
|||||||
log("join", message)
|
log("join", message)
|
||||||
|
|
||||||
room = message["room"]
|
room = message["room"]
|
||||||
name = message["room"]
|
name = message["name"]
|
||||||
|
|
||||||
if not is_valid_id(room) or not is_valid_id(name):
|
if not is_valid_id(room) or not is_valid_id(name):
|
||||||
emit("join_rsp", {
|
emit("join_rsp", {
|
||||||
"error": INVALID_ID_ERR
|
"error": ERR_INVALID_ID
|
||||||
}, room=room)
|
}, room=room)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
11
models.py
11
models.py
@ -102,6 +102,8 @@ class BingoCard:
|
|||||||
|
|
||||||
class GameMode(Enum):
|
class GameMode(Enum):
|
||||||
FREE = "free"
|
FREE = "free"
|
||||||
|
ADMIN = "admin"
|
||||||
|
CPU = "cpu"
|
||||||
|
|
||||||
|
|
||||||
class GameState(Enum):
|
class GameState(Enum):
|
||||||
@ -124,11 +126,11 @@ class BingoGame:
|
|||||||
},
|
},
|
||||||
"background": 0x282A36,
|
"background": 0x282A36,
|
||||||
"message": 0xF8F8F2,
|
"message": 0xF8F8F2,
|
||||||
"font": "'Roboto Mono', 'Lucida Console'"
|
"font": "Hack, 'Roboto Mono', 'Lucida Console'"
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, room, admin, mode=GameMode.FREE, pool=None, state=GameState.CREATING,
|
def __init__(self, room, admin, mode=GameMode.FREE, pool=None, state=GameState.CREATING,
|
||||||
players=None, winners=None, style=None):
|
players=None, winners=None, style=None, valid_cells=None):
|
||||||
self.room = room
|
self.room = room
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.admin = admin
|
self.admin = admin
|
||||||
@ -145,6 +147,9 @@ class BingoGame:
|
|||||||
if style is None:
|
if style is None:
|
||||||
style = BingoGame._default_style
|
style = BingoGame._default_style
|
||||||
self.style = style
|
self.style = style
|
||||||
|
if valid_cells is None:
|
||||||
|
valid_cells = []
|
||||||
|
self.valid_cells = valid_cells
|
||||||
|
|
||||||
def should_end(self):
|
def should_end(self):
|
||||||
# TODO: add winner count
|
# TODO: add winner count
|
||||||
@ -166,6 +171,7 @@ class BingoGame:
|
|||||||
"players": list(self.players),
|
"players": list(self.players),
|
||||||
"winners": self.winners,
|
"winners": self.winners,
|
||||||
"style": self.style,
|
"style": self.style,
|
||||||
|
"valid_cells": self.valid_cells,
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -179,6 +185,7 @@ class BingoGame:
|
|||||||
players=set(j["players"]),
|
players=set(j["players"]),
|
||||||
winners=j["winners"],
|
winners=j["winners"],
|
||||||
style=j["style"],
|
style=j["style"],
|
||||||
|
valid_cells=j["valid_cells"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +21,11 @@ function initNet() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
SOCKET.on("end_message", msg => {
|
SOCKET.on("end_message", msg => {
|
||||||
alert(msg.text)
|
if (msg.replay) {
|
||||||
|
createGameModal();
|
||||||
|
} else {
|
||||||
|
TEXT.text = msg.text;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
SOCKET.on("game_state", msg => {
|
SOCKET.on("game_state", msg => {
|
||||||
@ -33,7 +37,10 @@ function initNet() {
|
|||||||
"room": ROOM,
|
"room": ROOM,
|
||||||
})
|
})
|
||||||
} else if (msg.state === "ENDED") {
|
} else if (msg.state === "ENDED") {
|
||||||
SOCKET.emit("get_end_message")
|
SOCKET.emit("get_end_message", {
|
||||||
|
"oid": selfOid(),
|
||||||
|
"room": ROOM,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -53,8 +60,14 @@ function initNet() {
|
|||||||
SOCKET.on("get_card_rsp", msg => {
|
SOCKET.on("get_card_rsp", msg => {
|
||||||
// Add self card
|
// Add self card
|
||||||
let card = new BingoCard(msg.card.oid, msg.parent);
|
let card = new BingoCard(msg.card.oid, msg.parent);
|
||||||
|
|
||||||
card._self = msg.card;
|
card._self = msg.card;
|
||||||
|
|
||||||
|
Object.keys(CARDS).forEach(key => {
|
||||||
|
if (key !== "SELF") {
|
||||||
|
CARDS[key]._destroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
CARDS = {};
|
||||||
CARDS[msg.card.oid] = card;
|
CARDS[msg.card.oid] = card;
|
||||||
CARDS["SELF"] = card;
|
CARDS["SELF"] = card;
|
||||||
updateCards();
|
updateCards();
|
||||||
@ -81,7 +94,10 @@ function initNet() {
|
|||||||
"room": ROOM,
|
"room": ROOM,
|
||||||
})
|
})
|
||||||
} else if (msg.state === "ENDED") {
|
} else if (msg.state === "ENDED") {
|
||||||
SOCKET.emit("get_end_message")
|
SOCKET.emit("get_end_message", {
|
||||||
|
"oid": selfOid(),
|
||||||
|
"room": ROOM,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ function onCreateGameSubmit() {
|
|||||||
"mode": gameMode,
|
"mode": gameMode,
|
||||||
"maximum_size": maximumSize,
|
"maximum_size": maximumSize,
|
||||||
"middle_free": middleFree,
|
"middle_free": middleFree,
|
||||||
"pool": pool.split(/\s+/).map(w => w.trim())
|
"pool": pool.split(/\n+/).map(w => w.trim())
|
||||||
})
|
})
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -255,6 +255,20 @@ function BingoCard(oid, parent, small = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g._destroy = function () {
|
||||||
|
let toDestroy = [];
|
||||||
|
g.children.forEach(child => {
|
||||||
|
if (child !== this._text) {
|
||||||
|
toDestroy.push(child);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
toDestroy.forEach(x => {
|
||||||
|
x._destroy();
|
||||||
|
})
|
||||||
|
|
||||||
|
g.destroy({texture: true, baseTexture: true, children: true});
|
||||||
|
}
|
||||||
|
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,8 +279,7 @@ function makeText() {
|
|||||||
const t = new PIXI.Text("", {
|
const t = new PIXI.Text("", {
|
||||||
fontFamily: STYLE.font,
|
fontFamily: STYLE.font,
|
||||||
fontSize: 38,
|
fontSize: 38,
|
||||||
fill: STYLE.cell.text,
|
fill: STYLE.message,
|
||||||
strokeThickness: 2,
|
|
||||||
align: "left",
|
align: "left",
|
||||||
breakWords: true,
|
breakWords: true,
|
||||||
wordWrap: true,
|
wordWrap: true,
|
||||||
@ -360,6 +373,7 @@ function updateCards() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.onresize = function () {
|
window.onresize = function () {
|
||||||
|
calculateDimensions();
|
||||||
updateCards();
|
updateCards();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user