package Ġnet . z om is . games . server 2 . inv ites Ċ Ċ import Ġcom . fasterxml . jackson . module . kotlin . jackson ObjectMapper Ċ import Ġcom . fasterxml . jackson . module . kotlin . readValue Ċ import Ġnet . z om is . core . events . Event System Ċ import Ġnet . z om is . games . Features Ċ import Ġnet . z om is . games . dsl . Game Configs Ċ import Ġnet . z om is . games . example . Test Games Ċ import Ġnet . z om is . games . server 2 . Client Json Message Ċ import Ġnet . z om is . games . server 2 . Client Login Event Ċ import Ġnet . z om is . games . server 2 . Message Router Ċ import Ġnet . z om is . games . server 2 . do ct ools . Doc Writer Ċ import Ġnet . z om is . games . server 2 . do ct ools . Events Expect Ċ import Ġnet . z om is . games . server 2 . clients . Fake Client Ċ import Ġnet . z om is . games . server 2 . games .* Ċ import Ġnet . z om is . games . server 2 . test Doc Writer Ċ import Ġorg . junit . jupiter . api . Assertions Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . extension . Register Extension Ċ import Ġjava . util . UUID Ċ Ċ private Ġval Ġmapper Ġ= Ġj ack son ObjectMapper () Ċ fun Ġstring Map Expect ( actual : ĠString , Ġvar arg Ġexpected : ĠPair < String , ĠAny ?> ) Ġ{ ĊĠĠĠ Ġval Ġdocumentation Exc pect ed Ġ= Ġmap Of (* expected ). plus (" ..." Ġto Ġ"... ") ĊĠĠĠ Ġval Ġactual Map : ĠMap < String , ĠAny ?> Ġ= Ġmapper . readValue ( actual ) ĊĠĠĠ Ġval Ġmatch Ġ= Ġexpected . all Ġ{ Ġactual Map [ it . first ] Ġ== Ġit . second Ġ} ĊĊĠĠĠ ĠAssertions . assertTrue ( match ) Ġ{ Ġ" Expected \ n $ documentation Exc pect ed \ n but Ġwas \ n $ actual Map " Ġ} Ċ } Ċ Ċ class ĠInv ite System Test Ġ{ ĊĊĠĠĠ Ġlateinit Ġvar Ġevents : ĠEvent System ĊĠĠĠ Ġlateinit Ġvar Ġfeatures : ĠFeatures ĊĠĠĠ Ġlateinit Ġvar Ġhost : ĠFake Client ĊĠĠĠ Ġlateinit Ġvar Ġinvite e : ĠFake Client ĊĊĠĠĠ Ġ@ Register Extension ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġval Ġexpect : ĠEvents Expect Ġ= ĠEvents Expect () ĊĊĠĠĠ Ġval Ġdoc Writer : ĠDoc Writer Ġ= Ġtest Doc Writer (" INV ITES ") ĊĊĠĠĠ Ġprivate Ġval Ġid Generator : ĠGame Id Generator Ġ= Ġ{ Ġ" 1 " Ġ} ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsystem : ĠInv ite System ĊĠĠĠ Ġprivate Ġval Ġinvite Options Ġ= ĠInv ite Options ( false , ĠInv ite Turn Order . ORDER ED , Ġ- 1 , ĠGame Configs ( emptyList ()), Ġfalse ) ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun Ġbefore () Ġ{ ĊĠĠĠĠĠĠĠ Ġevents Ġ= ĠEvent System () ĊĠĠĠĠĠĠĠ Ġfeatures Ġ= ĠFeatures ( events ) ĊĊĠĠĠĠĠĠĠ Ġval Ġlob by System Ġ= ĠL obby System ( features ) ĊĠĠĠĠĠĠĠ Ġevents . with ( lob by System :: setup ) ĊĠĠĠĠĠĠĠ Ġval Ġrouter Ġ= ĠMessage Router ( this ). route (" lob by ", Ġlob by System . router ) ĊĠĠĠĠĠĠĠ Ġevents . listen (" Route ", ĠClient Json Message :: class , Ġ{ it . data . has (" route ") }) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrouter . handle ( it . data [" route "]. as Text (), Ġit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġgame System Ġ= ĠGame System ( lob by System :: game Clients , ĠGame Callback ({ null }) Ġ{}) ĊĠĠĠĠĠĠĠ Ġfeatures . add Ġ{ Ġf , Ġe Ġ-> Ġgame System . setup ( f , Ġe , Ġid Generator ) Ġ} ĊĊĠĠĠĠĠĠĠ Ġfun Ġcreate Game Callback ( game Type : ĠString , Ġoptions : ĠInv ite Options ): ĠServer Game ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ= Ġgame System . get Game Type ( game Type ) !!. create Game ( options ) ĊĊĠĠĠĠĠĠĠ Ġsystem Ġ= ĠInv ite System ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġgame Clients Ġ= Ġlob by System :: game Clients , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Game Callback Ġ= Ġ:: create Game Callback , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġstart Game Executor Ġ= Ġ{ Ġevents . execute ( it ) Ġ}, ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġinvite Id Generator Ġ= Ġ{ Ġ" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc " Ġ}) ĊĠĠĠĠĠĠĠ Ġrouter . route (" inv ites ", Ġsystem . router ) ĊĠĠĠĠĠĠĠ Ġ// ĠDon 't Ġneed Ġthe ĠL obby System Ġfeature Ġhere ĊĊĠĠĠĠĠĠĠ Ġhost Ġ= ĠFake Client (). apply Ġ{ Ġupdate Info (" Host ", ĠUUID . fromString (" 0 0 0 0 0 0 0 0 - 0 0 0 0 - 0 0 0 0 - 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 ")) Ġ} ĊĠĠĠĠĠĠĠ Ġinvite e Ġ= ĠFake Client (). apply Ġ{ Ġupdate Info (" Inv ited ", ĠUUID . fromString (" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 ")) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġfull Invite () Ġ{ ĊĠĠĠĠĠĠĠ Ġevents . with ( Lobby System ( features ):: setup ) Ġ// ĠWe Ġneed Ġto Ġlookup Ġplayer Ġby Ġname ĊĠĠĠĠĠĠĠ Ġevents . execute ( Game Type Register Event ( Test Games . test Game Type )) ĊĠĠĠĠĠĠĠ Ġevents . execute ( Game Type Register Event ( Test Games . other Game Type )) ĊĠĠĠĠĠĠĠ Ġhost Ġ= ĠFake Client (). apply Ġ{ Ġupdate Info (" Test Client A ", ĠUUID . fromString (" 0 0 0 0 0 0 0 0 - 0 0 0 0 - 0 0 0 0 - 0 0 0 0 - 0 0 0 0 0 0 0 0 0 0 0 0 ")) Ġ} ĊĠĠĠĠĠĠĠ Ġinvite e Ġ= ĠFake Client (). apply Ġ{ Ġupdate Info (" Test Client B ", ĠUUID . fromString (" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 ")) Ġ} Ċ Ċ // ĠĠĠĠĠĠĠ Ġevents . execute ( Client Login Event ( host , Ġhost . name !! , Ġ" tests ", Ġ" token ")) Ċ // ĠĠĠĠĠĠĠ Ġevents . execute ( Client Login Event ( invite e , Ġinvite e . name !! , Ġ" tests ", Ġ" token 2 ")) ĊĠĠĠĠĠĠĠ Ġhost . send ToServer ( events , Ġ""" { Ġ" route ": Ġ" lob by / join ", Ġ" game Types ": Ġ[" Test Game Type ", Ġ" Other Game Type "], Ġ" max Games ": Ġ 1 Ġ} """) ĊĠĠĠĠĠĠĠ Ġinvite e . send ToServer ( events , Ġ""" { Ġ" route ": Ġ" lob by / join ", Ġ" game Types ": Ġ[" Test Game Type ", Ġ" Other Game Type "], Ġ" max Games ": Ġ 1 Ġ} """) ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Lobby Change "," player ": ${ invite e . id Name }," action ":" joined "," game Types ":[" Test Game Type "," Other Game Type "]} """, Ġhost . next Message ()) ĊĊĠĠĠĠĠĠĠ Ġdoc Writer . document ( events , Ġ" Inv iting Ġsomeone Ġto Ġplay Ġa Ġgame ") Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtext (" Inv iting Ġplayers Ġis Ġdone Ġby Ġinv iting Ġtheir Ġplayer Id , Ġwhich Ġwill Ġbe Ġunique ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsend ( host , Ġ""" { Ġ" route ": Ġ" inv ites / invite ", Ġ" game Type ": Ġ" Test Game Type ", Ġ" invite ": Ġ[" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "] Ġ} """) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( invite e , Ġ""" {" type ":" Invite "," host ":" Test Client A "," game ":" Test Game Type "," invite Id ":" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc "} """) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( host , Ġ""" {" type ":" Invite Status "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," status ":" pending "," invite Id ":" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc "} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġdoc Writer . document ( events , Ġ" Accept ing Ġan Ġinvite ") Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsend ( invite e , Ġ""" { Ġ" route ": Ġ" inv ites / 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc / respond ", Ġ" accepted ": Ġtrue Ġ} """) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( host , Ġmap Of (" type " Ġto Ġ" Invite View ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( host , Ġ""" {" type ":" Invite Response "," invite Id ":" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," accepted ": true } """) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġtext (" When Ġa Ġuser Ġaccepts Ġan Ġinvite Ġthe Ġgame Ġis Ġstarted Ġautomatically Ġand Ġboth Ġplayers Ġwill Ġreceive Ġa Ġ` Game Started ` Ġmessage .") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġplayers String Ġ= Ġ""" ${ host . id Name }, ${ invite e . id Name } """ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( invite e , Ġmap Of (" type " Ġto Ġ" Invite View ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( invite e , Ġmap Of (" type " Ġto Ġ" Invite View ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( invite e , Ġ""" {" type ":" Game Started "," game Type ":" Test Game Type "," game Id ":" 1 "," access ":{" 1 ":" ADMIN "}," players ": [$ players String ]} """) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġsystem . create Invite (" Test Game Type ", Ġ" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc ", Ġinvite Options , Ġhost , ĠlistOf ( invite e )) ĊĠĠĠĠĠĠĠ Ġhost . clear Messages () ĊĊĠĠĠĠĠĠĠ Ġdoc Writer . document ( events , Ġ" Decl ining Ġan Ġinvite ") Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsend ( invite e , Ġ""" { Ġ" route ": Ġ" inv ites / 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc / respond ", Ġ" accepted ": Ġfalse Ġ} """) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreceive ( host , Ġ""" {" type ":" Invite Response "," invite Id ":" 1 2 3 4 5 6 7 8 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 - 1 2 3 4 5 6 7 8 9 abc "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," accepted ": false } """) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġinvite Accepted () Ġ{ ĊĠĠĠĠĠĠĠ Ġevents . execute ( Game Type Register Event ( Test Games . game Type (" My Game "))) ĊĠĠĠĠĠĠĠ Ġevents . execute ( Client Login Event ( host , Ġhost . name !! , Ġhost . name !! , Ġ" tests ", Ġ" token ")) ĊĠĠĠĠĠĠĠ Ġevents . execute ( Client Login Event ( invite e , Ġinvite e . name !! , Ġinvite e . name !! , Ġ" tests ", Ġ" token 2 ")) ĊĠĠĠĠĠĠĠ Ġexpect . event ( events Ġto ĠGame Started Event :: class ). condition Ġ{ Ġtrue Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġinvite Ġ= Ġsystem . create Invite (" My Game ", Ġ" inv - 1 ", Ġinvite Options , Ġhost , ĠlistOf ( invite e )) ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite "," host ":" Host "," game ":" My Game "," invite Id ":" inv - 1 "} """, Ġinvite e . next Message ()) ĊĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite Status "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," status ":" pending "," invite Id ":" inv - 1 "} """, Ġhost . next Message ()) ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( host . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĊĠĠĠĠĠĠĠ Ġinvite . respond ( invite e , Ġtrue ) ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite Response "," invite Id ":" inv - 1 "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," accepted ": true } """, Ġhost . next Message ()) ĊĊĠĠĠĠĠĠĠ Ġval Ġplayers String Ġ= Ġ""" ${ host . id Name }, ${ invite e . id Name } """ ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( host . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Game Started "," game Type ":" My Game "," game Id ":" 1 "," access ":{" 0 ":" ADMIN "}," players ": [$ players String ]} """, Ġhost . next Message ()) ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( invite e . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( invite e . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Game Started "," game Type ":" My Game "," game Id ":" 1 "," access ":{" 1 ":" ADMIN "}," players ": [$ players String ]} """, Ġinvite e . next Message ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġinvite Decl ined () Ġ{ ĊĠĠĠĠĠĠĠ Ġevents . execute ( Game Type Register Event ( Test Games . game Type (" My Game "))) ĊĠĠĠĠĠĠĠ Ġval Ġinvite Ġ= Ġsystem . create Invite (" My Game ", Ġ" inv - 1 ", Ġinvite Options , Ġhost , ĠlistOf ( invite e )) ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite "," host ":" Host "," game ":" My Game "," invite Id ":" inv - 1 "} """, Ġinvite e . next Message ()) ĊĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite Status "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," status ":" pending "," invite Id ":" inv - 1 "} """, Ġhost . next Message ()) ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( host . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĊĠĠĠĠĠĠĠ Ġinvite . respond ( invite e , Ġfalse ) ĊĠĠĠĠĠĠĠ ĠAssertions . assertEquals (""" {" type ":" Invite Response "," invite Id ":" inv - 1 "," player Id ":" 1 1 1 1 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1 1 1 1 1 1 1 1 1 "," accepted ": false } """, Ġhost . next Message ()) ĊĠĠĠĠĠĠĠ Ġstring Map Expect ( host . next Message (), Ġ" type " Ġto Ġ" Invite View ") ĊĠĠĠ Ġ} Ċ Ċ }