diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-08 15:43:25 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-08 15:43:25 +0100 |
commit | 2ab7cfe761b47eed848becbc9386f6a94ef44d3d (patch) | |
tree | 5b53e6b20069d74a3250d861533c73dc4a8cefac | |
parent | b56d17983facdbe29f397c08b7b14ac3ff783a88 (diff) |
game/resign thingy
-rw-r--r-- | api/game/socket.py | 26 | ||||
-rw-r--r-- | components/gameBar.tsx | 15 | ||||
-rw-r--r-- | components/ui.tsx | 8 | ||||
-rw-r--r-- | pages/game.tsx | 26 |
4 files changed, 59 insertions, 16 deletions
diff --git a/api/game/socket.py b/api/game/socket.py index c845e08..69e0144 100644 --- a/api/game/socket.py +++ b/api/game/socket.py @@ -32,6 +32,8 @@ class game: self.send("fieldUpdate", { "field": self.board.board }) self.send("turnUpdate", { "player1": self.board.player_1 }) if len(self.board.win_positions) > 0 or self.board.board_full: + winner = self.board.board[int(self.board.win_positions[0][0])] + self.close("finished", "w" if winner == "2" else "l") self.send("finish", { "winPositions": self.board.win_positions, "boardFull": self.board.board_full @@ -44,6 +46,12 @@ class game: if self.board.board_full: self.close("finished", "d") + def resign(self): + self.board.kill_voerbak() + self.send("resign", "") + self.close("resign", "d") + + def close(self, new_status, outcome): cursor.execute(" ".join([ "update games set", @@ -53,16 +61,18 @@ class game: "outcome = ?", "where game_id = ?" ]), [ - int( time.time() * 1000 ) - cursor.execute("select timestamp from games where game_id = ?", [self.game_id]).fetchone()[0], + int( time.time() * 1000 ) - cursor.execute("select started from games where game_id = ?", [self.game_id]).fetchone()[0], new_status, outcome, self.game_id ]) connection.commit() + games.pop(self.game_id) + listeners.pop(self.game_id) + @io.on("newMove") def new_move(data): - print(request.sid) if not data["game_id"] or \ not data["move"] or \ not data["token"]: return @@ -76,11 +86,17 @@ def new_move(data): @io.on("resign") def resign(data): if not data["game_id"] or \ - not data["token"]: return + not request.cookies.get("token"): return if not data["game_id"] in games: return - game = games[data["game_id"]] - game.move(user_id, data["move"]) + user_id = token_login(request.cookies.get("token")) + if not user_id: return + + if games[data["game_id"]].player_1_id != user_id and \ + games[data["game_id"]].player_2_id != user_id: + return + + games[data["game_id"]].resign() @io.on("registerGameListener") def register_game_listener(data): diff --git a/components/gameBar.tsx b/components/gameBar.tsx index 439caea..cacb5e8 100644 --- a/components/gameBar.tsx +++ b/components/gameBar.tsx @@ -6,18 +6,18 @@ import ExitToAppRoundedIcon from '@material-ui/icons/ExitToAppRounded'; import NavigateNextRoundedIcon from '@material-ui/icons/NavigateNextRounded'; import NavigateBeforeRoundedIcon from '@material-ui/icons/NavigateBeforeRounded'; -interface GameBarModuleProps { +function GameBarModule(props: { children?: ReactNode; -} - -function GameBarModule(props: GameBarModuleProps) { + onclick?: () => void; +}) { return <Vierkant style={{ backgroundColor: "var(--background-alt)", padding: 12, borderRadius: 6, margin: 0, - verticalAlign: "top" - }}>{props.children}</Vierkant> + verticalAlign: "top", + cursor: props.onclick ? "pointer" : "default" + }} onclick={props.onclick}>{props.children}</Vierkant> } var GameBarSpacer = () => <div style={{ width: 8, display: "inline-block" }}></div>; @@ -30,6 +30,7 @@ export function GameBar(props: { turn: boolean; player1: boolean; active: boolean; + resignFunction: () => void; }) { return <Vierkant className="gameBar" style={{ padding: 8, @@ -79,7 +80,7 @@ export function GameBar(props: { }}>00:00</span> </GameBarModule> <GameBarSpacer/> - <GameBarModule> + <GameBarModule onclick={props.resignFunction}> <ExitToAppRoundedIcon/> </GameBarModule> <GameBarSpacer/> diff --git a/components/ui.tsx b/components/ui.tsx index e6fdceb..e395a47 100644 --- a/components/ui.tsx +++ b/components/ui.tsx @@ -14,6 +14,7 @@ export function Vierkant(props: { className?: string; id?: string; fullwidth?: boolean; + onclick?: () => void; }) { return <a style={{ padding: 24, @@ -30,7 +31,12 @@ export function Vierkant(props: { undefined, height: props.height ? props.height : undefined, ...props.style - }} href={props.href} className={props.className} id={props.id}>{props.children}</a> + }} + href={props.href} + className={props.className} + id={props.id} + onClick={props.onclick} + >{props.children}</a> } export function Button(props: { diff --git a/pages/game.tsx b/pages/game.tsx index 660156d..58eb639 100644 --- a/pages/game.tsx +++ b/pages/game.tsx @@ -51,6 +51,10 @@ class VoerGame extends Component<VoerGameProps> { if (data.boardFull) outcome = 0; this.setState({ outcome }); }); + + this.io.on("resign", () => { + alert("resign") + }); } io: Socket; @@ -93,9 +97,22 @@ class VoerGame extends Component<VoerGameProps> { maxWidth: "100vh", margin: "0 auto" }}> - <VoerBord width={this.width} height={this.height} onMove={m => this.move(m % this.width + 1)} active={this.props.active == true && this.state.outcome == -1}/> - <GameBar turn={this.state.turn} player1={this.props.player1} active={this.props.active}/> - <GameOutcomeDialog outcome={this.state.outcome} player={this.props.player1 ? 1 : 2} visible={this.state.outcome != -1}/> + <VoerBord + width={this.width} height={this.height} + onMove={m => this.move(m % this.width + 1)} + active={this.props.active == true && this.state.outcome == -1} + /> + <GameBar + turn={this.state.turn} + player1={this.props.player1} + active={this.props.active} + resignFunction={() => {this.io.emit("resign", { game_id: this.props.gameID })}} + /> + <GameOutcomeDialog + outcome={this.state.outcome} + player={this.props.player1 ? 1 : 2} + visible={this.state.outcome != -1} + /> </div> } } @@ -167,6 +184,9 @@ var InviteButtonLabelStyle: CSSProperties = { export default class GamePage extends Component { constructor(props: {}) { super(props); + + if (typeof document === "undefined") return; + // gert } state: { |