using System; using System.Collections.Generic; using Newtonsoft.Json; using UnityEngine; using Oxide.Core; using System.Linq; namespace Oxide.Plugins { [Info("RollTheDice", "clang", "1.0.4")] [Description("Randomized item giving with possible negative outcomes")] /* //////// // Update for ItemV2 by Vladimir.Kzi version is 1.0.9, plugin author clang //////// */ class RollTheDice : HurtworldPlugin { ////////////////////////////////////////////////////////////////////////////////////////// ///// Plugin initialization ////////////////////////////////////////////////////////////////////////////////////////// string Msg(string msg, string SteamId = null) => lang.GetMessage(msg, this, SteamId); void ShowMsg(PlayerSession session, string prefix, string msg) => hurt.SendChatMessage(session, prefix, msg); void BroadcastMsg(string prefix, string msg) => Server.Broadcast(msg, prefix); class ItemsList { public ItemGeneratorAsset ItemGen; public string ItemName; public int ItemId; public string ItemGuID; } List allItemsList = new List(); void Loaded() { LoadData(); LoadMessages(); try { rollsData = Interface.Oxide.DataFileSystem.ReadObject>>("Rolls_Data"); } catch { rollsData = new Dictionary>(); } } void OnServerInitialized() { InitializePermissions(); AllItemsList(); } void InitializePermissions() { foreach (var roll in storedData.Rolls.Values) { if (string.IsNullOrEmpty(roll.permission)) continue; permission.RegisterPermission(roll.permission, this); } } ////////////////////////////////////////////////////////////////////////////////////////// ///// Configuration ////////////////////////////////////////////////////////////////////////////////////////// void OnPlayerRespawn(PlayerSession session) { if (!storedData.Rolls.ContainsKey("autoroll")) return; var thereturn = Interface.Oxide.CallHook("CanRedeemRoll", session); if (thereturn == null) { var playerinv = session.WorldPlayerEntity.GetComponent(); for(var j = 0; j < playerinv.Capacity; j++) { if (playerinv.GetSlot(j) == null) continue; if (playerinv.GetSlot(j).Generator == null) continue; Singleton.Instance.ReleaseInstanceExplicit(playerinv.GetSlot(j)); playerinv.SetSlot(j, null); } GiveRoll(session, "autoroll"); } } ////////////////////////////////////////////////////////////////////////////////////////// ///// Roll Redeemer ////////////////////////////////////////////////////////////////////////////////////////// private void TryGiveRoll(PlayerSession session, string rollname) { var success = CanRedeemARoll(session, rollname); if (!success) return; success = GiveRoll(session, rollname); if (!success) return; ShowMsg(session, Msg("MSG_Prefix"), Msg("TryGiveRoll_Completed")); ProccessRollGiven(session, rollname); } void ProccessRollGiven(PlayerSession session, string rollname) { Roll roll; if (string.IsNullOrEmpty(rollname) || !storedData.Rolls.TryGetValue(rollname, out roll)) return; var rollData = GetRollData(session.SteamId.m_SteamID, rollname); if (roll.max > 0) rollData.max += 1; if (roll.cooldown > 0) rollData.cooldown = CurrentTime() + roll.cooldown; } bool GiveRoll(PlayerSession session, string rollname) { Roll roll; if (string.IsNullOrEmpty(rollname) || !storedData.Rolls.TryGetValue(rollname, out roll)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("GiveRoll_NotExist").Replace("{rollname}", rollname)); return false; } var playerinv = session.WorldPlayerEntity.GetComponent(); var amanager = Singleton.Instance; var itemmanager = Singleton.Instance; var playerEntity = session.WorldPlayerEntity; var rolledItem = new RollItem(); var rolledReplacement = new RollItem(); var rolledStat = new RollStat(); var rolledSpawn = new RollSpawn(); var ManagerInstance = GameManager.Instance; var sumItemChance = 0; var sumStatChance = 0; var sumSpawnChance = 0; var sumReplaceChance = 0; var totalChance = 0; var count = 0; foreach (var rollem in roll.items) sumItemChance += rollem.chance; foreach (var rollstat in roll.stats) sumStatChance += rollstat.chance; foreach (var rollspawn in roll.spawns) sumSpawnChance += rollspawn.chance; foreach (var rollreplace in roll.replacements) sumReplaceChance += rollreplace.chance; totalChance = sumItemChance + sumStatChance + sumSpawnChance + sumReplaceChance; if (totalChance == 0) return false; var rnd = Core.Random.Range(1, totalChance); if (rnd <= sumItemChance) { for (int i = 0; i < roll.items.Count && rolledItem.guID == null; i++) { count += roll.items[i].chance; if (count >= rnd) rolledItem = roll.items[i]; } ItemGeneratorAsset generator = FindItem(rolledItem.guID.ToString()); if(generator == null) { PrintWarning("Something is wrong with the item:" + rolledItem.guID.ToString()); return false; } ItemObject item = itemmanager.CreateItem(generator, rolledItem.amount); if (playerinv.GetSlot(0) == null) { playerinv.SetSlot(0, item); amanager.ItemReceivedServer(item, item.StackSize, session.Player); playerinv.Invalidate(false); } else { itemmanager.GiveItem(session.Player, generator, rolledItem.amount); } BroadcastMsg(Msg("MSG_Prefix"), Msg("GiveRoll_ItemGive") .Replace("{player}", session.Identity.Name) .Replace("{amount}", rolledItem.amount.ToString()) .Replace("{itemname}", item.Generator.name.ToString().Split('/').Last())); } else if (rnd > sumItemChance && rnd <= sumStatChance + sumItemChance) { count = sumItemChance; for (int i = 0; i < roll.stats.Count && rolledStat.type == null; i++) { count += roll.stats[i].chance; if (count >= rnd) rolledStat = roll.stats[i]; } EntityStats stats = session.WorldPlayerEntity.GetComponent(); EntityFluidEffectKey keyStat; switch (rolledStat.type) { case "intimidation": keyStat = EntityFluidEffectKeyDatabase.Instance.Intimidation; break; case "toxin": keyStat = EntityFluidEffectKeyDatabase.Instance.Toxin; break; case "health": keyStat = EntityFluidEffectKeyDatabase.Instance.Health; break; case "inttemp": keyStat = EntityFluidEffectKeyDatabase.Instance.InternalTemperature; break; case "exttemp": keyStat = EntityFluidEffectKeyDatabase.Instance.ExternalTemperature; break; default: keyStat = EntityFluidEffectKeyDatabase.Instance.Toxin; break; } var currValue = stats.GetFluidEffect(keyStat).GetValue(); var newValue = currValue + rolledStat.amount; stats.GetFluidEffect(keyStat).SetValue(newValue); BroadcastMsg(Msg("MSG_Prefix"), Msg("GiveRoll_StatGive") .Replace("{player}", session.Identity.Name) .Replace("{amount}", rolledStat.amount.ToString()) .Replace("{stat}", rolledStat.type.ToString().ToLower())); } else if (rnd > sumItemChance + sumStatChance && rnd <= sumStatChance + sumItemChance + sumSpawnChance) { count = sumItemChance + sumStatChance; GameObject Obj = new GameObject(); for (int i = 0; i < roll.spawns.Count && rolledSpawn.spawnName == null; i++) { count += roll.spawns[i].chance; if (count >= rnd) rolledSpawn = roll.spawns[i]; } var iterations = 0; var numberSpawned = 0; for (numberSpawned = 0; numberSpawned < rolledSpawn.amount && iterations < rolledSpawn.amount*100;) { Vector3 position = new Vector3(playerEntity.transform.position.x + Core.Random.Range(-10, 10), playerEntity.transform.position.y + Core.Random.Range(0, 10), playerEntity.transform.position.z + Core.Random.Range(-10, 10)); RaycastHit hitInfo; Physics.Raycast(position, Vector3.down, out hitInfo); { Quaternion rotation = Quaternion.Euler(0.0f, (float)UnityEngine.Random.Range(0f, 360f), 0.0f); rotation = Quaternion.FromToRotation(Vector3.down, hitInfo.normal) * rotation; NetworkInstantiateConfig prefabES = null; foreach (NetworkInstantiateConfig prefabs in Resources.FindObjectsOfTypeAll()) { if (prefabs.name.ToString() == rolledSpawn.spawnName.ToString()) prefabES = prefabs; } Obj = Singleton.Instance.NetInstantiate(prefabES, hitInfo.point, Quaternion.identity, GameManager.GetSceneTime()); Destroy(Obj); numberSpawned++; } iterations++; } BroadcastMsg(Msg("MSG_Prefix"), Msg("GiveRoll_SpawnGive") .Replace("{player}", session.Identity.Name) .Replace("{amount}", numberSpawned.ToString()) .Replace("{creature}", creatures[rolledSpawn.spawnName.ToLower()])); } else { var replacementFound = false; count = sumStatChance + sumItemChance + sumSpawnChance; for (int i = 0; i < roll.replacements.Count && rolledReplacement.guID == null; i++) { count += roll.replacements[i].chance; if (count >= rnd) rolledReplacement = roll.replacements[i]; } if(playerinv.StorageConfig.Slots.Length > 0) { var noOfIterations = 0; for (int i = Core.Random.Range(0, playerinv.StorageConfig.Slots.Length-1); !replacementFound && noOfIterations < 1000; i = Core.Random.Range(0, playerinv.StorageConfig.Slots.Length - 1)) { if (playerinv.GetSlot(i) != null) { ItemGeneratorAsset generator = FindItem(rolledItem.guID.ToString()); if(generator == null) { PrintWarning("Something is wrong with the item:" + rolledItem.guID.ToString()); return false; } ItemObject item = itemmanager.CreateItem(generator, rolledReplacement.amount); var replaceedItem = playerinv.GetSlot(i); playerinv.SetSlot(i, item); amanager.ItemReceivedServer(item, item.StackSize, session.Player); playerinv.Invalidate(false); replacementFound = true; BroadcastMsg(Msg("MSG_Prefix"), Msg("GiveRoll_ItemReplace") .Replace("{player}", session.Identity.Name) .Replace("{amount1}", replaceedItem.StackSize.ToString()) .Replace("{itemname1}", replaceedItem.Generator.name.ToString().Split('/').Last()) .Replace("{amount2}", playerinv.GetSlot(i).StackSize.ToString()) .Replace("{itemname2}", playerinv.GetSlot(i).Generator.name.ToString().Split('/').Last())); } noOfIterations++; } } } return true; } void Destroy(GameObject obj) { timer.Once(600, () => { Singleton.Instance.NetDestroy(HNetworkExtensions.HNetworkView(obj)); }); } ////////////////////////////////////////////////////////////////////////////////////////// ///// Check Rolls ////////////////////////////////////////////////////////////////////////////////////////// bool isRoll(string rollname) => !string.IsNullOrEmpty(rollname) && storedData.Rolls.ContainsKey(rollname); static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0); static double CurrentTime() { return DateTime.UtcNow.Subtract(epoch).TotalSeconds; } bool CanSeeRoll(PlayerSession session, string rollname, out string reason) { reason = string.Empty; Roll roll; if (string.IsNullOrEmpty(rollname) || !storedData.Rolls.TryGetValue(rollname, out roll)) return false; if (roll.hide) return false; if (roll.authlevel > 0) if (!session.IsAdmin) return false; if (!string.IsNullOrEmpty(roll.permission)) if (!permission.UserHasPermission(session.SteamId.ToString(), roll.permission)) return false; if (roll.max > 0) { var left = GetRollData(session.SteamId.m_SteamID, rollname).max; if (left >= roll.max) { reason += "- 0 "+Msg("Roll_LeftUse")+""; return false; } reason += $"- {(roll.max - left)} "+Msg("Roll_LeftUse")+""; } if (roll.cooldown > 0) { var cd = GetRollData(session.SteamId.m_SteamID, rollname).cooldown; var ct = CurrentTime(); if (cd > ct && cd != 0.0) { reason += $"- {ConvertSecondsToDate(Math.Abs(Math.Ceiling(cd - ct)))}"; return false; } } return true; } bool CanRedeemARoll(PlayerSession session, string rollname) { Roll roll; if (string.IsNullOrEmpty(rollname) || !storedData.Rolls.TryGetValue(rollname, out roll)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_NotExist").Replace("{rollname}", rollname)); return false; } var thereturn = Interface.Oxide.CallHook("CanRedeemRoll", session); if (thereturn != null) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_NotAllowed")); return false; } if (roll.authlevel > 0) if (!session.IsAdmin) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_NoLevel")); return false; } if (!string.IsNullOrEmpty(roll.permission)) if (!permission.UserHasPermission(session.SteamId.ToString(), roll.permission)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_NoPerm")); return false; } var rollData = GetRollData(session.SteamId.m_SteamID, rollname); if (roll.max > 0) if (rollData.max >= roll.max) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_Out")); return false; } if (roll.cooldown > 0) { var ct = CurrentTime(); if (rollData.cooldown > ct && rollData.cooldown != 0.0){ ShowMsg(session, Msg("MSG_Prefix"), Msg("CanRedeemARoll_Wait") .Replace("{seconds}", ConvertSecondsToDate(Math.Abs(Math.Ceiling(rollData.cooldown - ct))))); return false; } } return true; } ////////////////////////////////////////////////////////////////////////////////////// // Roll Class ////////////////////////////////////////////////////////////////////////////////////// class RollItem : RollSelection { public string guID; } class RollStat : RollSelection { public string type; } class RollSpawn : RollSelection { public string spawnName; } class RollSelection { public int amount; public int chance; } class Roll { public string name; public string description; public int max; public double cooldown; public int authlevel; public bool hide; public string permission; public List items = new List(); public List stats = new List(); public List spawns = new List(); public List replacements = new List(); } ////////////////////////////////////////////////////////////////////////////////////// // Data Manager ////////////////////////////////////////////////////////////////////////////////////// void SaveRollsData() => Interface.Oxide.DataFileSystem.WriteObject("Rolls_Data", rollsData); StoredData storedData; Dictionary> rollsData; Dictionary creatures = new Dictionary { {"aiborserver","bor"}, {"aiyetiserver","yeti"}, {"aitokarserver","tokar"} }; void LoadMessages() { // English lang.RegisterMessages(new Dictionary { {"MSG_Prefix","[ROLL]"}, {"GiveRoll_ItemGive","{player} has rolled the dice and recieved {amount} {itemname}"}, {"GiveRoll_StatGive","{player} has rolled the dice and recieved {amount} {stat} points"}, {"GiveRoll_SpawnGive","{player} has rolled the dice and spawned {amount} {creature}"}, {"GiveRoll_ItemReplace","{player} has rolled the dice and replaced {amount1} {itemname1} with {amount2} {itemname2}"}, {"SendListRollEdition_1","permission \"permission name\" => set the permission needed to get this roll"}, {"SendListRollEdition_2","description \"description text here\" => set a description for this roll"}, {"SendListRollEdition_3","authlevel XXX"}, {"SendListRollEdition_4","cooldown XXX"}, {"SendListRollEdition_5","max XXX"}, {"SendListRollEdition_6","additem => set new items for your roll"}, {"SendListRollEdition_7","addstat => alter player stat"}, {"SendListRollEdition_8","addspawn => spawn creatures on player"}, {"SendListRollEdition_9","addreplace => replace random inventory item"}, {"SendListRollEdition_10","hide TRUE/FALSE => dont show this roll in lists (EVER)"}, {"CmdRoll_CantSeeRoll", "{rollname} - {desc} {reason}"}, {"CmdRoll_Player1","====== Player Commands ======"}, {"CmdRoll_Player2","/roll => to get the list of rolls"}, {"CmdRoll_Player3","/roll ROLLNAME => to redeem the roll"}, {"CmdRoll_Admin1", "====== Admin Commands ======"}, {"CmdRoll_Admin2","/roll add ROLLNAME => add a roll"}, {"CmdRoll_Admin3","/roll remove ROLLNAME => remove a roll"}, {"CmdRoll_Admin4","/roll edit ROLLNAME => edit a roll"}, {"CmdRoll_Admin5","/roll list => get a raw list of rolls (the real full list)"}, {"CmdRoll_Admin6","/roll give PLAYER/STEAMID ROLLNAME => make a player roll the dice"}, {"CmdRoll_Admin7","/roll resetrolls => deletes all rolls"}, {"CmdRoll_Admin8","/roll resetdata => reset player data"}, {"CmdRoll_NoAccess","You don't have access to this command"}, {"CmdRoll_RollList","{rollname} - {description}"}, {"CmdRoll_ResetAll","All player and roll data has been reset"}, {"CmdRoll_ResetPlayer", "All player data has been reset"}, {"CmdRoll_PlayerReset", "Data player '{player}' has been reset!"}, {"CmdRoll_PlayerNoReset", "Player '{player}' has not data!"}, {"CmdRoll_NewRollExists", "This roll already exists"}, {"CmdRoll_NewRollNotExists", "This roll doesn't seem to exist"}, {"CmdRoll_NewRoll", "You've created a new roll: {rollname}"}, {"CmdRoll_Give","/roll give PLAYER/STEAMID ROLLNAME"}, {"CmdRoll_GiveNoPlayer", "No player found"}, {"CmdRoll_GiveMultiple", "Multiple players found"}, {"CmdRoll_Given","You gave {player} the roll: {rollname}"}, {"CmdRoll_GivenReceive", "You've received the roll {rollname} from {player} enjoy!"}, {"CmdRoll_Edit", "You are now editing roll: {rollname}"}, {"CmdRoll_Remove","{rollname} was removed"}, {"CmdRoll_NotInEdit", "You are not creating or editing a roll"}, {"CmdRoll_Dirty", "There was an error while getting this roll, was it changed while you were editing it?"}, {"CmdRoll_AddItem", "Item added successfully"}, {"CmdRoll_AddStat", "Stat added successfully"}, {"CmdRoll_AddSpawn", "Spawn added successfully"}, {"CmdRoll_AddReplace", "Item replacement added successfully"}, {"CmdRoll_InvalidArgs", "{arg} Invalid agrument"}, {"GiveRoll_NotExist", "The roll '{rollname}' doesn't exist"}, {"TryGiveRoll_Completed", "Roll Completed" }, {"CanRedeemARoll_NotExist", "The roll '{rollname}' doesn't exist"}, {"CanRedeemARoll_NotAllowed", "You are not allowed to roll the dice at this moment"}, {"CanRedeemARoll_NoLevel", "You don't have the level to use this roll"}, {"CanRedeemARoll_NoPerm", "You don't have the permissions to use this roll"}, {"CanRedeemARoll_Out", "You already took all your chances with this roll"}, {"CanRedeemARoll_Wait", "You need to wait {seconds} seconds to use this roll"}, {"msg_Days", "d."}, {"msg_Hours", "h."}, {"msg_Minutes", "m."}, {"msg_Seconds", "s."}, {"Roll_LeftUse", "left"}, {"Roll_ListAll", "List:"} }, this, "en"); } class StoredData { public Dictionary Rolls = new Dictionary(); } class RollData { public int max; public double cooldown; } void ResetData() { rollsData.Clear(); SaveRollsData(); } void Unload() => SaveRollsData(); void OnServerSave() => SaveRollsData(); void SaveRolls() => Interface.Oxide.DataFileSystem.WriteObject("Rolls", storedData); void LoadData() { var rolls = Interface.Oxide.DataFileSystem.GetFile("Rolls"); try { rolls.Settings.NullValueHandling = NullValueHandling.Ignore; storedData = rolls.ReadObject(); } catch { storedData = new StoredData(); } rolls.Settings.NullValueHandling = NullValueHandling.Include; } RollData GetRollData(ulong userID, string rollname) { Dictionary rollDatas; if (!rollsData.TryGetValue(userID, out rollDatas)) rollsData[userID] = rollDatas = new Dictionary(); RollData rollData; if (!rollDatas.TryGetValue(rollname, out rollData)) rollDatas[rollname] = rollData = new RollData(); return rollData; } ////////////////////////////////////////////////////////////////////////////////////// // Roll Editor ////////////////////////////////////////////////////////////////////////////////////// readonly Dictionary rollEditor = new Dictionary(); ////////////////////////////////////////////////////////////////////////////////////// // Console Command ////////////////////////////////////////////////////////////////////////////////////// List FindPlayer(string arg) { var listPlayers = new List(); ulong steamid; ulong.TryParse(arg, out steamid); var lowerarg = arg.ToLower(); foreach (var pair in GameManager.Instance.GetSessions()) { var session = pair.Value; if (!session.IsLoaded) continue; if (steamid != 0L) if (session.SteamId.m_SteamID == steamid) { listPlayers.Clear(); listPlayers.Add(session); return listPlayers; } var lowername = session.Identity.Name.ToLower(); if (lowername.Contains(lowerarg)) listPlayers.Add(session); } return listPlayers; } ////////////////////////////////////////////////////////////////////////////////////// // Chat Command ////////////////////////////////////////////////////////////////////////////////////// bool HasAccess(PlayerSession session) => session.IsAdmin; void SendListRollEdition(PlayerSession session) { ShowMsg(session, null, Msg("SendListRollEdition_1")); ShowMsg(session, null, Msg("SendListRollEdition_2")); ShowMsg(session, null, Msg("SendListRollEdition_3")); ShowMsg(session, null, Msg("SendListRollEdition_4")); ShowMsg(session, null, Msg("SendListRollEdition_5")); ShowMsg(session, null, Msg("SendListRollEdition_6")); ShowMsg(session, null, Msg("SendListRollEdition_7")); ShowMsg(session, null, Msg("SendListRollEdition_8")); ShowMsg(session, null, Msg("SendListRollEdition_9")); ShowMsg(session, null, Msg("SendListRollEdition_10")); } [ChatCommand("rolls")] void cmdRolls(PlayerSession session, string command, string[] args) { var reason = string.Empty; ShowMsg(session, Msg("MSG_Prefix"), Msg("Roll_ListAll")); foreach (var pair in storedData.Rolls) { var cansee = CanSeeRoll(session, pair.Key, out reason); if (!cansee && string.IsNullOrEmpty(reason)) continue; ShowMsg(session, null, Msg("CmdRoll_CantSeeRoll") .Replace("{rollname}", pair.Value.name) .Replace("{desc}",pair.Value.description) .Replace("{reason}", reason)); } return; } [ChatCommand("roll")] void cmdRoll(PlayerSession session, string command, string[] args) { if (args.Length == 0) { var reason = string.Empty; ShowMsg(session, Msg("MSG_Prefix"), Msg("Roll_ListAll")); foreach (var pair in storedData.Rolls) { var cansee = CanSeeRoll(session, pair.Key, out reason); if (!cansee && string.IsNullOrEmpty(reason)) continue; ShowMsg(session, null, Msg("CmdRoll_CantSeeRoll") .Replace("{rollname}", pair.Value.name) .Replace("{desc}",pair.Value.description) .Replace("{reason}", reason)); } return; } if (args.Length == 1) { switch (args[0]) { case "help": ShowMsg(session, null, Msg("CmdRoll_Player1")); ShowMsg(session, null, Msg("CmdRoll_Player2")); ShowMsg(session, null, Msg("CmdRoll_Player3")); if (!HasAccess(session)) return; ShowMsg(session, null, Msg("CmdRoll_Admin1")); ShowMsg(session, null, Msg("CmdRoll_Admin2")); ShowMsg(session, null, Msg("CmdRoll_Admin3")); ShowMsg(session, null, Msg("CmdRoll_Admin4")); ShowMsg(session, null, Msg("CmdRoll_Admin5")); ShowMsg(session, null, Msg("CmdRoll_Admin6")); ShowMsg(session, null, Msg("CmdRoll_Admin7")); ShowMsg(session, null, Msg("CmdRoll_Admin8")); break; case "add": case "remove": case "edit": if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } ShowMsg(session, null, Msg("CmdRoll_Admin4")); break; case "give": if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } ShowMsg(session, null, Msg("CmdRoll_Admin6")); break; case "list": if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } ShowMsg(session, Msg("MSG_Prefix"), Msg("Roll_ListAll")); foreach (var roll in storedData.Rolls.Values) ShowMsg(session, null, Msg("CmdRoll_RollList").Replace("{rollname}", roll.name).Replace("{description}", roll.description)); break; case "additem": break; case "addstat": break; case "addspawn": break; case "addreplace": break; case "resetrolls": if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } storedData.Rolls.Clear(); rollEditor.Clear(); ResetData(); SaveRolls(); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_ResetAll")); break; case "resetdata": if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } ResetData(); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_ResetPlayer")); break; default: TryGiveRoll(session, args[0].ToLower()); break; } if (args[0] != "additem" && args[0] != "addstat" && args[0] != "addspawn" && args[0] != "addreplace") return; } if (!HasAccess(session)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NoAccess")); return; } string rollname; switch (args[0]) { case "add": rollname = args[1].ToLower(); if (storedData.Rolls.ContainsKey(rollname)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NewRollExists")); return; } storedData.Rolls[rollname] = new Roll { name = args[1] }; rollEditor[session.SteamId.m_SteamID] = rollname; ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NewRoll").Replace("{rollname}", args[1])); SendListRollEdition(session); break; case "give": if (args.Length < 3) { ShowMsg(session, null, Msg("CmdRoll_Admin6")); return; } rollname = args[2].ToLower(); if (!storedData.Rolls.ContainsKey(rollname)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NewRollNotExists")); return; } var findPlayers = FindPlayer(args[1]); if (findPlayers.Count == 0) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_GiveNoPlayer")); return; } if (findPlayers.Count > 1) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_GiveMultiple")); return; } GiveRoll(findPlayers[0], rollname); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_Given").Replace("{player}", findPlayers[0].Identity.Name).Replace("{rollname}", storedData.Rolls[rollname].name)); ShowMsg(findPlayers[0], Msg("MSG_Prefix"), Msg("CmdRoll_GivenReceive").Replace("{player}", session.Identity.Name).Replace("{rollname}", storedData.Rolls[rollname].name)); break; case "edit": rollname = args[1].ToLower(); if (!storedData.Rolls.ContainsKey(rollname)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NewRollNotExists")); return; } rollEditor[session.SteamId.m_SteamID] = rollname; ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_Edit").Replace("{rollname}", rollname)); SendListRollEdition(session); break; case "remove": rollname = args[1].ToLower(); if (!storedData.Rolls.Remove(rollname)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NewRollNotExists")); return; } ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_Remove").Replace("{rollname}", rollname)); if (rollEditor[session.SteamId.m_SteamID] == rollname) rollEditor.Remove(session.SteamId.m_SteamID); break; case "resetdata": var fPlayers = FindPlayer(args[1]); if (fPlayers.Count == 0) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_GiveNoPlayer")); return; } if (fPlayers.Count > 1) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_GiveMultiple")); return; } if (!rollsData.ContainsKey((ulong)fPlayers[0].SteamId)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_PlayerNoReset").Replace("{player}", fPlayers[0].Identity.Name)); return; } else { rollsData.Remove((ulong)fPlayers[0].SteamId); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_PlayerReset").Replace("{player}", fPlayers[0].Identity.Name)); } break; default: if (!rollEditor.TryGetValue(session.SteamId.m_SteamID, out rollname)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_NotInEdit")); return; } Roll roll; if (!storedData.Rolls.TryGetValue(rollname, out roll)) { ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_Dirty")); return; } for (var i = 0; i < args.Length; i++) { object editvalue; var key = args[i].ToLower(); switch (key) { case "additem": RollItem item = new RollItem(); item.guID = args[++i]; item.amount = Math.Abs(int.Parse(args[++i])); item.chance = Math.Abs(int.Parse(args[++i])); roll.items.Add(item); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_AddItem")); continue; case "addstat": RollStat stat = new RollStat(); var statType = args[++i].ToLower(); var statAmount = args[++i].ToLower(); var statChance = args[++i].ToLower(); bool statFound = true; switch (statType) { case "intimidation": stat.type = "intimidation"; break; case "toxin": stat.type = "toxin"; break; case "health": stat.type = "health"; break; case "inttemp": stat.type = "inttemp"; break; case "exttemp": stat.type = "exttemp"; break; default: statFound = false; break; } if (statFound) { stat.amount = int.Parse(statAmount); stat.chance = Math.Abs(int.Parse(statChance)); roll.stats.Add(stat); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_AddStat")); } continue; case "addspawn": RollSpawn spawn = new RollSpawn(); var spawnType = args[++i].ToLower(); var spawnAmount = args[++i].ToLower(); var spawnChance = args[++i].ToLower(); bool spawnFound = true; switch (spawnType) { case "bor": spawn.spawnName = "AIBorServer"; break; case "yeti": spawn.spawnName = "AIYetiServer"; break; case "tokar": spawn.spawnName = "AITokarServer"; break; default: spawnFound = false; break; } if (spawnFound) { spawn.amount = Math.Abs(int.Parse(spawnAmount)); spawn.chance = Math.Abs(int.Parse(spawnChance)); roll.spawns.Add(spawn); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_AddSpawn")); } continue; case "addreplace": RollItem itemReplace = new RollItem(); itemReplace.guID = args[++i]; itemReplace.amount = Math.Abs(int.Parse(args[++i])); itemReplace.chance = Math.Abs(int.Parse(args[++i])); roll.replacements.Add(itemReplace); ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_AddReplace")); continue; case "name": continue; case "description": editvalue = roll.description = args[++i]; break; case "max": editvalue = roll.max = int.Parse(args[++i]); break; case "cooldown": editvalue = roll.cooldown = double.Parse(args[++i]); break; case "authlevel": editvalue = roll.authlevel = int.Parse(args[++i]); break; case "hide": editvalue = roll.hide = bool.Parse(args[++i]); break; case "permission": editvalue = roll.permission = args[++i]; InitializePermissions(); break; default: ShowMsg(session, Msg("MSG_Prefix"), Msg("CmdRoll_InvalidArgs").Replace("{arg}", args[i])); continue; } } break; } SaveRolls(); } //////////////////////////////////////////////////////////////////////////////////////// // Создаем список всех предметов( Один раз за запуск сервера ) //////////////////////////////////////////////////////////////////////////////////////// void AllItemsList() { foreach (var item in GlobalItemManager.Instance.GetGenerators()) { if (item.Value?.name != null) { ItemsList aitem = new ItemsList(); aitem.ItemGen = item.Value; aitem.ItemName = item.Value.name; aitem.ItemId = item.Value.GeneratorId; aitem.ItemGuID = RuntimeHurtDB.Instance.GetGuid(item.Value); allItemsList.Add(aitem); } } } //////////////////////////////////////////////////////////////////////////////////////// // Ищем нужный предмет в списке всех предметов //////////////////////////////////////////////////////////////////////////////////////// ItemGeneratorAsset FindItem(string itemNameOrIdOrGuid) { List items = allItemsList; ItemsList aitem; ItemGeneratorAsset item; int itemId; if (int.TryParse(itemNameOrIdOrGuid, out itemId)) { aitem = items.First(i => i.ItemId == itemId); item = aitem.ItemGen; } else if((from x in items where x.ItemGuID == itemNameOrIdOrGuid select x).Count() > 0) { aitem = items.First(i => i.ItemGuID.Contains(itemNameOrIdOrGuid)); item = aitem.ItemGen; } else if((from x in items where x.ItemName.ToLower() == itemNameOrIdOrGuid.ToLower() select x).Count() > 0) { aitem = items.First(i => i.ItemName.ToLower() == itemNameOrIdOrGuid.ToLower()); item = aitem.ItemGen; } else { item = null; } return item; } //////////////////////////////////////// /// Время //////////////////////////////////////// private string ConvertSecondsToDate(double numOfSeconds) { int days = (int) (numOfSeconds / 3600) / 24; int hours = (int) (numOfSeconds / 3600); int minutes = (int) (numOfSeconds / 60) % 60; int seconds = (int)numOfSeconds % 60; string SecondsToDate = ""; string sDays = ""; string sHours = ""; string sMinutes = ""; string sSeconds = ""; if (days > 0) { sDays = days.ToString() + Msg("msg_Days") + " "; } if (hours > 0) { sHours = hours.ToString() + Msg("msg_Hours") + " "; } if (minutes > 0) { sMinutes = minutes.ToString() + Msg("msg_Minutes") + " "; } if (seconds > 0) { sSeconds = seconds.ToString() + Msg("msg_Seconds") + " "; } SecondsToDate = sDays + sHours + sMinutes + sSeconds; if (SecondsToDate.EndsWith(" ")) SecondsToDate = SecondsToDate.Substring(0, SecondsToDate.Length - 1); return SecondsToDate; } } }