ManoDoc.java 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
package servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import clases.Carta;
import clases.Poker;

/**
 * Servlet implementation class ManoDoc
 */
@WebServlet("/mano_doc")
public class ManoDoc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManoDoc() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "https://deckofcardsapi.com/api/deck/new/draw/?count=5";
		
		String json = obtenerJson(url);
		
		Carta[] mano = obtenerCartas(json);
		
		Poker p = new Poker();
		String nombreJugada = p.nombreJugada(mano);
		
		
		for(int i=0; i<mano.length; i++) {
			System.out.println(mano[i].valor + mano[i].palo);
		}
		
		System.out.println(nombreJugada);
		System.out.println("************************");
		
		request.setAttribute("mano", mano);
		request.setAttribute("nombreJugada", nombreJugada);
		
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/jsp/mano_doc.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	
	public static String obtenerJson(String myURL) {
		System.setProperty("http.agent", "Chrome");
		System.out.println("Requeted URL:" + myURL);
		StringBuilder sb = new StringBuilder();
		URLConnection urlConn = null;
		InputStreamReader in = null;
		try {
			URL url = new URL(myURL);
			urlConn = url.openConnection();
			if (urlConn != null)
				urlConn.setReadTimeout(60 * 1000);
			if (urlConn != null && urlConn.getInputStream() != null) {
				in = new InputStreamReader(urlConn.getInputStream(),
						Charset.defaultCharset());
				BufferedReader bufferedReader = new BufferedReader(in);
				if (bufferedReader != null) {
					int cp;
					while ((cp = bufferedReader.read()) != -1) {
						sb.append((char) cp);
					}
					bufferedReader.close();
				}
			}
		in.close();
		} catch (Exception e) {
			throw new RuntimeException("Exception while calling URL:"+ myURL, e);
		} 
 
		return sb.toString();
	}
	
	public Carta[] obtenerCartas(String json) {
		
		
		JSONObject obj = new JSONObject(json);
		
		JSONArray cartas = obj.getJSONArray("cards");
		
		Carta[] mano= new Carta[5];
		
		for(int i=0; i<mano.length;i++) {
			String valor = cartas.getJSONObject(i).getString("code").substring(0, 1);
			String palo = cartas.getJSONObject(i).getString("code").substring(1, 2);
			
			if(valor.equals("0")) {
				valor = "T";
			}
			
			String carta = valor+palo;
			
			mano[i] = new Carta(carta);
		}
		
		return mano;
	}

}