package pirateGameRoom;

import java.util.concurrent.ConcurrentHashMap;

import org.json.*;
import org.restlet.representation.*;
import org.restlet.ext.json.*;
import org.restlet.resource.*;

public class PirateGameRoomResource extends ServerResource {

	private static int playercnt = 0;
	private static ConcurrentHashMap<Integer, String> player = new ConcurrentHashMap();
	private static final String winner = "WINNER";
	// playercnt, stagecnt

	

	@Get("status")
	public Representation get(String status) throws JSONException {
		boolean isWinner = false;
		JSONObject response = new JSONObject();
		for (int name : player.keySet()) {

			int key = name;
			String stage = player.get(name).toString();
			if (stage.equalsIgnoreCase(winner)) {
				isWinner = true;
				response.put("player", key);
				response.put("winner", isWinner);
				break;

			}
			System.out.println(key + " " + stage);

			response.put("winner", isWinner);
		}

		return new JsonRepresentation(response);
	}

	@Post
	public Representation post(JsonRepresentation jsonRep) {

		JSONObject json = jsonRep.getJsonObject();
		String stage = json.getString("stage");
		System.out.println("satge: " + stage);

		playercnt++;
		player.put(playercnt, stage);

		JSONObject response = new JSONObject();
		response.put("player", playercnt);
		response.put("stage", stage);

		System.out.println("---List of Player-----");
		for (int name : player.keySet()) {

			int key = name;
			String value = player.get(name).toString();
			System.out.println(key + " " + value);

		}
		return new JsonRepresentation(response);

	}

	@Put
	public Representation put(JsonRepresentation jsonRep) {

		JSONObject json = jsonRep.getJsonObject();
		String stage = json.getString("stage");
		int playernumber = json.getInt("player");
		System.out.println("stage: " + stage);

		// update hashmap
		// update player with winner when he grabs the treasure
		player.put(playernumber, stage);

		JSONObject response = new JSONObject();
		response.put("player", playercnt);
		response.put("stage", stage);

		return new JsonRepresentation(response);

	}
	
	public static ConcurrentHashMap<Integer, String> getPlayerMap(){
			return player;
		
	}

}

@@ -928,16 +928,64 @@
 inner);%0A
+%09%09%09response.put(%22playerCount%22 , player.size());%0A
 %09%09%7D%0A%0A%09%09r
@@ -1238,16 +1238,245 @@
 rcnt++;%0A
+%09%09if(playercnt %3C= 5)%0A%09%09%09player.put(playercnt, stage);%0A%09%09else%0A%09%09%7B%0A%09%09%09JSONObject response = new JSONObject();%0A%09%09%09response.put(%22Error%22, %22Maximum number of players already allocated%22);%0A%09%09%09return new JsonRepresentation(response);%0A%09%09%7D%0A
 %09%09player

Update server to check and reject more than 5 players