ManoDoc.java 4.04 KB
Newer Older
1 2 3 4 5 6 7 8
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;
9 10
import java.util.ArrayList;
import java.util.Collections;
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

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("************************");
		
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
		ArrayList<String> manoString = new ArrayList<String>();
		
		for(int i=0; i<mano.length;i++) {
			if(mano[i].valor.equals("A")) {
				mano[i].valor = "R";
			}
			else if(mano[i].valor.equals("T")){
				mano[i].valor = "10";
			}
			manoString.add(mano[i].valor+mano[i].palo);
		}
		
		Collections.sort(manoString);
		
		for(int i=0; i<mano.length; i++) {
			if(manoString.get(i).charAt(0)=='R') {				
				mano[i] = new Carta("A"+manoString.get(i).charAt(1));
			}
			else if(manoString.get(i).charAt(0)=='1' && manoString.get(i).charAt(1)=='0'){
				mano[i] = new Carta("T"+manoString.get(i).charAt(2));
			}
			else {
				mano[i] = new Carta(manoString.get(i));
			}
		}
		
87 88 89
		request.setAttribute("mano", mano);
		request.setAttribute("nombreJugada", nombreJugada);
		
90 91
		
		
92 93 94 95 96 97 98 99 100 101 102 103 104
		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");
105
		System.out.println("Requested URL:" + myURL);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		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;
	}

}