Skip to content

Instantly share code, notes, and snippets.

@brantz
Created February 6, 2011 01:09
Show Gist options
  • Save brantz/812994 to your computer and use it in GitHub Desktop.
Save brantz/812994 to your computer and use it in GitHub Desktop.
package com.nagai.server;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.nagai.client.GameServerService;
import com.nagai.shared.Interpreter;
import com.nagai.shared.ShoppingCart;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
public class GameServerServiceImpl extends RemoteServiceServlet implements
GameServerService {
private Interpreter interpreter = new Interpreter();
private static final long serialVersionUID = 3185002270178659393L;
private static final Logger log = Logger
.getLogger(GameServerServiceImpl.class.getName());
private Vector<String> chatLog = new Vector<String>();
public String testClientServerCommunication(String input) {
return input;
}
@Override
public Integer getPlayerNo() {
String playerColor = server.interop.addPlayer();
log.log(Level.INFO,
"***SERVER*** getPlayerId(): giving player number to client "
+ interpreter.colorToPlayerNumber(playerColor));
return interpreter.colorToPlayerNumber(playerColor);
}
// XXX old stuff
@Override
public boolean isGameStarted() {
log.log(Level.INFO,
"***SERVER*** isGameStarted(): getting request from client");
return server.interop.isGameStarted();
}
@Override
public int getPlayerCountFromServer() {
log.log(Level.INFO,
"***SERVER*** getPlayerCountFromServer(): getting a player count request from client");
return server.interop.getPlayerCount();
}
@Override
public void startGame() {
server.interop.startGame();
log.log(Level.INFO,
"***SERVER*** startGame(): starting game request from client");
}
@Override
public Map<Integer, Map<String, Object>> getStaticField() {
// we have to deep copy the map because clojure returns clojure
// datatypes
@SuppressWarnings("unchecked")
Map<Integer, Map<String, Object>> map = (java.util.Map<Integer, java.util.Map<String, Object>>) server.interop
.getStaticField();
// System.out.println("result size " + map.size());
Map<Integer, Map<String, Object>> deepCopyMap = new HashMap<Integer, Map<String, Object>>();
for (Map.Entry<Integer, Map<String, Object>> entry : map.entrySet()) {
// System.out.println(entry.getKey() + " " + entry.getValue());
Map<String, Object> deepInner = new HashMap<String, Object>();
for (Map.Entry<String, Object> innerEntry : entry.getValue()
.entrySet()) {
// System.out.println(innerEntry.getKey() + " " +
// innerEntry.getValue());
deepInner.put(innerEntry.getKey(), innerEntry.getValue());
}
deepCopyMap.put(entry.getKey(), deepInner);
}
return deepCopyMap;
}
/*
* (non-Javadoc)
*
* @see com.nagai.client.GameServerService#getDynamicField()
*/
@Override
public Map<Set<Integer>, Map<String, String>> getDynamicField() {
@SuppressWarnings("unchecked")
Map<Set<Integer>, Map<String, String>> map = (java.util.Map<java.util.Set<Integer>, java.util.Map<String, String>>) server.interop
.getStaticField();
// System.out.println("result size " + map.size());
Map<Set<Integer>, Map<String, String>> deepCopyMap = new HashMap<Set<Integer>, Map<String, String>>();
for (Map.Entry<Set<Integer>, Map<String, String>> entry : map.entrySet()) {
// System.out.println(entry.getKey() + " " + entry.getValue());
Map<String, String> deepInner = new HashMap<String, String>();
for (Map.Entry<String, String> innerEntry : entry.getValue()
.entrySet()) {
// System.out.println(innerEntry.getKey() + " " +
// innerEntry.getValue());
deepInner.put(innerEntry.getKey(), innerEntry.getValue());
}
deepCopyMap.put(entry.getKey(), deepInner);
}
return deepCopyMap;
}
@Override
public Map<String, Integer> getMyResources(int playerNo) {
log.log(Level.INFO,
"***SERVER*** getMyResources(int playerNumber): giving resource count to client "
+ playerNo);
@SuppressWarnings("unchecked")
Map<String, Integer> map = (java.util.Map<String, Integer>) server.interop
.getStaticField();
// System.out.println("result size " + map.size());
Map<String, Integer> deepCopyMap = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
// System.out.println(entry.getKey() + " " + entry.getValue());
deepCopyMap.put(entry.getKey(), entry.getValue());
}
return deepCopyMap;
}
@Override
public int getRobber() {
log.log(Level.INFO,
"***SERVER*** getRobber(): giving robber position to client");
return server.interop.getRobber();
}
@Override
public boolean isMyTurn(int playerNo) {
log.log(Level.INFO,
"***SERVER*** isMyTurn(int playerNo): giving turn info to client "
+ playerNo);
return server.interop.isMyTurn(interpreter
.numberToPlayerColor(playerNo));
}
@Override
public int getVictoryPoints(int playerNo) {
log.log(Level.INFO,
"***SERVER*** getVictoryPoints(int playerNo): giving "
+ server.interop.getVictoryPoints(interpreter
.numberToPlayerColor(playerNo))
+ " victory points to client " + playerNo);
return server.interop.getVictoryPoints(interpreter
.numberToPlayerColor(playerNo));
}
@Override
public Integer[] getDiceNo() {
log.log(Level.INFO,
"***SERVER*** getDiceNo(): giving dice count to client");
return server.interop.getDiceNo();
}
@Override
public void turnFinished(int playerNo) {
log.log(Level.INFO,
"***SERVER*** turnFinished(int playerNo): getting turn finished flag from client "
+ playerNo);
server.interop.finishTurn(interpreter
.numberToPlayerColor(playerNo));
}
@Override
public boolean setRobber(int fieldNo, int playerNo) {
log.log(Level.INFO,
"***SERVER*** setRobber(int FieldNo, int playerNo): getting new robber position "
+ fieldNo + " from client " + playerNo);
return server.interop.setRobber(fieldNo,
interpreter.numberToPlayerColor(playerNo));
}
@Override
public boolean build(int knot1, int knot2, int playerNo) {
log.log(Level.INFO,
"***SERVER*** build(int knot1, int knot2, int playerNo): getting new build road request from client "
+ playerNo);
return server.interop.build(knot1, knot2,
interpreter.numberToPlayerColor(playerNo));
}
@Override
public boolean build(int knot, int playerNo) {
log.log(Level.INFO,
"***SERVER*** build(int knot, int playerNo): getting new build settlement / city request from client "
+ playerNo);
return server.interop.build(knot,
interpreter.numberToPlayerColor(playerNo));
}
/*
* (non-Javadoc)
*
* @see com.nagai.client.GameServerService#getLongestRoadGreatestPower()
*/
@Override
public Map<String, Boolean> getLongestRoadGreatestPower() {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see com.nagai.client.GameServerService#doTradeStatic(com.nagai.shared.
* ShoppingCart)
*/
@Override
public boolean doTradeStatic(ShoppingCart shoppingCart) {
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see
* com.nagai.client.GameServerService#doTradeWithPlayer(com.nagai.shared
* .ShoppingCart)
*/
@Override
public boolean doTradeWithPlayer(ShoppingCart shoppingCart) {
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see com.nagai.client.GameServerService#getTradingRequest()
*/
@Override
public ShoppingCart getTradingRequest() {
// TODO Auto-generated method stub
return null;
}
@Override
public void sendChatMsg(String msg, int playerNo) {
log.log(Level.INFO,
"***SERVER*** sendChatMsg(String msg, int playerNo): getting msg from client "
+ playerNo);
StringBuilder stringBuffer = new StringBuilder("");
stringBuffer.append("Player ").append(String.valueOf(playerNo))
.append(" : ").append(msg);
chatLog.add(stringBuffer.toString());
}
@Override
public String getChatMsg() {
log.log(Level.INFO,
"***SERVER*** getChatMsg(String msg): sending msg to client ");
return chatLog.lastElement();
}
/*
* (non-Javadoc)
*
* @see com.nagai.client.GameServerService#getDevelopmentCards(int)
*/
@Override
public Map<String, Integer> getDevelopmentCards(int playerNo) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see com.nagai.client.GameServerService#setTestWorld()
*/
@Override
public void setTestWorld() {
server.interop.setTestWorld();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment