package sfs.cluster.lobby;

import java.util.HashMap;
import java.util.Map;

import it.gotoandplay.smartfoxserver.config.ConfigData;

public class ClusterUtils
{
	private static Map<String, String> gameTypes = new HashMap<String, String>();
	
	static
	{
		gameTypes.put("tris", "10000");
		gameTypes.put("maze", "10001");
	}
	
	/*
	 * Here we establish a simple convention that allows us to create a relation between the game server
	 * and the type of game that it is running.
	 * 
	 * We'll assign game servers a port number >= 10000
	 * 
	 * Each port will be linked to a game type, example:
	 * 
	 * Tris game --> port 10000
	 * Maze game --> port 10001
	 * and so on...
	 * 
	 * Since each node registers in the cluster with a nodeID like --> W.X.Y.Z:nnnn
	 * we'll be able to search the node from its game type via this relation
	 */
	public static String getGameServerPortFromType(String gameType)
	{
		return gameTypes.get(gameType);
	}
	
	public static String getGameTypeFromPort(String port)
	{
		String type = null;
		
		for (Map.Entry<String, String> entry : gameTypes.entrySet())
		{
			if (entry.getValue().equals(port))
			{
				type = entry.getKey();
				break;
			}
		}
		
		return type;
	}
	
	/*
	 * Generate a unique ID by combining the IP address and the port number
	 * Throws exceptions if operation is not possible.
	 */
	public static String generateNodeId() // throws Exception
	{
		return ConfigData.SERVER_ADDR + ":" + ConfigData.SERVER_PORT;
	}
}
