[Cruce-commit] SF.net SVN: cruce:[112] Java/trunk/src/prc/bubulina/cruce
Status: Beta
Brought to you by:
caiusb
|
From: <hor...@us...> - 2010-04-12 16:21:43
|
Revision: 112
http://cruce.svn.sourceforge.net/cruce/?rev=112&view=rev
Author: horiaradu
Date: 2010-04-12 16:21:25 +0000 (Mon, 12 Apr 2010)
Log Message:
-----------
Am facut sa mearga join & start game. Am adaugat ClientRun (ala care porneste clientul) si Server (ala care porneste serverul).
Mai trebe partea cu flush input la askBinaryQuestion.
Modified Paths:
--------------
Java/trunk/src/prc/bubulina/cruce/client/Client.java
Java/trunk/src/prc/bubulina/cruce/remote/Player.java
Java/trunk/src/prc/bubulina/cruce/server/PlayerMap.java
Java/trunk/src/prc/bubulina/cruce/server/ServerImplementation.java
Added Paths:
-----------
Java/trunk/src/prc/bubulina/cruce/client/ClientRun.java
Java/trunk/src/prc/bubulina/cruce/server/Server.java
Java/trunk/src/prc/bubulina/cruce/tests/server/PlayerTest.java
Modified: Java/trunk/src/prc/bubulina/cruce/client/Client.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/client/Client.java 2010-04-12 09:12:08 UTC (rev 111)
+++ Java/trunk/src/prc/bubulina/cruce/client/Client.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -1,6 +1,9 @@
package prc.bubulina.cruce.client;
+import java.io.Serializable;
+import java.net.MalformedURLException;
import java.rmi.Naming;
+import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
@@ -12,7 +15,7 @@
import prc.bubulina.cruce.remote.Score;
import prc.bubulina.cruce.remote.ServerInterface;
-public class Client extends UnicastRemoteObject implements ClientInterface {
+public class Client extends UnicastRemoteObject implements ClientInterface, Serializable {
/**
*
@@ -41,12 +44,31 @@
this.game = game;
//TODO instantiate server
- //server = (ServerInterface) Naming.lookup(url);
+ try {
+ server = (ServerInterface) Naming.lookup(url);
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (NotBoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
// TODO this looks weird
- if (game.joinGame())
+ try {
+ if (game.joinGame()) {
player = server.joinGame(game.getPlayerName(), this);
- if (game.startGame())
+ System.out.println(player.getName());
+ System.out.println(player);
+ }
+ game.joinGame();
+ game.joinGame();
+ if (game.startGame()) {
+ System.out.println("Yes");
server.startGame(player);
+ }
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ }
}
@Override
Added: Java/trunk/src/prc/bubulina/cruce/client/ClientRun.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/client/ClientRun.java (rev 0)
+++ Java/trunk/src/prc/bubulina/cruce/client/ClientRun.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -0,0 +1,16 @@
+package prc.bubulina.cruce.client;
+
+import java.rmi.RemoteException;
+
+public class ClientRun {
+ public static void main(String[] args) {
+ UI ui = new TextUI();
+ Game game = new Game("bubu", ui);
+ try {
+ Client c = new Client(game, "localhost", "CruceServer");
+ } catch (RemoteException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
Modified: Java/trunk/src/prc/bubulina/cruce/remote/Player.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/remote/Player.java 2010-04-12 09:12:08 UTC (rev 111)
+++ Java/trunk/src/prc/bubulina/cruce/remote/Player.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -2,7 +2,7 @@
import java.io.Serializable;
-public class Player implements Serializable{
+public class Player implements Serializable {
/**
* Needed for serialization serialization
@@ -24,5 +24,15 @@
public int getID() {
return id;
}
-
+
+ public boolean equals(Object o) {
+ if (o instanceof Player)
+ if (((Player)o).name.equals(name) && ((Player)o).id == id)
+ return true;
+ return false;
+ }
+
+ public int hashCode() {
+ return id;
+ }
}
Modified: Java/trunk/src/prc/bubulina/cruce/server/PlayerMap.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/server/PlayerMap.java 2010-04-12 09:12:08 UTC (rev 111)
+++ Java/trunk/src/prc/bubulina/cruce/server/PlayerMap.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -44,4 +44,7 @@
public void reset() {
data.clear();
}
+ public String toString() {
+ return data.toString() + "\n" + clients.toString();
+ }
}
\ No newline at end of file
Added: Java/trunk/src/prc/bubulina/cruce/server/Server.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/server/Server.java (rev 0)
+++ Java/trunk/src/prc/bubulina/cruce/server/Server.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -0,0 +1,16 @@
+package prc.bubulina.cruce.server;
+
+import java.rmi.Naming;
+
+import prc.bubulina.cruce.remote.ServerInterface;
+
+public class Server {
+ public static void main(String[] args) {
+ try {
+ ServerInterface server = new ServerImplementation();
+ Naming.rebind("CruceServer", server);
+ System.out.println("Server running");
+ } catch (Exception e) {
+ }
+ }
+}
\ No newline at end of file
Modified: Java/trunk/src/prc/bubulina/cruce/server/ServerImplementation.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/server/ServerImplementation.java 2010-04-12 09:12:08 UTC (rev 111)
+++ Java/trunk/src/prc/bubulina/cruce/server/ServerImplementation.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -28,8 +28,37 @@
public ServerImplementation() throws RemoteException {
super();
+ teams = new LinkedList<Team>();
+ teams.add(new Team());
+ teams.add(new Team());
+ startGameLogic = new StartGameLogic(teams);
+ gameLogic = new GameLogic();
}
+
+ public synchronized Player joinGame(String name, ClientInterface client) throws RemoteException {
+ System.out.println(name + " joined the game");
+ client.inform("welcome to the game");
+ ServerSidePlayer ssplayer = startGameLogic.setupPlayer(name);
+ clients.add(client);
+ playerMap.put(ssplayer, client);
+ if (ssplayer != null)
+ return ssplayer.getInfo();
+ return null;
+ }
+ public synchronized void startGame(Player sender) throws RemoteException {
+ System.out.println(sender.getName() + " is ready to start the game");
+ startGameLogic.playerReady();
+ if (startGameLogic.gameReady()) {
+ for (ClientInterface client : clients)
+ client.inform("The game will start in a moment...");
+ gameLogic.startGame(teams, this);
+ } else {
+ playerMap.getClient(sender).inform("waiting for other players");
+ System.out.println("need more players");
+ }
+ }
+
public synchronized boolean sendCards(List<Card> cards, ServerSidePlayer player) {
ClientInterface client = playerMap.getClient(player.getInfo());
@@ -91,21 +120,4 @@
// TODO Auto-generated method stub
return null;
}
-
- public synchronized Player joinGame(String name, ClientInterface client) throws RemoteException {
- ServerSidePlayer ssplayer = startGameLogic.setupPlayer(name);
- playerMap.put(ssplayer, client);
- if (ssplayer != null)
- return ssplayer.getInfo();
- return null;
- }
-
- public synchronized void startGame(Player sender) throws RemoteException {
- startGameLogic.playerReady();
- if (startGameLogic.gameReady()) {
- for (ClientInterface client : clients)
- client.inform("The game will start in a moment...");
- gameLogic.startGame(teams, this);
- }
- }
}
\ No newline at end of file
Added: Java/trunk/src/prc/bubulina/cruce/tests/server/PlayerTest.java
===================================================================
--- Java/trunk/src/prc/bubulina/cruce/tests/server/PlayerTest.java (rev 0)
+++ Java/trunk/src/prc/bubulina/cruce/tests/server/PlayerTest.java 2010-04-12 16:21:25 UTC (rev 112)
@@ -0,0 +1,32 @@
+package prc.bubulina.cruce.tests.server;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+
+import org.junit.Test;
+
+import prc.bubulina.cruce.remote.Player;
+
+public class PlayerTest {
+ @Test
+ public void testEquals() {
+ Player p1 = new Player("Horia", 0);
+ Player p2 = new Player("Horia", 0);
+ assertTrue(p1.equals(p2));
+ assertEquals(p1, p2);
+ }
+ @Test
+ public void testEqualsInList() {
+ HashMap<Player, Integer> l = new HashMap<Player, Integer>();
+ Player p1 = new Player("Horia", 0);
+ Player p2 = new Player("Horia", 0);
+ Player p3 = new Player("Alin", 1);
+ l.put(p3, 0);
+ l.put(p1, 1);
+ Integer i = l.get(p2);
+ System.out.println(i);
+ assertEquals(i, new Integer(1));
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|