123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using DataTools;
- using System.Collections.Generic;
- using UnityEngine;
- using WebTools.Phone;
- public class InstaGigs : RandomGigs
- {
- [Header("Data")]
- [SerializeField] private DataGigTemplates dataTemplates;
- private string InstaGigId = "InstaGigId";//need to use same value in DataUser class
- private List<GigsBase> mainStoryGigs;
- private bool replay = false;
- public bool IsMainStoryGigExist
- {
- get
- {
- return mainStoryGigs.Exists(g => g.HasGigsRemaining);
- }
- }
- protected override void Start()
- {
- base.Start();
- mainStoryGigs = new List<GigsBase>(FindObjectsOfType<GigsBase>());
- mainStoryGigs.RemoveAll(g => !g.IsMainStoryGig);
- Invoke("LoadIncompleteGig", 1);
- }
- private void LoadSavedGigs()
- {
- if (PlayerPrefs.HasKey(persistentDataKey))
- {
- GenerateDefaultGigs(DataTools.GigType.Photoshoot, false);
- }
- }
- protected override void OnLocationChanged(Location location)
- {
- base.OnLocationChanged(location);
- if (currentGig != null)
- {
- SetLocationButtons(currentGig.Location);
- }
- }
- public void GenerateDefaultGigs(DataTools.GigType type, bool saveGigStartTime = true)
- {
- }
- public void GenerateGigs(string gigId, DataTools.Gig gig)
- {
- if (gig == null)
- {
- gig = GetGigByName(gigId);
- }
- GenerateGigs(gig);
- }
- public void GenerateGigs(DataTools.Gig gig)
- {
- currentGig = gig;
-
- SerializeGig();
- if (dataUser.UserData.CurrentInstantGig != gig.Id)
- {
- dataUser.UserData.CurrentInstantGig = gig.Id;
- dataUser.OnGigTimestampReceived += OnTimeReceived;
- dataUser.UpdateUserCurrentInstaGig();
- dataUser.UpdateGigStartTime();
- }
- else
- {
- OnTimeReceived();
- }
- RandomizeGigs(currentGig.StarsNeeded, currentGig.Type, currentGig.Location);
- }
- protected override void OnDestroy()
- {
- if (HasGigsRemaining)
- {
- SerializeGig();
- }
- base.OnDestroy();
- }
- private void SetDefaultGig(DataTools.GigType type=DataTools.GigType.Photoshoot)
- {
- if (PlayerPrefs.HasKey(InstaGigId))
- {
- currentGig = GetGigByName(PlayerPrefs.GetString(InstaGigId));
- }
- else
- {
- currentGig = GetGigByCurrentLocation(type);
- }
- }
- private void SerializeGig()
- {
- replay = !currentGig.Id.Contains("$");
- PlayerPrefs.SetString(InstaGigId, currentGig.Id);
- }
- private void UpdateGigAfterReplay(DataTools.Gig gig)
- {
- string replayed = gig.Id;
- DataTools.Gig gigSaved = dataUser.UserData.ProgressData.GigsPlayed.Find(g => g.Id == replayed);
- if (gigSaved.StarsEarned < gig.StarsEarned)
- {
- gigSaved.StarsEarned = gig.StarsEarned;
- }
- dataUser.UpdateUserProgress();
- }
- private bool DeserializeGig()
- {
- bool success = false;
- if (PlayerPrefs.HasKey(InstaGigId))
- {
- string savedGig = PlayerPrefs.GetString(InstaGigId);
- replay = !savedGig.Contains("$");
- if(replay)
- {
- DataTools.Gig gig = GetGigByName(savedGig);
- currentGig = new DataTools.Gig(gig.Duration, gig.Location, null, gig.Currency, null, "", gig.Type, gig.StarsNeeded, gig.Description);
- currentGig.Id = savedGig;
- success = true;
- }
- else
- {
- string[] values = savedGig.Split('$');
- if (values.Length > 1)
- {
- string gigTitle = values[0];
- string gigLocaton = values[1];
- DataTools.GigType gigType = TweetBehaviour.GetGigTypeFromString(values[2]);
- int gigStars = int.Parse(values[3]);
- int gigDuration = int.Parse(values[4]);
- DataTools.Gig gig = new DataTools.Gig(gigDuration, gigLocaton, null, InstaGigAtlas.GetAutoReward(gigDuration), null, "", gigType, gigStars, gigTitle);
- gig.Id = savedGig;
- currentGig = gig;
- success = true;
- }
- }
- }
- else
- {
- Debug.LogWarning("No key in PlayerPrefs");
- }
- return success;
- }
- private void LoadIncompleteGig()
- {
- if(DeserializeGig())
- {
- RandomizeGigs(currentGig.StarsNeeded, currentGig.Type, currentGig.Location);
- BlockerManager.Instance.UnblockAllButtons(GameplayManager.instance.isFirstGigPassed);
- if (replay)
- {
- SideQuestsManager.Instance.GenerateQuestForGig(currentGig, true);
- }
- OnTimeReceived();
- }
- }
- private DataTools.Gig GetGigByName(string gigId)
- {
- Debug.Log("Searching gig by name: " + gigId);
- foreach (DataTools.Chapter chapter in data.GameData.Chapters)
- {
- foreach (DataTools.Gig gig in chapter.Gigs)
- {
- if (gig.Id == gigId)
- {
- return gig;
- }
- }
- }
- return null;
- }
- public DataTools.Gig GetGigByCurrentLocation(DataTools.GigType type)
- {
- Debug.Log("Searching gig by location: " + GameGlobal.Instance.Location);
- DataTools.Gig tempGig = null;
- foreach (DataTools.Chapter chapter in data.GameData.Chapters)
- {
- if (chapter == data.GameData.Chapters[0])
- {
- continue;
- }
- foreach (DataTools.Gig gig in chapter.Gigs)
- {
- if (gig.Location == GameGlobal.Instance.Location)
- {
- tempGig = gig;
- if (gig.Type == type)
- {
- return gig;
- }
- }
- }
- }
- return tempGig;
- }
- protected override void Update()
- {
- if (HasGigsRemaining && IsMainStoryGigExist)
- {
- OnFinishTask();
- }
- base.Update();
- }
- protected override void OnFinishTask()
- {
- DataTools.Gig gig = currentGig;
- gig.StarsEarned = dataUser.UserData.StarsEarned;
- PlayerPrefs.DeleteKey(InstaGigId);
- dataUser.UserData.CurrentInstantGig = string.Empty;
- dataUser.UpdateUserCurrentInstaGig();
- if (SideQuestsManager.Instance.IsSideQuestActive)
- {
- SideQuestsManager.Instance.GigComleted(gig.Id);
- }
- FinishGig();
- base.OnFinishTask();
- if (gig.Type != DataTools.GigType.Practice)
- {
- AddTweetReward(gig);
- }
- if (replay)
- {
- UpdateGigAfterReplay(gig);
- }
- }
- protected override void ShowTweetOnGigFinish()
- {
- AddTweetReward();
- }
- private void AddTweetReward()
- {
- ChirpTimerAndFollowers chirpTimerAndFollow = gigsGigActionsData.GetChirpTimerAndFollowers(currentGig.Type, currentGig.Duration);
- TweetBehaviour.Instance.GigTweet(currentGig, chirpTimerAndFollow, pointsBar);
- }
- private void AddTweetReward(DataTools.Gig gig)
- {
- ChirpTimerAndFollowers chirpTimerAndFollow = gigsGigActionsData.GetInstantChirpTimerAndFollowers(gig.Duration);
- TweetBehaviour.Instance.GigTweet(gig, chirpTimerAndFollow, pointsBar);
- }
- }
|