@@ -10,34 +10,42 @@ import { Card, DebugState, DebugStateType, UnoGame } from "../types.js";
1010import { getUsername , hasStarted , next , shuffle , toTitleCase , updateStats , without } from "../utils.js" ;
1111import { games , makeStartMessage , sendGameMessage } from "./index.js" ;
1212
13- export function handleDrawCardProxy ( startedGame : UnoGame < true > , userId : string , t , p , n ) {
14- t [ p ] = n ;
15- if ( p === "length" ) startedGame . _debug . pushState ( {
16- type : "set-cards" ,
17- newState : t ,
18- meetsEndCondition : [ p === "length" && n === 0 , n ]
19- } ) ;
20- if ( p === "length" && n === 0 ) {
21- // TODO: check that the card shown here is the correct one and dont just pray it is
22- const card = startedGame . currentCard ;
23- timeouts . delete ( startedGame . channelID ) ;
24- updateStats ( startedGame , userId ) ;
25- delete games [ startedGame . channelID ] ;
26- sendMessage ( startedGame . channelID , {
27- content : `**${ getUsername ( userId , true , client . guilds . get ( startedGame . guildID ) ) } ** \
13+ export function makeDrawCardProxy ( cards : Card [ ] , game : UnoGame < true > , userId : string ) {
14+ return new Proxy ( [ ...cards ] , {
15+ get ( t , p ) {
16+ if ( p === "owner" ) return userId ;
17+ else return t [ p ] ;
18+ } ,
19+ set ( t , p , n ) {
20+ t [ p ] = n ;
21+ if ( p === "length" ) game . _debug . pushState ( {
22+ type : "set-cards" ,
23+ newState : t ,
24+ meetsEndCondition : [ p === "length" && n === 0 , n ]
25+ } ) ;
26+ if ( p === "length" && n === 0 ) {
27+ // TODO: check that the card shown here is the correct one and dont just pray it is
28+ const card = game . currentCard ;
29+ timeouts . delete ( game . channelID ) ;
30+ updateStats ( game , userId ) ;
31+ delete games [ game . channelID ] ;
32+ sendMessage ( game . channelID , {
33+ content : `**${ getUsername ( userId , true , client . guilds . get ( game . guildID ) ) } ** \
2834played ${ cardEmotes [ card ] } ${ toTitleCase ( card ) } , and won` ,
29- components : new ComponentBuilder < MessageActionRow > ( )
30- . addInteractionButton ( {
31- style : ButtonStyles . SUCCESS ,
32- label : "gg" ,
33- emoji : ComponentBuilder . emojiToPartial ( "🏆" , "default" ) ,
34- disabled : true ,
35- customID : "we-have-a-nerd-here🤓"
36- } )
37- . toJSON ( )
38- } ) ;
39- }
40- return true ;
35+ components : new ComponentBuilder < MessageActionRow > ( )
36+ . addInteractionButton ( {
37+ style : ButtonStyles . SUCCESS ,
38+ label : "gg" ,
39+ emoji : ComponentBuilder . emojiToPartial ( "🏆" , "default" ) ,
40+ disabled : true ,
41+ customID : "we-have-a-nerd-here🤓"
42+ } )
43+ . toJSON ( )
44+ } ) ;
45+ }
46+ return true ;
47+ }
48+ } ) ;
4149}
4250
4351const drawUntilNotSpecial = ( game : UnoGame < true > ) => {
@@ -147,11 +155,7 @@ won by default`,
147155 [ p , startedGame . draw ( 7 ) . cards . sort ( ( a , b ) => cards . indexOf ( a ) - cards . indexOf ( b ) ) ]
148156 ) ) ;
149157 Object . keys ( cardsToBeUsed ) . forEach ( id => {
150- cardsToBeUsed [ id ] = new Proxy ( cardsToBeUsed [ id ] , {
151- set ( t , p , n ) {
152- return handleDrawCardProxy ( startedGame , id , t , p , n ) ;
153- }
154- } ) ;
158+ cardsToBeUsed [ id ] = makeDrawCardProxy ( cardsToBeUsed [ id ] , startedGame , id ) ;
155159 } ) ;
156160
157161 startedGame . cards = cardsToBeUsed ;
0 commit comments