import { newParty , setGame } from "./rocketcrab" ; import { RocketCrab } from "../types/types" ; import { Application , Request , Response } from "express" ; import { RocketcrabMode } from "../types/enums" ; import { getModeFromHost } from "../utils/utils" ; export default ( server , rocketcrab ) : void => { const { partyList } = rocketcrab ; const getMode = ( req ) => getModeFromHost ( req ?. hostname ) ; const newPartyHandler = ( isPublic ) => ( req , res ) => { if ( isPublic && ! rocketcrab . isFinderActive ) { res . status ( 400 ) . end ( ) ; return ; } const { code } = newParty ( { rocketcrab , isPublic , mode : getMode ( req ) } ) ; res . json ( { code } ) ; } ; const localeRedirects = [ RocketcrabMode . MAIN , RocketcrabMode . KIDS ] . map ( ( mode ) => `/${ mode }/*` ) ; server . all ( localeRedirects , ( req , res ) => { const newPath = req . originalUrl . split ( "/" ) ; newPath . splice ( 1 , 1 ) ; res . redirect ( newPath . join ( "/" ) ) ; } ) ; server . post ( "/api/new" , newPartyHandler ( false ) ) ; server . post ( "/api/new-public" , newPartyHandler ( true ) ) ; server . get ( "/transfer/:gameid/:uuid?" , ( req , res ) => { const { uuid : givenUuid , gameid } = req . params ; const { name } = req . query ; if ( givenUuid && givenUuid . length < 10 ) { res . status ( 400 ) . end ( ) ; return ; } let party ; if ( givenUuid ) { party = partyList . find ( ( { uuid } ) => uuid === givenUuid ) || newParty ( { rocketcrab , forceUuid : givenUuid as string , mode : getMode ( req ) , } ) ; } else { party = newParty ( { rocketcrab , mode : getMode ( req ) } ) ; } if ( gameid && ! party . selectedGame ) { setGame ( gameid as string , party ) ; } if ( name ) { res . cookie ( "previousName" , name , { maxAge : 2147483647 , } ) ; } res . redirect ( "/" + party . code ) ; } ) ; server . get ( "/api/stats" , ( req , res ) => { res . json ( partyList . map ( ( { status , gameState , selectedGameId , playerList } ) => ( { partyStatus : status , gameStatus : gameState . status , selectedGameId , numberOfPlayers : playerList . length , } ) ) ) ; } ) ; } ;