@@ -12,17 +12,17 @@ import type {
1212import gameDeck from '../../features/gameDeck' ;
1313import { nameFromImg , devChangeStatus } from '../../dev/debugVisual/features' ;
1414import { dragCountUpdate } from '../../components/draggable/features/actions' ;
15+ import type { BtnType } from '../../types/types' ;
1516
1617type DeckProps = {
17- btns : { left : boolean ; right : boolean ; back : boolean ; flip : boolean } ;
18- btnsSet : React . Dispatch < React . SetStateAction < { left : boolean ; right : boolean ; back : boolean ; flip : boolean } > > ;
18+ setBtnHndlr : ( handler : ( btn : BtnType ) => void ) => void ;
1919 devDeckRestSet : React . Dispatch < React . SetStateAction < number > > ;
2020 devDeckVisibleSet : React . Dispatch < React . SetStateAction < CardType [ ] > > ;
2121 setDevDragsStatus : React . Dispatch < React . SetStateAction < DragsStatusType [ ] > > ;
2222 devSpeed : React . RefObject < DevSpeedControlType > ;
2323} ;
2424
25- const Deck = ( { btns , btnsSet , devDeckRestSet, devDeckVisibleSet, setDevDragsStatus, devSpeed } : DeckProps ) => {
25+ const Deck = ( { setBtnHndlr , devDeckRestSet, devDeckVisibleSet, setDevDragsStatus, devSpeed } : DeckProps ) => {
2626 const dragCountLimit = useRef < number > ( 5 ) ;
2727 const [ dragCount , setDragCount ] = useState < number > ( 5 ) ;
2828 const dragCountRef = useRef < number > ( 5 ) ;
@@ -36,9 +36,6 @@ const Deck = ({ btns, btnsSet, devDeckRestSet, devDeckVisibleSet, setDevDragsSta
3636
3737 const draggedId = useRef < Set < number > > ( new Set ( [ ] ) ) ;
3838
39- // Я могу вынести glow(right/wrong) на урвовень deck и управлять им by max и min value of xyDragged(всех карт в !== sleep)
40- const xyDragged = useRef < XYType [ ] > ( [ ] ) ;
41-
4239 // Удаление карточки:
4340 //* I. Clear
4441 // 0. Add field lastPos
@@ -54,6 +51,7 @@ const Deck = ({ btns, btnsSet, devDeckRestSet, devDeckVisibleSet, setDevDragsSta
5451 } ;
5552 const counter1Ref : React . RefObject < ( n ?: number ) => number > = useRef ( closure ( ) ) ;
5653 const counter2Ref : React . RefObject < ( n ?: number ) => number > = useRef ( closure ( ) ) ;
54+ const counter3Ref : React . RefObject < ( n ?: number ) => number > = useRef ( closure ( ) ) ;
5755
5856 // console.log('🍒', counter2Ref.current(), 'Deck Re-Render');
5957 const cardOutHndlr = ( cardId : number , direction : 'string' , lastPos : XYType , codePoint : string ) => {
@@ -97,7 +95,7 @@ const Deck = ({ btns, btnsSet, devDeckRestSet, devDeckVisibleSet, setDevDragsSta
9795 const prevStatus = prevJ . find ( ( j ) => j . id === card . id ) ?. status ;
9896
9997 let status : DragsStatusStatusType = 'sleep' ;
100- if ( card . comeBack ) status = 'comeBack' ;
98+ if ( card ?. lastPos ) status = 'comeBack' ;
10199 else if ( prevStatus ) status = prevStatus ;
102100 return { id : card . id , dragNum : i , card : nameFromImg ( [ card ] , 0 ) , status } ;
103101 } ) ;
@@ -107,112 +105,114 @@ const Deck = ({ btns, btnsSet, devDeckRestSet, devDeckVisibleSet, setDevDragsSta
107105 } ) ;
108106 } ;
109107
108+ const btnSwipeHndlr = ( direction : 'l' | 'r' ) => {
109+ let notRLDraged : number = 0 ;
110+
111+ for ( let i = 0 ; i < draggedId . current . size ; i ++ ) {
112+ if ( ! deckRef . current [ deckRef . current . length - 1 - i ] ?. btnLR ) notRLDraged ++ ;
113+ }
114+
115+ const indexTop = deckRef . current . length - 1 - ( draggedId . current . size - notRLDraged ) ;
116+ const topCard = deckRef . current [ indexTop ] ;
117+
118+ if ( ! topCard || topCard ?. btnLR || topCard ?. lastPos ) return ;
119+
120+ draggedId . current . add ( topCard . id ) ;
121+ topCard . btnLR = direction ;
122+ setDeck ( deckRef . current ) ;
123+
124+ dragCountUpdate ( draggedId , dragCountLimit , dragCountRef , setDragCount ) ;
125+
126+ //? DEV
127+ setDevDragsStatus ( ( prevJ ) => {
128+ const fresh : DragsStatusType [ ] = deckRef . current . slice ( - dragCountRef . current ) . map ( ( card , i ) => {
129+ const prevStatus = prevJ . find ( ( j ) => j . id === card . id ) ?. status ;
130+
131+ let status : DragsStatusStatusType = 'sleep' ;
132+ if ( card . lastPos ) status = 'comeBack' ;
133+ else if ( card . id === topCard . id ) status = 'fling' ;
134+ else if ( prevStatus ) status = prevStatus ;
135+ return { id : card . id , dragNum : i , card : nameFromImg ( [ card ] , 0 ) , status } ;
136+ } ) ;
137+ return fresh ;
138+ } ) ;
139+ } ;
140+
110141 const hasInitialized = useRef ( false ) ;
111142 useEffect ( ( ) => {
112143 if ( hasInitialized . current ) return ;
113144 hasInitialized . current = true ;
114145
146+ // deck Initialization
115147 const deckFirst = gameDeck ( ) ;
116148 setDeck ( deckFirst ) ;
117-
118149 for ( let i = 0 ; i < dragCountLimit . current ; i ++ ) {
119150 const indexTop = deckFirst . length - dragCountLimit . current + i ;
120151 const cardName = nameFromImg ( deckFirst , indexTop ) ;
121152 setDevDragsStatus ( ( prev ) => {
122153 return [ ...prev , { id : deckFirst [ indexTop ] . id , dragNum : prev . length , card : cardName , status : 'sleep' } ] ;
123154 } ) ;
124155 }
125- } , [ ] ) ;
126-
127- useEffect ( ( ) => {
128- if ( deck . length === 0 ) return ;
129- const vis = deck . slice ( - ( dragCountLimit . current + 15 ) ) ;
130156
131- setDeckVisible ( vis ) ;
132- devDeckVisibleSet ( vis ) ;
133-
134- deckRef . current = deck ;
157+ // btns action
158+ const btnHndlr = ( btn : BtnType ) => {
159+ if ( btn === 'left' ) btnSwipeHndlr ( 'l' ) ;
160+ else if ( btn === 'right' ) btnSwipeHndlr ( 'r' ) ;
161+ else if ( btn === 'back' ) {
162+ if ( deckHistory . current . length === 0 ) {
163+ deckHistoryTop . current = null ;
164+ return ;
165+ }
166+ if ( deckRef . current . some ( ( card ) => card . id === deckHistory . current [ deckHistory . current . length - 1 ] . id ) ) {
167+ console . warn ( '🔁 Карта уже в колоде, повторное добавление пропущено' ) ;
168+ return ;
169+ }
170+ if ( draggedId . current . size >= dragCountLimit . current ) return ;
135171
136- // Dev
137- devDeckRestSet ( deck . length ) ;
138- } , [ deck , dragCountLimit . current ] ) ;
172+ deckHistoryTop . current = deckHistory . current . pop ( ) ?? null ;
173+ const comeBackCard = { ...deckHistoryTop . current } ;
139174
140- useEffect ( ( ) => {
141- if ( ! btns . back ) return ;
175+ setDeck ( ( prev ) => {
176+ const freshDeck = [ ...prev , comeBackCard ] ;
177+ deckRef . current = freshDeck ;
142178
143- if ( deckHistory . current . length === 0 ) {
144- deckHistoryTop . current = null ;
145- return ;
146- }
147- if ( deckRef . current . some ( ( card ) => card . id === deckHistory . current [ deckHistory . current . length - 1 ] . id ) ) {
148- console . warn ( '🔁 Карта уже в колоде, повторное добавление пропущено' ) ;
149- return ;
150- }
151- if ( draggedId . current . size >= dragCountLimit . current ) return ;
179+ draggedId . current . add ( comeBackCard . id ) ;
180+ dragCountUpdate ( draggedId , dragCountLimit , dragCountRef , setDragCount ) ;
152181
153- deckHistoryTop . current = deckHistory . current . pop ( ) ?? null ;
154- const comeBackCard = { ...deckHistoryTop . current , comeBack : true } ;
182+ setDevDragsStatus ( ( prevJ ) => {
183+ const fresh : DragsStatusType [ ] = freshDeck . slice ( - dragCountRef . current ) . map ( ( card , i ) => {
184+ const prevStatus = prevJ . find ( ( j ) => j . id === card . id ) ?. status ;
155185
156- setDeck ( ( prev ) => {
157- const freshDeck = [ ...prev , comeBackCard ] ;
158- deckRef . current = freshDeck ;
186+ let status : DragsStatusStatusType = 'sleep' ;
187+ if ( card ?. lastPos ) status = 'comeBack' ;
188+ else if ( prevStatus ) status = prevStatus ;
189+ return { id : card . id , dragNum : i , card : nameFromImg ( [ card ] , 0 ) , status } ;
190+ } ) ;
191+ return fresh ;
192+ } ) ;
159193
160- draggedId . current . add ( comeBackCard . id ) ;
161- dragCountUpdate ( draggedId , dragCountLimit , dragCountRef , setDragCount ) ;
162-
163- setDevDragsStatus ( ( prevJ ) => {
164- const fresh : DragsStatusType [ ] = freshDeck . slice ( - dragCountRef . current ) . map ( ( card , i ) => {
165- const prevStatus = prevJ . find ( ( j ) => j . id === card . id ) ?. status ;
166-
167- let status : DragsStatusStatusType = 'sleep' ;
168- if ( card . comeBack ) status = 'comeBack' ;
169- else if ( prevStatus ) status = prevStatus ;
170- return { id : card . id , dragNum : i , card : nameFromImg ( [ card ] , 0 ) , status } ;
194+ return freshDeck ;
171195 } ) ;
172- return fresh ;
173- } ) ;
174-
175- return freshDeck ;
176- } ) ;
196+ } else if ( btn === 'flip' ) {
197+ // console.log('flip');
198+ }
199+ } ;
177200
178- btnsSet ( ( prev ) => ( { ... prev , back : false } ) ) ;
179- } , [ btns . back ] ) ;
201+ setBtnHndlr ( btnHndlr ) ;
202+ } , [ ] ) ;
180203
181204 useEffect ( ( ) => {
182- if ( ! btns . left && ! btns . right ) return ;
183- btnsSet ( ( prev ) => ( { ...prev , left : false , right : false } ) ) ;
184-
185- let notRLDraged : number = 0 ;
186-
187- for ( let i = 0 ; i < draggedId . current . size ; i ++ ) {
188- if ( ! deckRef . current [ deckRef . current . length - 1 - i ] ?. btnLR ) notRLDraged ++ ;
189- }
190-
191- const indexTop = deckRef . current . length - 1 - ( draggedId . current . size - notRLDraged ) ;
192- const topCard = deckRef . current [ indexTop ] ;
193-
194- if ( ! topCard || topCard ?. btnLR || topCard ?. comeBack ) return ;
195-
196- draggedId . current . add ( topCard . id ) ;
197- topCard . btnLR = btns . left ? 'l' : 'r' ;
198- setDeck ( deckRef . current ) ;
205+ if ( deck . length === 0 ) return ;
206+ const vis = deck . slice ( - ( dragCountLimit . current + 15 ) ) ;
199207
200- dragCountUpdate ( draggedId , dragCountLimit , dragCountRef , setDragCount ) ;
208+ setDeckVisible ( vis ) ;
209+ devDeckVisibleSet ( vis ) ;
201210
202- //? DEV
203- setDevDragsStatus ( ( prevJ ) => {
204- const fresh : DragsStatusType [ ] = deckRef . current . slice ( - dragCountRef . current ) . map ( ( card , i ) => {
205- const prevStatus = prevJ . find ( ( j ) => j . id === card . id ) ?. status ;
211+ deckRef . current = deck ;
206212
207- let status : DragsStatusStatusType = 'sleep' ;
208- if ( card . comeBack ) status = 'comeBack' ;
209- else if ( card . id === topCard . id ) status = 'fling' ;
210- else if ( prevStatus ) status = prevStatus ;
211- return { id : card . id , dragNum : i , card : nameFromImg ( [ card ] , 0 ) , status } ;
212- } ) ;
213- return fresh ;
214- } ) ;
215- } , [ btns . left , btns . right ] ) ;
213+ // Dev
214+ devDeckRestSet ( deck . length ) ;
215+ } , [ deck , dragCountLimit . current ] ) ;
216216
217217 const [ imgHasError , setImgHasError ] = useState ( false ) ;
218218
0 commit comments