Merge branch 'main' of codeberg.org:adrien-lsh/ft_transcendence

This commit is contained in:
Xamora 2024-05-05 09:33:10 +02:00
commit 7971222c50
6 changed files with 45 additions and 32 deletions

View File

@ -30,6 +30,11 @@ export class Profile extends AExchangeable
*/ */
this.avatar = avatar; this.avatar = avatar;
/**
* @type {Boolean}
**/
this.online = null;
/** /**
* @type {Boolean} * @type {Boolean}
*/ */
@ -58,6 +63,7 @@ export class Profile extends AExchangeable
this.id = response_data.id; this.id = response_data.id;
this.username = response_data.username; this.username = response_data.username;
this.avatar = response_data.avatar; this.avatar = response_data.avatar;
this.online = response_data.online
if (!this.client.me || this.client.me.id === this.id) if (!this.client.me || this.client.me.id === this.id)
return; return;

View File

@ -43,13 +43,12 @@ class TicTacToe
case 'o': case 'o':
this.sign = messageData.detail; this.sign = messageData.detail;
this.turn = messageData.detail == "x"; this.turn = messageData.detail == "x";
if (this.turn)
this.setOutline(4, false);
break; break;
case 'game_start': case 'game_start':
this.game.started = true; this.game.started = true;
this.game.finished = false; if (this.turn)
this.setOutline(4, false);
break; break;
case 'game_move': case 'game_move':
@ -63,6 +62,20 @@ class TicTacToe
this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion)); this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
this.printWin(messageData.winning_sign); this.printWin(messageData.winning_sign);
break; break;
case 'catchup':
this.map = messageData.morpion;
for (let i = 0; i < 9; i++)
{
for (let j = 0; j < 9; j++)
{
if (this.map[i][j] != -1)
this.printSign(i, j, this.map[i][j])
}
}
this.turn = (messageData.turn == this.sign);
this.currentMorpion = messageData.currentMorpion;
if (this.turn)
this.setOutline(this.currentMorpion, false);
} }
} }
@ -98,11 +111,6 @@ class TicTacToe
return -1 return -1
} }
} }
printWin()
{
}
onClick(event, morpion) onClick(event, morpion)
{ {
let x = event.offsetX; let x = event.offsetX;
@ -142,9 +150,7 @@ class TicTacToe
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1; this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1;
this.currentMorpion = targetCase; this.currentMorpion = targetCase;
this.printSign(targetMorpion, targetCase, this.sign); this.printSign(targetMorpion, targetCase, this.sign);
console.log(targetMorpion, targetCase)
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign})); this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign}));
console.log(this.turn);
this.turn = !this.turn; this.turn = !this.turn;
} }

View File

@ -38,11 +38,16 @@ class TicTacToeWebSocket(WebsocketConsumer):
self.game: TicTacToeGame = game_manager.get(self.game_id, "tictactoe") self.game: TicTacToeGame = game_manager.get(self.game_id, "tictactoe")
if (self.game is None):
return
self.member = self.game.join(self.user.pk, self) self.member = self.game.join(self.user.pk, self)
if (isinstance(self.member, TicTacToePlayer)): if (isinstance(self.member, TicTacToePlayer)):
self.member.send(self.member.sign) self.member.send(self.member.sign)
if (self.game._everbody_is_here() and self.game.model.started == True and self.game.model.finished == False):
self.member.send("catchup", {"morpion" : self.game._map, "turn" : self.game.turn, "currentMorpion": self.member.currentMorpion})
return
if (self.game._everbody_is_here() and self.game.model.started == False): if (self.game._everbody_is_here() and self.game.model.started == False):
if (self.game.time != -1): if (self.game.time != -1):
self.game.broadcast("opponent_joined") self.game.broadcast("opponent_joined")
@ -55,18 +60,18 @@ class TicTacToeWebSocket(WebsocketConsumer):
if (data.get("targetMorpion") is not None and data.get("targetCase") is not None): if (data.get("targetMorpion") is not None and data.get("targetCase") is not None):
if (self.game.add(data, self.member) == False): if (self.game.add(data, self.member) == False):
return return
if (data.get("catchup") is not None and self.game.model.finished == False and self.game.model.finished == True):
self.member.send("catchup", {"Morpion": self.game._map, "turn": self.game.turn})
if (self.game.checkWin() != False): if (self.game.checkWin() != False):
print(self.game.checkWin()) self.winner = self.game.checkWin()
self.game.broadcast("game_end", {"winning_sign": self.member.sign}) self.game.model.finish(self.user)
self.game.broadcast("game_end", {"winning_sign": self.winner})
self.game.broadcast("game_move", data, [self.member]) self.game.broadcast("game_move", data, [self.member])
pass pass
def disconnect(self, event): def disconnect(self, event):
self.member.socket = None try:
self.game.time = time.time() self.member.socket = None
self.game.broadcast("opponent_leave_timer") except:
pass
class PongWebSocket(WebsocketConsumer): class PongWebSocket(WebsocketConsumer):

