This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
mochapine64backup/MoCha/Assets/Scripts/GetGoals.cs

303 lines
8.6 KiB
C#
Raw Normal View History

using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
//Pretty much a copy of GetData...
namespace PedometerU.Tests
{
public class GetGoals : MonoBehaviour
{
// For HTTP requests to the server.
private MochaParser mp;
// Goals Progress Bar
public Image pbbg;
public Image pbf;
2018-04-30 19:24:40 -05:00
// My Goal Progress Bar
public Image mgpbbg;
public Image mgpbf;
// Monster Progress Bar
public Image mpbbg;
public Image mpbf;
2018-04-26 01:18:08 -05:00
2018-04-26 22:58:14 -05:00
private string[] items = { "circle_eye", "crazy_hair", "circle_head", "gap_mouth", "pig_nose", "crown" };
// Display Text
public Text rivalText;
2018-04-26 22:58:14 -05:00
public Text goalText;
public Text stepText;
public Text pointsText;
public Text remainingText;
// Goal information.
private string currentDayStr;
private string goalDayStr;
2018-04-26 22:58:14 -05:00
public string prefKey;
// Used for time calculation.
private int timeUsed;
private int timeRemaining;
private const int secondsperday = 86400;
2018-04-26 22:58:14 -05:00
//Progress Bar...
private float barDisplay; //current progress
//private Vector2 barPos;
// This gets overwritten later by the background's size values.
2018-04-26 22:58:14 -05:00
private Vector2 barSize = new Vector2(5000, 10);
//Pedometer
private Pedometer pedometer;
int userSteps;
int userPoints;
public int stepsGoal;
2018-04-26 22:58:14 -05:00
private int savedSteps;
private int savedPoints;
public string currentStepsKey;
public string totalPointsKey;
2018-04-26 22:58:14 -05:00
void OnStep (int steps, double distance) {
//userDistance = (distance * 3.28084);
2018-04-26 22:58:14 -05:00
userSteps = steps + savedSteps;
PlayerPrefs.SetInt(currentStepsKey,userSteps);
2018-04-26 22:58:14 -05:00
stepText.text = userSteps.ToString () + "/" + stepsGoal.ToString();
userPoints = steps*5 + savedPoints;
PlayerPrefs.SetInt(totalPointsKey,userPoints);
2018-04-26 22:58:14 -05:00
pointsText.text = "Points: " + userPoints.ToString ();
}
2018-04-26 00:18:01 -05:00
2018-04-26 22:58:14 -05:00
private void OnDisable () {
// Release the pedometer
pedometer.Dispose();
pedometer = null;
2018-04-26 04:38:54 -05:00
}
2018-04-26 22:58:14 -05:00
//saved value from last session loads in on START
//write usersteps + saved val to pref in ONSTEP
void Start ()
2018-04-26 04:38:54 -05:00
{
//PlayerPrefs.SetInt("currentGoals", 0);
//PlayerPrefs.DeleteAll(); //danger!!
if(prefKey == "daily")
{
// This should later only get called per goal
StartCoroutine(updateRival());
currentStepsKey = "currentSteps";
totalPointsKey = "totalPoints";
}
else
{
currentStepsKey = "currentSteps_p";
totalPointsKey = "totalPoints_p";
}
savedSteps = PlayerPrefs.GetInt(currentStepsKey);
savedPoints = PlayerPrefs.GetInt(totalPointsKey);
2018-04-26 22:58:14 -05:00
// Create a new pedometer
pedometer = new Pedometer(OnStep);
// Reset UI
2018-04-30 19:08:06 -05:00
stepText.text = savedSteps.ToString () + "/" + stepsGoal.ToString();
2018-04-30 17:08:14 -05:00
if(!PlayerPrefs.HasKey("goalComplete"))
{
PlayerPrefs.SetInt("goalComplete", 0);
}
if(PlayerPrefs.HasKey(totalPointsKey))
{
pointsText.text = "Points: " + PlayerPrefs.GetInt(totalPointsKey).ToString ();
}
else
{
PlayerPrefs.SetInt(totalPointsKey, 0);
pointsText.text = "Points: " + PlayerPrefs.GetInt(totalPointsKey).ToString ();
}
2018-04-30 19:08:06 -05:00
if(prefKey == "daily")
{
if(PlayerPrefs.HasKey("goalDate"))
{
goalDayStr = PlayerPrefs.GetString("goalDate");
}
else
{
PlayerPrefs.SetString("goalDate",
System.DateTime.Now.AddDays(1).ToString("MM/dd/yyyy"));
goalDayStr = PlayerPrefs.GetString("goalDate");
}
2018-04-30 19:08:06 -05:00
goalText.color = new Color(0f, 0f, 0f);
2018-04-30 17:08:14 -05:00
}
2018-04-26 22:58:14 -05:00
userSteps = savedSteps;
userPoints = savedPoints;
2018-04-26 01:36:56 -05:00
2018-04-30 17:08:14 -05:00
//OnStep(savedSteps, 0); //necessary?
//Debug.Log(savedSteps);
2018-04-26 01:18:08 -05:00
//Debug.Log(pbbg.rectTransform.anchoredPosition.x.ToString() + " " + pbbg.rectTransform.anchoredPosition.y.ToString());
2018-04-26 12:40:50 -05:00
2018-04-30 17:08:14 -05:00
//barPos = new Vector2(pbbg.transform.position.x, 1895 - pbbg.transform.position.y);
2018-04-26 22:58:14 -05:00
currentDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
}
2018-04-26 12:40:50 -05:00
2018-04-26 22:58:14 -05:00
// Was used for testing.
private float tempcounter = 0.1f;
2018-04-26 01:18:08 -05:00
// Call this to update the goals progress bar.
2018-04-26 22:58:14 -05:00
public void updateProgressBar()
{
//Debug.Log (pbbg.rectTransform.rect.width + " " + pbbg.rectTransform.rect.height);
barSize.x = pbbg.rectTransform.rect.width;
//tempcounter += 0.0005f;
// resize the fill.
RectTransform temp = pbf.rectTransform;
//temp.sizeDelta = new Vector2 (-barSize.x + barSize.x * tempcounter, 0);
temp.sizeDelta = new Vector2 (-barSize.x + (barSize.x * barDisplay), 0);
temp.anchoredPosition = new Vector2 (temp.rect.width * 0.5f - (barSize.x * 0.5f),0);
}
2018-04-30 19:24:40 -05:00
// Use this for the progress bar you're testing with.
public void updateMyGoalProgressBar()
{
//Debug.Log (pbbg.rectTransform.rect.width + " " + pbbg.rectTransform.rect.height);
barSize.x = mgpbbg.rectTransform.rect.width;
//tempcounter += 0.0005f;
// resize the fill.
RectTransform temp = mgpbf.rectTransform;
//temp.sizeDelta = new Vector2 (-barSize.x + barSize.x * tempcounter, 0);
temp.sizeDelta = new Vector2 (-barSize.x + (barSize.x * barDisplay), 0);
temp.anchoredPosition = new Vector2 (temp.rect.width * 0.5f - (barSize.x * 0.5f),0);
}
// Call this to update the monster's progress bar.
public void updateMonsterProgressBar()
{
float fractionused = (float)(timeUsed) / (float)(secondsperday);
//Debug.Log (dayleft);
barSize.x = mpbbg.rectTransform.rect.width;
//tempcounter += 0.0005f;
2018-04-26 22:58:14 -05:00
// resize the fill.
RectTransform temp = mpbf.rectTransform;
//temp.sizeDelta = new Vector2 (-barSize.x + barSize.x * tempcounter, 0);
temp.sizeDelta = new Vector2 (-barSize.x + (barSize.x * fractionused), 0);
2018-04-26 22:58:14 -05:00
temp.anchoredPosition = new Vector2 (temp.rect.width * 0.5f - (barSize.x * 0.5f),0);
}
// Calculates the time remaining for the day.
// Will likely make a separate function for smaller timed goals if we have time.
public void updateTimeRemaining()
{
timeUsed = ((System.DateTime.Now.Hour * 60 * 60) + (System.DateTime.Now.Minute * 60) + System.DateTime.Now.Second);
timeRemaining = secondsperday - timeUsed;
}
// Format the time remaining variable to a proper string.
public string formatTimeRemaining()
{
string output = "Time remaining: ";
output += "" + (timeRemaining / 3600);
output += ":" + (timeRemaining % 3600 / 60);
output += ":" + (timeRemaining % 60);
return output;
}
// Shell function for now to update rival text.
// You'll wanna call this with startcoroutine().
public IEnumerator updateRival()
{
mp = new MochaParser ();
yield return mp.MakeRequest ("http://corder.tech/mocha/random");
rivalText.text = "You are being chased by " + mp.players [0].username;
}
public void refreshSteps()
{
int fitbitSteps = PlayerPrefs.GetInt("fitbitSteps");
PlayerPrefs.SetInt(currentStepsKey, fitbitSteps);
}
2018-04-26 22:58:14 -05:00
void Update()
{
updateTimeRemaining ();
2018-04-26 22:58:14 -05:00
updateProgressBar ();
//updateMyGoalProgressBar ();
2018-04-30 19:24:40 -05:00
if(prefKey == "daily")
{
updateMonsterProgressBar ();
remainingText.text = formatTimeRemaining ();
currentDayStr = System.DateTime.Now.ToString("MM/dd/yyyy");
}
userSteps = PlayerPrefs.GetInt(currentStepsKey);
2018-04-26 22:58:14 -05:00
barDisplay = (float)userSteps/(float)stepsGoal;
if(barDisplay > 1.0f)
barDisplay = 1.0f;
stepText.text = userSteps.ToString () + "/" + stepsGoal.ToString();
2018-04-26 22:58:14 -05:00
2018-04-30 17:08:14 -05:00
//check if the current date matches the goal date
if(prefKey == "daily" && currentDayStr == goalDayStr)
{
2018-04-30 17:08:14 -05:00
Debug.Log("current: " + currentDayStr);
Debug.Log("goal: " + goalDayStr);
Debug.Log(prefKey + "...DAY HAS PASSED!! SETTING GOAL TO NEXT DAY!!");
2018-04-30 17:08:14 -05:00
PlayerPrefs.SetString("goalDate",
System.DateTime.Now.AddDays(1).ToString("MM/dd/yyyy"));
PlayerPrefs.SetInt("goalComplete", 0);
2018-04-30 17:08:14 -05:00
goalDayStr = PlayerPrefs.GetString("goalDate");
Debug.Log("new goal: " + goalDayStr);
//reset steps...
userSteps = 0;
PlayerPrefs.SetInt(currentStepsKey, userSteps);
2018-04-26 22:58:14 -05:00
}
//goal complete
else if(barDisplay >= 1.0f)
2018-04-26 22:58:14 -05:00
{
if(PlayerPrefs.GetInt("goalComplete") == 0)
{
Debug.Log(prefKey + "... Goal complete! +100pts!");
userSteps = 0;
PlayerPrefs.SetInt(currentStepsKey, userSteps);
savedSteps = 0;
//OnStep(0,0);
//destroy the goal and set a new one?
savedPoints += stepsGoal*10;
PlayerPrefs.SetInt(totalPointsKey, savedPoints);
pointsText.text = "Points: " + PlayerPrefs.GetInt(totalPointsKey).ToString ();
//Random item
if(!PlayerPrefs.HasKey("inventory"))
PlayerPrefs.SetString("inventory", "");
StringBuilder invList = new StringBuilder ();
invList.Append(PlayerPrefs.GetString ("inventory"));
invList.Append(items[(int)Math.Round(UnityEngine.Random.value*6.0f)]).Append(" ");
PlayerPrefs.SetString ("inventory", invList.ToString());
PlayerPrefs.SetInt("goalComplete", 1);
}
}
}
}
2018-04-26 01:18:08 -05:00
}