package Servlets;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Boolean;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author tmmcgrat
*/
public class Lobby extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* get games
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String[] players = new String[6];
players[0] = "tim";
players[1] = "mike";
this.getServletContext().setAttribute("game1", new GameTesting("London",players));
this.getServletContext().setAttribute("game2", new GameTesting("London",players));
this.getServletContext().setAttribute("game3", new GameTesting("London",players));
this.getServletContext().setAttribute("game4", new GameTesting("London",players));
// actual code
GameTesting game;
String gameName;
Enumeration games = this.getServletContext().getAttributeNames();
while(games.hasMoreElements())
{
gameName = (String) games.nextElement();
if(gameName.contains("game"))
{
game = (GameTesting)this.getServletContext().getAttribute(gameName); // servletContext works like
out.print("<game><map>");
out.print(game.map);
out.print("</map>");
for(int i =0 ; i < players.length;i++)
{
out.print("<player> ");
out.print(game.players[i]);
out.print(" </player>");
}
out.print("</game>");
out.print("<br />");
out.println();
}
}
} finally {
out.close();
}
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String gameID = request.getParameter("gameID");
String role = request.getParameter("role");
GameTesting game = (GameTesting) this.getServletContext().getAttribute(gameID);
Boolean result = true; // gameEngine.join( game ,role)
if(!result)
{
response.getWriter().print("<result>false</result");
}
else
{
doGet(request, response); // return all game data including changes
}
}
}