Skip to content

Commit a853c86

Browse files
committed
refix room class
1 parent 19140d0 commit a853c86

File tree

11 files changed

+227
-145
lines changed

11 files changed

+227
-145
lines changed

rac/servertest.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44
from flask import Flask, json, request
55
from flask_socketio import SocketIO, emit
66

7-
from dto.room import RoomDTO
8-
from src.room.room import Room
9-
import src.room.room as room
10-
from src.room.user import User
11-
import src.room.user as user
127

138
app = Flask(__name__)
149
app.config["SECRET_KEY"] = "secret!"
1510
CORS(app, resources={r"/*": {"origins": "*"}})
1611
socketio = SocketIO(app, cors_allowed_origins="*")
1712

1813

19-
def serialization(data):
20-
if isinstance(data, dict):
21-
return json.dumps(data)
22-
return data.__dict__
23-
2414

2515
@socketio.on("connect")
2616
def connect():
@@ -32,23 +22,6 @@ def disconnect():
3222
print(">> client {} disconnected to server".format(request.sid))
3323

3424

35-
@socketio.on("hehe")
36-
def hehe(payload):
37-
user1 = User(request.sid, "triet dep trai")
38-
#
39-
room = Room("room1", user1)
40-
# socketio.emit
41-
#
42-
# socketio.emit("hehe", {"room": serialization(room), "user": serialization(user1)}, to=request.sid)
43-
socketio.emit(
44-
"hehe",
45-
{
46-
"room": RoomDTO.to_dto(room),
47-
"user": User.to_dto(user1),
48-
},
49-
to=request.sid,
50-
)
51-
5225

5326
if __name__ == "__main__":
5427
socketio.run(app, debug=False)

src/game/Game.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
33
from abc import ABC, abstractmethod
4-
from src.util.cell import Cell
5-
from src.util.point import Point
4+
from util.point import Point
65
from typing import List
76
from rich.console import Console
87
from rich.table import Table
98

9+
from util.cell import Cell
10+
1011

1112
if TYPE_CHECKING:
1213
from src.player.Player import Player

src/game/TicTacToe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def getWinnerSymbol(self) -> Cell:
2727
if self.board[0][2] == self.board[1][1] == self.board[2][0] != Cell.NONE:
2828
return self.board[0][2]
2929
return Cell.NONE
30+
3031

3132

3233

src/model/CaroModel.py renamed to src/model/SumokuModel.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import time
2-
from src.util.cell import Cell
2+
from util.cell import Cell
33

44

55
class CaroModel:
66
board = []
77
start = time.time()
88

9-
time_limit = 3
9+
time_limit = 6
1010

1111
def __init__(self):
1212
pass
@@ -39,8 +39,8 @@ def player(self, board):
3939
x_count = 0
4040
o_count = 0
4141

42-
for i in range(self.sizeMatrix):
43-
for j in range(self.sizeMatrix):
42+
for i in range(board.__len__()):
43+
for j in range(board[i].__len__()):
4444
if board[j][i] == Cell.X:
4545
x_count += 1
4646
elif board[j][i] == Cell.O:
@@ -184,28 +184,28 @@ def min_alpha_beta(self, alpha, beta, board):
184184
return (0, 0, 0)
185185
start = time.time()
186186

187-
for i in range(0, board.__len__()):
188-
for j in range(0, board[i].__len__()):
189-
if (time.time() - start) > self.time_limit:
190-
return (minv, qx, qy)
187+
for action in self.actions(board):
188+
(i, j) = action
189+
if (time.time() - start) > self.time_limit:
190+
return (minv, qx, qy)
191191

