package Ġcom . template . webserver Ċ Ċ import Ġcom . template . flows . Create Flow Ċ import Ġcom . template . flows . Play Flow Ċ import Ġcom . template . states . Board State Ċ import Ġnet . cord a . core . contracts . Amount Ċ import Ġnet . cord a . core . contracts . State And Ref Ċ import Ġnet . cord a . core . contracts . Transaction Verification Exception Ċ import Ġnet . cord a . core . contracts . Unique Identifier Ċ import Ġnet . cord a . core . crypto . new Secure Random Ċ import Ġnet . cord a . core . identity . C ord a X 5 0 0 Name Ċ import Ġnet . cord a . core . messaging . C ord a RP CO ps Ċ import Ġnet . cord a . core . messaging . start Flow Ċ import Ġnet . cord a . core . messaging . start Tracked Flow Ċ import Ġnet . cord a . core . messaging . vault Query By Ċ 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 . utilities . get OrThrow Ċ import Ġorg . slf 4 j . LoggerFactory Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . beans . factory . annotation . Qualifier Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . MediaType Ċ import Ġorg . springframework . http . MediaType . TEXT _ PLAIN _ VALUE Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . web . bind . annotation .* Ċ import Ġjava . util .* Ċ import Ġjavax . servlet . http . HttpServletRequest Ċ Ċ val ĠSERVICE _ NAMES Ġ= ĠlistOf (" Not ary ", Ġ" Network ĠMap ĠService ") Ġ// Ġid k Ġwhy Ġit 's Ġlocated Ġhere Ċ Ċ /** Ċ Ġ* ĠDefine Ġyour ĠAPI Ġendpoints Ġhere . Ċ Ġ*/ Ċ @ RestController Ċ @ RequestMapping ("/") Ġ// ĠThe Ġpaths Ġfor ĠHTTP Ġrequests Ġare Ġrelative Ġto Ġthis Ġbase Ġpath . Ċ class ĠController ( rpc : ĠNode RPC Connection ) Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġlogger Ġ= ĠLoggerFactory . getLogger ( RestController :: class . java ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġprivate Ġval Ġme Ġ= Ġrpc . proxy . node Info (). legal Id entities . first () ĊĠĠĠ Ġprivate Ġval Ġmy Legal Name Ġ= Ġrpc . proxy . node Info (). legal Id entities . first (). name ĊĠĠĠ Ġprivate Ġval Ġproxy Ġ= Ġrpc . proxy ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠReturns Ġthe Ġnode 's Ġname . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[ Ġ" me " Ġ], Ġproduces Ġ= Ġ[ MediaType . APPLICATION _ JSON _ VALUE ]) ĊĠĠĠ Ġfun Ġwho ami () Ġ= Ġmap Of (" me " Ġto Ġmy Legal Name ) ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠReturns Ġall Ġparties Ġregistered Ġwith Ġthe Ġnetwork Ġmap Ġservice . ĠThese Ġnames Ġcan Ġbe Ġused Ġto Ġlook Ġup Ġident ities Ġusing ĊĠĠĠĠ Ġ* Ġthe Ġidentity Ġservice . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[ Ġ" peers " Ġ], Ġproduces Ġ= Ġ[ MediaType . APPLICATION _ JSON _ VALUE ]) ĊĠĠĠ Ġfun Ġget Peers (): ĠMap < String , ĠList < C ord a X 5 0 0 Name >> Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnode Info Ġ= Ġproxy . network Map Snapshot () ĊĠĠĠĠĠĠĠ Ġreturn Ġmap Of (" peers " Ġto Ġnode Info ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġit . legal Id entities . first (). name Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ// filter Ġout Ġmyself , Ġnot ary Ġand Ġevent ual Ġnetwork Ġmap Ġstarted Ġby Ġdriver ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. filter Ġ{ Ġit . organisation Ġ! in Ġ( SERVICE _ NAMES Ġ+ Ġmy Legal Name . organisation ) Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[ Ġ" active Games " Ġ], Ġproduces Ġ= Ġ[ MediaType . APPLICATION _ JSON _ VALUE ]) ĊĠĠĠ Ġfun Ġget Active Games () Ġ: ĠResponseEntity < List < State And Ref < Board State >>> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok ( proxy . vault Query By < Board State >(). states ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠDisplay Ġall Ġpast Ġgames Ġthis Ġplayer Ġhas Ġplayed Ġi . e . Ġstates Ġthat Ġhave Ġbeen Ġtaken Ġoff Ġledger ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[" past Games "], Ġproduces Ġ= Ġ[ MediaType . APPLICATION _ JSON _ VALUE ]) ĊĠĠĠ Ġfun Ġget Past States (): ĠResponseEntity < List < State And Ref < Board State >>> Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcriteria By State Status Ġ= ĠQuery Criteria . Vault Query Criteria ( status Ġ= ĠVault . State Status . CON SU MED ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok ( proxy . vault Query By < Board State >( criteria By State Status ). states . filter Ġ{ Ġit . state . data . outcome Ġ!= Ġ" game Ġstill Ġin Ġprogress " Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ PostMapping ( value Ġ= Ġ[ Ġ" create Game " Ġ], Ġproduces Ġ= Ġ[ ĠTEXT _ PLAIN _ VALUE Ġ], Ġheaders Ġ= Ġ[ Ġ" Content - Type = application / x - www - form - urlencoded " Ġ]) ĊĠĠĠ Ġfun Ġcreate Game ( request : ĠHttpServletRequest ): ĠResponseEntity < String > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġopponent Ġ= Ġrequest . getParameter (" opp onent ") ĊĠĠĠĠĠĠĠ Ġif ( opp onent Ġ== Ġnull ){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . bad Request (). body (" Query Ġparameter Ġ' party Name ' Ġmust Ġnot Ġbe Ġnull .\ n ") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġparty X 5 0 0 Name Ġ= ĠC ord a X 5 0 0 Name . parse ( opp onent ) ĊĠĠĠĠĠĠĠ Ġval Ġother Party Ġ= Ġproxy . well Known Party From X 5 0 0 Name ( party X 5 0 0 Name ) Ġ?: Ġreturn ĠResponseEntity . bad Request (). body (" Party Ġnamed Ġ$ opp onent Ġcannot Ġbe Ġfound .\ n ") ĊĊĠĠĠĠĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġsigned Tx Ġ= Ġproxy . start Tracked Flow (:: Create Flow , Ġother Party ). returnValue . get OrThrow () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . status ( HttpStatus . CREATED ). body (" Transaction Ġid Ġ${ signed Tx . id } Ġcommitted Ġto Ġledger .\ n ") ĊĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( ex : ĠThrowable ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . error ( ex . message , Ġex ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . bad Request (). body ( ex . message !! ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ PostMapping ( value Ġ= Ġ[ Ġ" play " Ġ], Ġproduces Ġ= Ġ[ ĠTEXT _ PLAIN _ VALUE Ġ]) ĊĠĠĠ Ġfun Ġplay ( request : ĠHttpServletRequest ): ĠResponseEntity < String > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġopponent String Ġ= Ġrequest . getParameter (" opp onent ") ĊĠĠĠĠĠĠĠ Ġval Ġpos Ġ= Ġrequest . getParameter (" pos "). toInt () ĊĠĠĠĠĠĠĠ Ġval Ġopponent Ġ= Ġproxy . well Known Party From X 5 0 0 Name ( C ord a X 5 0 0 Name . parse ( opp onent String )) Ġ?: Ġthrow ĠIllegalArgumentException (" Unknown Ġparty Ġname .") ĊĠĠĠĠĠĠĠ Ġval Ġquery Criteria Ġ= ĠQuery Criteria . Linear State Query Criteria ( exact Participants Ġ= ĠlistOf ( me , Ġopponent )) ĊĠĠĠĠĠĠĠ Ġval Ġstate Ref Ġ= Ġproxy . vault Query By < Board State >( query Criteria ). states . single () ĊĠĠĠĠĠĠĠ Ġval ĠnewState Ġ= Ġstate Ref . state . data . write Symbol ( me , Ġpos ) ĊĠĠĠĠĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġproxy . start Flow (:: Play Flow , Ġopponent , Ġpos ). returnValue . get () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ(( newState . player X == me Ġ&& ĠBoard State . check Outcome ( newState . board )== Board State . Outcome . X _ WIN S ) Ġ|| Ġ( newState . player O == me Ġ&& ĠBoard State . check Outcome ( newState . board )== Board State . Outcome . O _ WIN S )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠSystem . out . println (" c org i ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . status ( HttpStatus . CREATED ). body (" win " /* " You Ġplayed Ġthe Ġposition Ġ($ pos ) Ġagainst Ġ${ opp onent . name }. ĠYou Ġhave Ġwon ! ĠY ippe e !" */) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġif Ġ( Board State . check Outcome ( newState . board )== Board State . Outcome . DRAW ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . status ( HttpStatus . CREATED ). body (" You Ġplayed Ġthe Ġposition Ġ($ pos ) Ġagainst Ġ${ opp onent . name }. ĠYou Ġhave Ġreached Ġa Ġdraw .") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . status ( HttpStatus . CREATED ). body (" You Ġplayed Ġthe Ġposition Ġ($ pos ) Ġagainst Ġ${ opp onent . name }. ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠResponseEntity . status ( HttpStatus . BAD _ REQUEST ). body ( e . message ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊ Ċ }