Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Sfs2X.Core;
- using Sfs2X.Entities.Data;
- using Sfs2X.Requests;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class BuildQueueGUIManager : MonoBehaviour
- {
- private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- public int queueSize;
- public int oldQueueSize = 0;
- public TextMeshProUGUI timeLeft;
- public GameObject guiPanelQueuePrefab;
- public Sprite obsSatImage;
- public Image currentlyBuildingImage;
- //used to instansate planel obejcts as child
- public GameObject gridLayoutPanel;
- public List<long> buildTimeIdexs;
- public Dictionary<long, string> allItemsInQueueDict;
- public Dictionary<long, GameObject> spawnedGUIItems;
- // Start is called before the first frame update
- void Start()
- {
- allItemsInQueueDict = new Dictionary<long, string>();
- spawnedGUIItems = new Dictionary<long, GameObject>();
- ManagerMain.sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnBuildShipyardQueueResponse);
- }
- public void OnClick()
- {
- GetQueueFromServer();
- }
- private void GetQueueFromServer()
- {
- ISFSObject objOut = new SFSObject();
- objOut.PutUtfString("Planet", ManagerMain.currentlySelectedObject.name);
- ManagerMain.sfs.Send(new ExtensionRequest("GetShipyardQueue", objOut));
- objOut = null;
- }
- void OnBuildShipyardQueueResponse(BaseEvent e)
- {
- bool sucessfullReply = false;
- string cmd = (string)e.Params["cmd"];
- ISFSObject objIn = (ISFSObject)e.Params["params"];
- if (cmd == "GetShipyardQueue")
- {
- if (objIn.GetLongArray("ObsSatallite").Length > 0)
- {
- //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
- buildTimeIdexs = new List<long>();
- //must be repeated for each object
- sucessfullReply = true;
- buildTimeIdexs.AddRange(objIn.GetLongArray("ObsSatallite"));
- foreach (var a in objIn.GetLongArray("ObsSatallite"))
- {
- if (!allItemsInQueueDict.ContainsKey(a))
- {
- allItemsInQueueDict.Add(a, "ObsSatallite");
- }
- }
- queueSize = buildTimeIdexs.Count;
- }
- }
- //put lowest first
- if (sucessfullReply)
- {
- buildTimeIdexs.Sort();
- SpawnIconsInQueue();
- sucessfullReply = false;
- }
- }
- public void SpawnIconsInQueue()
- {
- if(allItemsInQueueDict[buildTimeIdexs[0]] == "ObsSatallite")
- {
- currentlyBuildingImage.sprite = obsSatImage;
- if (spawnedGUIItems.ContainsKey(buildTimeIdexs[0]))
- {
- Destroy(spawnedGUIItems[buildTimeIdexs[0]]);
- spawnedGUIItems.Remove(buildTimeIdexs[0]);
- }
- }
- for (int i = 1; i < buildTimeIdexs.Count; i++)
- {
- if (!spawnedGUIItems.ContainsKey(buildTimeIdexs[i]))
- {
- GameObject guiPanel = Instantiate(guiPanelQueuePrefab);
- guiPanel.name = buildTimeIdexs[i].ToString();
- guiPanel.transform.SetParent(gridLayoutPanel.transform);
- spawnedGUIItems.Add(buildTimeIdexs[i], guiPanel);
- }
- }
- //Find the list with the most recent
- }
- public static double GetCurrentUnixTimestampMillis()
- {
- DateTime localDateTime, univDateTime;
- localDateTime = DateTime.Now;
- univDateTime = localDateTime.ToUniversalTime();
- return Math.Round((univDateTime - UnixEpoch).TotalMilliseconds);
- }
- public string CalcTime(double ms)
- {
- var parts = new List<string>();
- Action<int, string> add = (val, unit) => { if (val > 0) parts.Add(val + unit); };
- var t = TimeSpan.FromMilliseconds(ms);
- add(t.Days, "d");
- add(t.Hours, "h");
- add(t.Minutes, "m");
- add(t.Seconds, "s");
- return string.Join(" ", parts);
- }
- // Update is called once per frame
- void Update()
- {
- if (buildTimeIdexs != null)
- {
- if (!(buildTimeIdexs.Count < 1F))
- {
- if (buildTimeIdexs[0] > 0)
- {
- double timeLeftD = buildTimeIdexs[0] - GetCurrentUnixTimestampMillis();
- timeLeft.text = CalcTime(timeLeftD);
- if (timeLeftD <= 0)
- {
- GetQueueFromServer();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement