KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > business > GameManagerImpl


1 package poker.business;
2
3 import java.util.Vector JavaDoc;
4 import java.sql.SQLException JavaDoc;
5 import poker.data.user.*;
6 import poker.data.game.*;
7 import poker.business.GameList;
8 import poker.spec.*;
9 /**
10  *
11  * EnhyDraw!, beta 4, 5/21/99
12  *
13  * Copyright 1999, Larry Wolcot & Daryl Tempesta
14  * ALL rights reserved. Not for commercial use
15  * without written permission from both authors.
16  *
17  * This class is a basic wrapper for the BDO's (like GameList)
18  * It manages all of the active cardGames, PitBoss Statistics, etc.
19  */

20 public class GameManagerImpl implements GameManager,java.io.Serializable JavaDoc {
21
22     private PokerGame game;
23     private GameList gameList = new GameList(0);
24     private PitBossListImpl pitBossList = new PitBossListImpl();
25     private String JavaDoc appName = "Poker";
26
27 /**
28  * Set the useDB flag in the GameList object. If useDB is false,
29  * the GameList object will use a standard Vector (in memory)
30  * to hold all of the active PokerGame objects. if useDB is set to
31  * true, it will mirror the Vector's elements into the database.
32  */

33   public void setUseDB(boolean useDB) throws Exception JavaDoc{
34     try {
35          gameList.setUseDB(useDB);
36          //PitBoss persistance is not finished
37
pitBossList.setUseDB(useDB);
38     } catch (Exception JavaDoc e) {
39          throw e;
40     }
41   }
42
43 /**
44  * Total number of players sent to the poor house!
45  */

46   public String JavaDoc getTotalBankrupt(){
47   //String ret = new Integer(totalBankrupt).toString();
48
//return ret;
49
int total = 0;
50       total = pitBossList.getPitBossVDO(appName).getTotalBankrupt();
51       return new Integer JavaDoc(total).toString();
52   }
53
54   /**
55    * Total dollars the house has won/lost
56    */

57   public String JavaDoc getHouseProfit(){
58       int total = 0;
59       total = pitBossList.getPitBossVDO(appName).getTotalDollars();
60       return new Integer JavaDoc(total).toString();
61   }
62
63   /**
64    * Check the active gameList and return total number of players
65    */

66   public String JavaDoc getTotalPlayers(){
67   String JavaDoc ret= new Integer JavaDoc(gameList.getCount()).toString();
68   return ret;
69   }
70
71   /**
72    * Total number hands dealt
73    */

74   public String JavaDoc getTotalHandsDealt(){
75       int total = 0;
76       total = pitBossList.getPitBossVDO(appName).getTotalHandsDealt();
77       return new Integer JavaDoc(total).toString();
78   }
79
80   /**
81    * Total number of hands won by all players
82    */

83   public String JavaDoc getTotalHandsWon(){
84       int total = 0;
85       total = pitBossList.getPitBossVDO(appName).getTotalHandsWon();
86       return new Integer JavaDoc(total).toString();
87   }
88
89   /**
90    * Total number of hands lost by all players
91    */

92   public String JavaDoc getTotalHandsLost(){
93     int totalWon = 0;
94     int totalPlayed = 0;
95     totalWon = pitBossList.getPitBossVDO(appName).getTotalHandsWon();
96     totalPlayed =
97     pitBossList.getPitBossVDO(appName).getTotalHandsDealt();
98     return new Integer JavaDoc(totalPlayed - totalWon).toString();
99   }
100
101   /**
102    * Incriment totalHandsWon by n
103    */

104   public synchronized void setTotalHandsWon(int n) {
105     synchronized(pitBossList) {
106        n = n + pitBossList.getPitBossVDO(appName).getTotalHandsWon();
107        pitBossList.getPitBossVDO(appName).setTotalHandsWon(n);
108       }
109   }
110
111   /**
112    * Incriment totalBankrupt by n
113    */

114   public synchronized void setTotalBankrupt(int n){
115       synchronized(pitBossList){
116          n = n + pitBossList.getPitBossVDO(appName).getTotalBankrupt();
117          pitBossList.getPitBossVDO(appName).setTotalBankrupt(n);
118       }
119   }
120
121   /**
122    * Incriment totalHandsDealt by n
123    */

124   public synchronized void setHandsDealt(int n){
125       synchronized(pitBossList){
126          n = n +pitBossList.getPitBossVDO(appName).getTotalHandsDealt();
127          pitBossList.getPitBossVDO(appName).setTotalHandsDealt(n);
128       }
129   }
130
131   /**
132    * Incriment totalDollars (won) by n
133    */

134   public synchronized void setDollars(int n){
135       synchronized(pitBossList){
136          n = n + pitBossList.getPitBossVDO(appName).getTotalDollars();
137          pitBossList.getPitBossVDO(appName).setTotalDollars(n);
138       }
139   }
140
141   /**
142    * same as setDollars
143    */

144   public synchronized void setTotalDollars(int n){
145     setDollars(n);
146   }
147
148   /**
149    * Return a (COPY)list of active games
150    */

151   public synchronized Vector JavaDoc getGameList(){
152     GameList gameListCopy = this.gameList.getCopy();
153     return gameListCopy;
154   }
155
156   /**
157    * Return the size() of gameList
158    */

159   public synchronized int getCount(){
160     return gameList.getCount();
161   }
162
163   /**
164   * return the top ten players ranked by cash won
165   */

166   public synchronized Vector JavaDoc getTopTen(){
167     Vector JavaDoc topTen = null;
168
169     topTen = gameList.getTopTen();
170
171     return topTen;
172
173   }
174
175   /**
176    * Check to see if this name has been used
177    */

178   public synchronized boolean getIsNameUsed(String JavaDoc name){
179     boolean isUsed = false;
180
181     if (gameList.getIsNameUsed(name)) { isUsed = true; }
182
183     return isUsed;
184   }
185
186   /**
187    * Authenticate user against gameList object
188    */

189   public synchronized boolean authenticate(String JavaDoc name, String JavaDoc pw){
190     boolean isAuth = false;
191
192     if (gameList.authenticate(name, pw)) {
193       isAuth = true;
194     }
195     return isAuth;
196   }
197
198   /**
199    * Return the game that belongs to user name String
200    */

201   public synchronized PokerGame getGame(String JavaDoc name) {
202     PokerGame thisGame = null;
203
204     thisGame = gameList.getGame(name);
205
206     return thisGame;
207   }
208
209   /**
210    * Add a game to the gameList
211    */

212   public synchronized void addGame(PokerGame newGame){
213   //lock gameList object for TOTAL threadsave opps
214
//This is OK for less intense apps like this one,
215
//but not the best way to go for high transaction
216
//DB volumes
217
synchronized(gameList){
218    gameList.addGame(newGame);
219   }
220   }
221
222   /**
223    * get rank of current dollars compared to the gameList
224    */

225   public synchronized int getRank(int cash){
226   int rank = gameList.getRank(cash);
227   return rank;
228   }
229
230   /**
231    * remove thisGame from the gameList
232    */

233   public synchronized void removeGame(String JavaDoc name){
234       gameList.removeGame(name);
235   }
236
237   /**
238    * Make sure the DB gets updated if this is a DODS object
239    */

240   public synchronized void updateGame(PokerGame thisGame){
241       gameList.updateGame(thisGame);
242   }
243
244   }
245
Popular Tags