Skip to content

Commit 8e8ca97

Browse files
committed
fix: fix drag, zoom in/out match and history
1 parent 884c583 commit 8e8ca97

File tree

8 files changed

+968
-103
lines changed

8 files changed

+968
-103
lines changed
Binary file not shown.
0 Bytes
Binary file not shown.

back-end/src/caro-online/Services/GameService.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ private void CheckWinner(Game game, int row, int col)
271271
{
272272
var directions = new[]
273273
{
274-
(1, 0), // Dọc
275-
(0, 1), // Ngang
276-
(1, 1), // Chéo xuống
277-
(1, -1) // Chéo lên
274+
(1, 0),
275+
(0, 1),
276+
(1, 1),
277+
(1, -1)
278278
};
279279

280280
var lastPlayerId = game.CurrentTurn == game.Player1Id ? game.Player2Id : game.Player1Id;
@@ -285,7 +285,6 @@ private void CheckWinner(Game game, int row, int col)
285285
var count = 1;
286286
var maxCount = 1;
287287

288-
// Kiểm tra theo chiều thuận
289288
for (var i = 1; i < 5; i++)
290289
{
291290
var newRow = row + dx * i;
@@ -296,7 +295,6 @@ private void CheckWinner(Game game, int row, int col)
296295
maxCount = Math.Max(maxCount, count);
297296
}
298297

299-
// Kiểm tra theo chiều ngược lại
300298
count = maxCount;
301299
for (var i = 1; i < 5; i++)
302300
{

front-end/src/components/Board.tsx

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ const GridContainer = styled.div`
509509
box-shadow: 0 4px 20px rgba(0,0,0,0.06);
510510
position: relative;
511511
overflow: hidden;
512+
touch-action: none;
512513
`;
513514

514515
const GridScroller = styled.div<{ $isDragging: boolean }>`
@@ -519,6 +520,7 @@ const GridScroller = styled.div<{ $isDragging: boolean }>`
519520
box-sizing: border-box;
520521
cursor: ${props => props.$isDragging ? 'grabbing' : 'grab'};
521522
user-select: none;
523+
touch-action: none;
522524
`;
523525

524526
const Grid = styled.div<{ $x: number; $y: number; $scale: number }>`
@@ -647,14 +649,15 @@ interface BoardProps {
647649
export const Board: React.FC<BoardProps> = ({ game, currentPlayerId, onCellClick, onExitRoom }) => {
648650
const [isExiting, setIsExiting] = useState(false);
649651
const [position, setPosition] = useState({ x: 0, y: 0 });
650-
const [scale, setScale] = useState(1);
652+
const [scale, setScale] = useState(0.8);
651653
const [isDragging, setIsDragging] = useState(false);
652654
const [dragStart, setDragStart] = useState({ x: 0, y: 0 });
653655
const gridRef = useRef<HTMLDivElement>(null);
654656
const isMyTurn = game.currentTurn === currentPlayerId;
655657
const isPlayer1 = currentPlayerId === game.player1Id;
656658

657659
const handleMouseDown = (e: React.MouseEvent) => {
660+
e.preventDefault();
658661
setIsDragging(true);
659662
setDragStart({
660663
x: e.clientX - position.x,
@@ -678,58 +681,36 @@ export const Board: React.FC<BoardProps> = ({ game, currentPlayerId, onCellClick
678681
const handleWheel = useCallback((e: React.WheelEvent) => {
679682
if (e.ctrlKey) {
680683
e.preventDefault();
681-
e.stopPropagation();
682-
683-
const rect = gridRef.current?.getBoundingClientRect();
684-
if (!rect) return;
685-
686-
const mouseX = e.clientX - rect.left;
687-
const mouseY = e.clientY - rect.top;
688-
689-
const delta = -e.deltaY * 0.0003;
690-
const newScale = Math.min(Math.max(0.5, scale + delta), 2);
691-
692-
if (Math.abs(newScale - scale) > 0.001) {
693-
const scaleChange = newScale / scale;
694-
const offsetX = (mouseX - position.x) * (1 - scaleChange);
695-
const offsetY = (mouseY - position.y) * (1 - scaleChange);
696-
697-
setScale(newScale);
698-
setPosition({
699-
x: position.x + offsetX,
700-
y: position.y + offsetY
701-
});
702-
}
684+
const delta = e.deltaY > 0 ? 0.9 : 1.1;
685+
setScale(prev => Math.min(2, Math.max(0.3, prev * delta)));
703686
} else {
704-
const deltaY = e.deltaY;
705-
const deltaX = e.deltaX;
706-
687+
const speed = 1;
707688
setPosition(prev => ({
708-
x: prev.x - deltaX,
709-
y: prev.y - deltaY
689+
x: prev.x - e.deltaX * speed,
690+
y: prev.y - e.deltaY * speed
710691
}));
711692
}
712-
}, [scale, position]);
693+
}, []);
713694

714695
React.useEffect(() => {
715-
const preventDefault = (e: Event) => {
716-
e.preventDefault();
717-
};
696+
if (!gridRef.current) return;
718697

719-
const gridElement = gridRef.current;
720-
if (gridElement) {
721-
gridElement.addEventListener('wheel', preventDefault, { passive: false });
722-
return () => {
723-
gridElement.removeEventListener('wheel', preventDefault);
724-
};
725-
}
698+
const containerRect = gridRef.current.getBoundingClientRect();
699+
const gridSize = 50 * 35;
700+
const initialScale = 0.8;
701+
const scaledSize = gridSize * initialScale;
702+
703+
setPosition({
704+
x: (containerRect.width - scaledSize) / 2,
705+
y: (containerRect.height - scaledSize) / 2
706+
});
707+
setScale(initialScale);
726708
}, []);
727709

728710
const renderCell = useCallback((index: number) => {
729711
const row = Math.floor(index / 50);
730712
const col = index % 50;
731713

732-
// Lấy giá trị trực tiếp từ vị trí trong mảng board
733714
const boardIndex = row * 50 + col;
734715
const value = game.board?.[boardIndex] ?? 0;
735716

0 commit comments

Comments
 (0)