/* TShock, a server mod for Terraria Copyright (C) 2011-2015 Nyx Studios (fka. The TShock Team) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ using System; using System.Collections.Generic; using System.Data; using System.Diagnostics.CodeAnalysis; using System.Linq; using MySql.Data.MySqlClient; using Terraria; namespace TShockAPI.DB { public class WarpManager { private IDbConnection database; /// /// The list of warps. /// public List Warps = new List(); [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] internal WarpManager(IDbConnection db) { database = db; var table = new SqlTable("Warps", new SqlColumn("Id", MySqlDbType.Int32){Primary = true, AutoIncrement = true}, new SqlColumn("WarpName", MySqlDbType.VarChar, 50) {Unique = true}, new SqlColumn("X", MySqlDbType.Int32), new SqlColumn("Y", MySqlDbType.Int32), new SqlColumn("WorldID", MySqlDbType.VarChar, 50) { Unique = true }, new SqlColumn("Private", MySqlDbType.Text) ); var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder) new SqliteQueryCreator() : new MysqlQueryCreator()); creator.EnsureTableStructure(table); } /// /// Adds a warp. /// /// The X position. /// The Y position. /// The name. /// Whether the opration succeeded. public bool Add(int x, int y, string name) { try { if (database.Query("INSERT INTO Warps (X, Y, WarpName, WorldID) VALUES (@0, @1, @2, @3);", x, y, name, Main.worldID.ToString()) > 0) { Warps.Add(new Warp(new Point(x, y), name)); return true; } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return false; } /// /// Reloads all warps. /// public void ReloadWarps() { Warps.Clear(); using (var reader = database.QueryReader("SELECT * FROM Warps WHERE WorldID = @0", Main.worldID.ToString())) { while (reader.Read()) { Warps.Add(new Warp( new Point(reader.Get("X"), reader.Get("Y")), reader.Get("WarpName"), (reader.Get("Private") ?? "0") != "0")); } } } /// /// Removes a warp. /// /// The warp name. /// Whether the operation succeeded. public bool Remove(string warpName) { try { if (database.Query("DELETE FROM Warps WHERE WarpName = @0 AND WorldID = @1", warpName, Main.worldID.ToString()) > 0) { Warps.RemoveAll(w => String.Equals(w.Name, warpName, StringComparison.OrdinalIgnoreCase)); return true; } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return false; } /// /// Finds the warp with the given name. /// /// The name. /// The warp, if it exists, or else null. public Warp Find(string warpName) { return Warps.FirstOrDefault(w => String.Equals(w.Name, warpName, StringComparison.OrdinalIgnoreCase)); } /// /// Finds the warp with the given name. /// /// The name. /// The warp, if it exists, or else null. [Obsolete] public Warp FindWarp(string warpName) { return Warps.FirstOrDefault(w => String.Equals(w.Name, warpName, StringComparison.OrdinalIgnoreCase)); } /// /// Sets the position of a warp. /// /// The warp name. /// The X position. /// The Y position. /// Whether the operation suceeded. public bool Position(string warpName, int x, int y) { try { if (database.Query("UPDATE Warps SET X = @0, Y = @1 WHERE WarpName = @2 AND WorldID = @3", x, y, warpName, Main.worldID.ToString()) > 0) { Warps.Find(w => String.Equals(w.Name, warpName, StringComparison.OrdinalIgnoreCase)).Position = new Point(x, y); return true; } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return false; } /// /// Sets the hidden state of a warp. /// /// The warp name. /// The state. /// Whether the operation suceeded. public bool Hide(string warpName, bool state) { try { if (database.Query("UPDATE Warps SET Private = @0 WHERE WarpName = @1 AND WorldID = @2", state ? "1" : "0", warpName, Main.worldID.ToString()) > 0) { Warps.Find(w => String.Equals(w.Name, warpName, StringComparison.OrdinalIgnoreCase)).IsPrivate = state; return true; } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return false; } } /// /// Represents a warp. /// public class Warp { /// /// Gets or sets the name. /// public string Name { get; set; } /// /// Gets or sets the warp's privacy state. /// public bool IsPrivate { get; set; } /// /// Gets or sets the position. /// public Point Position { get; set; } /// /// Gets or sets the position. /// [Obsolete] public Vector2 WarpPos { get { return new Vector2(Position.X, Position.Y); } set { Position = new Point((int)value.X, (int)value.Y); } } public Warp(Point position, string name, bool isPrivate = false) { Name = name; Position = position; IsPrivate = isPrivate; } [Obsolete] public Warp(Vector2 position, string name, bool isPrivate = false) { Name = name; WarpPos = position; IsPrivate = isPrivate; } public Warp() { Position = Point.Zero; Name = ""; IsPrivate = false; } } }