UPF/Assets/Game/Scripts/Shop/SpecialChest.cs

98 lines
3.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class SpecialChest : MonoBehaviour
{
public int quantity=3;
public DateTime lastCollectedTime;
public ChestButton chestButton;
public TMP_Text txtTimeLeft;
public Button button;
void Start(){
button.onClick.AddListener(OnClicked);
StartCoroutine(LoadData());
}
DateTime specialChestExpire=DateTime.MinValue;
float t =30;
IEnumerator LoadData(){
WWW storeData = new WWW(DBmanager.phpRoot + "get_store_data.php");
yield return storeData;
Debug.Log(storeData.text + " => " + DBmanager.LastCollectedDailyChest);
specialChestExpire= DateTime.Parse(storeData.text);
}
void Update()
{
if(t < 30){
t +=Time.deltaTime;
}else{
t=0;
UpdateStat();
}
// if((DateTime.Now - DBmanager.LastCollectedDailyChest).TotalHours > 24){
// button.interactable = true;
// txtTimeLeft.text = "Ready";
// }else{
// button.interactable = false;
// // txtTimeLeft.text = SceneData.SecondsToText((DateTime.Now - DBmanager.LastCollectedDailyChest).Seconds);
// txtTimeLeft.text = MinutesToText((24*60)-(DateTime.Now - DBmanager.LastCollectedDailyChest).TotalMinutes);
// // Debug.Log(DateTime.Now + "-" + DBmanager.LastCollectedDailyChest + " = " + (DateTime.Now - DBmanager.LastCollectedDailyChest));
// }
}
public async void UpdateStat(){
if(specialChestExpire == DateTime.MinValue){ chestButton.gameObject.SetActive(false);}
DateTime nowTime = await DBmanager.GetNetworkTime();
if(specialChestExpire < nowTime){
chestButton.gameObject.SetActive(false);
}else if(DBmanager.LastCollectedDailyChest == specialChestExpire){
chestButton.gameObject.SetActive(false);
}else{
//Set the timer
chestButton.gameObject.SetActive(true);
txtTimeLeft.text = MinutesToText(specialChestExpire.Subtract(nowTime).TotalMinutes);
}
}
void OnClicked(){
// gameObject.SetActive(false);
//AdsManager.instance.ShowRewarded(Buy);
// Buy();
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase This special offer for {chestButton.Price} Gems?", Buy,null);
}
void Buy(){
if(DBmanager.Gems < chestButton.Price){
MessageDialog.instance.ShowMessage("Failed","Insufficient Gems to complete the Purchase!");
return;
}
DBmanager.SetGems(DBmanager.Gems - chestButton.Price);
DBmanager.SetLastCollectedDailyChest(specialChestExpire.ToString("yyyy-MM-dd HH:mm:ss"));
UpdateStat();
StartCoroutine(StartChestList());
}
IEnumerator StartChestList(){
for(int i=0; i< quantity; i++){
ChestOpener.instance.OpenChest((int)chestButton.minLuck, (int)chestButton.maxLuck);
while(ChestOpener.instance.active){
yield return new WaitForEndOfFrame();
}
}
}
public static string MinutesToText(double minutes){
int mins = ((int)(minutes % 60));
int hours = Mathf.FloorToInt((float)(minutes/60));
return hours + "h "+ mins.ToString("0") + "m";
}
}