88import { SelectionManager } from "./SelectionManager" ;
99import { v4 as uuidv4 } from "uuid" ;
1010import { WS_DATA_TYPE } from "@repo/common/types" ;
11- import { getShapes } from "@/actions/shape" ;
1211
1312const CORNER_RADIUS_FACTOR = 20 ;
1413const RECT_CORNER_RADIUS_FACTOR = CORNER_RADIUS_FACTOR ;
@@ -34,7 +33,7 @@ export class Game {
3433 private roomId : string | null ;
3534 private canvasBgColor : string ;
3635 private sendMessage : ( ( data : string ) => void ) | null ;
37- private existingShape : Shape [ ] ;
36+ private existingShapes : Shape [ ] ;
3837 private clicked : boolean ;
3938 private roomName : string | null ;
4039 private activeTool : ToolType = "grab" ;
@@ -70,7 +69,7 @@ export class Game {
7069 this . roomId = roomId ;
7170 this . sendMessage = sendMessage ;
7271 this . clicked = false ;
73- this . existingShape = [ ] ;
72+ this . existingShapes = [ ] ;
7473 this . canvas . width = document . body . clientWidth ;
7574 this . canvas . height = document . body . clientHeight ;
7675 this . onScaleChangeCallback = onScaleChangeCallback ;
@@ -84,7 +83,7 @@ export class Game {
8483 if ( this . isStandalone ) {
8584 localStorage . setItem (
8685 LOCALSTORAGE_CANVAS_KEY ,
87- JSON . stringify ( this . existingShape )
86+ JSON . stringify ( this . existingShapes )
8887 ) ;
8988 }
9089 } ) ;
@@ -96,38 +95,11 @@ export class Game {
9695 const storedShapes = localStorage . getItem ( LOCALSTORAGE_CANVAS_KEY ) ;
9796 if ( storedShapes ) {
9897 const parsedShapes = JSON . parse ( storedShapes ) ;
99- this . existingShape = [ ...this . existingShape , ...parsedShapes ] ;
98+ this . existingShapes = [ ...this . existingShapes , ...parsedShapes ] ;
10099 }
101100 } catch ( e ) {
102101 console . error ( "Error loading shapes from localStorage:" , e ) ;
103102 }
104- } else if ( ! this . isStandalone && this . roomName ) {
105- try {
106- const getShapesResult = await getShapes ( { roomName : this . roomName } ) ;
107-
108- if ( getShapesResult . success && getShapesResult . shapes ?. length ) {
109- getShapesResult . shapes . forEach ( ( shape : Shape , index : number ) => {
110- try {
111- const alreadyExists = this . existingShape . some (
112- ( s ) => s . id === shape . id
113- ) ;
114-
115- if ( ! alreadyExists ) {
116- this . existingShape . push ( shape ) ;
117- console . log ( "init(): Pushing shape from getShapesResult" ) ;
118- } else {
119- console . log ( `Shape ${ shape . id } already exists. Skipping.` ) ;
120- }
121- } catch ( e ) {
122- console . error ( `Error processing shape ${ index } :` , e ) ;
123- }
124- } ) ;
125- } else if ( ! getShapesResult . success ) {
126- console . error ( "Error fetching room: " + getShapesResult . error ) ;
127- }
128- } catch ( error ) {
129- console . error ( "Error in init:" , error ) ;
130- }
131103 }
132104 this . clearCanvas ( ) ;
133105 }
@@ -166,7 +138,20 @@ export class Game {
166138 }
167139
168140 updateShapes ( shapes : Shape [ ] ) {
169- this . existingShape = shapes ;
141+ shapes . forEach ( ( newShape ) => {
142+ const existingIndex = this . existingShapes . findIndex (
143+ ( s ) => s . id === newShape . id
144+ ) ;
145+ if ( existingIndex !== - 1 ) {
146+ this . existingShapes [ existingIndex ] = {
147+ ...this . existingShapes [ existingIndex ] ,
148+ ...newShape ,
149+ } ;
150+ } else {
151+ this . existingShapes . push ( newShape ) ;
152+ }
153+ } ) ;
154+
170155 this . clearCanvas ( ) ;
171156 }
172157
@@ -205,7 +190,7 @@ export class Game {
205190 this . canvas . height / this . scale
206191 ) ;
207192
208- this . existingShape . map ( ( shape : Shape ) => {
193+ this . existingShapes . map ( ( shape : Shape ) => {
209194 if ( shape . type === "rectangle" ) {
210195 this . drawRect (
211196 shape . x ,
@@ -299,15 +284,15 @@ export class Game {
299284 ) {
300285 const selectedShape = this . selectionManager . getSelectedShape ( ) ;
301286 if ( selectedShape ) {
302- const index = this . existingShape . findIndex (
287+ const index = this . existingShapes . findIndex (
303288 ( shape ) => shape . id === selectedShape . id
304289 ) ;
305- this . existingShape [ index ] = selectedShape ;
290+ this . existingShapes [ index ] = selectedShape ;
306291 if ( index !== - 1 ) {
307292 if ( this . isStandalone ) {
308293 localStorage . setItem (
309294 LOCALSTORAGE_CANVAS_KEY ,
310- JSON . stringify ( this . existingShape )
295+ JSON . stringify ( this . existingShapes )
311296 ) ;
312297 } else if ( this . sendMessage && this . roomId ) {
313298 this . sendMessage (
@@ -332,7 +317,7 @@ export class Game {
332317 if ( this . selectedShape ) {
333318 localStorage . setItem (
334319 LOCALSTORAGE_CANVAS_KEY ,
335- JSON . stringify ( this . existingShape )
320+ JSON . stringify ( this . existingShapes )
336321 ) ;
337322 }
338323
@@ -420,7 +405,8 @@ export class Game {
420405 break ;
421406
422407 case "pen" :
423- const currentShape = this . existingShape [ this . existingShape . length - 1 ] ;
408+ const currentShape =
409+ this . existingShapes [ this . existingShapes . length - 1 ] ;
424410 if ( currentShape ?. type === "pen" ) {
425411 shape = {
426412 id : uuidv4 ( ) ,
@@ -442,13 +428,13 @@ export class Game {
442428 return ;
443429 }
444430
445- this . existingShape . push ( shape ) ;
431+ this . existingShapes . push ( shape ) ;
446432
447433 if ( this . isStandalone ) {
448434 try {
449435 localStorage . setItem (
450436 LOCALSTORAGE_CANVAS_KEY ,
451- JSON . stringify ( this . existingShape )
437+ JSON . stringify ( this . existingShapes )
452438 ) ;
453439 } catch ( e ) {
454440 console . error ( "Error saving shapes to localStorage:" , e ) ;
@@ -511,8 +497,8 @@ export class Game {
511497 return ;
512498 }
513499 }
514- for ( let i = this . existingShape . length - 1 ; i >= 0 ; i -- ) {
515- const shape = this . existingShape [ i ] ;
500+ for ( let i = this . existingShapes . length - 1 ; i >= 0 ; i -- ) {
501+ const shape = this . existingShapes [ i ] ;
516502
517503 if ( this . selectionManager . isPointInShape ( x , y , shape ) ) {
518504 this . selectedShape = shape ;
@@ -533,7 +519,7 @@ export class Game {
533519 this . startY = y ;
534520
535521 if ( this . activeTool === "pen" ) {
536- this . existingShape . push ( {
522+ this . existingShapes . push ( {
537523 id : uuidv4 ( ) ,
538524 type : "pen" ,
539525 points : [ { x, y } ] ,
@@ -652,7 +638,7 @@ export class Game {
652638
653639 case "pen" :
654640 const currentShape =
655- this . existingShape [ this . existingShape . length - 1 ] ;
641+ this . existingShapes [ this . existingShapes . length - 1 ] ;
656642 if ( currentShape ?. type === "pen" ) {
657643 currentShape . points . push ( { x, y } ) ;
658644 this . drawPencil (
@@ -1045,20 +1031,20 @@ export class Game {
10451031 }
10461032
10471033 eraser ( x : number , y : number ) {
1048- const shapeIndex = this . existingShape . findIndex ( ( shape ) =>
1034+ const shapeIndex = this . existingShapes . findIndex ( ( shape ) =>
10491035 this . isPointInShape ( x , y , shape )
10501036 ) ;
10511037
10521038 if ( shapeIndex !== - 1 ) {
1053- const erasedShape = this . existingShape [ shapeIndex ] ;
1054- this . existingShape . splice ( shapeIndex , 1 ) ;
1039+ const erasedShape = this . existingShapes [ shapeIndex ] ;
1040+ this . existingShapes . splice ( shapeIndex , 1 ) ;
10551041 this . clearCanvas ( ) ;
10561042
10571043 if ( this . isStandalone ) {
10581044 try {
10591045 localStorage . setItem (
10601046 LOCALSTORAGE_CANVAS_KEY ,
1061- JSON . stringify ( this . existingShape )
1047+ JSON . stringify ( this . existingShapes )
10621048 ) ;
10631049 } catch ( e ) {
10641050 console . error ( "Error saving shapes to localStorage:" , e ) ;
@@ -1103,7 +1089,7 @@ export class Game {
11031089 }
11041090
11051091 clearAllShapes ( ) {
1106- this . existingShape = [ ] ;
1092+ this . existingShapes = [ ] ;
11071093 this . clearCanvas ( ) ;
11081094 if ( this . isStandalone ) {
11091095 localStorage . removeItem ( LOCALSTORAGE_CANVAS_KEY ) ;
0 commit comments