import express from 'express' ; import { UsersService } from 'services/usersservice' ; import { InventoryService } from 'services/inventoryservice' ; import { MapImageList } from 'maps' ; import { FORM_SECURITY_QUESTIONS } from 'securityquestions' ; import { WebSession } from 'websession' ; /**
 * handles requests to pages
 */ export class PagesController { param app public static setup ( app ) { app . route ( '/' ) . get ( async ( req , res ) => { await PagesController . OnGetIndex ( req , res ) ; } ) ; app . route ( '/signup' ) . get ( async ( req , res ) => { await PagesController . OnGetSignup ( req , res ) ; } ) ; app . route ( '/login' ) . get ( async ( req , res ) => { await PagesController . OnGetLogin ( req , res ) ; } ) ; app . route ( '/logout' ) . get ( ( req , res ) => { PagesController . OnGetLogout ( req , res ) ; } ) ; app . route ( '/user' ) . get ( async ( req , res ) => { await PagesController . OnGetUser ( req , res ) ; } ) ; app . route ( '/user/delete' ) . get ( async ( req , res ) => { await PagesController . OnGetUserDelete ( req , res ) ; } ) ; app . route ( '/recover_pw' ) . get ( async ( req , res ) => { await PagesController . OnGetRecoverPw ( req , res ) ; } ) ; app . route ( '/about' ) . get ( async ( req , res ) => { await PagesController . OnGetAbout ( req , res ) ; } ) ; app . route ( '/downloads' ) . get ( async ( req , res ) => { await PagesController . OnGetDownloads ( req , res ) ; } ) ; } param req private static cleanUpStatus ( req ) { req . session . status = null ; req . session . error = null ; req . session . save ( ( err ) => { if ( err ) { throw err ; } } ) ; } param req param res private static async OnGetIndex ( req , res ) { const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'index' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetSignup ( req , res ) { if ( req . session . userId != null ) { return res . redirect ( '/user' ) ; } const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'signup' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session , questions : FORM_SECURITY_QUESTIONS } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetLogin ( req , res ) { if ( req . session . userId != null ) { return res . redirect ( '/user' ) ; } const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'login' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static OnGetLogout ( req , res ) { if ( req . session . userId != null ) { req . session . userId = null ; } req . session . status = 'Logged out succesfully.' ; req . session . save ( ( err ) => { if ( err ) { throw err ; } } ) ; res . redirect ( '/user' ) ; } param req param res private static async OnGetUser ( req , res ) { if ( req . session . userId == null ) { return res . redirect ( '/login' ) ; } const session = req . session as WebSession ; const [ user , cosmetics , numSessions ] = await Promise . all ( [ UsersService . get ( req . session . userId ) , InventoryService . GetCosmetics ( req . session . userId ) , UsersService . getSessions ( ) ] ) ; res . render ( 'user' , { user , cosmetics , playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetUserDelete ( req , res ) { if ( req . session . userId == null ) { return res . redirect ( '/login' ) ; } const session = req . session as WebSession ; const [ user , numSessions ] = await Promise . all ( [ UsersService . get ( req . session . userId ) , UsersService . getSessions ( ) ] ) ; res . render ( 'delete' , { user , playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetRecoverPw ( req , res ) { if ( req . session . userId != null ) { return res . redirect ( '/' ) ; } const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'recover_pw' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetAbout ( req , res ) { const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'about' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } param req param res private static async OnGetDownloads ( req , res ) { const session = req . session as WebSession ; const numSessions = await UsersService . getSessions ( ) ; res . render ( 'downloads' , { playersOnline : numSessions , mapImage : MapImageList . getRandomFile ( ) , session } ) ; PagesController . cleanUpStatus ( req ) ; } }