InstaGigs.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using DataTools;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using WebTools.Phone;
  5. public class InstaGigs : RandomGigs
  6. {
  7. [Header("Data")]
  8. [SerializeField] private DataGigTemplates dataTemplates;
  9. private string InstaGigId = "InstaGigId";//need to use same value in DataUser class
  10. private List<GigsBase> mainStoryGigs;
  11. private bool replay = false;
  12. public bool IsMainStoryGigExist
  13. {
  14. get
  15. {
  16. return mainStoryGigs.Exists(g => g.HasGigsRemaining);
  17. }
  18. }
  19. protected override void Start()
  20. {
  21. base.Start();
  22. mainStoryGigs = new List<GigsBase>(FindObjectsOfType<GigsBase>());
  23. mainStoryGigs.RemoveAll(g => !g.IsMainStoryGig);
  24. Invoke("LoadIncompleteGig", 1);
  25. }
  26. private void LoadSavedGigs()
  27. {
  28. if (PlayerPrefs.HasKey(persistentDataKey))
  29. {
  30. GenerateDefaultGigs(DataTools.GigType.Photoshoot, false);
  31. }
  32. }
  33. protected override void OnLocationChanged(Location location)
  34. {
  35. base.OnLocationChanged(location);
  36. if (currentGig != null)
  37. {
  38. SetLocationButtons(currentGig.Location);
  39. }
  40. }
  41. public void GenerateDefaultGigs(DataTools.GigType type, bool saveGigStartTime = true)
  42. {
  43. }
  44. public void GenerateGigs(string gigId, DataTools.Gig gig)
  45. {
  46. if (gig == null)
  47. {
  48. gig = GetGigByName(gigId);
  49. }
  50. GenerateGigs(gig);
  51. }
  52. public void GenerateGigs(DataTools.Gig gig)
  53. {
  54. currentGig = gig;
  55. SerializeGig();
  56. if (dataUser.UserData.CurrentInstantGig != gig.Id)
  57. {
  58. dataUser.UserData.CurrentInstantGig = gig.Id;
  59. dataUser.OnGigTimestampReceived += OnTimeReceived;
  60. dataUser.UpdateUserCurrentInstaGig();
  61. dataUser.UpdateGigStartTime();
  62. }
  63. else
  64. {
  65. OnTimeReceived();
  66. }
  67. RandomizeGigs(currentGig.StarsNeeded, currentGig.Type, currentGig.Location);
  68. }
  69. protected override void OnDestroy()
  70. {
  71. if (HasGigsRemaining)
  72. {
  73. SerializeGig();
  74. }
  75. base.OnDestroy();
  76. }
  77. private void SetDefaultGig(DataTools.GigType type=DataTools.GigType.Photoshoot)
  78. {
  79. if (PlayerPrefs.HasKey(InstaGigId))
  80. {
  81. currentGig = GetGigByName(PlayerPrefs.GetString(InstaGigId));
  82. }
  83. else
  84. {
  85. currentGig = GetGigByCurrentLocation(type);
  86. }
  87. }
  88. private void SerializeGig()
  89. {
  90. replay = !currentGig.Id.Contains("$");
  91. PlayerPrefs.SetString(InstaGigId, currentGig.Id);
  92. }
  93. private void UpdateGigAfterReplay(DataTools.Gig gig)
  94. {
  95. string replayed = gig.Id;
  96. DataTools.Gig gigSaved = dataUser.UserData.ProgressData.GigsPlayed.Find(g => g.Id == replayed);
  97. if (gigSaved.StarsEarned < gig.StarsEarned)
  98. {
  99. gigSaved.StarsEarned = gig.StarsEarned;
  100. }
  101. dataUser.UpdateUserProgress();
  102. }
  103. private bool DeserializeGig()
  104. {
  105. bool success = false;
  106. if (PlayerPrefs.HasKey(InstaGigId))
  107. {
  108. string savedGig = PlayerPrefs.GetString(InstaGigId);
  109. replay = !savedGig.Contains("$");
  110. if(replay)
  111. {
  112. DataTools.Gig gig = GetGigByName(savedGig);
  113. currentGig = new DataTools.Gig(gig.Duration, gig.Location, null, gig.Currency, null, "", gig.Type, gig.StarsNeeded, gig.Description);
  114. currentGig.Id = savedGig;
  115. success = true;
  116. }
  117. else
  118. {
  119. string[] values = savedGig.Split('$');
  120. if (values.Length > 1)
  121. {
  122. string gigTitle = values[0];
  123. string gigLocaton = values[1];
  124. DataTools.GigType gigType = TweetBehaviour.GetGigTypeFromString(values[2]);
  125. int gigStars = int.Parse(values[3]);
  126. int gigDuration = int.Parse(values[4]);
  127. DataTools.Gig gig = new DataTools.Gig(gigDuration, gigLocaton, null, InstaGigAtlas.GetAutoReward(gigDuration), null, "", gigType, gigStars, gigTitle);
  128. gig.Id = savedGig;
  129. currentGig = gig;
  130. success = true;
  131. }
  132. }
  133. }
  134. else
  135. {
  136. Debug.LogWarning("No key in PlayerPrefs");
  137. }
  138. return success;
  139. }
  140. private void LoadIncompleteGig()
  141. {
  142. if(DeserializeGig())
  143. {
  144. RandomizeGigs(currentGig.StarsNeeded, currentGig.Type, currentGig.Location);
  145. BlockerManager.Instance.UnblockAllButtons(GameplayManager.instance.isFirstGigPassed);
  146. if (replay)
  147. {
  148. SideQuestsManager.Instance.GenerateQuestForGig(currentGig, true);
  149. }
  150. OnTimeReceived();
  151. }
  152. }
  153. private DataTools.Gig GetGigByName(string gigId)
  154. {
  155. Debug.Log("Searching gig by name: " + gigId);
  156. foreach (DataTools.Chapter chapter in data.GameData.Chapters)
  157. {
  158. foreach (DataTools.Gig gig in chapter.Gigs)
  159. {
  160. if (gig.Id == gigId)
  161. {
  162. return gig;
  163. }
  164. }
  165. }
  166. return null;
  167. }
  168. public DataTools.Gig GetGigByCurrentLocation(DataTools.GigType type)
  169. {
  170. Debug.Log("Searching gig by location: " + GameGlobal.Instance.Location);
  171. DataTools.Gig tempGig = null;
  172. foreach (DataTools.Chapter chapter in data.GameData.Chapters)
  173. {
  174. if (chapter == data.GameData.Chapters[0])
  175. {
  176. continue;
  177. }
  178. foreach (DataTools.Gig gig in chapter.Gigs)
  179. {
  180. if (gig.Location == GameGlobal.Instance.Location)
  181. {
  182. tempGig = gig;
  183. if (gig.Type == type)
  184. {
  185. return gig;
  186. }
  187. }
  188. }
  189. }
  190. return tempGig;
  191. }
  192. protected override void Update()
  193. {
  194. if (HasGigsRemaining && IsMainStoryGigExist)
  195. {
  196. OnFinishTask();
  197. }
  198. base.Update();
  199. }
  200. protected override void OnFinishTask()
  201. {
  202. DataTools.Gig gig = currentGig;
  203. gig.StarsEarned = dataUser.UserData.StarsEarned;
  204. PlayerPrefs.DeleteKey(InstaGigId);
  205. dataUser.UserData.CurrentInstantGig = string.Empty;
  206. dataUser.UpdateUserCurrentInstaGig();
  207. if (SideQuestsManager.Instance.IsSideQuestActive)
  208. {
  209. SideQuestsManager.Instance.GigComleted(gig.Id);
  210. }
  211. FinishGig();
  212. base.OnFinishTask();
  213. if (gig.Type != DataTools.GigType.Practice)
  214. {
  215. AddTweetReward(gig);
  216. }
  217. if (replay)
  218. {
  219. UpdateGigAfterReplay(gig);
  220. }
  221. }
  222. protected override void ShowTweetOnGigFinish()
  223. {
  224. AddTweetReward();
  225. }
  226. private void AddTweetReward()
  227. {
  228. ChirpTimerAndFollowers chirpTimerAndFollow = gigsGigActionsData.GetChirpTimerAndFollowers(currentGig.Type, currentGig.Duration);
  229. TweetBehaviour.Instance.GigTweet(currentGig, chirpTimerAndFollow, pointsBar);
  230. }
  231. private void AddTweetReward(DataTools.Gig gig)
  232. {
  233. ChirpTimerAndFollowers chirpTimerAndFollow = gigsGigActionsData.GetInstantChirpTimerAndFollowers(gig.Duration);
  234. TweetBehaviour.Instance.GigTweet(gig, chirpTimerAndFollow, pointsBar);
  235. }
  236. }