PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 6ms 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
 18package com.cubeia.games.poker.activator;
 19
 20import java.lang.management.ManagementFactory;
 21import java.util.ArrayList;
 22import java.util.List;
 23
 24import javax.management.MBeanServer;
 25import javax.management.ObjectName;
 26
 27import org.apache.log4j.Logger;
 28
 29import com.cubeia.firebase.api.game.activator.ActivatorContext;
 30import com.cubeia.firebase.api.game.activator.DefaultActivator;
 31import com.cubeia.firebase.api.game.activator.DefaultActivatorConfig;
 32import com.cubeia.firebase.api.game.activator.MttAwareActivator;
 33import com.cubeia.firebase.api.game.lobby.LobbyTable;
 34import com.cubeia.firebase.api.game.table.Table;
 35import com.cubeia.firebase.api.lobby.LobbyAttributeAccessor;
 36import com.cubeia.firebase.api.server.SystemException;
 37import com.cubeia.games.poker.FirebaseState;
 38import com.cubeia.games.poker.tournament.activator.TournamentTableSettings;
 39import com.cubeia.poker.PokerGuiceModule;
 40import com.cubeia.poker.PokerState;
 41import com.cubeia.poker.gametypes.TexasHoldem;
 42import com.cubeia.poker.timing.TimingFactory;
 43import com.cubeia.poker.timing.TimingProfile;
 44import com.cubeia.poker.timing.Timings;
 45import com.google.inject.Guice;
 46import com.google.inject.Injector;
 47
 48/**
 49 * Override the default game activator in order to provide my own 
 50 * specific implementations. 
 51 *
 52 * @author Fredrik Johansson, Cubeia Ltd
 53 */
 54public class PokerActivator extends DefaultActivator implements MttAwareActivator, PokerActivatorMBean {
 55
 56    private static final String JMX_BIND_NAME = "com.cubeia.poker:type=PokerActivator";
 57    
 58    private transient Logger log = Logger.getLogger(this.getClass());
 59
 60    private int multiplier = 1;
 61    
 62    /**
 63     * Holds all participants that should be used for creating tables.
 64     * I hold the implementation instead of the interface so that I can keep
 65     * bingo-specific data in the participants, i.e. FQN and special attributes.
 66     */
 67    private List<PokerParticipant> participants = new ArrayList<PokerParticipant>();
 68
 69	private Injector injector;
 70	
 71    /**
 72     * Create the activator for Poker.
 73     * Set the Creation Participant as participant.
 74     *
 75     */
 76    public PokerActivator() {
 77        super();
 78        log.info("Created Game Activator for Poker");
 79    }
 80
 81    @Override
 82    public void init(ActivatorContext context) throws SystemException {
 83    	log.warn(">>>>>>>>> INIT PARTICIPANTS");
 84        super.init(context);
 85        log.debug("initJmx");
 86        initJmx();
 87        log.debug("Calling Guice.createInjector");
 88        injector = Guice.createInjector(
 89        		new ActivatorGuiceModule(context),
 90        		new PokerGuiceModule());
 91        log.debug("initParticipants");
 92        initParticipants();
 93    }
 94    
 95    @Override
 96    public void destroy() {
 97        super.destroy();
 98        destroyJmx();
 99    }
100    
101    /** 
102     * Create a number of participants, i.e. lobby branches 
103     * 
104     */
105    private void initParticipants() {	
106    	/*
107    	participants.add(new PokerParticipant(10,"holdem/real/nolimit/micro", 2, Timings.DEFAULT));
108    	
109    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/medium", 50, Timings.DEFAULT));
110    	participants.add(new PokerParticipant(6,  "holdem/real/nolimit/medium", 50, Timings.DEFAULT));
111    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/medium", 75, Timings.DEFAULT));
112    	
113    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 100, Timings.DEFAULT));
114    	participants.add(new PokerParticipant(6,  "holdem/real/nolimit/high", 100, Timings.DEFAULT));
115    	participants.add(new PokerParticipant(2,  "holdem/real/nolimit/high", 100, Timings.DEFAULT));
116    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 1000, Timings.DEFAULT));
117    	
118    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/micro", 2, Timings.EXPRESS));
119    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 100, Timings.EXPRESS));
120    	
121    	participants.add(new PokerParticipant(10, "holdem/points/nolimit/micro", 2, Timings.DEFAULT));
122    	participants.add(new PokerParticipant(10, "holdem/points/nolimit/low", 10, Timings.DEFAULT));
123    	
124    	participants.add(new PokerParticipant(10, "holdem/points/nolimit/medium", 50, Timings.DEFAULT));
125    	participants.add(new PokerParticipant(6,  "holdem/points/nolimit/medium", 50, Timings.DEFAULT));
126    	
127    	participants.add(new PokerParticipant(10, "holdem/points/nolimit/high", 200, Timings.DEFAULT));
128    	participants.add(new PokerParticipant(6,  "holdem/points/nolimit/high", 200, Timings.DEFAULT));
129    	participants.add(new PokerParticipant(2,  "holdem/points/nolimit/high", 500, Timings.DEFAULT));
130    	 */
131    	
132    	// participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 10, Timings.DEFAULT));
133    	// participants.add(new PokerParticipant(10, "holdem/real/nolimit/high", 100, Timings.SUPER_EXPRESS));
134    	participants.add(new PokerParticipant(10, "holdem/real/nolimit/low", 10, Timings.DEFAULT));
135    	
136    	for (PokerParticipant part : participants) {
137    		part.setInjector(injector);
138    	}
139    }
140
141    /**
142     * I will create a small set of different
143     * seat-numbers which will go under different lobby paths.
144     */
145    @Override
146    protected void initTables() {
147        DefaultActivatorConfig configuration = getConfiguration();
148
149        for (PokerParticipant part : participants) {
150            // Get all tables for given FQN
151            LobbyTable[] tables = tableRegistry.listTables(part.getLobbyPath());
152            if(tables.length == 0) {
153                incrementTables(configuration, part);
154            }
155
156        }
157    }
158    
159    public void createTable(String domain, int seats, int level) {
160    	this.tableRegistry.createTable(seats, new PokerParticipant(seats, domain, level, Timings.DEFAULT));
161    }
162
163    /** 
164     * Check if we need to create tables or if empty tables should be removed. 
165     * 
166     * @see com.cubeia.firebase.api.game.activator.DefaultActivator#checkTables()
167     */
168    @Override
169    protected void checkTables() {
170        for (PokerParticipant part : participants) {
171            LobbyTable[] tables = tableRegistry.listTables(part.getLobbyPath());
172            List<LobbyTable> empty = findEmpty(tables);
173            DefaultActivatorConfig config = getConfiguration();
174
175            if(empty.size() < config.getMinAvailTables()) {
176                incrementTables(config, part);
177            } else {
178                // checkTimeoutTables(tables.length, empty, config);
179            }
180        }
181    }
182
183    /**
184     * Create a new batch of fresh tables.
185     * The actual count is set trought the configuration.
186     * 
187     * @param config
188     */
189    private void incrementTables(DefaultActivatorConfig config, PokerParticipant participant) {
190        //for (int i = 0; i < config.getIncrementSize()*multiplier; i++) {
191            tableRegistry.createTables(config.getIncrementSize() * multiplier, participant.getSeats(), participant);
192        //}
193    }
194
195
196    public void mttTableCreated(Table table, int mttId, Object commandAttachment, LobbyAttributeAccessor acc) {
197        log.debug("Created poker tournament table: "+table.getId());
198
199        TimingProfile timing = TimingFactory.getRegistry().getDefaultTimingProfile();
200        if (commandAttachment instanceof TournamentTableSettings) {
201            TournamentTableSettings settings = (TournamentTableSettings) commandAttachment;
202            timing = settings.getTimingProfile();
203        }
204
205        log.debug("Created tournament table["+table.getId()+"] with timing profile: "+timing);
206
207        PokerState pokerState = injector.getInstance(PokerState.class);
208        pokerState.setId(table.getId());
209        
210        // PokerState pokerState = new PokerState();
211        // pokerState.setGameType(new TexasHoldem(pokerState));
212        
213        pokerState.setTimingProfile(timing);
214        pokerState.setTournamentTable(true);
215        pokerState.setTournamentId(mttId);
216        pokerState.setAdapterState(new FirebaseState());
217        table.getGameState().setState(pokerState);
218        
219    }
220
221    public void mttTableCreated(Table table, int mttId, LobbyAttributeAccessor acc) {
222        mttTableCreated(table, mttId, null, acc);
223    }
224
225    public int getMultiplier() {
226        return multiplier;
227    }
228
229    public void setMultiplier(int multiplier) {
230        this.multiplier = multiplier;
231    }
232    
233    public void destroyTable(int id) {
234        tableRegistry.destroyTable(id, true);
235    }
236    
237    
238
239    /*------------------------------------------------
240
241    JMX INITIALIZATION & DESTRUCTION
242
243 ------------------------------------------------*/
244
245    private void initJmx() {
246        try {
247            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
248            ObjectName monitorName = new ObjectName(JMX_BIND_NAME);
249            mbs.registerMBean(this, monitorName);
250        } catch(Exception e) {
251            log.error("failed to bind poker activator to JMX", e);
252        }
253    }
254
255
256    private void destroyJmx() {
257        try {
258            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
259            ObjectName monitorName = new ObjectName(JMX_BIND_NAME);
260            if(mbs.isRegistered(monitorName)) {
261                mbs.unregisterMBean(monitorName);
262            }
263        } catch(Exception e) {
264            log.error("failed to unbind poker activator to JMX", e);
265        }
266    }
267}