package com.zerno.main;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.zerno.helper.Globals.ErrorCodes;
import com.zerno.helper.SendError;
import com.zerno.helper.Validators;
public class CreateCharacter extends BaseClientRequestHandler {
@Override
public void handleClientRequest(User user, ISFSObject params) {
// create objects
SFSExtension ext = getParentExtension();
IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
Connection cn = null;
// create vars
int acc_id = user.getVariable("acc_id").getIntValue();
String className = this.getClass().getSimpleName();
int char_count_max = 0;
// create vars from params
String name = "";
String second_name = "";
int gender = 0;
int race = 0;
int map = 0;
int location_x = 0;
int location_y = 0;
int location_a = 0;
String look = "";
try{ name = params.getUtfString("name"); }
catch(Exception e)
{
// send name string error
new SendError(ext, className, user, ErrorCodes.INVALID_CHAR_NAME_STRING, e.getMessage());
return;
}
try{ second_name = params.getUtfString("second_name"); }
catch(Exception e)
{
// send name string error
new SendError(ext, className, user, ErrorCodes.INVALID_CHAR_SECOND_NAME_STRING, e.getMessage());
return;
}
try { gender = params.getInt("gender"); }
catch(Exception e)
{
// send gender int error
new SendError(ext, className, user, ErrorCodes.INVALID_GENDER_INTEGER, e.getMessage());
return;
}
try { race = params.getInt("race"); }
catch(Exception e)
{
// send race int error
new SendError(ext, className, user, ErrorCodes.INVALID_RACE_INTEGER, e.getMessage());
return;
}
try { look = params.getSFSObject("look").toJson(); }
catch(Exception e)
{
// send look string error
new SendError(ext, className, user, ErrorCodes.INVALID_LOOK_OBJECT, e.getMessage());
return;
}
// check name string
if(!Validators.validate(ErrorCodes.INVALID_CHAR_NAME_STRING, name))
{
// send name string error
new SendError(ext, className, user, ErrorCodes.INVALID_CHAR_NAME_STRING, "");
return;
}
// check second_name string
if(!Validators.validate(ErrorCodes.INVALID_CHAR_SECOND_NAME_STRING, second_name))
{
// send name string error
new SendError(ext, className, user, ErrorCodes.INVALID_CHAR_SECOND_NAME_STRING, "");
return;
}
// check gender int
if (!Validators.validate(ErrorCodes.INVALID_GENDER_INTEGER, gender))
{
// send gender int error
new SendError(ext, className, user, ErrorCodes.INVALID_GENDER_INTEGER, "");
return;
}
// check race int
if(!Validators.validate(ErrorCodes.INVALID_RACE_INTEGER, race))
{
// send race int error
new SendError(ext, className, user, ErrorCodes.INVALID_RACE_INTEGER, "");
return;
}
// create SQL connection
try
{
cn = dbManager.getConnection();
PreparedStatement st = null;
ResultSet rs = null;
// get char_count_max
try
{
// create SQL query
st = cn.prepareStatement("SELECT `char_count_max` FROM `user_accounts` where `acc_id` = ? ");
st.setInt(1, acc_id);
rs = st.executeQuery();
// verify that a record was found
if (rs.first())
{
// set var
char_count_max = rs.getInt("char_count_max");
}
else
{
// send unknown acc_id error
new SendError(ext, className, user, ErrorCodes.INVALID_ACCOUNTID, "");
return;
}
}
catch (SQLException e)
{
// send SQL error
new SendError(ext, className, user, ErrorCodes.SQL_ERROR, e.getMessage());
return;
}
finally
{
// close statement and result
try { if( rs != null ) rs.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
try { if( st != null ) st.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
}
// get character count
try
{
// create SQL query
st = cn.prepareStatement("SELECT COUNT(*) FROM `user_characters` where `acc_id` = ? ");
st.setInt(1, acc_id);
rs = st.executeQuery();
// verify that record was found
if (rs.first())
{
// check character space
if(rs.getInt("COUNT(*)") >= char_count_max)
{
// send character space error
new SendError(ext, className, user, ErrorCodes.NOT_ENOUGH_CHARACTER_PLACES, "");
return;
}
}
}
catch (SQLException e)
{
// send SQL error
new SendError(ext, className, user, ErrorCodes.SQL_ERROR, e.getMessage());
return;
}
finally
{
// close statement and result
try { if( rs != null ) rs.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
try { if( st != null ) st.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
}
// get char_count_max
try
{
// create SQL query
st = cn.prepareStatement("SELECT * FROM `race_positions` where `race_id` = ? ");
st.setInt(1, race);
rs = st.executeQuery();
// verify that a record was found
if (rs.first())
{
// set var
map = rs.getInt("map");
location_x = rs.getInt("location_x");
location_y = rs.getInt("location_y");
location_a = rs.getInt("location_a");
}
else
{
// send unknown acc_id error
new SendError(ext, className, user, ErrorCodes.INVALID_RACE, "");
return;
}
}
catch (SQLException e)
{
// send SQL error
new SendError(ext, className, user, ErrorCodes.SQL_ERROR, e.getMessage());
return;
}
finally
{
// close statement and result
try { if( rs != null ) rs.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
try { if( st != null ) st.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
}
// insert character
try
{
// create SQL query
st = cn.prepareStatement("INSERT into `user_characters`(`acc_id`, `name`, `second_name`, `gender`, `race`, `current_map`, `location_x`, `location_y`, `location_a`, `look`) values ( ? , ? , ?, ?, ?, ?, ?, ?, ?, ? )");
st.setInt(1, acc_id);
st.setString(2, name);
st.setString(3, second_name);
st.setInt(4, gender);
st.setInt(5, race);
st.setInt(6, map);
st.setInt(7, location_x);
st.setInt(8, location_y);
st.setInt(9, location_a);
st.setString(10, look);
st.executeUpdate();
}
catch (SQLException e)
{
// send SQL error
new SendError(ext, className, user, ErrorCodes.SQL_ERROR, e.getMessage());
return;
}
finally
{
// close statement and result
try { if( rs != null ) rs.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
try { if( st != null ) st.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
}
}
catch (SQLException e)
{
// send SQL error
new SendError(ext, className, user, ErrorCodes.SQL_ERROR, e.getMessage());
return;
}
finally
{
//close connection
try { if( cn != null ) cn.close(); } catch( Exception e ) {new SendError(ext, className, user, 0, e.getMessage());}
}
// send success
send(className + "Success", null, user);
trace(ExtensionLogLevel.INFO, className + ": " + acc_id);
}
}
[/code:3acd9izm]
My extension file list:
[img="https://dl.dropboxusercontent.com/u/95601836/javalist.png"]