KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > Poker


1 /*
2  * Larry and Daryl's EnhyDraw! Poker
3  * A sample Enhydra Application
4  *
5  * EnhyDraw!, beta 4, 5/21/99
6  *
7  * Copyright 1999, Larry Wolcot & Daryl Tempesta
8  * ALL rights reserved. Not for commercial use
9  * without written permission from both authors.
10  *
11  *
12  */

13
14 package poker;
15
16 import java.io.*;
17 import java.math.BigDecimal JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import com.lutris.util.*;
20 import com.lutris.logging.Logger;
21 import com.lutris.logging.LogChannel;
22 import com.lutris.appserver.server.session.*;
23 import com.lutris.appserver.server.user.*;
24 import com.lutris.appserver.server.*;
25 import com.lutris.appserver.server.httpPresentation.*;
26
27 import poker.spec.*;
28
29 import com.lutris.util.Config;
30
31 import javax.servlet.http.Cookie JavaDoc;
32
33
34 /**
35  * This is the main application object
36  * that handles the appication wide data,
37  * like the GameManager.
38  */

39 public class Poker extends StandardApplication implements java.io.Serializable JavaDoc {
40
41   private static GameManager gameManager = null;
42
43   private static FortuneManager fortuneManager = null;
44
45   // This is where you should enable the use of DODS_Objects
46
// instead of Vector Objects for persistance
47
private boolean useDB = false;
48 // private boolean useDB = true;
49

50   public void startup(Config appConfig) throws ApplicationException {
51
52     try {
53       // This is where you should enable the use of DODS_Objects
54
// instead of Vector Objects for persistance
55
useDB = appConfig.getBoolean("EnhyDrawApplication.useDB", false);
56     }
57     catch (ConfigException e) {
58       logChannel.write(Logger.INFO, "poker.conf errror:" + e.toString());
59     }
60
61     super.startup(appConfig);
62
63 //Yea, this needs to be finished
64
try{
65     fortuneManager = FortuneManagerFactory.getFortuneManager("poker.business.FortuneManagerImpl");
66     gameManager = GameManagerFactory.getGameManager("poker.business.GameManagerImpl");
67    
68     fortuneManager.addFortune("Who's there?!");
69     fortuneManager.addFortune("What do you want?!");
70     fortuneManager.addFortune("Don't come in here with loco motives!");
71     fortuneManager.addFortune("Whats the secret knock?!");
72     fortuneManager.addFortune("I don't like your looks?!");
73     fortuneManager.addFortune("Come to play poker or tiddly winks?");
74     fortuneManager.addFortune("BEA sure your app runs this nice!");
75     fortuneManager.addFortune("Watch your step, pal...");
76     fortuneManager.addFortune("Are you talking to me?");
77     fortuneManager.addFortune("I'll J-Run your ass right outta town!");
78     fortuneManager.addFortune("Feeling lucky? I hope so..");
79   }catch(Exception JavaDoc e){
80     
81    }
82     //This is tells the gameManager to use the DB instead of memory.
83
//FIX: Still need to add the useDB key into the
84
// Poker.conf configuration file..
85

86     try {
87       gameManager.setUseDB(useDB);
88     }
89     catch (Exception JavaDoc e) {
90       logChannel.write(Logger.INFO, "EnhyDraw! DB errror:" + e.toString());
91     //There was a problem with the DB init
92
}
93
94     if (logChannel != null)
95       logChannel.write(Logger.INFO, "EnhyDraw Poker now starting!");
96       state = RUNNING;
97   }
98
99   /**
100    * This is an optional function, used only by the Multiserver's graphical
101    * administration. This bit of HTML appears in the status page for this
102    * application. In this case, it displays the House Statistics
103    * which include total earnings, total players, etc.
104    *
105    * @return HTML that is displayed in the status page of the Multiserver.
106    */

107   public String JavaDoc toHtml() {
108     
109
110     GameManager g = GameManagerFactory.getGameManager("poker.business.GameManagerImpl");
111     
112     String JavaDoc n = new Integer JavaDoc(gameManager.getCount()).toString();
113     String JavaDoc m = "Welcome to Larry and Daryl's EnhyDraw Poker!<br>";
114     m += "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\"";
115     m += "width=\"271\"><tr><td width=\"171\">Total Players Cataloged";
116     m += "</td><td width=\"84\">" + g.getTotalPlayers() +"</td></tr><tr>";
117     m += "<td width=\"171\">Total Hands Dealt</td>";
118     m += "<td width=\"84\">"+ g.getTotalHandsDealt() +"</td></tr><tr>";
119     m += "<td width=\"171\">Total Hands Won</td>";
120     m += "<td width=\"84\">"+ g.getTotalHandsWon()+"</td></tr><tr>";
121     m += "<td width=\"171\">Total Hands Lost</td>";
122     m += "<td width=\"84\">"+ g.getTotalHandsLost()+"</td></tr><tr>";
123     m += "<td width=\"171\">Total Server Profit</td>";
124     m += "<td width=\"84\">"+ g.getHouseProfit() +"</td></tr><tr>";
125     m += "<td width=\"171\">Total Players Bankrupt</td>";
126     m += "<td width=\"84\">"+ g.getTotalBankrupt() +"</td></tr></table>";
127     m += "</div>";
128       return m;
129   }
130
131   /**
132    * Return the game manager for this application.
133    */

134   public GameManager getGameManager(){
135     return gameManager;
136   }
137
138   public FortuneManager getFortuneManager(){
139     return fortuneManager;
140   }
141
142   /**
143    * Shutdown the application.
144    */

145   public void shutdown() {
146     super.shutdown();
147   }
148
149 /**
150  * This method pre processes all requests
151  */

152   public boolean requestPreprocessor(HttpPresentationComms comms) throws Exception JavaDoc {
153     super.requestPreprocessor(comms);
154
155
156
157     if(!(comms.request.getRequestURI().toLowerCase().endsWith(".gif") ||
158                 comms.request.getRequestURI().toLowerCase().endsWith(".jpg")))
159       comms.session.getSessionData().set("app", this);
160       String JavaDoc pp = comms.request.getApplicationPath();
161       if (!pp.endsWith("/"))
162           pp += "/";
163       String JavaDoc loginDir = pp + "login/";
164       String JavaDoc mediaDir = pp + "media/";
165       String JavaDoc page = comms.request.getRequestURI();
166       if (page.startsWith(loginDir) || page.startsWith(mediaDir)) {
167           /*
168            * Let them go to the login and logout areas whether or not
169            * they are logged in. Also let them get media on welcomepage.
170            */

171           return false;
172       }
173       else {
174         
175         //We need to allow poker_pres to be dynamic , so if the requested url is poker_pres user dont need to be logged
176
String JavaDoc uri = comms.request.getRequestURI();
177          boolean is= uri.startsWith("/poker_pres");
178           
179           /*
180            * All other pages require the user to be logged in.
181            */

182          if(is)
183          
184           return false;
185         
186           else{
187           if (comms.session.getUser() != null)
188               // It's ok, they are logged in already.
189
return false;
190           else {
191               // They are not logged in! Redirect to login page.
192
String JavaDoc url = pp + "login/StartPresentation.po";
193               throw new ClientPageRedirectException(url);
194           }
195         }
196       }
197   }
198 }
199
200
Popular Tags