/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.19
 * Generated at: 2012-12-23 21:10:22 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp.addons.Cubi_0020Shop;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.sql.*;
import protocol.*;
import java.util.Iterator;
import com.goldhuman.Common.Octets;
import com.goldhuman.IO.Protocol.Rpc.Data.DataVector;
import org.apache.commons.lang.StringEscapeUtils;
import java.io.*;

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
    _jspx_dependants.put("/addons/Cubi Shop/../../WEB-INF/.pwadminconf.jsp", Long.valueOf(1356261856000L));
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write("\n");
      out.write('\n');
      out.write('\n');

//-------------------------------------------------------------------------------------------------------------------------
//------------------------------- SETTINGS --------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------

	// pwadmin access password as md5 encrypted string
	// default password is root -> 63a9f0ea7bb98050796b649e85481845--- 9eb71ab7420eb452a22787ca4fab501b
	String iweb_password = "f67e209e9b8164dcb6812b9ac4357ed1";

	// connection settings to the mysql pw database
	String db_host = "localhost";
	String db_port = "3306";
	String db_user = "root";
	String db_password = "1011";
	String db_database = "pw";

	// Type of your items database required for mapping id's to names
	// Options are my or pwi
	String item_labels = "pwi";

	// Absolute path to your PW-Server main directory (startscript, stopscript, /gamed)
	// requires a tailing slash
	String pw_server_path = "/root/";

	// If you have hundreds of characters or heavy web acces through this site
	// It is recommend to turn the realtime character list feature off (false)
	// to prevent server from overload injected by character list generation
	boolean enable_character_list = true;

	String pw_server_name = "Your PW Servername";
	String pw_server_description = "+ Your server description here<br>+ 2 x 2.4 GHz, 2048 MB RAM, 100 MBit LAN<br>+ all dungeons open<br>+ tideborn nirvana weapons<br>+ World bosses";

