game: add: backend input verification
This commit is contained in:
parent
8b9c4d7c1c
commit
a7af70f1f6
@ -17,7 +17,7 @@ class TicTacToe
|
||||
this.game = new AGame(client, game_id, this.onReceive.bind(this), this.uninit.bind(this), "tictactoe")
|
||||
this.canvas = canvas
|
||||
this.context = this.canvas.getContext("2d");
|
||||
this.sign = "x";
|
||||
this.sign;
|
||||
this.turn;
|
||||
}
|
||||
|
||||
@ -31,6 +31,7 @@ class TicTacToe
|
||||
async uninit()
|
||||
{
|
||||
this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
|
||||
this.game.leave()
|
||||
}
|
||||
|
||||
async onReceive(messageData)
|
||||
@ -46,10 +47,38 @@ class TicTacToe
|
||||
else
|
||||
{
|
||||
this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0;
|
||||
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x")
|
||||
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x");
|
||||
if (this.checkWin() != -1)
|
||||
printWin();
|
||||
}
|
||||
}
|
||||
|
||||
checkWin()
|
||||
{
|
||||
for (let i = 0; i < 9; i++)
|
||||
{
|
||||
for (let j = 0; j < 3; j++)
|
||||
{
|
||||
if (this.map[i][j] == this.map[i][j + 3] && this.map[i][j + 3] == this.map[i][j + 6])
|
||||
return (this.map[i][j])
|
||||
}
|
||||
for (let j = 0; i < 9; i += 3)
|
||||
{
|
||||
if (this.map[i][j] == this.map[i][j + 1] && this.map[i][j + 1] == this.map[i][j + 2])
|
||||
return (this.map[i][j])
|
||||
}
|
||||
if (this.map[i][0] == this.map[i][4] && this.map[i][4] == this.map[i][8])
|
||||
return (this.map[i][0]);
|
||||
if (this.map[i][6] == this.map[i][4] && this.map[i][4] == this.map[i][2])
|
||||
return (this.map[i][6]);
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
printWin()
|
||||
{
|
||||
|
||||
}
|
||||
onClick(event, morpion)
|
||||
{
|
||||
let x = event.offsetX;
|
||||
@ -72,7 +101,6 @@ class TicTacToe
|
||||
|
||||
checkCase(targetMorpion, targetCase)
|
||||
{
|
||||
console.log("TargetMorpion :" + targetMorpion + " targetCase :" + targetCase);
|
||||
return (this.map[targetMorpion][targetCase] == -1 && this.turn == true);
|
||||
}
|
||||
|
||||
@ -83,10 +111,10 @@ class TicTacToe
|
||||
|
||||
sendCase(targetMorpion, targetCase)
|
||||
{
|
||||
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1; // soit pas un bozo relis
|
||||
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1;
|
||||
this.printSign(targetMorpion, targetCase, this.sign);
|
||||
console.log(this.game.send, targetMorpion, targetCase)
|
||||
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase}));
|
||||
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign}));
|
||||
console.log(this.turn);
|
||||
this.turn = !this.turn;
|
||||
}
|
||||
|
@ -39,8 +39,8 @@ export class TicTacToeOnlineView extends AbstractView
|
||||
<canvas id="Morpion" width="${this.width}" height="${this.height}"></canvas>
|
||||
</div>
|
||||
<h1>Work in progress bozo</h1>
|
||||
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824821680275576/cat-thumbs-up-middle-finger-cat.png?ex=6613fc09&is=66018709&hm=d901ea182d58ce0ab80fbcc119ae0b769f318c448ae55a6500607ddb8b3f6ff8&">
|
||||
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824792148316180/thumbs-up-cat.png?ex=6613fc02&is=66018702&hm=16748a2df0cb8e8f111c0817ea456289333aa0f8047821e7ae4ea2e57720e843&">
|
||||
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824753686548580/catthumbsup-cat.png?ex=6613fbf9&is=660186f9&hm=94e93b8db9f72fc146942dedf167a3d7476b3dced95ed5c3e80a4ccce8e27f9e&">`;
|
||||
<img src="https://i.imgur.com/hh4yjO9.jpeg&">
|
||||
<img src="https://i.imgur.com/TpOtHY6.png&">
|
||||
<img src="https://i.imgur.com/O8F314O.jpeg&">`;
|
||||
}
|
||||
}
|
@ -8,6 +8,10 @@ import json
|
||||
|
||||
from .objects.GameManager import GameManager
|
||||
|
||||
from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
|
||||
|
||||
import time
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -16,7 +20,6 @@ if TYPE_CHECKING:
|
||||
from .objects.pong.PongGame import PongGame
|
||||
|
||||
from .objects.tictactoe.TicTacToeGame import TicTacToeGame
|
||||
from .objects.tictactoe.TicTacToePlayer import TicTacToePlayer
|
||||
from .objects.tictactoe.TicTacToeSpectator import TicTacToeSpectator
|
||||
|
||||
game_manager: GameManager = GameManager()
|
||||
@ -24,7 +27,7 @@ game_manager: GameManager = GameManager()
|
||||
class TicTacToeWebSocket(WebsocketConsumer):
|
||||
|
||||
def connect(self):
|
||||
|
||||
|
||||
self.user: User = self.scope["user"]
|
||||
if (self.user.pk is None):
|
||||
self.user.pk = 0
|
||||
@ -36,19 +39,27 @@ class TicTacToeWebSocket(WebsocketConsumer):
|
||||
self.game: TicTacToeGame = game_manager.get(self.game_id, "tictactoe")
|
||||
|
||||
self.member = self.game.join(self.user.pk, self)
|
||||
|
||||
self.member.send("x" if self.game._everbody_is_here() else "o")
|
||||
|
||||
if (self.game._everbody_is_here()):
|
||||
if (isinstance(self.member, TicTacToePlayer)):
|
||||
self.member.send(self.member.sign)
|
||||
|
||||
if (self.game._everbody_is_here() and self.game.model.started == False):
|
||||
self.game.broadcast("game_start")
|
||||
self.game.model.start()
|
||||
|
||||
def receive(self, text_data=None, bytes_data=None):
|
||||
print(text_data)
|
||||
self.game.broadcast("", json.loads(text_data), [self.member])
|
||||
data = json.loads(text_data)
|
||||
|
||||
if (data.get("targetMorpion") is not None and data.get("targetCase") is not None):
|
||||
if (self.game.add(data, self.member) == False):
|
||||
return
|
||||
self.game.broadcast("", data, [self.member])
|
||||
pass
|
||||
|
||||
def disconnect(self, event):
|
||||
self.member.socket = None
|
||||
self.game.time = time.time()
|
||||
self.game.broadcast("opponent_leave_timer")
|
||||
|
||||
class PongWebSocket(WebsocketConsumer):
|
||||
|
||||
|
@ -6,32 +6,74 @@ from .TicTacToePlayer import TicTacToePlayer
|
||||
|
||||
from .TicTacToeSpectator import TicTacToeSpectator
|
||||
|
||||
import time
|
||||
|
||||
class TicTacToeGame(AGame):
|
||||
|
||||
|
||||
def __init__(self, game_id: int, game_manager):
|
||||
super().__init__("tictactoe", game_id, game_manager)
|
||||
|
||||
players_id: list[int] = self.model.get_players_id()
|
||||
|
||||
self.players: list[TicTacToePlayer] = [TicTacToePlayer(player_id, None, self, ["x", "o"][i]) for i, player_id in enumerate(players_id)]
|
||||
|
||||
self._map = [[-1 for _ in range(9)] for _ in range(9)]
|
||||
|
||||
def _everbody_is_here(self):
|
||||
return len(self.players) == len(self.get_players_connected())
|
||||
|
||||
def _player_join(self, user_id: int, socket: WebsocketConsumer):
|
||||
|
||||
if (self.model.started):
|
||||
return None
|
||||
|
||||
player = self.get_player_by_user_id(user_id)
|
||||
if (player is None):
|
||||
return None
|
||||
|
||||
|
||||
# check if player is already connected
|
||||
if (player.is_connected()):
|
||||
player.disconnect(1001)
|
||||
|
||||
player.socket = socket
|
||||
|
||||
return player
|
||||
|
||||
def add(self, newmove, player):
|
||||
for i in self._map:
|
||||
print(i)
|
||||
|
||||
print(newmove.get("targetMorpion"), newmove.get("targetCase"), newmove.get("sign"))
|
||||
|
||||
if (self.checkMove(newmove, player)):
|
||||
self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign")
|
||||
player.currentMorpion = int(newmove.get("targetMorpion"))
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def checkMove(self, newmove, player):
|
||||
print(int(newmove.get("targetMorpion")), player.currentMorpion)
|
||||
if (int(newmove.get("targetMorpion")) != player.currentMorpion):
|
||||
return False
|
||||
|
||||
if (self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] != -1):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def checkWin(self):
|
||||
for tab in self._map:
|
||||
for i in range(3):
|
||||
if tab[i] == tab[i + 3] == tab[i + 6]:
|
||||
return tab[i]
|
||||
for i in range(0, 9, 3):
|
||||
if tab[i] == tab[i + 1] == tab[i + 2]:
|
||||
return tab[i]
|
||||
if tab[0] == tab[4] == tab[8]:
|
||||
return tab[0]
|
||||
if tab[6] == tab[4] == tab[2]:
|
||||
return tab[6]
|
||||
return None
|
||||
|
||||
|
||||
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):
|
||||
|
||||
spectator:TicTacToeSpectator = TicTacToeSpectator(user_id, socket, self)
|
||||
@ -42,6 +84,8 @@ class TicTacToeGame(AGame):
|
||||
|
||||
def join(self, user_id: int, socket: WebsocketConsumer) -> TicTacToeSpectator | TicTacToePlayer:
|
||||
member: TicTacToePlayer = self._player_join(user_id, socket)
|
||||
|
||||
if (member is None):
|
||||
member: TicTacToeSpectator = self._spectator_join(user_id, socket)
|
||||
|
||||
return member
|
@ -1,4 +1,12 @@
|
||||
from games.objects.AGame import AGame
|
||||
|
||||
from ..APlayer import APlayer
|
||||
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
class TicTacToePlayer(APlayer):
|
||||
pass
|
||||
def __init__(self, user_id: int, socket: WebsocketConsumer, game: AGame, sign):
|
||||
super().__init__(user_id, socket, game)
|
||||
self.sign = sign
|
||||
self.currentMorpion = 4
|
||||
self.timestamp = None
|
Loading…
Reference in New Issue
Block a user