Skip to content

Commit 27c426e

Browse files
authored
Merge pull request #2 from AndresFRJ98/dev
Refactoring
2 parents e2dd0ac + 1673f3b commit 27c426e

14 files changed

+858
-395
lines changed
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace Fizzyo
8+
{
9+
10+
// Serializable which holds high score data
11+
[System.Serializable]
12+
public class AllHighscoreData
13+
{
14+
public HighscoreData[] highscores;
15+
16+
}
17+
18+
// Serializable which holds individual high score data
19+
[System.Serializable]
20+
public class HighscoreData
21+
{
22+
public string tag;
23+
public int score;
24+
public bool belongsToUser;
25+
}
26+
27+
// Serializable which holds achievement data
28+
29+
[System.Serializable]
30+
public class AllAchievementData
31+
{
32+
public AchievementData[] achievements;
33+
}
34+
35+
// Serializable that is used to pull and hold the data of each Achievement in the Achievements.json file
36+
[System.Serializable]
37+
public class AchievementData
38+
{
39+
public string category;
40+
public string id;
41+
public string title;
42+
public string description;
43+
public int points;
44+
public int unlock;
45+
public int unlockProgress;
46+
public int unlockRequirement;
47+
public string dependency;
48+
public string unlockedOn;
49+
}
50+
51+
// Serializable which holds calibration data
52+
[System.Serializable]
53+
public class CalibrationData
54+
{
55+
public string calibratedOn;
56+
public float pressure;
57+
public int time;
58+
}
59+
60+
61+
/// <summary>
62+
/// Used to unlock Fizzyo achievements and post high scores in the Fizzyo rest API
63+
/// </summary>
64+
65+
public class FizzyoAchievements
66+
{
67+
/// <summary>
68+
/// Array of type AchievementData which holds all the achievements that the game has to offer.
69+
/// </summary>
70+
public AchievementData[] allAchievements;
71+
/// <summary>
72+
/// Array of type AchievementData which holds the achievements the user has unlocked.
73+
/// </summary>
74+
public AchievementData[] unlockedAchievements;
75+
76+
77+
78+
/// <summary>
79+
/// Loads all game achievements and the users unlocked achievements and achievement progres.
80+
/// </summary>
81+
/// <returns>
82+
/// A JSON formatted string containing the list of achievements
83+
/// </returns>
84+
public FizzyoRequestReturnType LoadAchievements()
85+
{
86+
//Get all achievements from server
87+
string getAchievements = "https://api.fizzyo-ucl.co.uk/api/v1/games/" + FizzyoFramework.Instance.gameID + "/achievements";
88+
89+
Dictionary<string, string> headers = new Dictionary<string, string>();
90+
headers.Add("Authorization", "Bearer " + FizzyoFramework.Instance.User.AccessToken);
91+
WWW sendGetAchievements = new WWW(getAchievements, null, headers);
92+
93+
while (!sendGetAchievements.isDone) { }
94+
95+
string achievementsJSONData = sendGetAchievements.text;
96+
allAchievements = JsonUtility.FromJson<AllAchievementData>(achievementsJSONData).achievements;
97+
98+
//get unlocked achievements
99+
string getUnlock = "https://api.fizzyo-ucl.co.uk/api/v1/users/" + FizzyoFramework.Instance.User.UserID + "/unlocked-achievements/" + FizzyoFramework.Instance.gameID;
100+
101+
headers = new Dictionary<string, string>();
102+
headers.Add("Authorization", "Bearer " + FizzyoFramework.Instance.User.AccessToken);
103+
WWW sendGetUnlock = new WWW(getUnlock, null, headers);
104+
105+
while (!sendGetUnlock.isDone) { }
106+
107+
if(sendGetUnlock.error != null)
108+
{
109+
return FizzyoRequestReturnType.FAILED_TO_CONNECT;
110+
}
111+
112+
string unlockedJSONData = sendGetUnlock.text;
113+
unlockedAchievements = JsonUtility.FromJson<AllAchievementData>(unlockedJSONData).achievements;
114+
115+
116+
return FizzyoRequestReturnType.SUCCESS;
117+
118+
}
119+
120+
internal void Load()
121+
{
122+
LoadAchievements();
123+
}
124+
125+
126+
/// <summary>
127+
/// Loads in the top 20 highscores for the current game
128+
/// </summary>
129+
/// <returns>
130+
/// A JSON formatted string containing tag and score for the top 20 scores of the game
131+
/// </returns>
132+
public FizzyoRequestReturnType GetHighscores()
133+
{
134+
string getHighscores = "https://api.fizzyo-ucl.co.uk/api/v1/games/" + PlayerPrefs.GetString("gameId") + "/highscores";
135+
136+
Dictionary<string, string> headers = new Dictionary<string, string>();
137+
headers.Add("Authorization", "Bearer " + PlayerPrefs.GetString("accessToken"));
138+
WWW sendGetHighscores = new WWW(getHighscores, null, headers);
139+
140+
while (!sendGetHighscores.isDone) { }
141+
142+
if (sendGetHighscores.error != null)
143+
{
144+
return FizzyoRequestReturnType.FAILED_TO_CONNECT;
145+
}
146+
147+
return FizzyoRequestReturnType.SUCCESS;
148+
}
149+
150+
151+
152+
153+
/// <summary>
154+
/// Uploads a players Score
155+
/// </summary>
156+
/// <returns>
157+
/// String - "High Score Upload Complete" - If upload completes
158+
/// String - "High Score Upload Failed" - If upload fails
159+
/// </returns>
160+
public FizzyoRequestReturnType PostScore(int score)
161+
{
162+
string uploadScore = "https://api.fizzyo-ucl.co.uk/api/v1/games/" + PlayerPrefs.GetString("gameId") + "/highscores";
163+
164+
WWWForm form = new WWWForm();
165+
form.AddField("gameSecret", PlayerPrefs.GetString("gameSecret"));
166+
form.AddField("userId", PlayerPrefs.GetString("userId"));
167+
form.AddField("score", score);
168+
Dictionary<string, string> headers = form.headers;
169+
headers["Authorization"] = "Bearer " + PlayerPrefs.GetString("accessToken");
170+
171+
byte[] rawData = form.data;
172+
173+
WWW sendPostUnlock = new WWW(uploadScore, rawData, headers);
174+
175+
while (!sendPostUnlock.isDone) { };
176+
177+
if (sendPostUnlock.error != null)
178+
{
179+
return FizzyoRequestReturnType.FAILED_TO_CONNECT;
180+
}
181+
182+
return FizzyoRequestReturnType.SUCCESS;
183+
}
184+
185+
186+
/// <summary>
187+
/// Unlocks the achievement specified in the parameter, its achievementID.
188+
/// </summary>
189+
/// <returns>
190+
/// String - "Achievement Upload Complete" - If upload completes
191+
/// String - "Achievement Upload Failed" - If upload fails
192+
/// </returns>
193+
///
194+
public FizzyoRequestReturnType UnlockAchievement(string achievementId)
195+
{
196+
string unlockAchievement = "https://api.fizzyo-ucl.co.uk/api/v1/games/" + PlayerPrefs.GetString("gameId") + "/achievements/" + achievementId + "/unlock" ;
197+
198+
WWWForm form = new WWWForm();
199+
form.AddField("gameSecret", PlayerPrefs.GetString("gameSecret"));
200+
form.AddField("userId", PlayerPrefs.GetString("userId"));
201+
form.AddField("achievementId", achievementId);
202+
Dictionary<string, string> headers = form.headers;
203+
headers["Authorization"] = "Bearer " + PlayerPrefs.GetString("accessToken");
204+
205+
byte[] rawData = form.data;
206+
207+
WWW sendPostUnlock = new WWW(unlockAchievement, rawData, headers);
208+
209+
while (!sendPostUnlock.isDone) { };
210+
211+
if (sendPostUnlock.error != null)
212+
{
213+
return FizzyoRequestReturnType.FAILED_TO_CONNECT;
214+
//TODO add upload que here
215+
}
216+
return FizzyoRequestReturnType.SUCCESS;
217+
}
218+
219+
/// <summary>
220+
/// Uploads a players achievements for a session
221+
/// </summary>
222+
/// <returns>
223+
/// String - "Achievement Upload Complete" - If upload completes
224+
/// String - "Achievement Upload Failed" - If upload fails
225+
/// </returns>
226+
private FizzyoRequestReturnType PostAchievements()
227+
{
228+
string achievementsToUpload = PlayerPrefs.GetString("achievementsToUpload");
229+
230+
if (achievementsToUpload != "")
231+
{
232+
233+
string[] achievementsToUploadArray = achievementsToUpload.Split(',');
234+
235+
for (int i = 0; i < achievementsToUploadArray.Length; i++)
236+
{
237+
238+
if (achievementsToUploadArray[i] != "")
239+
{
240+
241+
string postUnlock;
242+
243+
postUnlock = "https://api.fizzyo-ucl.co.uk/api/v1/game/" + PlayerPrefs.GetString("gameId") + "/achievements/" + achievementsToUploadArray[i] + "/unlock";
244+
245+
WWWForm form = new WWWForm();
246+
247+
form.AddField("gameSecret", PlayerPrefs.GetString("gameSecret"));
248+
form.AddField("userId", PlayerPrefs.GetString("userId"));
249+
250+
Dictionary<string, string> headers = form.headers;
251+
headers["Authorization"] = "Bearer " + PlayerPrefs.GetString("accessToken");
252+
253+
byte[] rawData = form.data;
254+
255+
WWW sendPostUnlock = new WWW(postUnlock, rawData, headers);
256+
257+
while (!sendPostUnlock.isDone) { }
258+
259+
if (sendPostUnlock.error != null)
260+
{
261+
return FizzyoRequestReturnType.FAILED_TO_CONNECT;
262+
}
263+
264+
}
265+
266+
}
267+
268+
}
269+
270+
string achievementsToProgress = PlayerPrefs.GetString("achievementsToProgress");
271+
272+
string[] achievementsToProgressArray = achievementsToProgress.Split(',');
273+
274+
AllAchievementData allUserProgress = JsonUtility.FromJson<AllAchievementData>(PlayerPrefs.GetString(PlayerPrefs.GetString("userId") + "AchievementProgress"));
275+
AllAchievementData allAchievements = JsonUtility.FromJson<AllAchievementData>(PlayerPrefs.GetString("achievements"));
276+
277+
// Add achievement progress to player preferences
278+
for (int i = 0; i < achievementsToProgressArray.Length; i++)
279+
{
280+
281+
if (achievementsToProgressArray[i] != "")
282+
{
283+
284+
for (int j = 0; j < allUserProgress.achievements.Length; j++)
285+
{
286+
287+
if (allUserProgress.achievements[j].id == achievementsToProgressArray[i])
288+
{
289+
for (int k = 0; k < allAchievements.achievements.Length; k++)
290+
{
291+
292+
if (allUserProgress.achievements[j].id == allAchievements.achievements[k].id)
293+
{
294+
allUserProgress.achievements[j].unlockProgress = allAchievements.achievements[k].unlockProgress;
295+
string newAllData = JsonUtility.ToJson(allUserProgress);
296+
PlayerPrefs.SetString(PlayerPrefs.GetString("userId") + "AchievementProgress", newAllData);
297+
break;
298+
}
299+
300+
}
301+
302+
break;
303+
}
304+
305+
}
306+
307+
}
308+
309+
}
310+
return FizzyoRequestReturnType.SUCCESS;
311+
}
312+
313+
}
314+
315+
}
316+

Assets/Fizzyo/Scripts/FizzyoFramework/FizzyoAchievements.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Fizzyo/Scripts/FizzyoFramework/FizzyoAnalytics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace Fizzyo
88
{
9+
/// <summary>
10+
/// An instance of this class can be created to send game analytics once a session is over.
11+
/// This class is waiting for the developments of the API endpoints to be created.
12+
/// </summary>
913
public class FizzyoAnalytics
1014
{
1115

Assets/Fizzyo/Scripts/FizzyoFramework/FizzyoAnalytics.cs.meta

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)