Minor code cleanup

This commit is contained in:
UnknownShadow200 2018-06-06 17:41:27 +10:00
parent f24fbe0250
commit 53751c4a8d
16 changed files with 74 additions and 108 deletions

View File

@ -189,53 +189,43 @@ namespace MCGalaxy.Gui {
void Main_UpdateMapList() {
Level[] loaded = LevelInfo.Loaded.Items;
string selected = GetSelected(main_Maps);
// Try to keep the same selection on update
string selected = null;
var selectedRows = main_Maps.SelectedRows;
if (selectedRows.Count > 0) {
selected = (string)selectedRows[0].Cells[0].Value;
}
// Update the data source and control
lc = new LevelCollection();
// Always new data source, avoids "-1 does not have a value" when clicking a row
LevelCollection lc = new LevelCollection();
foreach (Level lvl in loaded) { lc.Add(lvl); }
main_Maps.DataSource = lc;
// Reselect map
if (selected != null) {
foreach (DataGridViewRow row in main_Maps.Rows) {
string name = (string)row.Cells[0].Value;
if (name.CaselessEq(selected)) row.Selected = true;
}
}
Reselect(main_Maps, selected);
main_Maps.Refresh();
}
void Main_UpdatePlayersList() {
UpdateNotifyIconText();
Player[] players = PlayerInfo.Online.Items;
string selected = GetSelected(main_Players);
// Try to keep the same selection on update
string selected = null;
var selectedRows = main_Players.SelectedRows;
if (selectedRows.Count > 0) {
selected = (string)selectedRows[0].Cells[0].Value;
}
// Update the data source and control
pc = new PlayerCollection();
PlayerCollection pc = new PlayerCollection();
foreach (Player pl in players) { pc.Add(pl); }
main_Players.DataSource = pc;
// Reselect player
if (selected != null) {
foreach (DataGridViewRow row in main_Players.Rows) {
string name = (string)row.Cells[0].Value;
if (name.CaselessEq(selected)) row.Selected = true;
}
}
Reselect(main_Players, selected);
main_Players.Refresh();
}
static string GetSelected(DataGridView view) {
DataGridViewSelectedRowCollection selected = view.SelectedRows;
if (selected.Count == 0) return null;
return (string)selected[0].Cells[0].Value;
}
static void Reselect(DataGridView view, string selected) {
if (selected == null) return;
foreach (DataGridViewRow row in view.Rows) {
string name = (string)row.Cells[0].Value;
if (name.CaselessEq(selected)) row.Selected = true;
}
}
}
}

View File

