Now with less LINQ usage.

This commit is contained in:
UnknownShadow200 2016-07-26 11:14:04 +10:00
parent 9b1fc58bfe
commit 8a7ed8637f
6 changed files with 63 additions and 54 deletions

View File

@ -16,11 +16,9 @@
permissions and limitations under the Licenses.
*/
using System.Collections.Generic;
using System.Linq;
namespace MCGalaxy
{
public sealed class CommandList
{
namespace MCGalaxy {
public sealed class CommandList {
public List<Command> commands = new List<Command>();
public bool AddOtherPerms = false;
@ -54,7 +52,10 @@ namespace MCGalaxy
public Command Find(string name) {
name = name.ToLower();
return commands.FirstOrDefault(cmd => cmd.name == name || cmd.shortcut == name);
foreach (Command cmd in commands) {
if (cmd.name == name || cmd.shortcut == name) return cmd;
}
return null;
}
public string FindShort(string shortcut) {

View File

@ -18,10 +18,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MCGalaxy {
/// <summary> These are extra permissions for certain commands </summary>
public static class CommandOtherPerms {
@ -43,7 +41,10 @@ namespace MCGalaxy {
}
public static OtherPerms Find(Command cmd, int number = 1) {
return list.FirstOrDefault(OtPe => OtPe.cmd == cmd && OtPe.number == number);
foreach (OtherPerms perms in list) {
if (perms.cmd == cmd && perms.number == number) return perms;
}
return null;
}
public static void Add(Command command, int Perm, string desc, int number = 1) {

View File

@ -17,7 +17,6 @@
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace MCGalaxy.Commands {
@ -73,7 +72,7 @@ namespace MCGalaxy.Commands {
Level[] loaded = LevelInfo.Loaded.Items;
for (int i = start; i < end; i++) {
string level = Path.GetFileNameWithoutExtension(files[i]);
if (!all && loaded.Any(l => l.name.CaselessEq(level))) continue;
if (!all && IsLoaded(loaded, level)) continue;
LevelPermission visitP, buildP;
bool loadOnGoto;
@ -85,6 +84,13 @@ namespace MCGalaxy.Commands {
return builder;
}
static bool IsLoaded(Level[] loaded, string level) {
foreach (Level lvl in loaded) {
if (lvl.name.CaselessEq(level)) return true;
}
return false;
}
static void RetrieveProps(string level, out LevelPermission visit,
out LevelPermission build, out bool loadOnGoto) {
visit = LevelPermission.Guest;

View File

@ -19,11 +19,9 @@
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace MCGalaxy.Commands
{
public sealed class CmdPatrol : Command
{
namespace MCGalaxy.Commands {
public sealed class CmdPatrol : Command {
public override string name { get { return "patrol"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } }
@ -33,27 +31,32 @@ namespace MCGalaxy.Commands
get { return new[] { new CommandPerm(LevelPermission.Guest, " and below are patrolled") }; }
}
public override void Use(Player p, string message)
{
public override void Use(Player p, string message) {
if (Player.IsSuper(p)) { MessageInGameOnly(p); return; }
if (message != "") { Help(p); return; }
List<string> getpatrol = (from pl in PlayerInfo.players where (int) pl.@group.Permission <= CommandOtherPerms.GetPerm(this) select pl.name).ToList();
if (getpatrol.Count <= 0)
{
Player.Message(p, "There must be at least one guest online to use this command!");
return;
List<string> getpatrol = FindToPatrol();
if (getpatrol.Count <= 0) {
Player.Message(p, "There must be at least one guest online to use this command!"); return;
}
Random random = new Random();
int index = random.Next(getpatrol.Count);
string value = getpatrol[index];
string value = getpatrol[new Random().Next(getpatrol.Count)];
Player who = PlayerInfo.FindExact(value);
Command.all.Find("tp").Use(p, who.name);
Player.Message(p, "Now visiting " + who.ColoredName + "%S.");
}
List<string> FindToPatrol() {
List<string> players = new List<string>();
int perm = CommandOtherPerms.GetPerm(this);
Player[] online = PlayerInfo.Online.Items;
foreach (Player p in online) {
if ((int)p.Rank <= perm) players.Add(p.name);
}
return players;
}
public override void Help(Player p) {
Player.Message(p, "%T/patrol");
Player.Message(p, "%HTeleports you to a random " + Group.findPermInt(CommandOtherPerms.GetPerm(this)).name + " or lower");

View File

@ -17,7 +17,6 @@
*/
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

View File

@ -17,7 +17,6 @@
*/
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
@ -38,13 +37,13 @@ namespace MCGalaxy.Util {
plainText = plainText.Replace(">", ")");
MD5 hash = MD5.Create();
byte[] saltB = hash.ComputeHash(Encoding.ASCII.GetBytes(salt));
byte[] textB = hash.ComputeHash(Encoding.ASCII.GetBytes(plainText));
byte[] textBuffer = Encoding.ASCII.GetBytes(plainText);
byte[] saltBuffer = Encoding.ASCII.GetBytes(salt);
byte[] hashedTextBuffer = hash.ComputeHash(textBuffer);
byte[] hashedSaltBuffer = hash.ComputeHash(saltBuffer);
return hash.ComputeHash(hashedSaltBuffer.Concat(hashedTextBuffer).ToArray());
byte[] data = new byte[saltB.Length + textB.Length];
Array.Copy(saltB, 0, data, 0, saltB.Length);
Array.Copy(textB, 0, data, saltB.Length, textB.Length);
return hash.ComputeHash(data);
}
internal static void StoreHash(string salt, string plainText) {