Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. using Sfs2X.Core;
  2. using Sfs2X.Entities.Data;
  3. using Sfs2X.Requests;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10.  
  11. public class BuildQueueGUIManager : MonoBehaviour
  12. {
  13. private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  14.  
  15. public int queueSize;
  16. public int oldQueueSize = 0;
  17.  
  18. public TextMeshProUGUI timeLeft;
  19. public GameObject guiPanelQueuePrefab;
  20.  
  21. public Sprite obsSatImage;
  22.  
  23. public Image currentlyBuildingImage;
  24.  
  25. //used to instansate planel obejcts as child
  26. public GameObject gridLayoutPanel;
  27.  
  28. public List<long> buildTimeIdexs;
  29.  
  30. public Dictionary<long, string> allItemsInQueueDict;
  31.  
  32. public Dictionary<long, GameObject> spawnedGUIItems;
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. allItemsInQueueDict = new Dictionary<long, string>();
  37. spawnedGUIItems = new Dictionary<long, GameObject>();
  38. ManagerMain.sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnBuildShipyardQueueResponse);
  39. }
  40.  
  41. public void OnClick()
  42. {
  43. GetQueueFromServer();
  44. }
  45. private void GetQueueFromServer()
  46. {
  47. ISFSObject objOut = new SFSObject();
  48. objOut.PutUtfString("Planet", ManagerMain.currentlySelectedObject.name);
  49.  
  50. ManagerMain.sfs.Send(new ExtensionRequest("GetShipyardQueue", objOut));
  51. objOut = null;
  52. }
  53.  
  54. void OnBuildShipyardQueueResponse(BaseEvent e)
  55. {
  56. bool sucessfullReply = false;
  57. string cmd = (string)e.Params["cmd"];
  58. ISFSObject objIn = (ISFSObject)e.Params["params"];
  59.  
  60. if (cmd == "GetShipyardQueue")
  61. {
  62.  
  63. if (objIn.GetLongArray("ObsSatallite").Length > 0)
  64. {
  65.  
  66. //cannot be in here has to be outside this loop, but also has to be null so we can add to it from no value
  67. buildTimeIdexs = new List<long>();
  68. //must be repeated for each object
  69. sucessfullReply = true;
  70. buildTimeIdexs.AddRange(objIn.GetLongArray("ObsSatallite"));
  71. foreach (var a in objIn.GetLongArray("ObsSatallite"))
  72. {
  73. if (!allItemsInQueueDict.ContainsKey(a))
  74. {
  75. allItemsInQueueDict.Add(a, "ObsSatallite");
  76. }
  77. }
  78. queueSize = buildTimeIdexs.Count;
  79. }
  80. }
  81.  
  82. //put lowest first
  83. if (sucessfullReply)
  84. {
  85. buildTimeIdexs.Sort();
  86. SpawnIconsInQueue();
  87. sucessfullReply = false;
  88.  
  89. }
  90. }
  91.  
  92. public void SpawnIconsInQueue()
  93. {
  94. if(allItemsInQueueDict[buildTimeIdexs[0]] == "ObsSatallite")
  95. {
  96. currentlyBuildingImage.sprite = obsSatImage;
  97. if (spawnedGUIItems.ContainsKey(buildTimeIdexs[0]))
  98. {
  99. Destroy(spawnedGUIItems[buildTimeIdexs[0]]);
  100. spawnedGUIItems.Remove(buildTimeIdexs[0]);
  101. }
  102. }
  103.  
  104. for (int i = 1; i < buildTimeIdexs.Count; i++)
  105. {
  106. if (!spawnedGUIItems.ContainsKey(buildTimeIdexs[i]))
  107. {
  108. GameObject guiPanel = Instantiate(guiPanelQueuePrefab);
  109. guiPanel.name = buildTimeIdexs[i].ToString();
  110. guiPanel.transform.SetParent(gridLayoutPanel.transform);
  111. spawnedGUIItems.Add(buildTimeIdexs[i], guiPanel);
  112. }
  113. }
  114. //Find the list with the most recent
  115. }
  116.  
  117. public static double GetCurrentUnixTimestampMillis()
  118. {
  119. DateTime localDateTime, univDateTime;
  120. localDateTime = DateTime.Now;
  121. univDateTime = localDateTime.ToUniversalTime();
  122. return Math.Round((univDateTime - UnixEpoch).TotalMilliseconds);
  123.  
  124. }
  125.  
  126. public string CalcTime(double ms)
  127. {
  128. var parts = new List<string>();
  129. Action<int, string> add = (val, unit) => { if (val > 0) parts.Add(val + unit); };
  130. var t = TimeSpan.FromMilliseconds(ms);
  131.  
  132. add(t.Days, "d");
  133. add(t.Hours, "h");
  134. add(t.Minutes, "m");
  135. add(t.Seconds, "s");
  136.  
  137. return string.Join(" ", parts);
  138. }
  139. // Update is called once per frame
  140. void Update()
  141. {
  142. if (buildTimeIdexs != null)
  143. {
  144. if (!(buildTimeIdexs.Count < 1F))
  145. {
  146. if (buildTimeIdexs[0] > 0)
  147. {
  148.  
  149. double timeLeftD = buildTimeIdexs[0] - GetCurrentUnixTimestampMillis();
  150.  
  151. timeLeft.text = CalcTime(timeLeftD);
  152. if (timeLeftD <= 0)
  153. {
  154. GetQueueFromServer();
  155. }
  156. }
  157. }
  158.  
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement