View Javadoc
1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 /*
22 * Created on Apr 27, 2004
23 *
24 * TODO To change the template for this generated file go to Window -
25 * Preferences - Java - Code Generation - Code and Comments
26 */
27 package net.mlw.gfw.ext.basic.server.event;
28
29 import net.mlw.gfw.event.Event;
30 import net.mlw.gfw.ext.basic.event.ChooseTeamEvent;
31 import net.mlw.gfw.ext.basic.event.ErrorEvent;
32 import net.mlw.gfw.ext.basic.event.SitEvent;
33 import net.mlw.gfw.ext.basic.event.StartRoundEvent;
34 import net.mlw.gfw.ext.basic.server.Player;
35 import net.mlw.gfw.ext.basic.server.TurnBasedGame;
36 import net.mlw.gfw.server.impl.AbstractServerEventHandler;
37
38 /***
39 * @author mwilson
40 *
41 * TODO To change the template for this generated type comment go to Window -
42 * Preferences - Java - Code Generation - Code and Comments
43 */
44 public abstract class ChooseTeamEventHandler extends AbstractServerEventHandler
45 {
46 protected TurnBasedGame game;
47
48 /***
49 * @param game
50 */
51 public ChooseTeamEventHandler(TurnBasedGame game)
52 {
53 this.game = game;
54 }
55
56 public abstract Player getPlayerImpl(ChooseTeamEvent cte);
57
58 /***
59 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event)
60 */
61 public void onEvent(Event event)
62 {
63 ChooseTeamEvent cte = (ChooseTeamEvent) event;
64
65 Player player = getPlayerImpl(cte);
66
67 boolean foundSeat = false;
68
69 for (int i = 0;(i <= game.getNumberOfPlayersPerTeam()); i++)
70 {
71 int numberOfTeams = game.getNumberOfSeats() / game.getNumberOfPlayersPerTeam();
72
73 int seat = i + cte.getTeam();
74 foundSeat = (game.getPlayerSeatedAt(seat) == null);
75
76 if (foundSeat)
77 {
78 game.setPlayerSeatedAt(player, seat);
79 context.getClients().onEvent(cte);
80 context.getClients().onEvent(new SitEvent(cte.getUserName(), seat));
81 break;
82 }
83 }
84
85 if (!foundSeat)
86 {
87 //There is no more room on this team!
88 context.getClients().onEvent(cte.getUserName(), new ErrorEvent("Choose a different team.", "No room left on this team."));
89 }
90
91 if (game.getNumberOfSeats() == game.getNumberSeated())
92 {
93 context.getServer().onEvent(new StartRoundEvent());
94 }
95 }
96 }
This page was automatically generated by Maven