KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > presentation > main > BetProcessorPresentation


1 package poker.presentation.main;
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 /**
13  * Presentation Object that processes the bet request and redirects
14  * to the appropriate page.
15  *
16  * EnhyDraw!, beta 4, 5/21/99
17  *
18  * Copyright 1999, Larry Wolcot & Daryl Tempesta
19  * ALL rights reserved. Not for commercial use
20  * without written permission from both authors.
21  *
22  */

23 public class BetProcessorPresentation implements HttpPresentation{
24
25   static final String JavaDoc continuePage = "main/FirstDealPresentation.po";
26   static final String JavaDoc errorPage = "main/PlaceBetPresentation.po";
27   static final String JavaDoc debugArg = "debug";
28   static final String JavaDoc helpArg = "help";
29
30
31
32   public void run(HttpPresentationComms comms) throws Exception JavaDoc{
33     String JavaDoc bet = comms.request.getParameter("bet");
34     String JavaDoc def = comms.request.getParameter("default");
35     String JavaDoc quit = comms.request.getParameter("QUIT");
36     if (quit != null){
37       if (quit.equals("QUIT")){
38         throw new ClientPageRedirectException(
39             comms.request.getAppFileURIPath("main/QuitPresentation.po"));
40         }
41       }
42     if (def == null) {def = "empty";}
43     PokerGame game;
44     game = (PokerGame)comms.session.getSessionData().get("game");
45     String JavaDoc redirect = "";
46 /*
47  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run poker_pres )
48  * We need to allow poker_pres to be dynamic
49  */

50 try{
51     
52     try{
53     game.setBet(new Integer JavaDoc(bet).intValue());
54     }catch(NumberFormatException JavaDoc e){
55     
56        game.setLastError("Invalid bet value!!");
57        throw new ClientPageRedirectException(
58                 comms.request.getAppFileURIPath(errorPage));
59     }
60     String JavaDoc pp = comms.request.getApplicationPath();
61     if (!pp.endsWith("/"))
62        pp += "/";
63
64     HttpPresentationOutputStream out =comms.response.getOutputStream();
65     boolean validBet = false;
66     
67     int thisBet = 0;
68     if (bet != null){
69        thisBet = new Integer JavaDoc(bet).intValue();
70     }
71
72     if (game.getPlayRound() > 1) {
73       redirect = continuePage;
74     } else {
75         if ((thisBet > game.getCash()) || (thisBet == 0)){
76        redirect = errorPage;
77        game.setLastError("Invalid bet value!!");
78        game.setPlayRound(0);
79         } else {
80            Poker mainApp;
81            mainApp = (Poker)comms.session.getSessionData().get("app");
82            GameManager g = mainApp.getGameManager();
83      g.setTotalDollars(thisBet);
84      g.setHandsDealt(1);
85        redirect = continuePage;
86        game.setBet(thisBet);
87        game.setTotalPlayed(game.getTotalPlayed() + 1);
88        game.setCash(game.getCash() - thisBet);
89        if (def.equals("on")){
90      game.setDefaultBet(thisBet);
91        }
92        g.updateGame(game);
93         }
94      }
95 }catch (NullPointerException JavaDoc e){
96        throw new ClientPageRedirectException(
97                 comms.request.getAppFileURIPath(errorPage));
98
99     }
100
101    throw new ClientPageRedirectException(
102                 comms.request.getAppFileURIPath(redirect));
103   }
104 }
105
Popular Tags