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. permissions and limitations under the Licenses.
*/ */
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace MCGalaxy namespace MCGalaxy {
{ public sealed class CommandList {
public sealed class CommandList
{
public List<Command> commands = new List<Command>(); public List<Command> commands = new List<Command>();
public bool AddOtherPerms = false; public bool AddOtherPerms = false;
@ -54,7 +52,10 @@ namespace MCGalaxy
public Command Find(string name) { public Command Find(string name) {
name = name.ToLower(); 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) { public string FindShort(string shortcut) {

View File

@ -18,10 +18,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
namespace MCGalaxy { namespace MCGalaxy {
/// <summary> These are extra permissions for certain commands </summary> /// <summary> These are extra permissions for certain commands </summary>
public static class CommandOtherPerms { public static class CommandOtherPerms {
@ -43,7 +41,10 @@ namespace MCGalaxy {
} }
public static OtherPerms Find(Command cmd, int number = 1) { 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) { public static void Add(Command command, int Perm, string desc, int number = 1) {

View File

@ -17,7 +17,6 @@
*/ */
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
namespace MCGalaxy.Commands { namespace MCGalaxy.Commands {
@ -72,8 +71,8 @@ namespace MCGalaxy.Commands {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
Level[] loaded = LevelInfo.Loaded.Items; Level[] loaded = LevelInfo.Loaded.Items;
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
string level = Path.GetFileNameWithoutExtension(files[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; LevelPermission visitP, buildP;
bool loadOnGoto; bool loadOnGoto;
@ -85,6 +84,13 @@ namespace MCGalaxy.Commands {
return builder; 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, static void RetrieveProps(string level, out LevelPermission visit,
out LevelPermission build, out bool loadOnGoto) { out LevelPermission build, out bool loadOnGoto) {
visit = LevelPermission.Guest; visit = LevelPermission.Guest;

View File

@ -2,28 +2,26 @@
Written by Jack1312 Written by Jack1312
Copyright 2011 MCForge Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands {
{ public sealed class CmdPatrol : Command {
public sealed class CmdPatrol : Command
{
public override string name { get { return "patrol"; } } public override string name { get { return "patrol"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } } public override string type { get { return CommandTypes.Moderation; } }
@ -32,28 +30,33 @@ namespace MCGalaxy.Commands
public override CommandPerm[] ExtraPerms { public override CommandPerm[] ExtraPerms {
get { return new[] { new CommandPerm(LevelPermission.Guest, " and below are patrolled") }; } 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 (Player.IsSuper(p)) { MessageInGameOnly(p); return; }
if (message != "") { Help(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(); List<string> getpatrol = FindToPatrol();
if (getpatrol.Count <= 0) if (getpatrol.Count <= 0) {
{ Player.Message(p, "There must be at least one guest online to use this command!"); return;
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[new Random().Next(getpatrol.Count)];
string value = getpatrol[index];
Player who = PlayerInfo.FindExact(value); Player who = PlayerInfo.FindExact(value);
Command.all.Find("tp").Use(p, who.name); Command.all.Find("tp").Use(p, who.name);
Player.Message(p, "Now visiting " + who.ColoredName + "%S."); 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) { public override void Help(Player p) {
Player.Message(p, "%T/patrol"); Player.Message(p, "%T/patrol");
Player.Message(p, "%HTeleports you to a random " + Group.findPermInt(CommandOtherPerms.GetPerm(this)).name + " or lower"); 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;
using System.IO; using System.IO;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;

View File

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