@ -34,8 +34,6 @@ namespace MCGalaxy.Gui {
delegate void VoidDelegate();
bool mapgen = false;
PlayerCollection pc;
LevelCollection lc;
public NotifyIcon notifyIcon = new NotifyIcon();
Player curPlayer;
@ -59,11 +57,9 @@ namespace MCGalaxy.Gui {
Text = ServerConfig.Name + " - " + Server.SoftwareNameVersioned;
MakeNotifyIcon();
// Bind player list
main_Players.DataSource = pc;
main_Players.Font = new Font("Calibri", 8.25f);
main_Maps.DataSource = new LevelCollection(); // Otherwise "-1 does not have a value" exception when clicking a row
main_Players.DataSource = new PlayerCollection();
main_Players.Font = new Font("Calibri", 8.25f);
main_Maps.DataSource = new LevelCollection();
main_Maps.Font = new Font("Calibri", 8.25f);
}

View File

@ -87,7 +87,7 @@ namespace MCGalaxy.Commands.Fun {
who.SetPrefix();
}
team.RemoveIfEmpty();
team.DeleteIfEmpty();
Team.SaveList();
} else {
Player.Message(p, "The given player was not found. You need to use their full account name.");
@ -182,7 +182,7 @@ namespace MCGalaxy.Commands.Fun {
team.Remove(p.name);
p.Game.Team = null;
team.RemoveIfEmpty();
team.DeleteIfEmpty();
p.SetPrefix();
Team.SaveList();
}

View File

@ -40,7 +40,7 @@ namespace MCGalaxy.Commands.Maintenance {
if (args[0] == null) return;
Player who = PlayerInfo.FindExact(args[0]);
if (args.Length == 1) {
Player.Message(p, Colors.red + "You must specify a type to modify.");
Player.Message(p, "&cYou must specify a type to modify.");
MessageValidTypes(p); return;
}
@ -98,7 +98,7 @@ namespace MCGalaxy.Commands.Maintenance {
} else if (opt == "titlecolor") {
SetColor(p, args, PlayerData.ColumnTColor, who, v => who.titlecolor = v);
} else {
Player.Message(p, Colors.red + "Invalid type.");
Player.Message(p, "&cInvalid type");
MessageValidTypes(p);
}
}

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Moderation {
}
if (HttpUtil.IsPrivateIP(ip)) {
Player.Message(p, Colors.red + "Player has an internal IP, cannot trace"); return;
Player.Message(p, "&cPlayer has an internal IP, cannot trace"); return;
}
string country = null;

View File

@ -32,7 +32,7 @@ namespace MCGalaxy.Commands.Misc {
if (message.Length == 0) { Help(p); return; }
if (p.hackrank) {
Player.Message(p, Colors.red + "You have already hacked a rank!"); return;
Player.Message(p, "&cYou have already hacked a rank!"); return;
}
Group grp = Matcher.FindRanks(p, message);

View File

@ -48,7 +48,7 @@ namespace MCGalaxy.DB {
if (!p.level.Props[block].IsMessageBlock) return;
try {
if (!Database.Backend.TableExists("Messages" + p.level.name)) return;
if (!Database.TableExists("Messages" + p.level.name)) return;
DataTable messages = Database.Backend.GetRows("Messages" + p.level.name, "*",
"WHERE X=@0 AND Y=@1 AND Z=@2", x, y, z);
int last = messages.Rows.Count - 1;
@ -65,7 +65,7 @@ namespace MCGalaxy.DB {
if (!p.level.Props[block].IsPortal) return;
try {
if (!Database.Backend.TableExists("Portals" + p.level.name)) return;
if (!Database.TableExists("Portals" + p.level.name)) return;
DataTable portals = Database.Backend.GetRows("Portals" + p.level.name, "*",
"WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", x, y, z);
int last = portals.Rows.Count - 1;

View File

@ -25,9 +25,7 @@ namespace MCGalaxy.SQL {
public static IDatabaseBackend Backend;
/// <summary> Returns whether the given table exists in the database. </summary>
public static bool TableExists(string table) {
return Backend.TableExists(table);
}
public static bool TableExists(string table) { return Backend.TableExists(table); }
/// <summary> Executes an SQL command that does not return any results. </summary>
@ -74,7 +72,7 @@ namespace MCGalaxy.SQL {
static void DoDatabaseCall(ParameterisedQuery query, string sql, bool createDB,
DataTable results, ReaderCallback callback, params object[] args) {
BindParams(query, args);
query.parameters = args;
string connString = Backend.ConnectionString;
Exception e = null;
@ -88,38 +86,27 @@ namespace MCGalaxy.SQL {
query.Fill(sql, connString, results);
}
query.ClearParams();
query.parameters = null;
return;
} catch (Exception ex) {
e = ex; // try yet again
}
}
query.parameters = null;
File.AppendAllText("MySQL_error.log", DateTime.Now + " " + sql + "\r\n");
Logger.LogError(e);
}
static readonly object idsLock = new object();
static string[] ids = null;
static void BindParams(ParameterisedQuery query, object[] args) {
if (args == null || args.Length == 0) return;
string[] names = GetParamNames(args.Length);
for (int i = 0; i < args.Length; i++)
query.AddParam(names[i], args[i]);
}
volatile static string[] ids;
internal static string[] GetParamNames(int count) {
// Avoid allocation overhead from string concat every query by caching
string[] names = null;
lock (idsLock) {
names = ids;
if (ids == null || count > ids.Length) {
ids = new string[count];
for (int i = 0; i < count; i++)
ids[i] = "@" + i;
names = ids;
}
string[] names = ids;
if (names == null || count > names.Length) {
names = new string[count];
for (int i = 0; i < names.Length; i++) { names[i] = "@" + i; }
ids = names;
}
return names;
}

View File

@ -28,23 +28,12 @@ namespace MCGalaxy.SQL {
/// <summary> Represents an SQL command or query, that takes named parameters/arguments. </summary>
public abstract class ParameterisedQuery {
protected Dictionary<string, object> parameters = new Dictionary<string, object>();
/// <summary> Adds a named parameter/argument to this query. </summary>
public void AddParam(string name, object param) { parameters.Add(name, param); }
/// <summary> Clears the cached named parameters/arguments. </summary>
public void ClearParams() { parameters.Clear(); }
internal object[] parameters;
protected abstract bool MultipleSchema { get; }
protected abstract IDbConnection CreateConnection(string connString);
protected abstract IDbCommand CreateCommand(string query, IDbConnection conn);
protected abstract DbDataAdapter CreateDataAdapter(string query, IDbConnection conn);
protected abstract IDbConnection CreateConnection(string connString);
protected abstract IDbCommand CreateCommand(string query, IDbConnection conn);
protected abstract DbDataAdapter CreateDataAdapter(string query, IDbConnection conn);
protected abstract IDbDataParameter CreateParameter();
@ -97,12 +86,16 @@ namespace MCGalaxy.SQL {
}
void FillParams(IDbCommand cmd) {
foreach (var param in parameters) {
IDbDataParameter dParam = CreateParameter();
dParam.ParameterName = param.Key;
dParam.Value = param.Value;
cmd.Parameters.Add(dParam);
object[] args = parameters;
if (args == null || args.Length == 0) return;
string[] names = Database.GetParamNames(args.Length);
for (int i = 0; i < args.Length; i++) {
IDbDataParameter dbParam = CreateParameter();
dbParam.ParameterName = names[i];
dbParam.Value = args[i];
cmd.Parameters.Add(dbParam);
}
}
}
}
}

View File

@ -42,8 +42,10 @@ namespace MCGalaxy.Games {
if (Status != CountdownGameStatus.RoundInProgress || !FreezeMode) return;
if (!Remaining.Contains(p)) return;
if (next.X != p.CountdownFreezeX || next.Z != p.CountdownFreezeZ) {
next.X = p.CountdownFreezeX; next.Z = p.CountdownFreezeZ;
int freezeX = p.Extras.GetInt("MCG_CD_X");
int freezeZ = p.Extras.GetInt("MCG_CD_Z");
if (next.X != freezeX || next.Z != freezeZ) {
next.X = freezeX; next.Z = freezeZ;
p.SendPos(Entities.SelfID, next, new Orientation(yaw, pitch));
}

View File

@ -104,8 +104,8 @@ namespace MCGalaxy.Games {
Player[] players = Players.Items;
foreach (Player pl in players) {
Position pos = pl.Pos;
pl.CountdownFreezeX = pos.X;
pl.CountdownFreezeZ = pos.Z;
pl.Extras.PutInt("MCG_CD_X", pos.X);
pl.Extras.PutInt("MCG_CD_Z", pos.Z);
}
RemoveAllSquareBorders();
}

View File

@ -47,10 +47,10 @@ namespace MCGalaxy.Games {
}
public bool Remove(string name) {
return Members.CaselessRemove(name);
return Members.CaselessRemove(name);
}
public void RemoveIfEmpty() {
public void DeleteIfEmpty() {
if (Members.Count > 0) return;
Teams.Remove(this);
}

View File

@ -82,7 +82,7 @@ namespace MCGalaxy.Generator {
}
static Dictionary<string, MapGenerator> simpleGens, advGens;
static Dictionary<string, MapGenerator> simpleGens, advGens;
public static void RegisterSimpleGen(string theme, MapGenerator gen) {
simpleGens[theme.ToLower()] = gen;
}

View File

@ -63,8 +63,9 @@ namespace MCGalaxy {
}
static void RenameDatabaseTables(string src, string dst) {
if (Database.Backend.TableExists("Block" + src))
if (Database.TableExists("Block" + src)) {
Database.Backend.RenameTable("Block" + src, "Block" + dst);
}
object srcLocker = ThreadSafeCache.DBCache.GetLocker(src);
object dstLockder = ThreadSafeCache.DBCache.GetLocker(dst);
@ -132,8 +133,9 @@ namespace MCGalaxy {
}
static void DeleteDatabaseTables(string map) {
if (Database.Backend.TableExists("Block" + map))
if (Database.TableExists("Block" + map)) {
Database.Backend.DeleteTable("Block" + map);
}
object locker = ThreadSafeCache.DBCache.GetLocker(map);
lock (locker) {

View File

@ -40,10 +40,10 @@ namespace MCGalaxy {
public const string Chat = "Chat", Guns = "Guns", Buildable = "Buildable", Deletable = "Deletable", LoadDelay = "LoadDelay";
public static List<LevelOption> Options = new List<LevelOption>() {
new LevelOption(MOTD, SetMotd, "%HSets the motd for this map. (leave blank to use default motd)"),
new LevelOption(RealmOwner, SetOwner, "%HSets the players allowed to use /realm on this map."),
new LevelOption(TreeType, SetTree, "%HSets the type of trees saplings grow into."),
new LevelOption(Speed, SetSpeed, "%HSets the delay (in milliseconds) between physics ticks."),
new LevelOption(MOTD, SetMotd, "%HSets the motd for this map. (leave blank to use default motd)"),
new LevelOption(RealmOwner, SetOwner, "%HSets the players allowed to use /realm on this map."),
new LevelOption(TreeType, SetTree, "%HSets the type of trees saplings grow into."),
new LevelOption(Speed, SetSpeed, "%HSets the delay (in milliseconds) between physics ticks."),
new LevelOption(Overload, SetOverload, "%HSets how hard (high values) or easy (low values) it is to kill physics."),
new LevelOption(Fall, SetFall, "%HSets how many blocks you can fall before dying."),
new LevelOption(Drown, SetDrown, "%HSets how long you can stay underwater (in tenths of a second) before drowning."),

View File

@ -143,10 +143,6 @@ namespace MCGalaxy {
/// <summary> Temp unique ID for this session only. </summary>
public int SessionID;
//Countdown
public int CountdownFreezeX;
public int CountdownFreezeZ;
//Tnt Wars
public bool PlayingTntWars;
public int CurrentAmountOfTnt;