View File

@ -24,6 +24,8 @@ class TicTacToeGame(AGame):
self._map = [[-1 for _ in range(9)] for _ in range(9)] self._map = [[-1 for _ in range(9)] for _ in range(9)]
self.winner = None
def _everbody_is_here(self): def _everbody_is_here(self):
return len(self.players) == len(self.get_players_connected()) return len(self.players) == len(self.get_players_connected())
@ -41,21 +43,18 @@ class TicTacToeGame(AGame):
return player return player
def add(self, newmove, player): def add(self, newmove, player):
for i in self._map: if (self.checkMove(newmove, player) and self.checkWin() == False):
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") self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign")
player.currentMorpion = int(newmove.get("targetCase")) player.currentMorpion = int(newmove.get("targetCase"))
self.turn = newmove.get("sign") self.changeTurn()
return True return True
return False return False
def changeTurn(self):
self.turn = 'x' if (self.turn == 'o') else 'o'
def checkMove(self, newmove, player): def checkMove(self, newmove, player):
print(int(newmove.get("targetMorpion")), player.currentMorpion)
if (int(newmove.get("targetMorpion")) != player.currentMorpion or newmove.get("sign") != self.turn): if (int(newmove.get("targetMorpion")) != player.currentMorpion or newmove.get("sign") != self.turn):
return False return False
@ -68,7 +67,7 @@ class TicTacToeGame(AGame):
for tab in self._map: for tab in self._map:
for i in range(3): for i in range(3):
if tab[i] != -1 and tab[i] == tab[i + 3] and tab[i + 3] == tab[i + 6]: if tab[i] != -1 and tab[i] == tab[i + 3] and tab[i + 3] == tab[i + 6]:
return tab[i] return
for i in range(0, 9, 3): for i in range(0, 9, 3):
if tab[i] != -1 and tab[i] == tab[i + 1] and tab[i + 1] == tab[i + 2]: if tab[i] != -1 and tab[i] == tab[i + 1] and tab[i + 1] == tab[i + 2]:
return tab[i] return tab[i]

View File

@ -25,12 +25,9 @@ class ProfileSerializer(serializers.ModelSerializer):
user = request.user user = request.user
if user is None: if user is None:
return False return None
if user.pk == obj.pk: if not user.profilemodel.is_friend(obj) and user.pk != obj.pk:
return True
if not user.profilemodel.is_friend(obj):
return None return None
return notice_manager.get_consumer_by_user(obj.user) is not None return notice_manager.get_consumer_by_user(obj.user) is not None

View File

@ -35,4 +35,4 @@ class MyProfileViewSet(viewsets.ModelViewSet):
return Response(ProfileSerializer(profile).data) return Response(ProfileSerializer(profile).data)
def retrieve(self, pk=None): def retrieve(self, pk=None):
return Response(self.serializer_class(self.get_object()).data) return Response(self.serializer_class(self.get_object(), context={'request': self.request}).data)