public bool TryDoCommand(Players.Player causedBy, string chattext) { var matched = Regex.Match(chattext, @"/spawnprotect (?<access>.+) (?<steamid>.+)"); if (!matched.Success) { Chat.Send(causedBy, "Command didn't match, use /spawnprotect [spawn|nospawn|banner|deny] steamid"); return(true); } string access = matched.Groups ["access"].Value; ulong steamid; if (!ulong.TryParse(matched.Groups ["steamid"].Value, out steamid)) { Chat.Send(causedBy, "Failed parsing steamid"); return(true); } Steamworks.CSteamID csteamid = new Steamworks.CSteamID(steamid); if (!csteamid.IsValid()) { Chat.Send(causedBy, "steamid is not valid"); return(true); } NetworkID networkId = new NetworkID(csteamid); Players.Player targetPlayer; if (!Players.TryGetPlayer(networkId, out targetPlayer)) { Chat.Send(causedBy, "Player not found or offline"); return(true); } if (access.Equals("spawn")) { if (!PermissionsManager.CheckAndWarnPermission(causedBy, SpawnProtectModEntries.PERMISSION_SUPER)) { return(true); } PermissionsManager.AddPermissionToUser(causedBy, targetPlayer, SpawnProtectModEntries.PERMISSION_SPAWN_CHANGE); } else if (access.Equals("nospawn")) { if (PermissionsManager.HasPermission(causedBy, SpawnProtectModEntries.PERMISSION_SUPER)) { PermissionsManager.RemovePermissionOfUser(causedBy, targetPlayer, SpawnProtectModEntries.PERMISSION_SPAWN_CHANGE); } } else if (access.Equals("banner")) { PermissionsManager.AddPermissionToUser(causedBy, targetPlayer, SpawnProtectModEntries.PERMISSION_BANNER_PREFIX + causedBy.ID.steamID); } else if (access.Equals("deny")) { PermissionsManager.RemovePermissionOfUser(causedBy, targetPlayer, SpawnProtectModEntries.PERMISSION_BANNER_PREFIX + causedBy.ID.steamID); } else { Chat.Send(causedBy, "Unknown access level, use /spawnprotect [spawn|nospawn|banner|deny] steamid"); } return(true); }
public static bool TryGetPlayer(string identifier, out Players.Player targetPlayer, out string error, bool includeOffline) { targetPlayer = null; if (identifier.StartsWith("'")) { if (identifier.EndsWith("'")) { identifier = identifier.Substring(1, identifier.Length - 2); } else { error = "missing ' after playername"; return(false); } } if (identifier.Length < 1) { error = "no playername given"; return(false); } ulong steamid; if (ulong.TryParse(identifier, out steamid)) { Steamworks.CSteamID csteamid = new Steamworks.CSteamID(steamid); if (csteamid.IsValid()) { NetworkID networkId = new NetworkID(csteamid); error = ""; if (Players.TryGetPlayer(networkId, out targetPlayer)) { return(true); } else { targetPlayer = null; } } } float closestDist = float.MaxValue; Players.Player closestMatch = null; foreach (Players.Player player in Players.PlayerDatabase.ValuesAsList) { if (!player.IsConnected && !includeOffline) { continue; } if (player.Name != null) { if (string.Equals(player.Name, identifier, StringComparison.InvariantCultureIgnoreCase)) { if (targetPlayer == null) { targetPlayer = player; } else { targetPlayer = null; error = "duplicate player name, pls use SteamID"; return(false); } } else { float dis = Vector3.Distance(player.Position, targetPlayer.Position); if (dis < closestDist) { closestDist = dis; closestMatch = player; } else if (dis == closestDist) { closestMatch = null; } } } } if (targetPlayer != null) { error = ""; return(true); } else if (closestMatch != null && (closestDist < closestMatch.Name.Length * 0.2)) { error = ""; targetPlayer = closestMatch; Pipliz.Log.Write(string.Format("Name '{0}' did not match, picked closest match '{1}' instead", identifier, targetPlayer.Name)); return(true); } error = "player not found"; return(false); }
public void OnPlayerDeath(UnturnedPlayer player, EDeathCause deathCause, ELimb limb, Steamworks.CSteamID murdererID) { // From observations, ID is only invalid if the player was killed by an admin command. if (!murdererID.IsValid()) { return; } // Killed if (player.CSteamID != murdererID) { var murderer = UnturnedPlayer.FromCSteamID(murdererID); // Murderer exists (doesn't exist in cases like bleeding and explosions) if (murderer.Player != null) { // Teamkilling if (Config.CheckSteamGroupTeamkill && player.SteamGroupID.Equals(murderer.SteamGroupID)) { // If the murderer is not exempt from the penalty, apply it. // Or if they are exempt, apply the death penalty to the victim. if (!murderer.HasPermission(Config.NoTeamkillPenaltyPermission)) { ApplyPenalty(murderer, Config.TeamkillXP, Instance.Translate("experience_teamkill_penalty")); } else { ApplyPenalty(player, Config.DeathXP, Instance.Translate("experience_death_penalty")); } } // Killed by Player else { KillReward(murderer, player, limb); ApplyPenalty(player, Config.DeathXP, Instance.Translate("experience_death_penalty")); } } // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault else { switch (deathCause) { // Killed by a zombie case EDeathCause.ZOMBIE: case EDeathCause.SPIT: case EDeathCause.ACID: case EDeathCause.SPARK: case EDeathCause.BURNER: case EDeathCause.BOULDER: ApplyPenalty(player, Config.ZombieXP, Instance.Translate("experience_zombie_penalty")); break; // Killed by suffocation (either high altitude or under water) case EDeathCause.BREATH: ApplyPenalty(player, Config.BreathXP, Instance.Translate("experience_breath_penalty")); break; // Killed by fire case EDeathCause.BURNING: ApplyPenalty(player, Config.FireXP, Instance.Translate("experience_fire_penalty")); break; // Killed by any other reason default: ApplyPenalty(player, Config.DeathXP, Instance.Translate("experience_death_penalty")); break; } } } // Suicide else { ApplyPenalty(player, Config.SuicideXP, Instance.Translate("experience_suicide_penalty")); } }
public static bool TryGetPlayer(string identifier, out Players.Player targetPlayer, out string error, bool includeOffline = false) { targetPlayer = null; if (identifier.StartsWith("'")) { if (identifier.EndsWith("'")) { identifier = identifier.Substring(1, identifier.Length - 2); } else { error = "missing ' after playername"; return(false); } } if (identifier.Length < 1) { error = "no playername given"; return(false); } // try to find by steamID first ulong steamid; if (ulong.TryParse(identifier, out steamid)) { Steamworks.CSteamID csteamid = new Steamworks.CSteamID(steamid); if (csteamid.IsValid()) { NetworkID networkId = new NetworkID(csteamid); error = ""; if (Players.TryGetPlayer(networkId, out targetPlayer)) { return(true); } else { targetPlayer = null; } } } // try to find by hash code var m = Regex.Match(identifier, @"#(?<hash>\d{8})"); int givenHash; if (m.Success && int.TryParse(m.Groups["hash"].Value, out givenHash)) { foreach (Players.Player player in Players.PlayerDatabase.Values) { if (!includeOffline && player.ConnectionState != Players.EConnectionState.Connected) { continue; } if (player.ID.steamID.GetHashCode() == givenHash) { if (targetPlayer == null) { targetPlayer = player; } else { targetPlayer = null; error = "duplicate hash code, please use full SteamID"; return(false); } } } if (targetPlayer != null) { error = ""; return(true); } } // try to find by string closest match Players.Player closestMatch = null; int closestDist = int.MaxValue; foreach (var player in Players.PlayerDatabase.Values) { if (!includeOffline && player.ConnectionState != Players.EConnectionState.Connected) { continue; } if (player.Name != null) { if (string.Equals(player.Name, identifier, StringComparison.InvariantCultureIgnoreCase)) { if (targetPlayer == null) { targetPlayer = player; } else { targetPlayer = null; error = "duplicate player name, pls use SteamID"; return(false); } } else { int levDist = LevenshteinDistance.Compute(player.Name.ToLower(), identifier.ToLower()); if (levDist < closestDist) { closestDist = levDist; closestMatch = player; } else if (levDist == closestDist) { closestMatch = null; } } } } if (targetPlayer != null) { error = ""; return(true); } else if (closestMatch != null && (closestDist < closestMatch.Name.Length * 0.2)) { error = ""; targetPlayer = closestMatch; Log.Write($"Name '{identifier}' did not match, picked closest match '{targetPlayer.Name}' instead"); return(true); } error = "player not found"; return(false); }