private static string GivePackage(string steamID, string packageID) { PlayerInfo player = GetPlayerInfo(steamID); if (player == null) { return("User not found!"); } PackageInfo pi = GetPackage(packageID); if (pi == null) { return("Package not found!"); } var peer = ZNet.instance.GetConnectedPeers().Find(p => (p.m_socket as ZSteamSocket).GetPeerID().ToString() == steamID); if (peer == null) { return("User not online!"); } JsonCommand sendCommand = new JsonCommand() { command = "PurchaseResult", currency = player.currency, items = GetPackageItems(pi, player) }; peer.m_rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); return(null); }
private void WindowBuilder(int id) { GUI.DragWindow(new Rect(0, 0, windowWidth.Value, 20)); GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(windowWidth.Value) }); GUILayout.Label(windowTitleText, titleStyle); GUILayout.Label(string.Format(myCurrencyString.Value, string.Format(currencyString.Value, myCurrency)), currencyStyle); float width = windowWidth.Value - 70; float itemWidth = width / packagesPerRow.Value; scrollPosition = GUILayout.BeginScrollView(scrollPosition, new GUILayoutOption[] { GUILayout.Width(windowWidth.Value - 20) }); GUILayout.Space(10); GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(width) }); GUILayout.BeginHorizontal(); for (int i = 0; i < storePackages.Count; i++) { if (i > 0 && i % packagesPerRow.Value == 0) { GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); } PackageInfo pi = storePackages[i]; string texture = textureDict.ContainsKey(pi.type) ? pi.type : "Common"; GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(itemWidth) }); if (GUILayout.Button(textureDict[texture], new GUILayoutOption[] { GUILayout.Width(itemWidth), GUILayout.Height(itemWidth) })) { if (myCurrency >= pi.price) { ZRpc serverRPC = ZNet.instance.GetServerRPC(); if (serverRPC != null) { Dbgl("Requesting store data"); JsonCommand command = new JsonCommand() { command = "BuyPackage", packageid = pi.id }; string commandJson = JsonUtility.ToJson(command); Dbgl(commandJson); serverRPC.Invoke("SendServerRewardsJSON", new object[] { commandJson }); } } } GUILayout.Label(string.Format(packageString.Value, pi.name, pi.price), labelStyle, new GUILayoutOption[] { GUILayout.Width(itemWidth) }); GUILayout.EndVertical(); } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.EndVertical(); GUILayout.EndScrollView(); GUILayout.EndVertical(); }
private void Update() { if (!modEnabled.Value) { return; } if (testing.Value) { if (AedenthornUtils.CheckKeyDown(openUIKey.Value)) { storeOpen = !storeOpen; } return; } if (ZNet.instance && Player.m_localPlayer && !AedenthornUtils.IgnoreKeyPresses(true) && AedenthornUtils.CheckKeyDown(openUIKey.Value)) { Dbgl("Pressed hotkey"); if (storeOpen) { Traverse.Create(GameCamera.instance).Field("m_mouseCapture").SetValue(true); Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; Dbgl("Closing store"); storeOpen = false; } else { ZRpc serverRPC = ZNet.instance.GetServerRPC(); if (serverRPC != null) { Dbgl("Requesting store data"); JsonCommand command = new JsonCommand() { command = "RequestStoreInfo", id = SteamUser.GetSteamID().ToString() }; string commandJson = JsonUtility.ToJson(command); Dbgl(commandJson); serverRPC.Invoke("SendServerRewardsJSON", new object[] { commandJson }); } } } }
private void Update() { /* * if (AedenthornUtils.CheckKeyDown(openUIKey.Value)) * { * storeOpen = !storeOpen; * * } * return; */ if (ZNet.instance && Player.m_localPlayer && !AedenthornUtils.IgnoreKeyPresses(true) && AedenthornUtils.CheckKeyDown(openUIKey.Value)) { Dbgl("Pressed hotkey"); if (storeOpen) { Dbgl("Closing store"); storeOpen = false; } else { ZRpc serverRPC = ZNet.instance.GetServerRPC(); if (serverRPC != null) { Dbgl("Requesting store data"); JsonCommand command = new JsonCommand() { command = "RequestStoreInfo", id = SteamUser.GetSteamID().ToString() }; string commandJson = JsonUtility.ToJson(command); Dbgl(commandJson); serverRPC.Invoke("SendServerRewardsJSON", new object[] { commandJson }); } } } }
private static void RPC_SendJSON(ZRpc rpc, string json) { JsonCommand command = JsonUtility.FromJson <JsonCommand>(json); Dbgl($"RPC_SendJSON received command {command.command} {json} from id {command.id}"); ZNetPeer peer = Traverse.Create(ZNet.instance).Method("GetPeer", new object[] { rpc }).GetValue <ZNetPeer>(); var steamID = (peer.m_socket as ZSteamSocket).GetPeerID(); if (ZNet.instance.IsServer()) { context.UpdatePlayers(true); if (command.command == "BuyPackage") { var inv = GetStoreInventory(); PackageInfo package; try { package = inv.First(p => p.id == command.packageid); } catch { Dbgl($"Package {command.packageid} not found"); return; } PlayerInfo player = GetPlayerInfo(steamID.ToString()); if (player.currency < package.price) { Dbgl($"Player doesn't have enough currency {player.currency}, price {package.price}"); return; } player.currency -= package.price; WritePlayerData(player); JsonCommand sendCommand = new JsonCommand() { command = "PurchaseResult", currency = player.currency, items = GetPackageItems(package, player) }; rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); } else if (command.command == "RequestStoreInfo") { int currency = GetUserCurrency(steamID.ToString()); if (currency == -1) { Dbgl("Error getting store info"); return; } JsonCommand sendCommand = new JsonCommand() { command = "SendStoreInfo", storeTitle = storeTitle.Value, storeInventory = GetStoreInventoryString(), currencyString = currencyString.Value, currency = currency, }; rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); } } else { if (command.command == "PurchaseResult") { myCurrency = command.currency; foreach (string itemString in command.items.Split(';')) { Dbgl($"Receving {itemString}"); string[] itemAmount = itemString.Split(','); string name = itemAmount[0]; GameObject prefab = ZNetScene.instance.GetPrefab(name); if (!prefab) { Dbgl($"Item {name} not found!"); continue; } int amount = int.Parse(itemAmount[1]); if (amount == 1) { Instantiate(prefab, Player.m_localPlayer.transform.position + Player.m_localPlayer.transform.forward * 2f + Vector3.up, Quaternion.identity); Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "Spawning object " + name, 0, null); } else { for (int j = 0; j < amount; j++) { Vector3 b = UnityEngine.Random.insideUnitSphere * 0.5f; Instantiate(prefab, Player.m_localPlayer.transform.position + Player.m_localPlayer.transform.forward * 2f + Vector3.up + b, Quaternion.identity); Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "Spawning object " + name, 0, null); } } } } else if (command.command == "SendStoreInfo") { if (command.currency == -1) { Dbgl("Error getting store info"); return; } myCurrency = command.currency; windowTitleText = command.storeTitle; currencyString.Value = command.currencyString; storePackages = GetStorePackagesFromString(command.storeInventory); Dbgl($"Got user currency: {myCurrency}"); if (Player.m_localPlayer) { Player.m_localPlayer.Message(MessageHud.MessageType.Center, $"You have {string.Format(command.currencyString, myCurrency)}"); } storeOpen = true; } } }
private static void RPC_SendJSON(ZRpc rpc, string json) { if (!modEnabled.Value) { return; } JsonCommand command = JsonUtility.FromJson <JsonCommand>(json); Dbgl($"RPC_SendJSON received command {command.command} {json} from id {command.id}"); ZNetPeer peer = Traverse.Create(ZNet.instance).Method("GetPeer", new object[] { rpc }).GetValue <ZNetPeer>(); var steamID = (peer.m_socket as ZSteamSocket).GetPeerID(); if (ZNet.instance.IsServer()) { context.UpdatePlayers(true); if (command.command == "BuyPackage") { var packages = GetAllPackages(); PackageInfo package; try { package = packages.First(p => p.id == command.packageid); } catch { Dbgl($"Package {command.packageid} not found"); return; } PlayerInfo player = GetPlayerInfo(steamID.ToString()); if (!CanBuyPackage(ref player, package, true, true, out string result)) { WritePlayerData(player); return; } Dbgl(result); player.currency -= package.price; WritePlayerData(player); JsonCommand sendCommand = new JsonCommand() { command = "PurchaseResult", currency = player.currency, packageid = package.id, packagename = package.name, packagedescription = package.description, items = GetPackageItems(package, player) }; rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); } else if (command.command == "RequestStoreInfo") { int currency = GetUserCurrency(steamID.ToString()); if (currency == -1) { Dbgl("Error getting store info"); return; } PlayerInfo player = GetPlayerInfo(steamID.ToString()); JsonCommand sendCommand = new JsonCommand() { command = "SendStoreInfo", storeTitle = storeTitleString.Value, storeInventory = GetStoreInventoryString(player), currencyString = currencyString.Value, currency = currency, }; rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); } } else { if (command.command == "PurchaseResult") { Traverse.Create(GameCamera.instance).Field("m_mouseCapture").SetValue(true); Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; storeOpen = false; //PlayEffects(); var items = command.items.Split(';'); GameObject chest = null; PlayerProfile playerProfile = Game.instance.GetPlayerProfile(); Traverse inventoryT = null; if (useTombstone.Value) { chest = Instantiate(Player.m_localPlayer.m_tombstone, Player.m_localPlayer.GetCenterPoint() + Player.m_localPlayer.transform.forward, Player.m_localPlayer.transform.rotation); chest.GetComponent <Container>().m_name = command.packagename; inventoryT = Traverse.Create(chest.GetComponent <Container>().GetInventory()); inventoryT.Field("m_name").SetValue(command.packagename); inventoryT.Field("m_width").SetValue(8); int rows = items.Count() / 8 + 2; if (items.Count() % 8 != 0) { rows++; } inventoryT.Field("m_height").SetValue(rows); TombStone tombstone = chest.GetComponent <TombStone>(); tombstone.Setup(command.packagename, playerProfile.GetPlayerID()); } myCurrency = command.currency; List <string> itemStrings = new List <string>(); foreach (string itemString in items) { Dbgl($"Receving {itemString}"); string[] nameAmount = itemString.Split(','); string name = nameAmount[0]; GameObject prefab = ZNetScene.instance.GetPrefab(name); if (!prefab) { Dbgl($"Item {name} not found!"); continue; } int amount = int.Parse(nameAmount[1]); if (useTombstone.Value) { ItemDrop.ItemData item = prefab.GetComponent <ItemDrop>().m_itemData; double slotsNeed = amount / item.m_shared.m_maxStackSize / 8; inventoryT.Field("m_height").SetValue((int)inventoryT.Field("m_height").GetValue() + (int)Math.Round(slotsNeed, 0)); while (amount >= item.m_shared.m_maxStackSize) { int stack = Mathf.Min(item.m_shared.m_maxStackSize, amount); //chest.GetComponent<Container>().GetInventory().AddItem(_newItem); chest.GetComponent <Container>().GetInventory().AddItem(name, stack, item.m_quality, item.m_variant, Player.m_localPlayer.GetPlayerID(), Player.m_localPlayer.GetPlayerName()); amount = amount - stack; } if (amount > 0) { //chest.GetComponent<Container>().GetInventory().AddItem(_newItem2); chest.GetComponent <Container>().GetInventory().AddItem(name, amount, item.m_quality, item.m_variant, Player.m_localPlayer.GetPlayerID(), Player.m_localPlayer.GetPlayerName()); } itemStrings.Add($"{Localization.instance.Localize(item.m_shared.m_name)} {nameAmount[0]}"); } else { if (amount == 1) { var go = Instantiate(prefab, Player.m_localPlayer.transform.position + Player.m_localPlayer.transform.forward * 2f + Vector3.up, Quaternion.identity); var item = go.GetComponent <ItemDrop>().m_itemData; item.m_durability = item.m_shared.m_maxDurability; Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, string.Format(rewardString.Value, Localization.instance.Localize(go.GetComponent <ItemDrop>().m_itemData.m_shared.m_name)), 0, null); } else { for (int j = 0; j < amount; j++) { Vector3 b = UnityEngine.Random.insideUnitSphere * 0.5f; var go = Instantiate(prefab, Player.m_localPlayer.transform.position + Player.m_localPlayer.transform.forward * 2f + Vector3.up + b, Quaternion.identity); var item = go.GetComponent <ItemDrop>().m_itemData; item.m_durability = item.m_shared.m_maxDurability; Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, Localization.instance.Localize(go.GetComponent <ItemDrop>().m_itemData.m_shared.m_name), 0, null); } } } } if (useTombstone.Value) { inventoryT.Method("Changed").GetValue(); chest.GetComponent <ZNetView>().GetZDO().Set("ServerReward", string.Format(packageInfoString.Value, command.packagename, playerProfile.GetName()) + "\r\n" + string.Join("\r\n", itemStrings)); } } else if (command.command == "SendStoreInfo") { if (command.currency == -1) { Dbgl("Error getting store info"); return; } myCurrency = command.currency; windowTitleText = command.storeTitle; currencyString.Value = command.currencyString; storePackages = GetStorePackagesFromString(command.storeInventory); Dbgl($"Got store inventory {storePackages.Count}, user currency: {myCurrency}"); storeOpen = true; } else if (command.command == "SendConsoleString") { Traverse.Create(Console.instance).Method("AddString", new object[] { command.data }).GetValue(); Dbgl(command.data); } } }
private static void RPC_ConsoleCommand(ZRpc rpc, string command) { if (!modEnabled.Value || !ZNet.instance.IsServer()) { return; } ZNetPeer peer = Traverse.Create(ZNet.instance).Method("GetPeer", new object[] { rpc }).GetValue <ZNetPeer>(); var steamID = (peer.m_socket as ZSteamSocket).GetPeerID(); Dbgl($"RPC_ConsoleCommand received command {command} from {steamID}"); if (!Traverse.Create(ZNet.instance).Field("m_adminList").GetValue <SyncedList>().Contains(rpc.GetSocket().GetHostName())) { Dbgl("User is not admin!"); return; } var parts = command.Split(' ').Skip(1).ToArray(); string result = ""; if (parts[0] == "help") { result = "Usage:\r\n" + "serverrewards list users\r\n" + "serverrewards list packages\r\n" + "serverrewards give <steamID> <currency>\r\n" + "serverrewards give all <currency>\r\n" + "serverrewards set <steamID> <currency>\r\n" + "serverrewards set all <currency>\r\n" + "serverrewards givepackage <steamID> <packageID>\r\n" + "serverrewards givepackage all <packageID>\r\n" + "serverrewards spawn <spawnName>"; } else if (parts[0] == "list" && parts.Length == 2) { if (parts[1] == "users") { List <string> userList = new List <string>(); List <string> users = GetAllPlayerIDs(); var peerList = ZNet.instance.GetConnectedPeers(); foreach (string user in users) { string online = "(offline)"; var tp = peerList.Find(p => (p.m_socket as ZSteamSocket).GetPeerID().ToString() == user); if (tp != null) { online = tp.m_playerName + " (online)"; } userList.Add(user + " " + online); } result = string.Join("\r\n", userList); } else if (parts[1] == "packages") { List <string> packageList = new List <string>(); var packages = GetAllPackages(); foreach (PackageInfo p in packages) { packageList.Add(p.id + " " + p.price); } result = string.Join("\r\n", packageList); } else { result = "Syntax error."; } } else if (parts[0] == "give" && parts.Length == 3) { try { string id = GetSteamID(parts[1]); if (id == null) { result = "User not found."; } else if (AdjustCurrency(id, int.Parse(parts[2]))) { result = "Balance adjusted."; } else { result = "Error adjusting player balance."; } } catch { result = "Syntax error."; } } else if (parts[0] == "set" && parts.Length == 3) { try { string id = GetSteamID(parts[1]); if (id == null) { result = "User not found."; } else if (SetCurrency(id, int.Parse(parts[2]))) { result = "Balance set."; } else { result = "Error setting player balance."; } } catch { result = "Syntax error."; } } else if (parts[0] == "givepackage" && parts.Length == 3) { if (parts[1] == "all") { IEnumerable <string> users = GetAllPlayerIDs(); int count = 0; foreach (string user in users) { string r = GivePackage(parts[1], parts[2]); if (r == null) { count++; } } result = $"Package sent to {count} users!"; } else { string id = GetSteamID(parts[1]); if (id == null) { result = "User not found."; } else { result = GivePackage(id, parts[2]); if (result == null) { result = "Package sent!"; } } } } else if (parts[0] == "spawn" && parts.Length == 2) { GameObject prefab = ZNetScene.instance.GetPrefab(parts[1]); if (!prefab) { result = $"Item {parts[1]} not found!"; } else { var go = Instantiate(prefab, Player.m_localPlayer.transform.position + Player.m_localPlayer.transform.forward * 2f + Vector3.up, Quaternion.identity); Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, string.Format(packageInfoString.Value, Localization.instance.Localize(go.GetComponent <ItemDrop>().m_itemData.m_shared.m_name)), 0, null); } } else { result = "Syntax error."; } JsonCommand sendCommand = new JsonCommand() { command = "SendConsoleString", data = result }; rpc.Invoke("SendServerRewardsJSON", new object[] { JsonUtility.ToJson(sendCommand) }); Dbgl(result); }
private void WindowBuilder(int id) { GUI.DragWindow(new Rect(0, 0, windowWidth.Value, 20)); GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(windowWidth.Value) }); GUILayout.Label(windowTitleText, titleStyle); GUILayout.BeginHorizontal(); GUILayout.Label(myCurrencyString.Value, currencyStyle); if (coinBeforeAmount.Value) { GUILayout.Space(5); GUILayout.Button(textureDict["currency"], coinStyle, new GUILayoutOption[] { GUILayout.Width(currencyFontSize.Value * coinFactor), GUILayout.Height(currencyFontSize.Value) }); } GUILayout.Space(5); GUILayout.Label(string.Format(currencyString.Value, myCurrency), currencyStyle); if (!coinBeforeAmount.Value) { GUILayout.Space(5); GUILayout.Button(textureDict["currency"], coinStyle, new GUILayoutOption[] { GUILayout.Width(currencyFontSize.Value * coinFactor), GUILayout.Height(currencyFontSize.Value) }); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); float width = windowWidth.Value - 70; float itemWidth = width / packagesPerRow.Value; scrollPosition = GUILayout.BeginScrollView(scrollPosition, new GUILayoutOption[] { GUILayout.Width(windowWidth.Value - 20) }); GUILayout.Space(10); GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(width) }); GUILayout.BeginHorizontal(); storePackages.Sort(delegate(PackageInfo a, PackageInfo b) { return(a.price.CompareTo(b.price)); }); for (int i = 0; i < storePackages.Count; i++) { if (i > 0 && i % packagesPerRow.Value == 0) { GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.BeginHorizontal(); } PackageInfo pi = storePackages[i]; string texture = textureDict.ContainsKey(pi.type) ? pi.type : "Common"; GUILayout.BeginVertical(new GUILayoutOption[] { GUILayout.Width(itemWidth) }); if (GUILayout.Button(new GUIContent(textureDict[texture], "This is a test"), new GUILayoutOption[] { GUILayout.Width(itemWidth), GUILayout.Height(itemWidth) })) { if (myCurrency >= pi.price) { if (testing.Value) { myCurrency -= pi.price; storeOpen = false; PlayEffects(); } else { ZRpc serverRPC = ZNet.instance.GetServerRPC(); if (serverRPC != null) { Dbgl("Requesting store data"); JsonCommand command = new JsonCommand() { command = "BuyPackage", packageid = pi.id }; string commandJson = JsonUtility.ToJson(command); Dbgl(commandJson); serverRPC.Invoke("SendServerRewardsJSON", new object[] { commandJson }); } } } } if (GUI.tooltip != null && GUI.tooltip.Length > 0) { thisTooltip = GUI.tooltip; } GUILayout.Label(string.Format(packageString.Value, pi.name), labelStyle, new GUILayoutOption[] { GUILayout.Width(itemWidth) }); GUILayout.BeginHorizontal(new GUILayoutOption[] { GUILayout.Width(itemWidth) }); GUILayout.FlexibleSpace(); if (coinBeforeAmount.Value) { GUILayout.Button(textureDict["currency"], coinStyle, new GUILayoutOption[] { GUILayout.Width(labelFontSize.Value * coinFactor), GUILayout.Height(labelFontSize.Value) }); GUILayout.Space(5); } GUILayout.Label(string.Format(currencyString.Value, pi.price), labelStyle); if (!coinBeforeAmount.Value) { GUILayout.Space(5); GUILayout.Button(textureDict["currency"], coinStyle, new GUILayoutOption[] { GUILayout.Width(labelFontSize.Value * coinFactor), GUILayout.Height(labelFontSize.Value) }); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndVertical(); } GUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.EndVertical(); GUILayout.EndScrollView(); GUILayout.EndVertical(); }