I am trying to figure out how to implement my TimeManager script into my DailyReward script. My DailyReward script currently rewards players every 24 hours with a daily reward or every 12 hours if they have a special item. The issue with the DailyReward script is if you set the time or date ahead offline, the daily reward will be available early even if the server time is the same. My TimeManager script gets the time and date from a server, which prevents you from setting time and date ahead to get the reward early.
This is the TimeManager script:
using System.Collections;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
/*
NEED TO FIND A WORKING URL TO GET THE DATE AND TIME
necessary variables to hold all the things we need.
php url
timedata, the data we get back
current time
current date
*/
public static TimeManager sharedInstance = null;
private string url = "http://leatonm.net/wp-content/uploads/2017/candlepin/getdate.php"; //This is a placeholder URL. Change URL in final script version.
private string timeData;
private string currentTime;
private string currentDate;
//Make sure there is only one instance of this always.
private void Awake()
{
if (sharedInstance == null)
{
sharedInstance = this;
}
else if (sharedInstance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
//Time fether coroutine
public IEnumerator GetTime()
{
Debug.Log("==> step 1. Getting info from internet now!");
//Debug.Log("connecting to php");
WWW www = new WWW(url);
yield return www;
//if (www.error != null) {
//Debug.Log ("Error");
//} else {
//Debug.Log("got the php information");
//}
Debug.Log("==> step 2. Got the info from internet!");
timeData = www.text;
string[] words = timeData.Split('/');
//timerTestLabel.text = www.text;
Debug.Log("The date is : " + words[0]);
Debug.Log("The time is : " + words[1]);
//setting current time
currentDate = words[0];
currentTime = words[1];
}
//get the current time at startup
private void Start()
{
Debug.Log("==> TimeManager script is Ready.");
//Debug.Log ("TimeManager script is Ready.");
StartCoroutine("getTime");
}
//get the current date - also converting from string to int.
//where 12-4-2017 is 1242017
public string GetCurrentDateNow()
{
return currentDate;
}
//public int getCurrentDateNow()
//{
//string[] words = _currentDate.Split('-');
//int x = int.Parse(words[0] + words[1] + words[2]);
//return x;
//}
//get the current Time
public string GetCurrentTimeNow()
{
return currentTime;
}
}
This is the DailyReward script:
using System;
using UnityEngine;
using UnityEngine.UI;
public class DailyReward : MonoBehaviour {
public static DailyReward DailyRwd;
private Text chestTimer;
private float msToWait = 86400000.0f;
private Button chestButton;
private ulong lastChestOpen;
void Start(){
if (DailyRwd == null)
{
DailyRwd = this;
}
chestTimer = GetComponentInChildren<Text>();
chestButton = GetComponent<Button>();
lastChestOpen = ulong.Parse(PlayerPrefs.GetString("LastChestOpen"));
if(!isChestReady())
chestButton.interactable = false;
}
void Update(){
if (GameController.GameCon.tierTwoPrizeCharmThree == 1) {
msToWait = 43200000.0f;
}
if(!chestButton.IsInteractable()){
if(isChestReady()){
chestButton.interactable = true;
return;
}
//Set the timer
ulong diff = ((ulong)DateTime.Now.Ticks - lastChestOpen);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;
string r = "";
//Hours
r += ((int)secondsLeft / 3600).ToString() + "h ";
secondsLeft -= ((int)secondsLeft / 3600) * 3600;
//Minutes
r += ((int)secondsLeft / 60).ToString("00") + "m ";
//Seconds
r += (secondsLeft % 60).ToString("00") + "s"; ;
chestTimer.text = r;
}
if (chestButton.interactable == true) {
GameController.GameCon.claimFreeCards.SetActive (false);
} else {
GameController.GameCon.claimFreeCards.SetActive (true);
}
}
public void ChestClick(){
lastChestOpen = (ulong)DateTime.Now.Ticks;
PlayerPrefs.SetString("LastChestOpen", lastChestOpen.ToString());
chestButton.interactable = false;
GameController.GameCon.alreadySwiped = true;
}
private bool isChestReady(){
ulong diff = ((ulong)DateTime.Now.Ticks - lastChestOpen);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;
if(secondsLeft < 0){
chestTimer.text = "Daily Reward";
return true;
}
return false;
}
}