package Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . service Ċ Ċ import Ġco . parallel universe . fib ers . Suspend able Ċ import Ġcom . r 3 . cord a . lib . accounts . contracts . states . Account Info Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . flows . Issue Account To Group Flow Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . flows . Update Account Group Flow Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . flows . generate Group Ids For Accounts Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . flows . split Accounts Into Groups Of Four Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . states . Account Group Ċ import Ġcom . r 3 . cord a . lib . accounts . examples . sweep stake . states . Team State Ċ import Ġnet . cord a . core . concurrent . C ord a Future Ċ import Ġnet . cord a . core . contracts . State And Ref Ċ import Ġnet . cord a . core . flows . Flow Logic Ċ import Ġnet . cord a . core . identity . Party Ċ import Ġnet . cord a . core . internal . concurrent . done Future Ċ import Ġnet . cord a . core . node . App Service Hub Ċ import Ġnet . cord a . core . node . services . C ord a Service Ċ import Ġnet . cord a . core . node . services . Vault Ċ import Ġnet . cord a . core . node . services . query By Ċ import Ġnet . cord a . core . node . services . vault . Query Criteria Ċ import Ġnet . cord a . core . serialization . Singleton Serialize As Token Ċ import Ġnet . cord a . core . utilities . get OrThrow Ċ import Ġjava . util .* Ċ Ċ @ C ord a Service Ċ class ĠTo urnament Service ( val Ġservices : ĠApp Service Hub ) Ġ: ĠSingleton Serialize As Token () Ġ{ ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġfun Ġassign Accounts To Groups ( accounts : ĠList < State And Ref < Account Info >>, ĠnumOf Teams : ĠInt , Ġother Party : ĠParty ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġaccount Groups Ġ= Ġsplit Accounts Into Groups Of Four ( accounts ) ĊĠĠĠĠĠĠĠ Ġval Ġgroup Ids Ġ= Ġgenerate Group Ids For Accounts ( accounts . size , ĠnumOf Teams ) ĊĠĠĠĠĠĠĠ Ġval Ġmap Ġ= Ġgroup Ids . zip ( account Groups ) ĊĊĠĠĠĠĠĠĠ Ġmap . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassign Accounts To Group ( it . first , Ġit . second , Ġother Party ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġprivate Ġfun Ġassign Accounts To Group ( groupId : ĠInt , Ġaccounts : ĠList < State And Ref < Account Info >>, Ġother Party : ĠParty ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠinitialState Ġ= Ġflow Aware Start Flow ( Issue Account To Group Flow ( other Party , Ġaccounts . first (), ĠgroupId )). to Comp letableFuture (). get OrThrow () ĊĠĠĠĠĠĠĠ Ġupdate States ( accounts . drop ( 1 ), ĠinitialState , Ġother Party ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġprivate Ġfun Ġupdate States ( accounts : ĠList < State And Ref < Account Info >>, ĠinitialState : ĠState And Ref < Account Group >, Ġother Party : ĠParty ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwhile Ġ( accounts . isNotEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval ĠnewState Ġ= Ġflow Aware Start Flow ( Update Account Group Flow ( other Party , Ġaccounts . first (), ĠinitialState . state . data . linear Id )). to Comp letableFuture (). get OrThrow () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( accounts . size Ġ== Ġ 1 ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġupdate States ( accounts . drop ( 1 ), ĠnewState , Ġother Party ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġfun Ġget Account Ids For Group ( accountId : ĠUUID ): ĠList < UUID > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġgroups Containing Account Ġ= Ġservices . vault Service . query By < Account Group >(). states . filter Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . state . data . accounts . contains ( accountId ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġgroups Containing Account . forEach Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġprintln ( it . state . data . groupName ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġreturn Ġgroups Containing Account . flatMap Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . state . data . accounts ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġfun Ġget Team States (): ĠList < State And Ref < Team State >> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġservices . vault Service . query By < Team State >(). states ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġfun Ġget Win ning Team States (): ĠList < State And Ref < Team State >> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġservices . vault Service . query By < Team State >( Query Criteria . Linear State Query Criteria ( status Ġ= ĠVault . State Status . UN CON SU MED )). states ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ@ Suspend able ĊĠĠĠ Ġinline Ġfun Ġ< re ified ĠT Ġ: ĠAny > Ġflow Aware Start Flow ( flow Logic : ĠFlow Logic < T >): ĠC ord a Future < T > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcurrent Flow Ġ= ĠFlow Logic . current TopLevel ĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( current Flow Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresult Ġ= Ġcurrent Flow . sub Flow ( flow Logic ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdone Future ( result ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthis . services . start Flow ( flow Logic ). returnValue ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }