1 package poker.presentation.login; 2 3 import java.io.*; 4 import java.net.*; 5 import com.lutris.http.*; 6 import com.lutris.appserver.server.httpPresentation.*; 7 import com.lutris.appserver.server.user.*; 8 import com.lutris.util.*; 9 import poker.spec.*; 10 import poker.Poker; 11 12 26 public class LoginProcessorPresentation implements HttpPresentation{ 27 28 29 private GameManager gameManager; 30 private final String homePage = "main/PlaceBetPresentation.po"; 31 private final String loginPage = "login/LoginPresentation.po"; 32 private final String debugArg = "debug"; 33 private final String helpArg = "help"; 34 35 public void run(HttpPresentationComms comms) throws Exception { 36 37 Poker application = (Poker)comms.application; 38 gameManager = application.getGameManager(); 39 40 String username = comms.request.getParameter("username"); 41 String pw = comms.request.getParameter("pw"); 42 String cashAsString = comms.request.getParameter("cash"); 43 44 String redirect = loginPage + "?" + "pw=" + pw + "&" + 45 "username=" + username + "&" + 46 "cash=" + cashAsString; 47 48 if( username==null || pw==null ) { 49 comms.session.getSessionData().set("loginMessage", 50 "You must specify a username and password to login."); 51 throw new ClientPageRedirectException( 52 comms.request.getAppFileURIPath(redirect)); 53 } 54 55 int cash = 0; 56 if(cashAsString != null && !cashAsString.equals("")) { 57 try { 58 cash = Integer.parseInt(cashAsString); 59 } 60 catch( NumberFormatException e ) { 61 comms.session.getSessionData().set("loginMessage", 62 "Invalid input in cash field!"); 63 throw new ClientPageRedirectException( 64 comms.request.getAppFileURIPath(redirect)); 65 } 66 } 67 68 if( cash < 0 || cash > 1000000 ) { 69 comms.session.getSessionData().set("error", 70 "Cash amount can't be negative or greather than 1000000!"); 71 throw new ClientPageRedirectException( 72 comms.request.getAppFileURIPath(redirect)); 73 } 74 75 76 77 boolean auth = false; 78 79 83 84 try{ 85 if ((!username.equals("")) && (!pw.equals(""))){ 86 auth = gameManager.authenticate(username, pw); 87 } 88 89 if (!auth){ 90 comms.session.getSessionData().set("loginMessage", 91 "Invalid username or password."); 92 } 94 else { 95 PokerGame game = (PokerGame)gameManager.getGame(username); 96 game.setCash(game.getCash()+cash); 97 comms.session.setUser((User)game.getPlayer()); 98 comms.session.getSessionData().set("game", game); 99 redirect = homePage; 100 } 101 102 103 }catch (NullPointerException e){ 104 105 106 throw new ClientPageRedirectException( 107 comms.request.getAppFileURIPath(homePage)); 108 109 } 110 throw new ClientPageRedirectException( 111 comms.request.getAppFileURIPath(redirect)); 112 113 } 114 115 } 116 | Popular Tags |