using System.Collections.Generic; using Chop.Database; using System.Data; using Chop.Rooms; namespace Chop.Game.Rooms { public class NavigatorManager { private List _officialRooms; private List _categories; public void Init() { _officialRooms = new List(); _categories = new List(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) table = dbClient.ReadDataTable("SELECT * FROM room_categories"); foreach (DataRow row in table.Rows) _categories.Add(NavigatorCategory.Parse(row)); using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) table = dbClient.ReadDataTable("SELECT * FROM official_rooms"); foreach (DataRow row in table.Rows) _officialRooms.Add(new OfficialRoom(row)); } public NavigatorRoom GetRoom(int ID) { DataRow row; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", ID); row = dbClient.ReadDataRow("SELECT * FROM rooms WHERE id = @id"); } if(row != null) { return NavigatorRoom.StaticParse(row); } return null; } public NavigatorCategory GetCategory(int ID) { foreach (NavigatorCategory category in _categories) if (category.ID == ID) return category; return null; } public List GetCategories(int ID) { List categories = new List(); foreach (NavigatorCategory category in _categories) if (category.ParentCategory == ID) categories.Add(category); return categories; } public List GetCategories(CategoryType type, byte rank) { List categories = new List(); foreach (NavigatorCategory category in _categories) if (category.Type == type && category.mMinimumCreateRank <= rank) categories.Add(category); return categories; } public List GetRooms(int ID, bool hideFull) { List rooms = new List(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", ID); dbClient.AddParamWithValue("limit", Core.Config.GetInt("game.navigator.maxresult")); table = dbClient.ReadDataTable("SELECT * FROM rooms WHERE category = @id LIMIT @limit"); } foreach (DataRow row in table.Rows) rooms.Add(NavigatorRoom.StaticParse(row)); return rooms; } public List GetUsersRooms(string username) { List rooms = new List(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("owner", username); dbClient.AddParamWithValue("limit", Core.Config.GetInt("game.navigator.maxrooms")); table = dbClient.ReadDataTable("SELECT * FROM rooms WHERE owner = @owner LIMIT @limit"); } foreach (DataRow row in table.Rows) rooms.Add(NavigatorRoom.StaticParse(row)); return rooms; } public List GetRooms(string searchText) { List rooms = new List(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("text", searchText + "%"); dbClient.AddParamWithValue("limit", Core.Config.GetInt("game.navigator.maxresult")); table = dbClient.ReadDataTable("SELECT * FROM rooms WHERE owner LIKE @text LIMIT @limit"); } foreach (DataRow row in table.Rows) rooms.Add(NavigatorRoom.StaticParse(row)); return rooms; } public List GetRandomRooms() { List rooms = new List(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) table = dbClient.ReadDataTable("SELECT * FROM rooms WHERE casts IS NULL ORDER BY RAND() LIMIT 3"); foreach (DataRow row in table.Rows) rooms.Add(NavigatorRoom.StaticParse(row)); return rooms; } public List GetOfficialRooms() { return _officialRooms; } } }