PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/server/game/src/main/java/com/cubeia/games/poker/activator/PokerActivator.java

https://github.com/boltonli/Poker-Galore
Java | 267 lines | 136 code | 42 blank | 89 comment | 9 complexity | e1d101bfe61f7ec96a6b3d10f7c9bec5 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 Cubeia Ltd <info@cubeia.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as
  6. * published by the Free Software Foundation, either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.cubeia.games.poker.activator;
  18. import java.lang.management.ManagementFactory;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import javax.management.MBeanServer;
  22. import javax.management.ObjectName;
  23. import org.apache.log4j.Logger;
  24. import com.cubeia.firebase.api.game.activator.ActivatorContext;
  25. import com.cubeia.firebase.api.game.activator.DefaultActivator;
  26. import com.cubeia.firebase.api.game.activator.DefaultActivatorConfig;
  27. import com.cubeia.firebase.api.game.activator.MttAwareActivator;
  28. import com.cubeia.firebase.api.game.lobby.LobbyTable;
  29. import com.cubeia.firebase.api.game.table.Table;
  30. import com.cubeia.firebase.api.lobby.LobbyAttributeAccessor;
  31. import com.cubeia.firebase.api.server.SystemException;
  32. import com.cubeia.games.poker.FirebaseState;
  33. import com.cubeia.games.poker.tournament.activator.TournamentTableSettings;
  34. import com.cubeia.poker.PokerGuiceModule;
  35. import com.cubeia.poker.PokerState;
  36. import com.cubeia.poker.gametypes.TexasHoldem;
  37. import com.cubeia.poker.timing.TimingFactory;
  38. import com.cubeia.poker.timing.TimingProfile;
  39. import com.cubeia.poker.timing.Timings;
  40. import com.google.inject.Guice;
  41. import com.google.inject.Injector;
  42. /**
  43. * Override the default game activator in order to provide my own
  44. * specific implementations.
  45. *
  46. * @author Fredrik Johansson, Cubeia Ltd
  47. */
  48. public class PokerActivator extends DefaultActivator implements MttAwareActivator, PokerActivatorMBean {
  49. private static final String JMX_BIND_NAME = "com.cubeia.poker:type=PokerActivator";
  50. private transient Logger log = Logger.getLogger(this.getClass());
  51. private int multiplier = 1;
  52. /**
  53. * Holds all participants that should be used for creating tables.
  54. * I hold the implementation instead of the interface so that I can keep
  55. * bingo-specific data in the participants, i.e. FQN and special attributes.
  56. */
  57. private List<PokerParticipant> participants = new ArrayList<PokerParticipant>();
  58. private Injector injector;
  59. /**
  60. * Create the activator for Poker.
  61. * Set the Creation Participant as participant.
  62. *
  63. */
  64. public PokerActivator() {
  65. super();
  66. log.info("Created Game Activator for Poker");
  67. }
  68. @Override
  69. public void init(ActivatorContext context) throws SystemException {
  70. log.warn(">>>>>>>>> INIT PARTICIPANTS");
  71. super.init(context);
  72. log.debug("initJmx");
  73. initJmx();
  74. log.debug("Calling Guice.createInjector");
  75. injector = Guice.createInjector(
  76. new ActivatorGuiceModule(context),
  77. new PokerGuiceModule());
  78. log.debug("initParticipants");
  79. initParticipants();
  80. }
  81. @Override
  82. public void destroy() {
  83. super.destroy();
  84. destroyJmx();
  85. }
  86. /**
  87. * Create a number of participants, i.e. lobby branches
  88. *
  89. */
  90. private void initParticipants() {
  91. /*
  92. participants.add(new PokerParticipant(10,"holdem/real/nolimit/micro", 2, Timings.DEFAULT));
  93. participants.add(new PokerParticipant(10, "holdem/real/nolimit/medium", 50, Timings.DEFAULT));
  94. participants.add(new PokerParticipant(6, "holdem/real/nolimit/medium", 50, Timings.DEFAULT));
  95. participants.add(new PokerParticipant(10, "holdem/real/nolimit/medium", 75, Timings.DEFAULT));
  96. participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 100, Timings.DEFAULT));
  97. participants.add(new PokerParticipant(6, "holdem/real/nolimit/high", 100, Timings.DEFAULT));
  98. participants.add(new PokerParticipant(2, "holdem/real/nolimit/high", 100, Timings.DEFAULT));
  99. participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 1000, Timings.DEFAULT));
  100. participants.add(new PokerParticipant(10, "holdem/real/nolimit/micro", 2, Timings.EXPRESS));
  101. participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 100, Timings.EXPRESS));
  102. participants.add(new PokerParticipant(10, "holdem/points/nolimit/micro", 2, Timings.DEFAULT));
  103. participants.add(new PokerParticipant(10, "holdem/points/nolimit/low", 10, Timings.DEFAULT));
  104. participants.add(new PokerParticipant(10, "holdem/points/nolimit/medium", 50, Timings.DEFAULT));
  105. participants.add(new PokerParticipant(6, "holdem/points/nolimit/medium", 50, Timings.DEFAULT));
  106. participants.add(new PokerParticipant(10, "holdem/points/nolimit/high", 200, Timings.DEFAULT));
  107. participants.add(new PokerParticipant(6, "holdem/points/nolimit/high", 200, Timings.DEFAULT));
  108. participants.add(new PokerParticipant(2, "holdem/points/nolimit/high", 500, Timings.DEFAULT));
  109. */
  110. // participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 10, Timings.DEFAULT));
  111. // participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 100, Timings.SUPER_EXPRESS));
  112. participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 10, Timings.DEFAULT));
  113. for (PokerParticipant part : participants) {
  114. part.setInjector(injector);
  115. }
  116. }
  117. /**
  118. * I will create a small set of different
  119. * seat-numbers which will go under different lobby paths.
  120. */
  121. @Override
  122. protected void initTables() {
  123. DefaultActivatorConfig configuration = getConfiguration();
  124. for (PokerParticipant part : participants) {
  125. // Get all tables for given FQN
  126. LobbyTable[] tables = tableRegistry.listTables(part.getLobbyPath());
  127. if(tables.length == 0) {
  128. incrementTables(configuration, part);
  129. }
  130. }
  131. }
  132. public void createTable(String domain, int seats, int level) {
  133. this.tableRegistry.createTable(seats, new PokerParticipant(seats, domain, level, Timings.DEFAULT));
  134. }
  135. /**
  136. * Check if we need to create tables or if empty tables should be removed.
  137. *
  138. * @see com.cubeia.firebase.api.game.activator.DefaultActivator#checkTables()
  139. */
  140. @Override
  141. protected void checkTables() {
  142. for (PokerParticipant part : participants) {
  143. LobbyTable[] tables = tableRegistry.listTables(part.getLobbyPath());
  144. List<LobbyTable> empty = findEmpty(tables);
  145. DefaultActivatorConfig config = getConfiguration();
  146. if(empty.size() < config.getMinAvailTables()) {
  147. incrementTables(config, part);
  148. } else {
  149. // checkTimeoutTables(tables.length, empty, config);
  150. }
  151. }
  152. }
  153. /**
  154. * Create a new batch of fresh tables.
  155. * The actual count is set trought the configuration.
  156. *
  157. * @param config
  158. */
  159. private void incrementTables(DefaultActivatorConfig config, PokerParticipant participant) {
  160. //for (int i = 0; i < config.getIncrementSize()*multiplier; i++) {
  161. tableRegistry.createTables(config.getIncrementSize() * multiplier, participant.getSeats(), participant);
  162. //}
  163. }
  164. public void mttTableCreated(Table table, int mttId, Object commandAttachment, LobbyAttributeAccessor acc) {
  165. log.debug("Created poker tournament table: "+table.getId());
  166. TimingProfile timing = TimingFactory.getRegistry().getDefaultTimingProfile();
  167. if (commandAttachment instanceof TournamentTableSettings) {
  168. TournamentTableSettings settings = (TournamentTableSettings) commandAttachment;
  169. timing = settings.getTimingProfile();
  170. }
  171. log.debug("Created tournament table["+table.getId()+"] with timing profile: "+timing);
  172. PokerState pokerState = injector.getInstance(PokerState.class);
  173. pokerState.setId(table.getId());
  174. // PokerState pokerState = new PokerState();
  175. // pokerState.setGameType(new TexasHoldem(pokerState));
  176. pokerState.setTimingProfile(timing);
  177. pokerState.setTournamentTable(true);
  178. pokerState.setTournamentId(mttId);
  179. pokerState.setAdapterState(new FirebaseState());
  180. table.getGameState().setState(pokerState);
  181. }
  182. public void mttTableCreated(Table table, int mttId, LobbyAttributeAccessor acc) {
  183. mttTableCreated(table, mttId, null, acc);
  184. }
  185. public int getMultiplier() {
  186. return multiplier;
  187. }
  188. public void setMultiplier(int multiplier) {
  189. this.multiplier = multiplier;
  190. }
  191. public void destroyTable(int id) {
  192. tableRegistry.destroyTable(id, true);
  193. }
  194. /*------------------------------------------------
  195. JMX INITIALIZATION & DESTRUCTION
  196. ------------------------------------------------*/
  197. private void initJmx() {
  198. try {
  199. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  200. ObjectName monitorName = new ObjectName(JMX_BIND_NAME);
  201. mbs.registerMBean(this, monitorName);
  202. } catch(Exception e) {
  203. log.error("failed to bind poker activator to JMX", e);
  204. }
  205. }
  206. private void destroyJmx() {
  207. try {
  208. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  209. ObjectName monitorName = new ObjectName(JMX_BIND_NAME);
  210. if(mbs.isRegistered(monitorName)) {
  211. mbs.unregisterMBean(monitorName);
  212. }
  213. } catch(Exception e) {
  214. log.error("failed to unbind poker activator to JMX", e);
  215. }
  216. }
  217. }