Hey, ich würde gerne einen MySQL Query erstellen, wo in einer Tabelle etwas geupdated wird und in einer anderen Tabelle etwas erstellt wird.
Das funktioniert soweit auch.
In der "anderen" Tabelle würde ich aber gerne eine Funktion einbauen, um doppelte Namen zu verhindern. Das heißt wenn `name1` bereits irgendwo in der Datenbank `skins` existiert, soll die Funktion darunter namens "catch (Exception ex)" aufgerufen werden, wodurch das Event "characterNameDuplicated" ausgeführt wird.
public static int CreatePlayerSkin(Client player, AccountModel account, SkinModel skin)
{
NAPI.Util.ConsoleOutput("Debug 1");
int playerId = 0;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
NAPI.Util.ConsoleOutput("Debug 2");
try
{
NAPI.Util.ConsoleOutput("Debug 3");
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "UPDATE accounts SET sex = @sex, name = @name WHERE socialName = @socialName LIMIT 1";
command.Parameters.AddWithValue("@sex", account.sex);
command.Parameters.AddWithValue("@name", account.realName);
command.Parameters.AddWithValue("@socialName", account.socialName);
command.ExecuteNonQuery();
player.SetData(EntityData.PLAYER_SEX, account.sex);
NAPI.Util.ConsoleOutput("Debug 4");
playerId = (int)command.LastInsertedId;
command.CommandText = "INSERT INTO `skins` (`name1`, `socialName`, `firstHeadShape`, `secondHeadShape`, `firstSkinTone`, `secondSkinTone`, `headMix`, `skinMix`, ";
command.CommandText += "`hairModel`, `firstHairColor`, `secondHairColor`, `beardModel`, `beardColor`, `chestModel`, `chestColor`, `blemishesModel`, ";
command.CommandText += "`ageingModel`, `complexionModel`, `sundamageModel`, `frecklesModel`, `noseWidth`, `noseHeight`, `noseLength`, `noseBridge`, `noseTip`, `noseShift`, ";
command.CommandText += "`browHeight`, `browWidth`, `cheekboneHeight`, `cheekboneWidth`, `cheeksWidth`, `eyes`, `lips`, `jawWidth`, `jawHeight`, `chinLength`, ";
command.CommandText += "`chinPosition`, `chinWidth`, `chinShape`, `neckWidth`, `eyesColor`, `eyebrowsModel`, `eyebrowsColor`, `makeupModel`, `blushModel`, `blushColor`, ";
command.CommandText += "`lipstickModel`, `lipstickColor`) VALUES (@name1, @socName, @firstHeadShape, @secondHeadShape, @firstSkinTone, @secondSkinTone, @headMix, @skinMix, ";
command.CommandText += "@hairModel, @firstHairColor, @secondHairColor, @beardModel, @beardColor, @chestModel, @chestColor, @blemishesModel, @ageingModel, ";
command.CommandText += "@complexionModel, @sundamageModel, @frecklesModel, @noseWidth, @noseHeight, @noseLength, @noseBridge, @noseTip, @noseShift, @browHeight, ";
command.CommandText += "@browWidth, @cheekboneHeight, @cheekboneWidth, @cheeksWidth, @eyes, @lips, @jawWidth, @jawHeight, @chinLength, @chinPosition, @chinWidth, ";
command.CommandText += "@chinShape, @neckWidth, @eyesColor, @eyebrowsModel, @eyebrowsColor, @makeupModel, @blushModel, @blushColor, @lipstickModel, @lipstickColor)";
command.Parameters.AddWithValue("@name1", account.realName);
command.Parameters.AddWithValue("@socName", player.SocialClubName);
command.Parameters.AddWithValue("@firstHeadShape", skin.firstHeadShape);
command.Parameters.AddWithValue("@secondHeadShape", skin.secondHeadShape);
command.Parameters.AddWithValue("@firstSkinTone", skin.firstSkinTone);
command.Parameters.AddWithValue("@secondSkinTone", skin.secondSkinTone);
command.Parameters.AddWithValue("@headMix", skin.headMix);
command.Parameters.AddWithValue("@skinMix", skin.skinMix);
command.Parameters.AddWithValue("@hairModel", skin.hairModel);
command.Parameters.AddWithValue("@firstHairColor", skin.firstHairColor);
command.Parameters.AddWithValue("@secondHairColor", skin.secondHairColor);
command.Parameters.AddWithValue("@beardModel", skin.beardModel);
command.Parameters.AddWithValue("@beardColor", skin.beardColor);
command.Parameters.AddWithValue("@chestModel", skin.chestModel);
command.Parameters.AddWithValue("@chestColor", skin.chestColor);
command.Parameters.AddWithValue("@blemishesModel", skin.blemishesModel);
command.Parameters.AddWithValue("@ageingModel", skin.ageingModel);
command.Parameters.AddWithValue("@complexionModel", skin.complexionModel);
command.Parameters.AddWithValue("@sundamageModel", skin.sundamageModel);
command.Parameters.AddWithValue("@frecklesModel", skin.frecklesModel);
command.Parameters.AddWithValue("@noseWidth", skin.noseWidth);
command.Parameters.AddWithValue("@noseHeight", skin.noseHeight);
command.Parameters.AddWithValue("@noseLength", skin.noseLength);
command.Parameters.AddWithValue("@noseBridge", skin.noseBridge);
command.Parameters.AddWithValue("@noseTip", skin.noseTip);
command.Parameters.AddWithValue("@noseShift", skin.noseShift);
command.Parameters.AddWithValue("@browHeight", skin.browHeight);
command.Parameters.AddWithValue("@browWidth", skin.browWidth);
command.Parameters.AddWithValue("@cheekboneHeight", skin.cheekboneHeight);
command.Parameters.AddWithValue("@cheekboneWidth", skin.cheekboneWidth);
command.Parameters.AddWithValue("@cheeksWidth", skin.cheeksWidth);
command.Parameters.AddWithValue("@eyes", skin.eyes);
command.Parameters.AddWithValue("@lips", skin.lips);
command.Parameters.AddWithValue("@jawWidth", skin.jawWidth);
command.Parameters.AddWithValue("@jawHeight", skin.jawHeight);
command.Parameters.AddWithValue("@chinLength", skin.chinLength);
command.Parameters.AddWithValue("@chinPosition", skin.chinPosition);
command.Parameters.AddWithValue("@chinWidth", skin.chinWidth);
command.Parameters.AddWithValue("@chinShape", skin.chinShape);
command.Parameters.AddWithValue("@neckWidth", skin.neckWidth);
command.Parameters.AddWithValue("@eyesColor", skin.eyesColor);
command.Parameters.AddWithValue("@eyebrowsModel", skin.eyebrowsModel);
command.Parameters.AddWithValue("@eyebrowsColor", skin.eyebrowsColor);
command.Parameters.AddWithValue("@makeupModel", skin.makeupModel);
command.Parameters.AddWithValue("@blushModel", skin.blushModel);
command.Parameters.AddWithValue("@blushColor", skin.blushColor);
command.Parameters.AddWithValue("@lipstickModel", skin.lipstickModel);
command.Parameters.AddWithValue("@lipstickColor", skin.lipstickColor);
command.ExecuteNonQuery();
NAPI.Util.ConsoleOutput("Character created");
player.TriggerEvent("cancelCharacterCreation2");
player.SetData(EntityData.PLAYER_TUTORIAL, 2);
Globals.OnPlayerSpawn(player);
}
catch (Exception ex)
{
NAPI.Util.ConsoleOutput("[EXCEPTION CreateCharacter] " + ex.Message);
NAPI.Util.ConsoleOutput("[EXCEPTION CreateCharacter] " + ex.StackTrace);
player.TriggerEvent("characterNameDuplicated");
}
}
return playerId;
}
Alles anzeigen
Die Funktion ansich funktioniert einwandfrei, das Problem dabei ist halt wie gesagt das mit den doppelten Namen.