2015-08-26 13:38:58 -04:00
using FFXIVClassic_Lobby_Server.dataobjects ;
using MySql.Data.MySqlClient ;
using System ;
using System.Collections.Generic ;
2016-03-20 11:49:09 -04:00
using FFXIVClassic_Lobby_Server.utils ;
2015-08-26 13:38:58 -04:00
namespace FFXIVClassic_Lobby_Server
{
2015-09-13 18:21:28 -04:00
//charState: 0 - Reserved, 1 - Inactive, 2 - Active
2015-08-26 13:38:58 -04:00
class Database
{
2016-06-14 21:29:10 +01:00
public static uint GetUserIdFromSession ( String sessionId )
2015-08-26 13:38:58 -04:00
{
2015-08-27 10:19:00 -04:00
uint id = 0 ;
2015-08-26 13:38:58 -04:00
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
2015-09-09 00:08:46 -04:00
MySqlCommand cmd = new MySqlCommand ( "SELECT * FROM sessions WHERE id = @sessionId AND expiration > NOW()" , conn ) ;
2015-08-27 10:19:00 -04:00
cmd . Parameters . AddWithValue ( "@sessionId" , sessionId ) ;
using ( MySqlDataReader Reader = cmd . ExecuteReader ( ) )
{
while ( Reader . Read ( ) )
{
id = Reader . GetUInt32 ( "userId" ) ;
}
}
}
catch ( MySqlException e )
2016-06-08 22:29:04 +01:00
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
}
2015-08-27 10:19:00 -04:00
finally
{
2015-09-09 00:08:46 -04:00
conn . Dispose ( ) ;
2015-09-13 18:21:28 -04:00
}
2015-08-27 10:19:00 -04:00
}
return id ;
}
2016-06-14 21:29:10 +01:00
public static bool ReserveCharacter ( uint userId , uint slot , uint serverId , String name , out uint pid , out uint cid )
2015-08-27 10:19:00 -04:00
{
bool alreadyExists = false ;
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
//Check if exists
2016-02-20 00:11:51 -05:00
MySqlCommand cmd = new MySqlCommand ( "SELECT * FROM characters WHERE name=@name AND serverId=@serverId AND state != 2 AND state != 1" , conn ) ;
2015-08-26 13:38:58 -04:00
cmd . Parameters . AddWithValue ( "@serverId" , serverId ) ;
cmd . Parameters . AddWithValue ( "@name" , name ) ;
2015-08-27 10:19:00 -04:00
using ( MySqlDataReader Reader = cmd . ExecuteReader ( ) )
{
if ( Reader . HasRows )
2015-09-08 00:42:02 -04:00
{
alreadyExists = true ;
}
2015-08-27 10:19:00 -04:00
}
//Reserve
if ( ! alreadyExists )
{
MySqlCommand cmd2 = new MySqlCommand ( ) ;
cmd2 . Connection = conn ;
2015-09-09 00:08:46 -04:00
cmd2 . CommandText = "INSERT INTO characters(userId, slot, serverId, name, state) VALUES(@userId, @slot, @serverId, @name, 0)" ;
2015-08-27 10:19:00 -04:00
cmd2 . Prepare ( ) ;
cmd2 . Parameters . AddWithValue ( "@userId" , userId ) ;
cmd2 . Parameters . AddWithValue ( "@slot" , slot ) ;
cmd2 . Parameters . AddWithValue ( "@serverId" , serverId ) ;
cmd2 . Parameters . AddWithValue ( "@name" , name ) ;
cmd2 . ExecuteNonQuery ( ) ;
2015-09-09 00:08:46 -04:00
cid = ( ushort ) cmd2 . LastInsertedId ;
pid = 0xBABE ;
2015-09-08 00:42:02 -04:00
}
else
{
pid = 0 ;
cid = 0 ;
}
2015-08-26 13:38:58 -04:00
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2015-09-08 00:42:02 -04:00
pid = 0 ;
cid = 0 ;
2015-08-26 13:38:58 -04:00
}
finally
{
2015-09-09 00:08:46 -04:00
conn . Dispose ( ) ;
2015-08-26 13:38:58 -04:00
}
2015-09-13 18:21:28 -04:00
2016-06-14 21:29:10 +01:00
Program . Log . Debug ( "[SQL] CID={0} Created on 'characters' table." , cid ) ;
2015-08-26 13:38:58 -04:00
}
2015-08-27 10:19:00 -04:00
return alreadyExists ;
}
2016-06-14 21:29:10 +01:00
public static void MakeCharacter ( uint accountId , uint cid , CharaInfo charaInfo )
2015-08-26 13:38:58 -04:00
{
2015-10-05 19:37:03 -04:00
//Update character entry
2015-08-26 13:38:58 -04:00
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
MySqlCommand cmd = new MySqlCommand ( ) ;
cmd . Connection = conn ;
2016-02-20 00:11:51 -05:00
cmd . CommandText = @ "
UPDATE characters SET
state = 2 ,
currentZoneId = @zoneId ,
positionX = @x ,
positionY = @y ,
positionZ = @z ,
rotation = @r ,
guardian = @guardian ,
birthDay = @birthDay ,
birthMonth = @birthMonth ,
initialTown = @initialTown ,
2016-03-20 11:49:09 -04:00
tribe = @tribe
2016-02-20 00:11:51 -05:00
WHERE userId = @userId AND id = @cid ;
INSERT INTO characters_appearance
2016-03-20 12:18:41 -04:00
( characterId , baseId , size , voice , skinColor , hairStyle , hairColor , hairHighlightColor , hairVariation , eyeColor , faceType , faceEyebrows , faceEyeShape , faceIrisSize , faceNose , faceMouth , faceFeatures , ears , characteristics , characteristicsColor , mainhand , offhand , head , body , hands , legs , feet , waist )
2016-02-20 00:11:51 -05:00
VALUES
2016-03-20 12:18:41 -04:00
( @cid , 4294967295 , @size , @voice , @skinColor , @hairStyle , @hairColor , @hairHighlightColor , @hairVariation , @eyeColor , @faceType , @faceEyebrows , @faceEyeShape , @faceIrisSize , @faceNose , @faceMouth , @faceFeatures , @ears , @characteristics , @characteristicsColor , @mainhand , @offhand , @head , @body , @hands , @legs , @feet , @waist )
2016-02-20 00:11:51 -05:00
";
2015-09-09 00:08:46 -04:00
cmd . Parameters . AddWithValue ( "@userId" , accountId ) ;
cmd . Parameters . AddWithValue ( "@cid" , cid ) ;
2016-02-20 00:11:51 -05:00
cmd . Parameters . AddWithValue ( "@guardian" , charaInfo . guardian ) ;
cmd . Parameters . AddWithValue ( "@birthDay" , charaInfo . birthDay ) ;
cmd . Parameters . AddWithValue ( "@birthMonth" , charaInfo . birthMonth ) ;
cmd . Parameters . AddWithValue ( "@initialTown" , charaInfo . initialTown ) ;
cmd . Parameters . AddWithValue ( "@tribe" , charaInfo . tribe ) ;
cmd . Parameters . AddWithValue ( "@zoneId" , charaInfo . zoneId ) ;
cmd . Parameters . AddWithValue ( "@x" , charaInfo . x ) ;
cmd . Parameters . AddWithValue ( "@y" , charaInfo . y ) ;
cmd . Parameters . AddWithValue ( "@z" , charaInfo . z ) ;
cmd . Parameters . AddWithValue ( "@r" , charaInfo . rot ) ;
cmd . Parameters . AddWithValue ( "@size" , charaInfo . appearance . size ) ;
cmd . Parameters . AddWithValue ( "@voice" , charaInfo . appearance . voice ) ;
cmd . Parameters . AddWithValue ( "@skinColor" , charaInfo . appearance . skinColor ) ;
cmd . Parameters . AddWithValue ( "@hairStyle" , charaInfo . appearance . hairStyle ) ;
cmd . Parameters . AddWithValue ( "@hairColor" , charaInfo . appearance . hairColor ) ;
cmd . Parameters . AddWithValue ( "@hairHighlightColor" , charaInfo . appearance . hairHighlightColor ) ;
2016-03-20 12:18:41 -04:00
cmd . Parameters . AddWithValue ( "@hairVariation" , charaInfo . appearance . hairVariation ) ;
2016-02-20 00:11:51 -05:00
cmd . Parameters . AddWithValue ( "@eyeColor" , charaInfo . appearance . eyeColor ) ;
cmd . Parameters . AddWithValue ( "@faceType" , charaInfo . appearance . faceType ) ;
cmd . Parameters . AddWithValue ( "@faceEyebrows" , charaInfo . appearance . faceEyebrows ) ;
cmd . Parameters . AddWithValue ( "@faceEyeShape" , charaInfo . appearance . faceEyeShape ) ;
cmd . Parameters . AddWithValue ( "@faceIrisSize" , charaInfo . appearance . faceIrisSize ) ;
cmd . Parameters . AddWithValue ( "@faceNose" , charaInfo . appearance . faceNose ) ;
cmd . Parameters . AddWithValue ( "@faceMouth" , charaInfo . appearance . faceMouth ) ;
cmd . Parameters . AddWithValue ( "@faceFeatures" , charaInfo . appearance . faceFeatures ) ;
cmd . Parameters . AddWithValue ( "@ears" , charaInfo . appearance . ears ) ;
cmd . Parameters . AddWithValue ( "@characteristics" , charaInfo . appearance . characteristics ) ;
cmd . Parameters . AddWithValue ( "@characteristicsColor" , charaInfo . appearance . characteristicsColor ) ;
2016-02-20 11:20:59 -05:00
cmd . Parameters . AddWithValue ( "@mainhand" , charaInfo . weapon1 ) ;
cmd . Parameters . AddWithValue ( "@offhand" , charaInfo . weapon2 ) ;
cmd . Parameters . AddWithValue ( "@head" , charaInfo . head ) ;
cmd . Parameters . AddWithValue ( "@body" , charaInfo . body ) ;
cmd . Parameters . AddWithValue ( "@legs" , charaInfo . legs ) ;
2016-03-20 11:49:09 -04:00
cmd . Parameters . AddWithValue ( "@hands" , charaInfo . hands ) ;
2016-02-20 11:20:59 -05:00
cmd . Parameters . AddWithValue ( "@feet" , charaInfo . feet ) ;
2016-03-05 18:02:37 -05:00
cmd . Parameters . AddWithValue ( "@waist" , charaInfo . belt ) ;
2016-03-20 11:49:09 -04:00
2015-08-26 13:38:58 -04:00
cmd . ExecuteNonQuery ( ) ;
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2016-03-20 11:49:09 -04:00
conn . Dispose ( ) ;
return ;
2015-08-26 13:38:58 -04:00
}
finally
{
}
2015-09-13 18:21:28 -04:00
2015-10-05 19:37:03 -04:00
2016-03-20 11:49:09 -04:00
//Create Level and EXP entries
2015-10-05 19:37:03 -04:00
try
{
MySqlCommand cmd = new MySqlCommand ( ) ;
cmd . Connection = conn ;
2016-03-20 11:49:09 -04:00
cmd . CommandText = String . Format ( "INSERT INTO characters_class_levels(characterId, {0}) VALUES(@characterId, 1); INSERT INTO characters_class_exp(characterId) VALUES(@characterId)" , CharacterCreatorUtils . GetClassNameForId ( ( short ) charaInfo . currentClass ) ) ;
2015-10-05 19:37:03 -04:00
cmd . Prepare ( ) ;
cmd . Parameters . AddWithValue ( "@characterId" , cid ) ;
2016-03-20 11:49:09 -04:00
cmd . ExecuteNonQuery ( ) ;
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2016-03-20 11:49:09 -04:00
conn . Dispose ( ) ;
return ;
}
//Create Parameter Save
try
{
MySqlCommand cmd = new MySqlCommand ( ) ;
cmd . Connection = conn ;
2017-10-10 13:32:47 -05:00
cmd . CommandText = String . Format ( "INSERT INTO characters_parametersave(characterId, hp, hpMax, mp, mpMax, mainSkill, mainSkillLevel) VALUES(@characterId, 1900, 1000, 115, 115, @mainSkill, 1);" , CharacterCreatorUtils . GetClassNameForId ( ( short ) charaInfo . currentClass ) ) ;
2016-03-20 11:49:09 -04:00
cmd . Prepare ( ) ;
cmd . Parameters . AddWithValue ( "@characterId" , cid ) ;
cmd . Parameters . AddWithValue ( "@mainSkill" , charaInfo . currentClass ) ;
2015-10-05 19:37:03 -04:00
cmd . ExecuteNonQuery ( ) ;
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2017-09-30 07:28:08 -05:00
conn . Dispose ( ) ;
return ;
}
//Create Hotbar
try
{
MySqlCommand cmd = new MySqlCommand ( ) ;
cmd . Connection = conn ;
cmd . CommandText = "SELECT id FROM server_battle_commands WHERE classJob = @classjob AND lvl = 1 ORDER BY id DESC" ;
cmd . Prepare ( ) ;
cmd . Parameters . AddWithValue ( "@classJob" , charaInfo . currentClass ) ;
List < uint > defaultActions = new List < uint > ( ) ;
using ( var reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
defaultActions . Add ( reader . GetUInt32 ( "id" ) ) ;
}
}
MySqlCommand cmd2 = new MySqlCommand ( ) ;
cmd2 . Connection = conn ;
cmd2 . CommandText = "INSERT INTO characters_hotbar (characterId, classId, hotbarSlot, commandId, recastTime) VALUES (@characterId, @classId, @hotbarSlot, @commandId, 0)" ;
cmd2 . Prepare ( ) ;
cmd2 . Parameters . AddWithValue ( "@characterId" , cid ) ;
cmd2 . Parameters . AddWithValue ( "@classId" , charaInfo . currentClass ) ;
cmd2 . Parameters . Add ( "@hotbarSlot" , MySqlDbType . Int16 ) ;
cmd2 . Parameters . Add ( "@commandId" , MySqlDbType . Int16 ) ;
2015-10-05 19:37:03 -04:00
2017-09-30 07:28:08 -05:00
for ( int i = 0 ; i < defaultActions . Count ; i + + )
{
cmd2 . Parameters [ "@hotbarSlot" ] . Value = i ;
cmd2 . Parameters [ "@commandId" ] . Value = defaultActions [ i ] ;
cmd2 . ExecuteNonQuery ( ) ;
}
}
catch ( MySqlException e )
{
Program . Log . Error ( e . ToString ( ) ) ;
2015-10-05 19:37:03 -04:00
}
finally
{
conn . Dispose ( ) ;
}
}
2016-03-20 11:49:09 -04:00
2016-06-14 05:09:30 +01:00
Program . Log . Debug ( "[SQL] CID={0} state updated to active(2)." , cid ) ;
2015-08-26 13:38:58 -04:00
}
2016-06-14 21:29:10 +01:00
public static bool RenameCharacter ( uint userId , uint characterId , uint serverId , String newName )
2015-08-26 13:38:58 -04:00
{
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
2015-09-13 11:30:33 -04:00
//Check if exists
MySqlCommand cmd = new MySqlCommand ( "SELECT * FROM characters WHERE name=@name AND serverId=@serverId" , conn ) ;
cmd . Parameters . AddWithValue ( "@serverId" , serverId ) ;
cmd . Parameters . AddWithValue ( "@name" , newName ) ;
using ( MySqlDataReader Reader = cmd . ExecuteReader ( ) )
{
if ( Reader . HasRows )
{
return true ;
}
}
cmd = new MySqlCommand ( ) ;
2015-08-26 13:38:58 -04:00
cmd . Connection = conn ;
2016-06-14 21:29:10 +01:00
cmd . CommandText = "UPDATE characters SET name=@name, DoRename=0 WHERE id=@cid AND userId=@uid" ;
2015-08-26 13:38:58 -04:00
cmd . Prepare ( ) ;
2015-09-13 11:30:33 -04:00
cmd . Parameters . AddWithValue ( "@uid" , userId ) ;
2015-08-26 13:38:58 -04:00
cmd . Parameters . AddWithValue ( "@cid" , characterId ) ;
cmd . Parameters . AddWithValue ( "@name" , newName ) ;
cmd . ExecuteNonQuery ( ) ;
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2015-08-26 13:38:58 -04:00
}
finally
{
2015-09-09 00:08:46 -04:00
conn . Dispose ( ) ;
2015-08-26 13:38:58 -04:00
}
2015-09-13 11:30:33 -04:00
2016-06-14 05:09:30 +01:00
Program . Log . Debug ( "[SQL] CID={0} name updated to \"{1}\"." , characterId , newName ) ;
2015-09-13 18:21:28 -04:00
2015-09-13 11:30:33 -04:00
return false ;
2015-08-26 13:38:58 -04:00
}
}
2016-06-14 21:29:10 +01:00
public static void DeleteCharacter ( uint characterId , String name )
2015-08-26 13:38:58 -04:00
{
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
MySqlCommand cmd = new MySqlCommand ( ) ;
cmd . Connection = conn ;
2016-02-20 00:11:51 -05:00
cmd . CommandText = "UPDATE characters SET state=1 WHERE id=@cid AND name=@name" ;
2015-08-26 13:38:58 -04:00
cmd . Prepare ( ) ;
cmd . Parameters . AddWithValue ( "@cid" , characterId ) ;
cmd . Parameters . AddWithValue ( "@name" , name ) ;
cmd . ExecuteNonQuery ( ) ;
}
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2016-06-08 22:29:04 +01:00
2015-08-26 13:38:58 -04:00
}
finally
{
2015-09-09 00:08:46 -04:00
conn . Dispose ( ) ;
2015-08-26 13:38:58 -04:00
}
}
2015-09-13 18:21:28 -04:00
2016-06-14 05:09:30 +01:00
Program . Log . Debug ( "[SQL] CID={0} deleted." , characterId ) ;
2015-08-26 13:38:58 -04:00
}
2016-06-14 21:29:10 +01:00
public static List < World > GetServers ( )
2015-08-26 13:38:58 -04:00
{
2018-10-20 12:08:47 -04:00
string query ;
MySqlCommand cmd ;
List < World > worldList = new List < World > ( ) ;
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
2015-08-26 13:38:58 -04:00
{
try
{
conn . Open ( ) ;
2018-10-20 12:08:47 -04:00
query = "SELECT * FROM servers WHERE isActive=true" ;
cmd = new MySqlCommand ( query , conn ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
ushort id ;
string address ;
ushort port ;
ushort listPosition ;
ushort population ;
string name ;
bool isActive ;
id = reader . GetUInt16 ( "id" ) ;
address = reader . GetString ( "address" ) ;
port = reader . GetUInt16 ( "port" ) ;
listPosition = reader . GetUInt16 ( "listPosition" ) ;
2018-10-20 12:31:24 -04:00
population = 2 ;
2018-10-20 12:08:47 -04:00
name = reader . GetString ( "name" ) ;
isActive = reader . GetBoolean ( "isActive" ) ;
worldList . Add ( new World ( id , address , port , listPosition , population , name , isActive ) ) ;
}
}
2015-08-26 13:38:58 -04:00
}
catch ( MySqlException e )
2016-06-08 22:29:04 +01:00
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2018-10-20 12:08:47 -04:00
worldList = new List < World > ( ) ;
}
2015-08-26 13:38:58 -04:00
finally
2018-10-20 12:08:47 -04:00
{
2015-09-09 00:08:46 -04:00
conn . Dispose ( ) ;
2015-08-26 13:38:58 -04:00
}
}
2018-10-20 12:08:47 -04:00
return worldList ;
2015-08-26 13:38:58 -04:00
}
2016-06-14 21:29:10 +01:00
public static World GetServer ( uint serverId )
2015-08-26 13:38:58 -04:00
{
2018-10-20 12:08:47 -04:00
string query ;
MySqlCommand cmd ;
World world = null ;
using ( MySqlConnection conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
2015-08-26 13:38:58 -04:00
{
try
{
conn . Open ( ) ;
2018-10-20 12:08:47 -04:00
query = "SELECT * FROM servers WHERE id=@ServerId" ;
cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@ServerId" , serverId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
ushort id ;
string address ;
ushort port ;
ushort listPosition ;
ushort population ;
string name ;
bool isActive ;
id = reader . GetUInt16 ( "id" ) ;
address = reader . GetString ( "address" ) ;
port = reader . GetUInt16 ( "port" ) ;
listPosition = reader . GetUInt16 ( "listPosition" ) ;
2018-10-20 12:31:24 -04:00
population = 2 ; //TODO
2018-10-20 12:08:47 -04:00
name = reader . GetString ( "name" ) ;
isActive = reader . GetBoolean ( "isActive" ) ;
world = new World ( id , address , port , listPosition , population , name , isActive ) ;
}
}
2015-08-26 13:38:58 -04:00
}
catch ( MySqlException e )
2016-06-08 22:29:04 +01:00
{
2018-10-20 12:08:47 -04:00
Program . Log . Error ( e . ToString ( ) ) ;
2015-08-26 13:38:58 -04:00
}
finally
{
2015-09-08 00:42:02 -04:00
conn . Dispose ( ) ;
2015-08-26 13:38:58 -04:00
}
}
2018-10-20 12:08:47 -04:00
return world ;
2015-08-26 13:38:58 -04:00
}
2015-08-27 10:19:00 -04:00
2016-06-14 21:29:10 +01:00
public static List < Character > GetCharacters ( uint userId )
2015-09-09 00:08:46 -04:00
{
2016-03-20 11:49:09 -04:00
List < Character > characters = new List < Character > ( ) ;
2015-09-09 00:08:46 -04:00
using ( var conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
2016-03-20 11:49:09 -04:00
conn . Open ( ) ;
//Load basic info
string query = @ "
SELECT
id ,
slot ,
serverId ,
name ,
isLegacy ,
2016-06-15 00:08:05 +01:00
doRename ,
2016-03-20 11:49:09 -04:00
currentZoneId ,
guardian ,
birthMonth ,
birthDay ,
initialTown ,
tribe ,
mainSkill ,
mainSkillLevel
FROM characters
INNER JOIN characters_parametersave ON id = characters_parametersave . characterId
WHERE userId = @userId AND state = 2
ORDER BY slot ";
MySqlCommand cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@userId" , userId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
2015-09-09 00:08:46 -04:00
{
2016-03-20 11:49:09 -04:00
while ( reader . Read ( ) )
{
Character chara = new Character ( ) ;
chara . id = reader . GetUInt32 ( "id" ) ;
chara . slot = reader . GetUInt16 ( "slot" ) ;
chara . serverId = reader . GetUInt16 ( "serverId" ) ;
chara . name = reader . GetString ( "name" ) ;
chara . isLegacy = reader . GetBoolean ( "isLegacy" ) ;
chara . doRename = reader . GetBoolean ( "doRename" ) ;
chara . currentZoneId = reader . GetUInt32 ( "currentZoneId" ) ;
chara . guardian = reader . GetByte ( "guardian" ) ;
chara . birthMonth = reader . GetByte ( "birthMonth" ) ;
chara . birthDay = reader . GetByte ( "birthDay" ) ;
chara . initialTown = reader . GetByte ( "initialTown" ) ;
chara . tribe = reader . GetByte ( "tribe" ) ;
chara . currentClass = reader . GetByte ( "mainSkill" ) ;
//chara.currentJob = ???
chara . currentLevel = reader . GetInt16 ( "mainSkillLevel" ) ;
characters . Add ( chara ) ;
}
2015-09-09 00:08:46 -04:00
}
2016-03-20 11:49:09 -04:00
2015-09-09 00:08:46 -04:00
}
2016-03-20 11:49:09 -04:00
return characters ;
2015-09-09 00:08:46 -04:00
}
2016-06-14 21:29:10 +01:00
public static Character GetCharacter ( uint userId , uint charId )
2015-09-13 11:30:33 -04:00
{
2016-03-20 11:49:09 -04:00
Character chara = null ;
2015-09-13 11:30:33 -04:00
using ( var conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
2018-10-20 12:08:47 -04:00
try
{
conn . Open ( ) ;
2016-03-20 11:49:09 -04:00
2018-10-20 12:08:47 -04:00
string query = @ "
2016-03-20 11:49:09 -04:00
SELECT
id ,
slot ,
serverId ,
name ,
isLegacy ,
2016-06-15 00:08:05 +01:00
doRename ,
2016-03-20 11:49:09 -04:00
currentZoneId ,
guardian ,
birthMonth ,
birthDay ,
initialTown ,
tribe ,
mainSkill ,
mainSkillLevel
FROM characters
INNER JOIN characters_parametersave ON id = characters_parametersave . characterId
WHERE id = @charId ";
2018-10-20 12:08:47 -04:00
MySqlCommand cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@charId" , charId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
2016-03-20 11:49:09 -04:00
{
2018-10-20 12:08:47 -04:00
if ( reader . Read ( ) )
{
chara = new Character ( ) ;
chara . id = reader . GetUInt32 ( "id" ) ;
chara . slot = reader . GetUInt16 ( "slot" ) ;
chara . serverId = reader . GetUInt16 ( "serverId" ) ;
chara . name = reader . GetString ( "name" ) ;
chara . isLegacy = reader . GetBoolean ( "isLegacy" ) ;
chara . doRename = reader . GetBoolean ( "doRename" ) ;
chara . currentZoneId = reader . GetUInt32 ( "currentZoneId" ) ;
chara . guardian = reader . GetByte ( "guardian" ) ;
chara . birthMonth = reader . GetByte ( "birthMonth" ) ;
chara . birthDay = reader . GetByte ( "birthDay" ) ;
chara . initialTown = reader . GetByte ( "initialTown" ) ;
chara . tribe = reader . GetByte ( "tribe" ) ;
chara . currentClass = reader . GetByte ( "mainSkill" ) ;
//chara.currentJob = ???
chara . currentLevel = reader . GetInt16 ( "mainSkillLevel" ) ;
}
2016-03-20 11:49:09 -04:00
}
2015-09-13 11:30:33 -04:00
}
2015-10-05 19:37:03 -04:00
catch ( MySqlException e )
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2018-10-20 12:08:47 -04:00
2015-10-05 19:37:03 -04:00
}
finally
{
conn . Dispose ( ) ;
}
}
2018-10-20 12:08:47 -04:00
return chara ;
2015-10-05 19:37:03 -04:00
}
2018-10-20 12:08:47 -04:00
public static Appearance GetAppearance ( uint charaId )
2015-09-10 00:52:31 -04:00
{
2018-10-20 12:31:24 -04:00
Appearance appearance = new Appearance ( ) ;
2015-09-10 00:52:31 -04:00
using ( var conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
2018-10-20 12:08:47 -04:00
//Load appearance
string query = @ "
SELECT
baseId ,
size ,
voice ,
skinColor ,
hairStyle ,
hairColor ,
hairHighlightColor ,
eyeColor ,
characteristics ,
characteristicsColor ,
faceType ,
ears ,
faceMouth ,
faceFeatures ,
faceNose ,
faceEyeShape ,
faceIrisSize ,
faceEyebrows ,
mainHand ,
offHand ,
head ,
body ,
legs ,
hands ,
feet ,
waist ,
leftFinger ,
rightFinger ,
leftEar ,
rightEar
FROM characters_appearance WHERE characterId = @charaId ";
MySqlCommand cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@charaId" , charaId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
{
if ( reader . Read ( ) )
{
appearance . size = reader . GetByte ( "size" ) ;
appearance . voice = reader . GetByte ( "voice" ) ;
2018-10-20 12:31:24 -04:00
appearance . skinColor = reader . GetUInt16 ( "skinColor" ) ;
appearance . hairStyle = reader . GetUInt16 ( "hairStyle" ) ;
appearance . hairColor = reader . GetUInt16 ( "hairColor" ) ;
appearance . hairHighlightColor = reader . GetUInt16 ( "hairHighlightColor" ) ;
appearance . eyeColor = reader . GetUInt16 ( "eyeColor" ) ;
2018-10-20 12:08:47 -04:00
appearance . characteristics = reader . GetByte ( "characteristics" ) ;
appearance . characteristicsColor = reader . GetByte ( "characteristicsColor" ) ;
appearance . faceType = reader . GetByte ( "faceType" ) ;
appearance . ears = reader . GetByte ( "ears" ) ;
appearance . faceMouth = reader . GetByte ( "faceMouth" ) ;
appearance . faceFeatures = reader . GetByte ( "faceFeatures" ) ;
appearance . faceNose = reader . GetByte ( "faceNose" ) ;
appearance . faceEyeShape = reader . GetByte ( "faceEyeShape" ) ;
appearance . faceIrisSize = reader . GetByte ( "faceIrisSize" ) ;
appearance . faceEyebrows = reader . GetByte ( "faceEyebrows" ) ;
2018-10-20 12:31:24 -04:00
appearance . mainHand = reader . GetUInt32 ( "mainHand" ) ;
appearance . offHand = reader . GetUInt32 ( "offHand" ) ;
appearance . head = reader . GetUInt32 ( "head" ) ;
appearance . body = reader . GetUInt32 ( "body" ) ;
appearance . mainHand = reader . GetUInt32 ( "mainHand" ) ;
appearance . legs = reader . GetUInt32 ( "legs" ) ;
appearance . hands = reader . GetUInt32 ( "hands" ) ;
appearance . feet = reader . GetUInt32 ( "feet" ) ;
appearance . waist = reader . GetUInt32 ( "waist" ) ;
appearance . leftFinger = reader . GetUInt32 ( "leftFinger" ) ;
appearance . rightFinger = reader . GetUInt32 ( "rightFinger" ) ;
appearance . leftEar = reader . GetUInt32 ( "leftEar" ) ;
appearance . rightEar = reader . GetUInt32 ( "rightEar" ) ;
2018-10-20 12:08:47 -04:00
}
}
2015-09-10 00:52:31 -04:00
}
catch ( MySqlException e )
2016-06-08 22:29:04 +01:00
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2018-10-20 12:08:47 -04:00
}
2015-09-10 00:52:31 -04:00
finally
{
conn . Dispose ( ) ;
}
}
2018-10-20 12:08:47 -04:00
return appearance ;
2015-09-10 00:52:31 -04:00
}
2018-10-20 12:08:47 -04:00
public static List < String > GetReservedNames ( uint userId )
2015-09-10 00:52:31 -04:00
{
2018-10-20 12:08:47 -04:00
List < String > reservedNames = new List < String > ( ) ;
2015-09-10 00:52:31 -04:00
using ( var conn = new MySqlConnection ( String . Format ( "Server={0}; Port={1}; Database={2}; UID={3}; Password={4}" , ConfigConstants . DATABASE_HOST , ConfigConstants . DATABASE_PORT , ConfigConstants . DATABASE_NAME , ConfigConstants . DATABASE_USERNAME , ConfigConstants . DATABASE_PASSWORD ) ) )
{
try
{
conn . Open ( ) ;
2018-10-20 12:08:47 -04:00
string query = "SELECT name FROM reserved_names WHERE userId=@UserId" ;
MySqlCommand cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@UserId" , userId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
{
while ( reader . Read ( ) )
{
reservedNames . Add ( reader . GetString ( "name" ) ) ;
}
}
2015-09-10 00:52:31 -04:00
}
catch ( MySqlException e )
2016-06-08 22:29:04 +01:00
{
2016-06-12 20:12:59 +01:00
Program . Log . Error ( e . ToString ( ) ) ;
2018-10-20 12:08:47 -04:00
}
2015-09-10 00:52:31 -04:00
finally
{
conn . Dispose ( ) ;
}
}
2018-10-20 12:08:47 -04:00
return reservedNames ;
}
public static List < Retainer > GetRetainers ( uint userId )
{
return new List < Retainer > ( ) ;
2015-09-10 00:52:31 -04:00
}
2015-08-26 13:38:58 -04:00
}
}