192-
if board[i][j] == Cell.NONE:
193-
board[i][j] = Cell.O
194-
(m, max_i, max_j) = self.max_alpha_beta(alpha, beta, board)
195-
if m == -1:
196-
board[i][j] = Cell.NONE
197-
return (-1, i, j)
198-
199-
if m < minv:
200-
minv = m
201-
qx = i
202-
qy = j
192+
if board[i][j] == Cell.NONE:
193+
board[i][j] = Cell.O
194+
(m, max_i, max_j) = self.max_alpha_beta(alpha, beta, board)
195+
if m == -1:
203196
board[i][j] = Cell.NONE
197+
return (-1, i, j)
198+
199+
if m < minv:
200+
minv = m
201+
qx = i
202+
qy = j
203+
board[i][j] = Cell.NONE
204204

205-
if minv <= alpha:
206-
return (minv, qx, qy)
205+
if minv <= alpha:
206+
return (minv, qx, qy)
207207

208-
if minv < beta:
209-
beta = minv
208+
if minv < beta:
209+
beta = minv
210210

211211
return (minv, qx, qy)

src/model/TictactoeModel.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List
22

3-
from src.util.cell import Cell
3+
from util.cell import Cell
44

55

66
class TictactoeModel:
@@ -9,16 +9,32 @@ def __init__(self):
99

1010
def checkWinner(self, board: List[List[Cell]]):
1111
for i in range(3):
12-
if board[i][0] == board[i][1] and board[i][1] == board[i][2] and board[i][0] != Cell.NONE:
12+
if (
13+
board[i][0] == board[i][1]
14+
and board[i][1] == board[i][2]
15+
and board[i][0] != Cell.NONE
16+
):
1317
return board[i][0]
1418

15-
if board[0][i] == board[1][i] and board[1][i] == board[2][i] and board[0][i] != Cell.NONE:
19+
if (
20+
board[0][i] == board[1][i]
21+
and board[1][i] == board[2][i]
22+
and board[0][i] != Cell.NONE
23+
):
1624
return board[0][i]
1725

18-
if board[0][0] == board[1][1] and board[1][1] == board[2][2] and board[0][0] != Cell.NONE:
26+
if (
27+
board[0][0] == board[1][1]
28+
and board[1][1] == board[2][2]
29+
and board[0][0] != Cell.NONE
30+
):
1931
return board[0][0]
2032

21-
if board[0][2] == board[1][1] and board[1][1] == board[2][0] and board[0][2] != Cell.NONE:
33+
if (
34+
board[0][2] == board[1][1]
35+
and board[1][1] == board[2][0]
36+
and board[0][2] != Cell.NONE
37+
):
2238
return board[0][2]
2339

2440
return None
@@ -74,6 +90,7 @@ def min_alpha_beta(self, alpha: int, beta: int, board: List[List[Cell]]):
7490
for action in self.actions(board):
7591
(i, j) = action
7692
board[i][j] = Cell.O
93+
7794
(m, max_i, max_j) = self.max_alpha_beta(alpha, beta, board)
7895
if m == -1:
7996
board[i][j] = Cell.NONE
@@ -118,8 +135,8 @@ def max_alpha_beta(self, alpha: int, beta: int, board: List[List[Cell]]):
118135

119136
for action in self.actions(board):
120137
(i, j) = action
121-
print(i, j)
122138
board[i][j] = Cell.X
139+
123140
(m, min_i, min_j) = self.min_alpha_beta(alpha, beta, board)
124141
if m == 1:
125142
board[i][j] = Cell.NONE

src/player/AIPlayer.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from src.model.TictactoeModel import TictactoeModel
2-
from src.model.CaroModel import CaroModel
3-
from src.player.Player import Player
4-
from src.util.point import Point
5-
from src.util.cell import Cell
1+
from User import User
2+
from model.TictactoeModel import TictactoeModel
3+
from model.SumokuModel import CaroModel
4+
from player.Player import Player
5+
from util.point import Point
6+
from util.cell import Cell
67

78

89
class AIPlayer(Player):
9-
def __init__(self):
10-
super().__init__()
10+
def __init__(self, user: User | None):
11+
super().__init__(user)
1112
self.caroModel = CaroModel()
1213
self.tictactoeModel = TictactoeModel()
1314
return

