KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Poker
3  *
4  * Enhydra super-servlet presentation object
5  *
6  */

7
8 package poker.presentation.main;
9
10 // Enhydra SuperServlet imports
11
import com.lutris.appserver.server.httpPresentation.HttpPresentation;
12 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
13 //import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
14
//import com.lutris.util.KeywordValueException;
15
import org.w3c.dom.html.HTMLDivElement;
16 import org.w3c.dom.html.HTMLInputElement;
17 import org.w3c.dom.html.HTMLImageElement;
18 import org.w3c.dom.html.HTMLTableRowElement;
19 import org.w3c.dom.Node JavaDoc;
20
21 import java.util.Vector JavaDoc;
22
23 import poker.Poker;
24 import poker.spec.*;
25
26 //import poker.data.game.*;
27

28
29 public class FirstDealPresentation implements HttpPresentation {
30
31 /*
32  * This Vector keeps track of the cards already dealt (no 2 cards are the same!)
33  */

34   private static Vector JavaDoc dealtCards = new Vector JavaDoc(0);
35
36   private static int pointer = 0;
37
38
39   public void run(HttpPresentationComms comms)
40       throws Exception JavaDoc {
41
42     FirstDealHTML firstDeal;
43     firstDeal = (FirstDealHTML)comms.xmlcFactory.create(FirstDealHTML.class);
44
45     this.startGame(comms, firstDeal);
46     this.getCards(comms, firstDeal);
47     this.checkHand(comms, firstDeal);
48
49     HTMLDivElement div = null;
50     HTMLTableRowElement row = null;
51     Node JavaDoc parent = null;
52     if(this.isLastDeal(comms)) {
53       div = firstDeal.getElementIsNotLastDeal1();
54       parent = div.getParentNode();
55       parent.removeChild(div);
56
57       firstDeal.setTextIsLastDeal2("Click on NEW HAND to play another round");
58       firstDeal.setTextIsNotLastDeal2("");
59
60       row = firstDeal.getElementIsNotLastDeal3();
61       parent = row.getParentNode();
62       parent.removeChild(row);
63
64       row = firstDeal.getElementIsNotLastDeal4();
65       parent = row.getParentNode();
66       parent.removeChild(row);
67
68       row = firstDeal.getElementIsNotLastDeal5();
69       parent = row.getParentNode();
70       parent.removeChild(row);
71     }
72     else {
73       div = firstDeal.getElementIsLastDeal1();
74       parent = div.getParentNode();
75       parent.removeChild(div);
76
77       firstDeal.setTextIsLastDeal2("");
78       firstDeal.setTextIsNotLastDeal2("Select the cards you wish to keep, then press DRAW!");
79
80       row = firstDeal.getElementIsLastDeal3();
81       parent = row.getParentNode();
82       parent.removeChild(row);
83
84       row = firstDeal.getElementIsLastDeal4();
85       parent = row.getParentNode();
86       parent.removeChild(row);
87
88       row = firstDeal.getElementIsLastDeal5();
89       parent = row.getParentNode();
90       parent.removeChild(row);
91     }
92
93     if(this.isBankrupt(comms)) {
94       div = firstDeal.getElementIsNotBankrupt();
95       parent = div.getParentNode();
96       parent.removeChild(div);
97     }
98     else {
99       div = firstDeal.getElementIsBankrupt();
100       parent = div.getParentNode();
101       parent.removeChild(div);
102     }
103
104     comms.response.writeDOM(firstDeal);
105   }
106
107
108
109   static public void getCards(HttpPresentationComms comms0, FirstDealHTML firstDeal0) throws Exception JavaDoc {
110     PokerGame game = (PokerGame) comms0.session.getSessionData().get("game");
111     Vector JavaDoc theseCards = game.getCardsInHand();
112     Vector JavaDoc usedCards = game.getCardsUsed();
113     Vector JavaDoc dropCards = game.getDropCards();
114     String JavaDoc but[] ={ "DROP", "DROP", "DROP", "DROP", "DROP" };
115     
116     Dealer dealer = DealerFactory.getDealer("poker.business.DealerImpl");
117     String JavaDoc flag;
118     
119     if (game.getPlayRound() == 3) {
120       theseCards = dealer.drawMoreCards(theseCards, dropCards);
121       for (int i = 0; i < 5; i ++) {
122         flag = (String JavaDoc)dropCards.elementAt(i);
123         if (flag.equals("DROP")) {
124           dropCards.setElementAt("DEALT", i);
125         }
126         else {
127           dropCards.setElementAt("HELD", i);
128         }
129       }
130     }
131
132     Integer JavaDoc card[] = new Integer JavaDoc[5];
133     String JavaDoc[] drop = new String JavaDoc[5];
134
135     for (int i = 0; i < 5; i++) {
136       HTMLImageElement image = null;
137       HTMLInputElement input = null;
138       card[i] = (Integer JavaDoc) theseCards.elementAt(i);
139       String JavaDoc srcImage = "../media/" + card[i].toString() + ".gif";
140       drop[i] = (String JavaDoc)dropCards.elementAt(i);
141
142 // page.data.set("drop" + n, drop[i]); ? gde ovo smestiti na html strani
143
switch (i) {
144         case 0 : {
145           image = firstDeal0.getElementCard1();
146           image.setSrc(srcImage);
147           image = firstDeal0.getElementCard11();
148           image.setSrc(srcImage);
149
150           input = firstDeal0.getElementBut1();
151           if (drop[i] != "DROP") {
152             but[i] = "DROP";
153           }
154           input.setValue(but[i]);
155           break;
156         }
157         case 1 : {
158           image = firstDeal0.getElementCard2();
159           image.setSrc(srcImage);
160           image = firstDeal0.getElementCard22();
161           image.setSrc(srcImage);
162
163           input = firstDeal0.getElementBut2();
164           if (drop[i] != "DROP") {
165             but[i] = "DROP";
166           }
167           input.setValue(but[i]);
168           break;
169         }
170         case 2 : {
171           image = firstDeal0.getElementCard3();
172           image.setSrc(srcImage);
173           image = firstDeal0.getElementCard33();
174           image.setSrc(srcImage);
175
176           input = firstDeal0.getElementBut3();
177           if (drop[i] != "DROP") {
178             but[i] = "DROP";
179           }
180           input.setValue(but[i]);
181           break;
182         }
183         case 3 : {
184           image = firstDeal0.getElementCard4();
185           image.setSrc(srcImage);
186           image = firstDeal0.getElementCard44();
187           image.setSrc(srcImage);
188
189           input = firstDeal0.getElementBut4();
190           if (drop[i] != "DROP") {
191             but[i] = "DROP";
192           }
193           input.setValue(but[i]);
194           break;
195         }
196         case 4 : {
197           image = firstDeal0.getElementCard5();
198           image.setSrc(srcImage);
199           image = firstDeal0.getElementCard55();
200           image.setSrc(srcImage);
201
202           input = firstDeal0.getElementBut5();
203           if (drop[i] != "DROP") {
204             but[i] = "DROP";
205           }
206           input.setValue(but[i]);
207           break;
208         }
209       }
210     }
211   }
212
213   public void startGame(HttpPresentationComms comms0, FirstDealHTML firstDeal0) throws Exception JavaDoc {
214    Dealer dealer = DealerFactory.getDealer("poker.business.DealerImpl");
215     PokerGame game = (PokerGame)comms0.session.getSessionData().get("game");
216     Poker mainApp = (Poker)comms0.session.getSessionData().get("app");
217     GameManager gameManager = mainApp.getGameManager();
218     if ((game.getBet() < game.getSmallestBet()) | (game.getSmallestBet() == 0)) {
219       game.setSmallestBet(game.getBet());
220     }
221     if (game.getBet() > game.getLargestBet()) {
222        game.setLargestBet(game.getBet());
223     }
224     if (game.getPlayRound() == 3) {
225       game.setPlayRound(4);
226     }
227     String JavaDoc dl = (String JavaDoc)comms0.request.getParameter("DROP1");
228     if ((dl != null) & (game.getPlayRound() < 4)) {
229        game.setPlayRound(3);
230     }
231     if (game.getPlayRound() == 1) {
232        game.setCardsInHand(dealer.dealFiveCards());
233        game.setPlayRound(2);
234     }
235     String JavaDoc d[] = { "DROP", "DROP", "DROP", "DROP", "DROP" };
236     String JavaDoc iS = "";
237     for (int i=0; i < 5; i++) {
238       iS = Integer.toString(i+1);
239       if ((String JavaDoc)comms0.request.getParameter("DROP" + iS) != null) {
240         d[i] = (String JavaDoc)comms0.request.getParameter("DROP" + iS);
241       }
242     }
243
244     Vector JavaDoc dropCards = game.getDropCards();
245
246     for (int i = 0; i < 5; i++) {
247       String JavaDoc n = Integer.toString(i+1);
248       if (d[i].equals("HOLD")) {
249         dropCards.setElementAt("HOLD" ,i);
250       }
251       if (d[i].equals("DROP")){
252         dropCards.setElementAt("DROP" ,i);
253       }
254     }
255
256     firstDeal0.setTextName(game.getName());
257     firstDeal0.setTextCash(Integer.toString(game.getCash()));
258     firstDeal0.setTextBet(Integer.toString(game.getBet()));
259     firstDeal0.setTextRank(Integer.toString(gameManager.getRank(game.getCash())));
260     firstDeal0.setTextHandsPlayed(Integer.toString(game.getTotalPlayed()));
261     firstDeal0.setTextWon(Integer.toString(game.getTotalWon()));
262
263     //gameManager.updateGame(game);
264
}
265
266
267   static public void checkHand(HttpPresentationComms comms0, FirstDealHTML firstDeal0) throws Exception JavaDoc {
268     Dealer dealer = DealerFactory.getDealer("poker.business.DealerImpl");
269     PokerGame game = (PokerGame)comms0.session.getSessionData().get("game");
270     Vector JavaDoc theseCards = game.getCardsInHand();
271     String JavaDoc handTotal = dealer.checkHand(theseCards);
272     int payRate = dealer.getPayout(handTotal);
273     String JavaDoc message = handTotal + ", pays $" +
274       new Integer JavaDoc(payRate * game.getBet()).toString() + " (" +
275       new Integer JavaDoc(payRate).toString() + " to 1)";
276     firstDeal0.setTextResult(message);
277
278     if (game.getPlayRound() > 2) {
279       if (game.getCash() == 0) {
280         Poker app =(Poker)comms0.session.getSessionData().get("app");
281         GameManager g = app.getGameManager();
282         g.setTotalBankrupt(+1);
283       }
284       Poker mainApp = (Poker)comms0.session.getSessionData().get("app");
285       GameManager g = mainApp.getGameManager();
286       if (game.getPlayRound() == 3) {
287         if (payRate > 0) {
288           game.setTotalWon(game.getTotalWon() + 1);
289         }
290         if (payRate > 0) {
291           g.setTotalHandsWon(1);
292         }
293         g.setTotalDollars(-(game.getBet() * payRate));
294         game.setCash(game.getCash() + (game.getBet() * payRate));
295         firstDeal0.setTextCash(Integer.toString(game.getCash()));
296       }
297       g.updateGame(game);
298     }
299   }
300
301
302   static public boolean isLastDeal( HttpPresentationComms comms0 ) throws Exception JavaDoc {
303     PokerGame game = (PokerGame)comms0.session.getSessionData().get("game");
304     if (game.getPlayRound() > 2){
305       return true;
306     }
307     else {
308       return false;
309     }
310   }
311
312
313    static public boolean isBankrupt( HttpPresentationComms comms0 ) throws Exception JavaDoc {
314       PokerGame game = (PokerGame)comms0.session.getSessionData().get("game");
315       if ((game.getCash() == 0) & (game.getPlayRound() == 3)) {
316         return true;
317       }
318       else {
319         return false;
320       }
321    }
322
323
324    static public void getMarker( HttpPresentationComms comms0, FirstDealHTML firstDeal0 ) throws Exception JavaDoc {
325       PokerGame game = (PokerGame)comms0.session.getSessionData().get("game");
326       Vector JavaDoc drops = game.getDropCards();
327       String JavaDoc marker = (String JavaDoc)drops.elementAt(pointer);
328       HTMLImageElement image = null;
329
330       switch (pointer) {
331         case 0: {
332           image = firstDeal0.getElementGetMarker1();
333           break;
334         }
335         case 1: {
336           image = firstDeal0.getElementGetMarker2();
337           break;
338         }
339         case 2: {
340           image = firstDeal0.getElementGetMarker3();
341           break;
342         }
343         case 3: {
344           image = firstDeal0.getElementGetMarker4();
345           break;
346         }
347         case 4: {
348           image = firstDeal0.getElementGetMarker5();
349           break;
350         }
351       }
352
353       if ( (marker.equals("DROP")) | (marker.equals("DEALT")) ) {
354         image.setSrc("../media/draw_n.gif");
355         image.setName("\"but" + Integer.toString(pointer+1));
356       }
357       else {
358         image.setSrc("../media/hold_n.gif");
359         image.setName("\"but" + Integer.toString(pointer+1));
360       }
361
362       pointer++;
363       if (pointer == 5) {
364         pointer = 0;
365       }
366    }
367
368 }
369
Popular Tags