public static bool SelectAllNPCs(out Dictionary <int, DB_NPC> data) { var locked = false; data = new Dictionary <int, DB_NPC>(); if (!IsConnected) { return(false); } try { using (MySqlCommand _cmd = _connection.CreateCommand()) { _cmd.CommandText = $"SELECT a.*, b.item, c.* FROM {tb_05} a LEFT JOIN {tb_05_02} b ON a.id = b.id LEFT JOIN {tb_05_01} c ON a.id = c.id;"; Monitor.Enter(s_lock, ref locked); using (MySqlDataReader _result = _cmd.ExecuteReader()) { DB_NPC entry; int id; if (_result.HasRows) { while (_result.Read()) { id = _result.GetInt32(0); if (data.ContainsKey(id)) { data[id].Items.Add(_result.GetInt32(24)); } else { entry = new DB_NPC(id, _result.GetByte(1), _result.GetInt16(2), _result.GetUInt16(3), _result.GetByte(4), _result.GetUInt16(5), _result.GetPonyOld(6)); if ((entry.Flags & NPCFlags.Trader) > 0) { entry.Items.Add(_result.GetInt32(24)); } if ((entry.Flags & NPCFlags.Wears) > 0) { for (int i = 26; i <= 33; i++) { entry.Wears.Add(_result.GetInt32(i)); } } data[id] = entry; } } } } } return(true); } catch { return(false); } finally { if (locked) { Monitor.Exit(s_lock); } } }
public static async Task <Dictionary <int, DB_NPC> > SelectAllNpcsAsync() { try { using (var connection = await GetConnectionAsync()) { using (var command = connection.CreateCommand()) { command.CommandText = $"SELECT a.*, b.item, c.* FROM {tb_05} a LEFT JOIN {tb_05_02} b ON a.id = b.id LEFT JOIN {tb_05_01} c ON a.id = c.id;"; using (var reader = await command.ExecuteReaderAsyncEx()) { var result = new Dictionary <int, DB_NPC>(); if (reader.HasRows) { while (await reader.ReadAsyncEx()) { var id = reader.GetInt32(0); if (result.TryGetValue(id, out var entry)) { entry.Items.Add(reader.GetInt32(24)); } else { entry = new DB_NPC(id, reader.GetByte(1), reader.GetInt16(2), reader.GetUInt16(3), reader.GetByte(4), reader.GetUInt16(5), reader.GetPonyOld(6)); if ((entry.Flags & NPCFlags.Trader) > 0) { entry.Items.Add(reader.GetInt32(24)); } if ((entry.Flags & NPCFlags.Wears) > 0) { for (int i = 26; i <= 33; i++) { entry.Wears.Add(reader.GetInt32(i)); } } result[id] = entry; } } } return(result); } } } } catch (Exception exp) { ServerLogger.LogException(exp); return(null); } }
public WO_NPC(WO_NPC original, MapPlayer owner) : base(original._manager.GetNewGuid() | Constants.CRObject, original._manager) { _owner = owner; _original = original; _npc = original._npc; _data = original._data; if ((_npc.Flags & NPCFlags.Dialog) > 0) { _dialog = _server.Dialogs.GetClone(_npc.Dialog, owner); } if ((_npc.Flags & NPCFlags.Trader) > 0) { shop_ser = new SER_Shop(_npc.Items, $"{_npc.Pony.Name}'s shop"); } if ((_npc.Flags & NPCFlags.Wears) > 0) { DB_Item entry; byte slot; _wears = new Dictionary <int, InventoryItem>(); wears_ser = new SER_Wears(_wears); foreach (var item in _npc.Wears) { if (item > 0 && DataMgr.Select(item, out entry)) { slot = entry.Slot.ToWearableIndex(); if (_wears.ContainsKey(slot)) { ServerLogger.LogWarn($"NPC id {_data.ObjectID} duplicate wear slot {entry.Slot}"); } else { _wears[slot] = new InventoryItem(item); } } } } OnSpawn += WO_NPC_OnSpawn; OnDespawn += WO_NPC_OnDespawn; OnDestroy += WO_NPC_OnDestroy; if ((_npc.Flags & NPCFlags.ScriptedMovement) > 0) { AddComponent(new ScriptedMovement(original._movement as ScriptedMovement, this)); } else { AddComponent(new NullMovement(this)); } m_char = new Character(_npc.ID, -1, _npc.Level, -1, _npc.Pony, null); Spawn(); }
public static bool Select(int id, out DB_NPC entry) { return(m_npcs.TryGetValue(id, out entry)); }