src/player/Player.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
33
from abc import ABC, abstractmethod
4-
from src.util.point import Point
5-
from src.util.cell import Cell
6-
from src.User import User
4+
from util.point import Point
5+
from util.cell import Cell
6+
from User import User
77

88
if TYPE_CHECKING:
99
from src.game.Game import Game

src/room/Room.py

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,81 @@
1-
from typing import List
1+
from typing import Dict, List
22
import uuid
33

4-
from src.game import Game
5-
from src.User import User
4+
from flask import request
5+
6+
from game.Game import Game
7+
from game.TicTacToe import TicTacToe
8+
from game.CasualGame import CasualGame
9+
from User import User
10+
from player.AIPlayer import AIPlayer
11+
from player.PersonPlayer import PersonPlayer
12+
from player.Player import Player
613

714

815
class Room:
916
def __init__(self, name, owner: User):
1017
self.id: str = str(uuid.uuid4())
1118
self.name: str = name
12-
# self.users:List[User] = []
13-
# self.game:Game
14-
self.owner = owner
19+
self.competitor: User
20+
self.owner: User = owner
21+
self.guests: List[User] = []
22+
self.game: Game
23+
24+
def kick(self, userId: str):
25+
requestId = request.sid
26+
if requestId != self.owner.id:
27+
raise Exception("You are not the owner")
28+
29+
if self.competitor is not None and self.competitor.id == userId:
30+
self.competitor = None
1531

16-
def kick(self, uuid: str):
17-
self.users.remove(uuid)
32+
else:
33+
for user in self.guests:
34+
if user.id == userId:
35+
self.guests.remove(user)
36+
break
1837

19-
def add_user(self, user: User):
20-
self.users.append(user.id)
38+
def addGuest(self, user: User):
39+
self.guests.append(user)
2140

22-
def add_game(self, game: Game):
41+
def addCompetitor(self, user: User):
42+
self.competitor = user
43+
44+
def addGame(self, game: Game):
2345
self.game = game
46+
player1: Player = PersonPlayer(self.owner)
47+
player2: Player
48+
49+
if(self.competitor.id == "bot"):
50+
player2 = AIPlayer(self.competitor)
51+
else:
52+
player2 = PersonPlayer(self.competitor)
53+
54+
self.game.addPlayer(player1)
55+
self.game.addPlayer(player2)
56+
57+
58+
def getOwner(self) -> User:
59+
return self.owner
60+
61+
def onLeave(self, userId: str) -> None:
62+
if userId == self.owner.id:
63+
self.owner = self.competitor
64+
self.competitor = None
65+
elif userId == self.competitor.id:
66+
self.competitor = None
67+
else:
68+
userTmp = None
69+
for user in self.guests:
70+
if user.id == userId:
71+
userTmp = user
72+
break
73+
74+
if userTmp is not None:
75+
self.guests.remove(userTmp)
76+
77+
def onJoin(self, user: User) -> None:
78+
self.guests[user.id] = user
2479

25-
# self.lead = None
26-
# self.guest = None
27-
# self.status = None
28-
# self.ready_player = []
29-
# self.game = []
30-
# self.watchers = []
31-
# self.users = []
32-
33-
#
34-
# def leave_room(self, id):
35-
# if self.lead == id:
36-
# self.lead = None
37-
# if self.guest != None:
38-
# self.lead = self.guest
39-
# self.guest = None
40-
# elif self.guest == id:
41-
# self.guest = None
42-
# elif id in self.watchers:
43-
# self.watchers.remove(id)
44-
# if id in self.ready_player:
45-
# self.ready_player.remove(id)
46-
#
47-
# def is_in_room(self, id):
48-
# return self.lead == id or self.guest == id or id in self.watchers
49-
#
50-
# def is_ready(self):
51-
# return len(self.ready_player) == 2
52-
#
53-
# def is_full(self):
54-
# return self.lead != None and self.guest != None
80+
def onDispose(self) -> None:
81+
pass

0 commit comments

Comments
 (0)