public static async Task <Character> CreateCharacterAsync(int user, PonyData pony, short level = 1)
 {
     try
     {
         using (var connection = await GetConnectionAsync())
         {
             using (var command = connection.CreateCommand())
             {
                 var data = CharsMgr.CreateCharacterData(pony, level);
                 command.CommandText = $"INSERT INTO {tb_02} (user, level, race, gender, name, vdata, gdata) VALUES (@user, {level}, @race, @gender, @name, @vdata, @gdata);";
                 command.Parameters.AddWithValue("user", user);
                 command.Parameters.AddWithValue("name", pony.Name);
                 command.Parameters.AddWithValue("race", pony.Race);
                 command.Parameters.AddWithValue("gender", pony.Gender);
                 command.Parameters.AddWithValue("vdata", pony.GetBytes());
                 command.Parameters.AddWithValue("gdata", data.GetBytes());
                 if (await command.ExecuteNonQueryAsyncEx() == 1)
                 {
                     return(new Character((int)command.LastInsertedId, user, level, 0, pony, data));
                 }
             }
         }
         return(null);
     }
     catch (Exception exp)
     {
         ServerLogger.LogException(exp);
         return(null);
     }
 }
Beispiel #2
0
        public static bool CreateCharacter(int user, PonyData pony, out Character entry)
        {
            entry = null;
            var locked = false;

            if (!IsConnected)
            {
                return(false);
            }
            try
            {
                Monitor.Enter(_connection, ref locked);
                using (MySqlCommand _cmd = _connection.CreateCommand())
                {
                    entry            = new Character(pony);
                    _cmd.CommandText = $"INSERT INTO {tb_02} (user, level, race, gender, name, vdata, gdata) VALUES (@user, @level, @race, @gender, @name, @vdata, @gdata);";
                    _cmd.Parameters.AddWithValue("user", user);
                    _cmd.Parameters.AddWithValue("level", entry.Level);
                    _cmd.Parameters.AddWithValue("race", pony.Race);
                    _cmd.Parameters.AddWithValue("gender", pony.Gender);
                    _cmd.Parameters.AddWithValue("name", pony.Name);
                    _cmd.Parameters.AddWithValue("vdata", pony.GetBytes());
                    _cmd.Parameters.AddWithValue("gdata", entry.Data.GetBytes());
                    if (_cmd.ExecuteNonQuery() == 1)
                    {
                        entry.ID = (int)_cmd.LastInsertedId;
                        return(true);
                    }
                }
                return(false);
            }
            catch { return(false); }
            finally { if (locked)
                      {
                          Monitor.Exit(_connection);
                      }
            }
        }