From d0a9b47f2f1a19f2e6312ed9d2eea94f23fbce1a Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Sat, 24 Dec 2011 15:21:16 -0500 Subject: [PATCH] You can now add entire groups to a region. You can define as many groups as you want to a region, just like users. The new commands are /region allowg group region and /region removeg group region. Any player in the group has build rights in the region. --- TShockAPI/Commands.cs | 74 +++++++++++++++++++++- TShockAPI/DB/RegionManager.cs | 114 ++++++++++++++++++++++++++++------ 2 files changed, 167 insertions(+), 21 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index d1f8e6a4..da012ed1 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -1984,7 +1984,7 @@ namespace TShockAPI var width = Math.Abs(args.Player.TempPoints[0].X - args.Player.TempPoints[1].X); var height = Math.Abs(args.Player.TempPoints[0].Y - args.Player.TempPoints[1].Y); - if (TShock.Regions.AddRegion(x, y, width, height, regionName, Main.worldID.ToString())) + if (TShock.Regions.AddRegion(x, y, width, height, regionName, args.Player.UserAccountName, Main.worldID.ToString())) { args.Player.TempPoints[0] = Point.Zero; args.Player.TempPoints[1] = Point.Zero; @@ -2120,8 +2120,78 @@ namespace TShockAPI } } else - args.Player.SendMessage("Invalid syntax! Proper syntax: /region allow [name] [region]", Color.Red); + args.Player.SendMessage("Invalid syntax! Proper syntax: /region remove [name] [region]", Color.Red); break; + case "allowg": + { + if (args.Parameters.Count > 2) + { + string group = args.Parameters[1]; + string regionName = ""; + + for (int i = 2; i < args.Parameters.Count; i++) + { + if (regionName == "") + { + regionName = args.Parameters[2]; + } + else + { + regionName = regionName + " " + args.Parameters[i]; + } + } + if (TShock.Groups.GroupExists(group)) + { + if (TShock.Regions.AllowGroup(regionName, group)) + { + args.Player.SendMessage("Added group " + group + " to " + regionName, Color.Yellow); + } + else + args.Player.SendMessage("Region " + regionName + " not found", Color.Red); + } + else + { + args.Player.SendMessage("Group " + group + " not found", Color.Red); + } + } + else + args.Player.SendMessage("Invalid syntax! Proper syntax: /region allow [group] [region]", Color.Red); + break; + } + case "removeg": + if (args.Parameters.Count > 2) + { + string group = args.Parameters[1]; + string regionName = ""; + + for (int i = 2; i < args.Parameters.Count; i++) + { + if (regionName == "") + { + regionName = args.Parameters[2]; + } + else + { + regionName = regionName + " " + args.Parameters[i]; + } + } + if (TShock.Groups.GroupExists(group)) + { + if (TShock.Regions.RemoveGroup(regionName, group)) + { + args.Player.SendMessage("Removed group " + group + " from " + regionName, Color.Yellow); + } + else + args.Player.SendMessage("Region " + regionName + " not found", Color.Red); + } + else + { + args.Player.SendMessage("Group " + group + " not found", Color.Red); + } + } + else + args.Player.SendMessage("Invalid syntax! Proper syntax: /region removeg [group] [region]", Color.Red); + break; case "list": { //How many regions per page diff --git a/TShockAPI/DB/RegionManager.cs b/TShockAPI/DB/RegionManager.cs index 5c78241b..da2f7246 100644 --- a/TShockAPI/DB/RegionManager.cs +++ b/TShockAPI/DB/RegionManager.cs @@ -38,7 +38,6 @@ namespace TShockAPI.DB public RegionManager(IDbConnection db) { database = db; - var table = new SqlTable("Regions", new SqlColumn("X1", MySqlDbType.Int32), new SqlColumn("Y1", MySqlDbType.Int32), @@ -47,7 +46,10 @@ namespace TShockAPI.DB new SqlColumn("RegionName", MySqlDbType.VarChar, 50) { Primary = true }, new SqlColumn("WorldID", MySqlDbType.Text), new SqlColumn("UserIds", MySqlDbType.Text), - new SqlColumn("Protected", MySqlDbType.Int32) + new SqlColumn("Protected", MySqlDbType.Int32), + new SqlColumn("Groups", MySqlDbType.Text), + new SqlColumn("Owner", MySqlDbType.VarChar, 50) + ); var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator()); creator.EnsureExists(table); @@ -180,11 +182,13 @@ namespace TShockAPI.DB int Protected = reader.Get("Protected"); string mergedids = reader.Get("UserIds"); string name = reader.Get("RegionName"); + string owner = reader.Get("Owner"); + string groups = reader.Get("Groups"); string[] splitids = mergedids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - Region r = new Region(new Rectangle(X1, Y1, width, height), name, Protected != 0, Main.worldID.ToString()); - + Region r = new Region(new Rectangle(X1, Y1, width, height), name, owner, Protected != 0, Main.worldID.ToString()); + r.SetAllowedGroups( groups ); try { for (int i = 0; i < splitids.Length; i++) @@ -231,8 +235,11 @@ namespace TShockAPI.DB string MergedIDs = reader.Get("UserIds"); string name = reader.Get("RegionName"); string[] SplitIDs = MergedIDs.Split(','); + string owner = reader.Get("Owner"); + string groups = reader.Get("Groups"); - Region r = new Region(new Rectangle(X1, Y1, width, height), name, Protected != 0, Main.worldID.ToString()); + Region r = new Region(new Rectangle(X1, Y1, width, height), name, owner, Protected != 0, Main.worldID.ToString()); + r.SetAllowedGroups( groups ); try { for (int i = 0; i < SplitIDs.Length; i++) @@ -260,7 +267,7 @@ namespace TShockAPI.DB } - public bool AddRegion(int tx, int ty, int width, int height, string regionname, string worldid) + public bool AddRegion(int tx, int ty, int width, int height, string regionname, string owner, string worldid) { if (GetRegionByName(regionname) != null) { @@ -268,9 +275,9 @@ namespace TShockAPI.DB } try { - database.Query("INSERT INTO Regions (X1, Y1, width, height, RegionName, WorldID, UserIds, Protected) VALUES (@0, @1, @2, @3, @4, @5, @6, @7);", - tx, ty, width, height, regionname, worldid, "", 1); - Regions.Add(new Region(new Rectangle(tx, ty, width, height), regionname, true, worldid)); + database.Query("INSERT INTO Regions (X1, Y1, width, height, RegionName, WorldID, UserIds, Protected, Groups, Owner) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9);", + tx, ty, width, height, regionname, worldid, "", 1, "", owner); + Regions.Add(new Region(new Rectangle(tx, ty, width, height), regionname, owner, true, worldid)); return true; } catch (Exception ex) @@ -524,23 +531,84 @@ namespace TShockAPI.DB } return null; } + + public bool ChangeOwner( string regionName, string newOwner ) + { + var region = GetRegionByName(regionName); + if( region != null ) + { + region.Owner = newOwner; + int q = database.Query("UPDATE Regions SET Owner=@0 WHERE RegionName=@1 AND WorldID=@2", newOwner, + regionName, Main.worldID.ToString()); + if (q > 0) + return true; + } + return false; + } + + public bool AllowGroup( string regionName, string groups) + { + string groupsNew = ""; + using (var reader = database.QueryReader("SELECT * FROM Regions WHERE RegionName=@0 AND WorldID=@1", regionName, Main.worldID.ToString())) + { + if (reader.Read()) + groupsNew = reader.Get("Groups"); + } + if (groupsNew != "") + groupsNew += ","; + groupsNew += groups; + + int q = database.Query("UPDATE Regions SET Groups=@0 WHERE RegionName=@1 AND WorldID=@2", groupsNew, + regionName, Main.worldID.ToString()); + + Region r = GetRegionByName(regionName); + if( r != null ) + { + r.SetAllowedGroups( groupsNew ); + } + else + { + return false; + } + + return q > 0; + } + + public bool RemoveGroup( string regionName, string group ) + { + Region r = GetRegionByName(regionName); + if (r != null) + { + r.RemoveGroup(group); + string groups = string.Join(",", r.AllowedGroups); + int q = database.Query("UPDATE Regions SET Groups=@0 WHERE RegionName=@1 AND WorldID=@2", groups, + regionName, Main.worldID.ToString()); + if (q > 0) + return true; + } + return false; + } } public class Region { public Rectangle Area { get; set; } public string Name { get; set; } + public string Owner { get; set; } public bool DisableBuild { get; set; } public string WorldID { get; set; } public List AllowedIDs { get; set; } + public List AllowedGroups { get; set; } - public Region(Rectangle region, string name, bool disablebuild, string RegionWorldIDz) + public Region(Rectangle region, string name, string owner, bool disablebuild, string RegionWorldIDz) : this() { Area = region; Name = name; - DisableBuild = disablebuild; + Owner = owner; + DisableBuild = disablebuild; WorldID = RegionWorldIDz; + } public Region() @@ -577,14 +645,7 @@ namespace TShockAPI.DB return true; } - for (int i = 0; i < AllowedIDs.Count; i++) - { - if (AllowedIDs[i] == ply.UserID) - { - return true; - } - } - return false; + return AllowedIDs.Contains(ply.UserID) || AllowedGroups.Contains(ply.Group.Name) || Owner == ply.UserAccountName; } public void setAllowedIDs(String ids) @@ -601,6 +662,16 @@ namespace TShockAPI.DB AllowedIDs = id_list; } + public void SetAllowedGroups( String groups ) + { + List groupArr = groups.Split(',').ToList(); + + for (int i = 0; i < groupArr.Count; i++) + groupArr[i] = groupArr[i].Trim(); + + AllowedGroups = groupArr; + } + public void RemoveID(int id) { var index = -1; @@ -614,5 +685,10 @@ namespace TShockAPI.DB } AllowedIDs.RemoveAt(index); } + + public bool RemoveGroup(string groupName) + { + return AllowedGroups.Remove(groupName); + } } }