//-------------------------------------------------------------------------------------------------------------------------
//----------------------------- END SETTINGS ------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------

	String pw_server_exp = "?";
	String pw_server_sp = "?";
	String pw_server_drop = "?";
	String pw_server_coins = "?";

	BufferedReader bfr = new BufferedReader(new FileReader(pw_server_path + "gamed/ptemplate.conf"));

	String row;
	while((row = bfr.readLine()) != null)
	{
		row = row.replaceAll("\\s", "");
		if(row.indexOf("exp_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_exp = row.substring(10, pos);
		}
		if(row.indexOf("sp_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_sp = row.substring(9, pos);
		}
		if(row.indexOf("drop_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_drop = row.substring(11, pos);
		}
		if(row.indexOf("money_bonus=") != -1)
		{
			int pos = row.length();
			if(row.indexOf("#") != -1)
			{
				pos = row.indexOf("#");
			}
			pw_server_coins = row.substring(12, pos);
		}
	}

	bfr.close();

	if(request.getSession().getAttribute("items") == null)
	{
		String[] items = new String[30001];

		try
		{
			bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/default.dat")), "UTF8"));
			if(item_labels.compareTo("my") == 0)
			{
				bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/my.dat")), "UTF8"));
			}
			if(item_labels.compareTo("pwi") == 0)
			{
				bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(request.getRealPath("/include/items") + "/pwi.dat")), "UTF8"));
			}
			int count = 0;
			while((row = bfr.readLine()) != null && count < 30001)
			{
				items[count] = row;
				count++;
			}
			bfr.close();
		}
		catch(Exception e)
		{
		}

		request.getSession().setAttribute("items", items);
	}

      out.write('\n');
      out.write('\n');
      out.write('\n');

	int unitprice = 10000;

      out.write("\n");
      out.write("\n");
      out.write("<html>\n");
      out.write("\n");
      out.write("<head>\n");
      out.write("\t<link rel=\"shortcut icon\" href=\"../../include/fav.ico\">\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../include/style.css\">\n");
      out.write("\n");
      out.write("\t<script type=\"text/javascript\" language=\"JavaScript\">\n");
      out.write("\t\tfunction updatePrice()\n");
      out.write("\t\t{\n");
      out.write("\t\t\tthis.addcubi.price.value = this.addcubi.cubi.value * ");
      out.print( unitprice );
      out.write(";\n");
      out.write("\t\t}\n");
      out.write("\t</script>\n");
      out.write("</head>\n");
      out.write("\n");
      out.write("<body>\n");
      out.write("\n");
      out.write("<table align=\"center\" width=\"400\" cellpadding=\"2\" cellspacing=\"0\" style=\"border: 1px solid #cccccc;\">\n");
      out.write("\n");
      out.write("\t<tr>\n");
      out.write("\t\t<th align=\"center\" style=\"padding: 5;\">\n");
      out.write("\t\t\t<b>CUBI SHOP</b>\n");
      out.write("\t\t</th>\n");
      out.write("\t</tr>\n");
      out.write("\n");
      out.write("\t<tr bgcolor=\"#f0f0f0\">\n");
      out.write("\t\t<td align=\"right\">\n");
      out.write("\t\t\t");

				pageContext.include("login.jsp");
			
      out.write("\n");
      out.write("\t\t</td>\n");
      out.write("\t</tr>\n");
      out.write("\n");
      out.write("\t<tr>\n");
      out.write("\t\t<td align=\"center\" valign=\"middle\">\n");
      out.write("\t\t\t");

				if(request.getSession().getAttribute("user") != null)
				{
					int uid = Integer.parseInt((String)request.getSession().getAttribute("user"));

					Class.forName("com.mysql.jdbc.Driver").newInstance();
					Connection connection = DriverManager.getConnection("jdbc:mysql://" + db_host + ":" + db_port + "/" + db_database, db_user, db_password);
					Statement statement = connection.createStatement();
					ResultSet rs = statement.executeQuery("SELECT zoneid FROM point WHERE uid='" + uid + "'");

					if(rs.next() && rs.getString("zoneid") != null)
					{
						out.print(" <font color=\"#ee0000\"><b>Disconnect from the game to purchase Cubi with Coins!</b></font><br><a href=\"index.jsp\">Reload</a>");
					}
					else
					{
						int roleid = uid;
						if(request.getParameter("roleid") != null)
						{
							roleid = Integer.parseInt(request.getParameter("roleid"));
						}

						RoleBean character = GameDB.get(roleid);
						int max_cubi = character.pocket.money/unitprice;

						if(request.getParameter("cubi") != null)
						{
							int cubi_amount = Integer.parseInt(request.getParameter("cubi"));

							if(cubi_amount < 1 || cubi_amount > max_cubi)
							{
								out.println("<font color=\"#ff0000\"><b>Invalid Cubi Amount!</b></font>");
							}
							else
							{
								// transfer cubi to account
								rs = statement.executeQuery("call usecash ( '" + uid + "' , 1, 0, 1, 0, '" + 100*cubi_amount + "', 1, @error)");
								
								// remove coins from character
								character.pocket.money = character.pocket.money - (cubi_amount*unitprice);
								GameDB.update(character);

								out.println("<font color=\"#00ee00\"><b>" + cubi_amount + " Cubi Purchased<br>Transaction may take up to 5 Minutes<br>Relog Required!</b></font>");
							}
						}

						out.println("<table width=\"300\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">");
						out.println("<form name=\"changerole\" action=\"index.jsp\" method=\"post\">");
						out.println("<tr><td><font color=\"#000000\"><b>Selected Character: </b></font></td><td><select name=\"roleid\" onchange=\"document['changerole'].submit();\" style=\"width: 100%; text-align: center;\">");

						// Get all character of current userid
						DataVector dv = GameDB.getRolelist(uid);
						if(dv != null)
						{
							Iterator itr = dv.iterator();
							while(itr.hasNext())
							{
								IntOctets ios = (IntOctets)itr.next();
								int role = ios.m_int;
								String rolename = ios.m_octets.getString();
								if(role == roleid)
								{
									out.println("<option value=\"" + role + "\" selected>" + rolename + "</option>");
								}
								else
								{
									out.println("<option value=\"" + role + "\">" + rolename + "</option>");
								}
							}	
						}
						out.println("</select></td></tr>");
						out.println("</form>");

						out.println("<tr><td><font color=\"#000000\"><b>Available Coins: </b></font></td><td>" + character.pocket.money + "</td></tr>");
						out.println("<tr><td><font color=\"#000000\"><b>Price / Unit: </b></font></td><td>" + unitprice + " Coins / Cubi</td></tr>");
						out.println("<form name=\"addcubi\" action=\"index.jsp?roleid=" + roleid + "\" method=\"post\">");
						out.println("<tr><td><font color=\"#000000\"><b>Purchase Cubi(s): </b></font></td><td><input type=\"number\" name=\"cubi\" value=\"0\" min=\"0\" max=\"" + max_cubi + "\" onChange=\"javascript:updatePrice()\" style=\"width: 100%; text-align: center;\"></input></td></tr>");
						out.println("<tr><td><font color=\"#000000\"><b>Overall Price: </b></font></td><td><input type=\"number\" name=\"price\" disabled style=\"width: 100%; text-align: center;\"></input></td></tr>");
						out.println("<tr><td align=\"center\" colspan=\"2\"><input type=\"image\" src=\"../../include/btn_submit.jpg\" style=\"border: 0px; vertical-align: middle;\"></input></td></tr>");
						out.println("</form>");

						out.println("</table>");
					}
					rs.close();
					connection.close();
				}
				else
				{
					out.println("<font color=\"#ee0000\"><b>Access Denied!</b></font>");
				}
			
      out.write("\n");
      out.write("\t\t</td>\n");
      out.write("\t</tr>\n");
      out.write("\n");
      out.write("</table>\n");
      out.write("\n");
      out.write("</body>\n");
      out.write("\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
