1 package poker.business; 2 3 import java.util.Vector ; 4 import java.sql.SQLException ; 5 import poker.data.DODS_GameData.*; 6 import poker.data.DODS_Fortune.*; 7 import com.lutris.appserver.server.sql.*; 8 import com.lutris.appserver.server.Enhydra; 9 import com.lutris.logging.Logger; 10 import com.lutris.util.*; 11 import com.lutris.dods.builder.generator.query.*; 13 import poker.spec.*; 14 34 public class GameList extends Vector implements java.io.Serializable { 35 36 private boolean useDB = false; 37 38 41 public GameList(int n){ 42 super(n); 43 } 44 45 48 private GameList(){ 49 super(0); 50 } 51 52 55 protected GameList getCopy(){ 56 GameList newCopy = new GameList(); 57 newCopy = this; 58 return newCopy; 59 } 60 61 62 65 public void setUseDB(boolean useDB) throws Exception { 66 try { 67 this.useDB = useDB; 68 } catch(Exception e) { 69 throw e; 70 } 71 } 72 73 77 public synchronized void addGame(PokerGame thisGame){ 78 if (useDB == false) { 79 super.addElement(thisGame); 80 } else { 81 try { 82 addGameToDB(thisGame); 83 } catch (Exception e) { 84 } 86 } 87 } 88 89 93 public synchronized void addGameToDB(PokerGame newGame) throws Exception { 94 GameDataDO nullDO = null; 96 GameDataDO newGameDataDO = mapToDO(nullDO, newGame); 97 98 try { 99 DBTransaction db =Enhydra.getDatabaseManager().createTransaction(); 100 101 try { 102 103 db.insert(newGameDataDO); 105 db.commit(); 106 } catch (SQLException se) { 107 108 db.rollback(); 110 throw se; 111 } finally { 112 113 db.release(); 115 } 116 } catch (Exception e) { 117 throw e; 118 } 119 } 120 121 129 private synchronized GameDataDO mapToDO( 130 GameDataDO newGameDataDO, PokerGame newGame){ 131 try { 132 newGameDataDO = GameDataDO.createVirgin(); 133 newGameDataDO.setName(newGame.getName()); 134 newGameDataDO.setPassword(newGame.getPassword()); 135 newGameDataDO.setEmail(newGame.getEmail()); 136 newGameDataDO.setCash(newGame.getCash()); 137 newGameDataDO.setLargestBet(newGame.getLargestBet()); 138 newGameDataDO.setSmallestBet(newGame.getSmallestBet()); 139 newGameDataDO.setTotalPlayed(newGame.getTotalPlayed()); 140 newGameDataDO.setTotalWon(newGame.getTotalWon()); 141 newGameDataDO.setDeck(newGame.getDeck()); 142 } catch (Exception e){ 143 System.out.print(e.getMessage()); 144 e.printStackTrace(); 145 } 147 return newGameDataDO; 148 } 149 150 156 private synchronized PokerGame mapNewPokerGame(GameDataDO thisGame){ 157 PokerGameImpl newPokerGame = new PokerGameImpl("", "", "",0); 158 159 try { 160 newPokerGame.setName(thisGame.getName()); 161 newPokerGame.setPassword(thisGame.getPassword()); 162 newPokerGame.setEmail(thisGame.getEmail()); 163 newPokerGame.setCash(thisGame.getCash()); 164 newPokerGame.setLargestBet(thisGame.getLargestBet()); 165 newPokerGame.setSmallestBet(thisGame.getSmallestBet()); 166 newPokerGame.setTotalPlayed(thisGame.getTotalPlayed()); 167 newPokerGame.setTotalWon(thisGame.getTotalWon()); 168 newPokerGame.setDeck(thisGame.getDeck()); 169 newPokerGame.setID(thisGame.getOId().toString()); 170 } catch (Exception e){ 171 } 173 return newPokerGame; 174 } 175 176 180 public synchronized void removeGame(String thisName){ 181 if (this.useDB) { 182 try { 183 removeDBGame(thisName); 184 } catch (Exception e) { 185 } 187 } else { 188 removeMemoryGame(thisName); 189 } 190 } 191 192 195 public synchronized void removeDBGame(String name) throws Exception { 196 GameDataDO gameDataDO = getGameDataByName(name); 197 try { 198 DBTransaction db = 199 Enhydra.getDatabaseManager().createTransaction(); 200 try { 201 db.delete(gameDataDO); 205 db.commit(); 206 } catch (SQLException sqle) { 207 db.rollback(); 208 throw sqle; 209 } finally { 210 db.release(); 213 } 214 } catch (Exception e) { 215 throw e; 217 } 218 } 219 220 223 public void removeMemoryGame(String name){ 224 int marker = 0; 225 PokerGame thatGame; 226 227 synchronized (this){ 229 for (int i = 0; i < super.size(); i++){ 231 thatGame = (PokerGame)super.elementAt(i); 232 if(thatGame.getName().equals(name)){ 234 marker = i; 235 } 236 } 237 super.removeElementAt(marker); 238 } 239 } 240 241 245 private GameDataDO getGameDataByName(String name) 246 throws Exception { 247 GameDataDO gotGameDataDO = null; 248 GameDataQuery dq = new GameDataQuery(); 249 dq.setQueryName(name); 250 try { 251 gotGameDataDO = (GameDataDO) dq.getNextDO(); 252 } 253 finally { 254 } 257 return gotGameDataDO; 258 } 259 260 264 public PokerGame getGame(String name){ 265 PokerGame thisGame = null; 266 267 if (useDB){ 268 try { 269 GameDataDO thisDO = null; 270 thisDO = getGameDataByName(name); 271 thisGame = mapNewPokerGame(thisDO); 272 273 } catch (Exception e) { 274 Enhydra.getLogChannel().write(Logger.DEBUG, "ERROR! " + 275 e.toString()); 276 } 278 } else { 279 thisGame = getGameInMemory(name); 280 } 281 282 return thisGame; 283 284 } 285 286 290 public boolean getIsNameUsed(String name){ 291 boolean isUsed = true; 292 293 if (this.useDB) { 294 isUsed = getIsNameUsedInDB(name); 295 } else { 296 isUsed = getIsNameUsedInMemory(name); 297 } 298 299 return isUsed; 300 } 301 302 306 public Vector getTopTen(){ 307 Vector topTen = new Vector (0); 308 if (this.useDB) { 309 topTen = getTopTenInDB(); 310 } else { 311 topTen = getTopTenInMemory(); 312 } 313 314 return topTen; 315 } 316 317 321 public boolean authenticate(String name, String pw){ 322 boolean auth = false; 323 324 if (useDB) { 325 auth = authenticateInDB(name, pw); 326 } 327 else { 328 auth = authenticateInMemory(name, pw); 329 } 330 331 return auth; 332 } 333 334 337 public synchronized boolean getIsNameUsedInDB(String name){ 338 boolean isUsed = false; 339 try { 341 if (getGameDataByName(name) == null) { 342 isUsed = false; 343 } 344 else { 345 isUsed = true; 346 } 347 } catch (Exception e) { isUsed = false; } 348 return isUsed; 349 } 350 351 354 public synchronized boolean getIsNameUsedInMemory(String name){ 355 boolean isUsed = false; 356 PokerGame thisGame; 357 358 GameList gameListCopy = this.getCopy(); 361 362 for (int i = 0; i < gameListCopy.size(); i++){ 365 thisGame = (PokerGame)gameListCopy.elementAt(i); 366 if (thisGame.getName().equals(name)){ 368 isUsed = true; 369 } 370 } 371 return isUsed; 372 } 373 374 377 public synchronized boolean authenticateInDB 378 (String name, String pw) { 379 boolean isAuth = false; 380 try { 381 GameDataDO thisGameDataDO = getGameDataByName(name); 382 if (thisGameDataDO != null) { 383 if (thisGameDataDO.getPassword().equalsIgnoreCase(pw)) { 384 isAuth = true; 385 } 386 } 387 else { 388 isAuth = false; 389 } 390 } 391 catch (Exception e) { 392 e.printStackTrace(); } 395 return isAuth; 396 } 397 398 401 public synchronized boolean authenticateInMemory (String name, String pw){ 402 boolean isAuth = false; 403 PokerGame thisGame; 404 405 GameList gameListCopy = this.getCopy(); 408 409 for (int i = 0; i < gameListCopy.size(); i++) { 412 thisGame = (PokerGame)gameListCopy.elementAt(i); 413 if ((thisGame.getName().equals(name)) & 416 (thisGame.getPassword().equals(pw))) { 417 418 isAuth = true; 419 } 420 } 421 return isAuth; 422 } 423 424 428 public synchronized PokerGame getGameInMemory(String name){ 429 PokerGameImpl thisGame = new PokerGameImpl(name , "", "",0); 430 431 GameList gameListCopy = this.getCopy(); 434 synchronized(thisGame){ 435 for (int i = 0; i < gameListCopy.size(); i++){ 437 thisGame = (PokerGameImpl)gameListCopy.elementAt(i); 438 if (thisGame.getName().equals(name)){ 440 return thisGame; 441 } 442 } 443 } 444 return thisGame; 445 } 446 447 450 public synchronized Vector getTopTenInMemory(){ 451 Vector topTen = new Vector (0); 452 PokerGameImpl blankGame; 453 for (int i = 0; i < 10; i++){ 454 blankGame = new PokerGameImpl("", "", "",0); 455 topTen.addElement(blankGame); 456 } 457 PokerGame game; 458 int rank[] = new int[10]; int Cash = 0; 459 460 GameList gameListCopy = this.getCopy(); 463 464 for (int x = 0; x < 10; x++){ 466 for (int i = 0; i < gameListCopy.size(); i++){ 467 game = (PokerGame)gameListCopy.elementAt(i); 468 if (x < 1){ 469 if (game.getCash() > rank[x]){ 470 rank[x] = game.getCash(); 471 topTen.setElementAt(game, x); 472 } 473 } else { 474 if ((rank[x-1] >= game.getCash()) & 475 (!topTen.contains(game)) & 476 (rank[x] <= game.getCash())){ 477 rank[x] = game.getCash(); 478 topTen.setElementAt(game, x); 479 } 480 } 481 } 482 } 483 return topTen; 484 } 485 486 489 public synchronized void updateGame(PokerGame thisGame){ 490 if ( (this.useDB) & (thisGame.getIsDirty()) ) { 491 try { 492 GameDataDO gameDataDO = 493 getGameDataByName(thisGame.getName()); 494 gameDataDO.setDeck(thisGame.getDeck()); 495 gameDataDO.setLargestBet(thisGame.getLargestBet()); 496 gameDataDO.setSmallestBet(thisGame.getSmallestBet()); 497 gameDataDO.setTotalPlayed(thisGame.getTotalPlayed()); 498 gameDataDO.setTotalWon(thisGame.getTotalWon()); 499 gameDataDO.setCash(thisGame.getCash()); 500 501 try { 503 gameDataDO.save(); } 507 catch (SQLException sqle) { 508 Enhydra.getLogChannel().write(Logger.INFO, "ERROR! " + 509 sqle.toString()); 510 } 513 finally { 514 } 516 } catch (Exception e) { 517 Enhydra.getLogChannel().write(Logger.DEBUG, "ERROR! " + 518 e.toString()); 519 } 521 } 522 } 523 524 528 public int getCount(){ 529 int n = 0; 530 if (!useDB){ 531 n = this.size(); 532 } else { 533 GameDataQuery dq = new GameDataQuery(); 534 dq.getQueryBuilder().addEndClause( " order by Cash" ); 535 PokerGame tempGame = null; 536 try { 537 GameDataDO[] allGames = dq.getDOArray(); 538 n = allGames.length; 539 } catch (Exception e) { 540 Enhydra.getLogChannel().write(Logger.INFO, "ERROR: " + 541 e.toString()); 542 } 543 544 } 545 546 return n; 547 } 548 549 553 public Vector getTopTenInDB(){ 554 GameDataQuery dq = new GameDataQuery(); 555 dq.getQueryBuilder().addEndClause( " order by Cash" ); 556 Vector topTen = new Vector (0); 557 for (int i = 0; i < 10; i++){ 558 topTen.addElement(new PokerGameImpl("","","",0)); 559 } 560 561 PokerGame tempGame = null; 562 try { 563 GameDataDO[] allGames = dq.getDOArray(); 564 565 int x = 0; 566 for (int i = allGames.length -1; i > -1; i--){ 567 tempGame = mapNewPokerGame(allGames[i]); 568 topTen.setElementAt(tempGame,x); 569 x++; 570 if (x == 10) { i = -1; } 571 } 572 573 } catch (Exception e) { 574 Enhydra.getLogChannel().write(Logger.INFO, "ERROR: " + 575 e.toString()); 576 } 578 579 return topTen; 580 } 581 582 585 public synchronized int getRank(int cash){ 586 PokerGame game; 587 GameList gameListCopy = this.getCopy(); 588 int rank = 0; 589 if (!useDB){ 590 for (int i = 0; i < gameListCopy.size(); i++){ 592 game = (PokerGame)gameListCopy.elementAt(i); 593 if (game.getCash() > cash) { rank++; } 594 } 595 } else { 596 GameDataQuery dq = new GameDataQuery(); 597 dq.getQueryBuilder().addEndClause( " order by Cash" ); 598 try { 599 GameDataDO[] allGames = dq.getDOArray(); 600 for (int i = 0; i < allGames.length; i++){ 601 if (allGames[i].getCash() > cash) { rank++; } 602 } 603 } catch (Exception e) { 604 Enhydra.getLogChannel().write(Logger.INFO, "ERROR: " + 605 e.toString()); 606 } 607 } 608 609 return rank + 1; 610 } 611 612 613 } 614 | Popular Tags |