1 |
781 |
jeremybenn |
/* GameManagerImpl.java --
|
2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.classpath.examples.CORBA.swing.x5;
|
39 |
|
|
|
40 |
|
|
import java.rmi.RemoteException;
|
41 |
|
|
|
42 |
|
|
import org.omg.CORBA.ORB;
|
43 |
|
|
import org.omg.CORBA.Object;
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* The manager connects two players into the game.
|
47 |
|
|
*
|
48 |
|
|
* @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
|
49 |
|
|
*/
|
50 |
|
|
public class GameManagerImpl
|
51 |
|
|
implements GameManager
|
52 |
|
|
{
|
53 |
|
|
/**
|
54 |
|
|
* The game manager IOR.
|
55 |
|
|
*/
|
56 |
|
|
static String ior;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* The game manager ORB.
|
60 |
|
|
*/
|
61 |
|
|
static ORB orb;
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* True if the manager started ok.
|
65 |
|
|
*/
|
66 |
|
|
static boolean ok;
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Another player that is already waiting for the game.
|
70 |
|
|
*/
|
71 |
|
|
Player queuedPlayer = null;
|
72 |
|
|
|
73 |
|
|
public synchronized void requestTheGame(Player newPlayer)
|
74 |
|
|
throws RemoteException
|
75 |
|
|
{
|
76 |
|
|
System.out.println("Game requested");
|
77 |
|
|
|
78 |
|
|
if (queuedPlayer == null)
|
79 |
|
|
{
|
80 |
|
|
// No other player so far.
|
81 |
|
|
newPlayer.receive_chat(ChatConstants.GAME_SERVER,
|
82 |
|
|
"Request registered, waiting for the other player to come...");
|
83 |
|
|
System.out.println("Player queued.");
|
84 |
|
|
queuedPlayer = newPlayer;
|
85 |
|
|
}
|
86 |
|
|
else if (queuedPlayer.equals(newPlayer))
|
87 |
|
|
{
|
88 |
|
|
// The same player applies again.
|
89 |
|
|
newPlayer.receive_chat(ChatConstants.GAME_SERVER,
|
90 |
|
|
"No other player so far... Please wait.");
|
91 |
|
|
}
|
92 |
|
|
else
|
93 |
|
|
{
|
94 |
|
|
// As the queued player waited for the game, we allow him/her
|
95 |
|
|
// to start the game. This is a reward for waiting.
|
96 |
|
|
newPlayer.receive_chat(ChatConstants.GAME_SERVER,
|
97 |
|
|
"The other player is waiting. The game started, your "
|
98 |
|
|
+ "partner begins...");
|
99 |
|
|
queuedPlayer.receive_chat(ChatConstants.GAME_SERVER,
|
100 |
|
|
"The other player arrived. Lets play, you begin the game now...");
|
101 |
|
|
|
102 |
|
|
newPlayer.start_game(queuedPlayer, false);
|
103 |
|
|
queuedPlayer.start_game(newPlayer, true);
|
104 |
|
|
|
105 |
|
|
queuedPlayer = null;
|
106 |
|
|
System.out.println("Players connected.");
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Unregister the player who left and is no longer waiting for another side.
|
112 |
|
|
*/
|
113 |
|
|
public void unregister(Player player)
|
114 |
|
|
throws RemoteException
|
115 |
|
|
{
|
116 |
|
|
if (queuedPlayer != null)
|
117 |
|
|
{
|
118 |
|
|
// We need to verify the identity of the player being unregistered.
|
119 |
|
|
// The stubs, being derived from the org.omg.CORBA.Object, have the
|
120 |
|
|
// method for this. This method compares the player host address,
|
121 |
|
|
// used port and the object key.
|
122 |
|
|
if (player instanceof Object && queuedPlayer instanceof Object)
|
123 |
|
|
{
|
124 |
|
|
Object a = (Object) player;
|
125 |
|
|
Object b = (Object) queuedPlayer;
|
126 |
|
|
|
127 |
|
|
if (a._is_equivalent(b))
|
128 |
|
|
queuedPlayer = null;
|
129 |
|
|
}
|
130 |
|
|
else
|
131 |
|
|
queuedPlayer = null;
|
132 |
|
|
}
|
133 |
|
|
System.out.println("Unregistering player");
|
134 |
|
|
}
|
135 |
|
|
}
|