2015-08-26 13:38:58 -04:00
using FFXIVClassic_Lobby_Server.dataobjects ;
using MySql.Data.MySqlClient ;
2015-09-09 00:08:46 -04:00
using Dapper ;
2015-08-26 13:38:58 -04:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2016-03-20 11:49:09 -04:00
using FFXIVClassic_Lobby_Server.utils ;
2016-06-12 20:12:59 +01:00
using FFXIVClassic.Common ;
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 ;
cmd . CommandText = String . Format ( "INSERT INTO characters_parametersave(characterId, hp, hpMax, mp, mpMax, mainSkill, mainSkillLevel) VALUES(@characterId, 1, 1, 1, 1, @mainSkill, 1);" , CharacterCreatorUtils . GetClassNameForId ( ( short ) charaInfo . currentClass ) ) ;
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 ( ) ) ;
2016-06-08 22:29:04 +01:00
2015-10-05 19:37:03 -04:00
}
finally
{
conn . Dispose ( ) ;
}
2016-04-17 19:09:01 -04:00
2015-10-05 19:37:03 -04:00
}
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
{
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 ) ) )
2015-08-26 13:38:58 -04:00
{
2015-09-09 00:08:46 -04:00
List < World > worldList = null ;
2015-08-26 13:38:58 -04:00
try
{
conn . Open ( ) ;
2015-09-09 00:08:46 -04:00
worldList = conn . Query < World > ( "SELECT * FROM servers WHERE isActive=true" ) . ToList ( ) ;
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 ( ) ) ;
2016-06-08 22:29:04 +01:00
worldList = new List < World > ( ) ; }
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
}
return worldList ;
}
}
2016-06-14 21:29:10 +01:00
public static World GetServer ( uint serverId )
2015-08-26 13:38:58 -04:00
{
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 ) ) )
2015-08-26 13:38:58 -04:00
{
World world = null ;
try
{
conn . Open ( ) ;
2015-09-09 00:08:46 -04:00
world = conn . Query < World > ( "SELECT * FROM servers WHERE id=@ServerId" , new { ServerId = serverId } ) . SingleOrDefault ( ) ;
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 ( ) ) ;
2016-06-08 22:29:04 +01:00
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
}
return world ;
}
}
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 ) ) )
{
2016-03-20 11:49:09 -04:00
conn . Open ( ) ;
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 id = @charId ";
MySqlCommand cmd = new MySqlCommand ( query , conn ) ;
cmd . Parameters . AddWithValue ( "@charId" , charId ) ;
using ( MySqlDataReader reader = cmd . ExecuteReader ( ) )
2015-09-13 11:30:33 -04:00
{
2016-03-20 11:49:09 -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" ) ;
}
2015-09-13 11:30:33 -04:00
}
}
2016-03-20 11:49:09 -04:00
return chara ;
2015-09-13 11:30:33 -04:00
}
2016-06-14 21:29:10 +01:00
public static Appearance GetAppearance ( uint charaId )
2015-10-05 19:37:03 -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 ) ) )
{
Appearance appearance = null ;
try
{
conn . Open ( ) ;
2016-02-16 23:35:21 -05:00
appearance = conn . Query < Appearance > ( "SELECT * FROM characters_appearance WHERE characterId=@CharaId" , new { CharaId = charaId } ) . SingleOrDefault ( ) ;
2015-10-05 19:37:03 -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
2015-10-05 19:37:03 -04:00
}
finally
{
conn . Dispose ( ) ;
}
return appearance ;
}
}
2016-06-14 21:29:10 +01:00
public static List < String > GetReservedNames ( uint userId )
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 ) ) )
{
List < String > nameList = null ;
try
{
conn . Open ( ) ;
nameList = conn . Query < String > ( "SELECT name FROM reserved_names WHERE userId=@UserId" , new { UserId = userId } ) . ToList ( ) ;
}
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
nameList = new List < String > ( ) ; }
2015-09-10 00:52:31 -04:00
finally
{
conn . Dispose ( ) ;
}
return nameList ;
}
}
2016-06-14 21:29:10 +01:00
public static List < Retainer > GetRetainers ( uint userId )
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 ) ) )
{
List < Retainer > retainerList = null ;
try
{
conn . Open ( ) ;
retainerList = conn . Query < Retainer > ( "SELECT * FROM retainers WHERE id=@UserId ORDER BY characterId, slot" , new { UserId = userId } ) . ToList ( ) ;
}
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
retainerList = new List < Retainer > ( ) ; }
2015-09-10 00:52:31 -04:00
finally
{
conn . Dispose ( ) ;
}
return retainerList ;
}
}
2015-08-26 13:38:58 -04:00
}
}