Tanoda
UserStatManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using GILES;
6using UnityEngine;
7using UnityEngine.UI;
8
9public class UserStatManager : pb_MonoBehaviourSingleton<UserStatManager>
10{
11 public GameObject TrainingPrefab;
12 public GameObject QuizPrefab;
13 public GameObject empty;
14 public GameObject statusWindow;
15 public Transform contentParent;
16 public static UserStatManager Instance { get; private set; }
17 internal float bestTime, averageTime;
18 private string selectedUsername = "";
19 private readonly List<GameObject> spawnedObjects = new List<GameObject>();
20 internal List<float> RecognitionList = new List<float>();
21 internal List<float> TimeSpendList = new List<float>();
22 internal List<float> PenaltyList = new List<float>();
23
24 public void Start()
25 {
26 Instance = this;
27 }
28 public void ClearUserStats()
29 {
30 var user = UserManager.instance.GetUserByName(selectedUsername);
31 user.trainings.Clear();
32 user.trainings = null;
33 UserManager.instance.UpdateUser(user);
34 statusWindow.SetActive(false);
35 }
36
37 public void ShowStats(UserManager.User user, string trainingName)
38 {
39 selectedUsername = user.Username;
40 if (user.trainings == null || user.trainings.Count == 0)
41 {
42 statusWindow.SetActive(true);
43 empty.SetActive(true);
44 foreach (var spawnedObject in spawnedObjects)
45 {
46 Destroy(spawnedObject);
47 }
48 spawnedObjects.Clear();
49 return;
50 }
51 else
52 {
53 statusWindow.SetActive(true);
54 empty.SetActive(false);
55 foreach (var spawnedObject in spawnedObjects)
56 {
57 Destroy(spawnedObject);
58 }
59 spawnedObjects.Clear();
60 }
61
62 var trainings = new List<Trainings>();
63
64 foreach (var training in user.trainings)
65 {
66 var first = trainings.FirstOrDefault(x => x.Name == training.Name);
67 if (first != default(Trainings))
68 {
69 first.successes.Add(training.Success);
70 first.times.Add(training.Time);
71 first.quizzes.AddQuizList(training.Quizzes);
72 }
73 else
74 {
75 trainings.Add(new Trainings()
76 {
77 Name = training.Name,
78 successes = new List<bool>() {training.Success},
79 times = new List<float>() {training.Time},
80 quizzes = Collector.ConvertQuiz(training.Quizzes)
81 });
82 }
83 }
84
85 foreach (var training in trainings)
86 {
87 var go = Instantiate(TrainingPrefab, contentParent);
88 if (training.Name == trainingName)
89 {
90
91
92 spawnedObjects.Add(go);
93 go.SetActive(true);
94 go.SetText("Name", training.Name);
95 var notzerotimes = training.times.Where(x => x != 0.0f);
96 if (!notzerotimes.Any())
97 {
98 notzerotimes = new List<float>(){3599.999f};
99 }
100 go.SetText("BestTime", TimeSpan.FromSeconds(notzerotimes.Min()).ToString("mm':'ss'.'fff"));
101 go.SetText("AverageTime", TimeSpan.FromSeconds(notzerotimes.Average()).ToString("mm':'ss'.'fff"));
102 go.SetText("LastTime", TimeSpan.FromSeconds(training.times.Last()).ToString("mm':'ss'.'fff"));
103 go.SetText("SuccessRate", ((float)training.successes.Count(x => x) / training.successes.Count).ToString("P"));
104
105 var quizTransform = go.transform.Find("Quizzes");
106
107
108 if (training.quizzes.Count == 0)
109 {
110 Destroy(quizTransform.gameObject);
111 }
112 else
113 {
114 foreach (var trainingQuiz in training.quizzes)
115 {
116 var qu = Instantiate(QuizPrefab, quizTransform);
117 qu.SetActive(true);
118 qu.SetText("Name", trainingQuiz.Name);
119 qu.SetText("BestTime", TimeSpan.FromSeconds(trainingQuiz.times.Min()).ToString("mm':'ss'.'fff"));
120 qu.SetText("SuccessRate", ((float)trainingQuiz.successes.Count(x => x) / trainingQuiz.successes.Count).ToString("P"));
121 }
122 }
123 }
124 }
125 }
126
127 public void GetStats(UserManager.User user, out IEnumerable<float> notzerotimes , out IEnumerable<float> notzerotimesAll, out List<float> recognitionList , out List<float> timeSpendList, out List<float> penaltyList)
128 {
129 notzerotimes = new List<float>();
130 notzerotimesAll = new List<float>();
131 recognitionList = new List<float>();
132 timeSpendList = new List<float>();
133 penaltyList = new List<float>();
134 var _trainings = new List<Trainings>();
135
136 if (user.trainings == null || user.trainings.Count == 0)
137 {
138 return;
139 }
140 var currentUserValue = UserManager.instance.GetUserByName(SavedUser.instance.currentUser.Value.Username);
141 var lastTraining = currentUserValue.trainings[currentUserValue.trainings.Count - 1];
142 //foreach (var action in lastTraining.Actions/*user.trainings.LastOrDefault().Actions*/)
143 //{
144 // recognitionList.Add(action.timeRecognition);
145 // timeSpendList.Add(action.timeSpent);
146 // penaltyList.Add(action.penaltyTime);
147
148 //}
149 foreach (var training in user.trainings)
150 {
151 if (training.Name == SavedUser.instance.levelName)
152 {
153 var first = _trainings.FirstOrDefault(x => x.Name == training.Name);
154 if (first != default(Trainings))
155 {
156 first.successes.Add(training.Success);
157 first.times.Add(training.Time);
158 first.quizzes.AddQuizList(training.Quizzes);
159 }
160 else
161 {
162 _trainings.Add(new Trainings()
163 {
164 Name = training.Name,
165 successes = new List<bool>() { training.Success },
166 times = new List<float>() { training.Time },
167 quizzes = Collector.ConvertQuiz(training.Quizzes)
168 });
169 }
170
171
172 }
173
174 }
175 foreach (var training in _trainings)
176 {
177 notzerotimes = training.times.Where(x => x != 0.0f);
178 if (!notzerotimes.Any())
179 {
180 notzerotimes = new List<float>() { 3599.999f };
181 }
182 }
183
184 var allTrainings = new List<Trainings>();
185 foreach (var _user in UserManager.instance.users)
186 {
187 if (_user.trainings != null)
188 foreach (var userTraining in _user.trainings)
189 {
190 if (userTraining.Name == SavedUser.instance.levelName)
191 {
192 var first = allTrainings.FirstOrDefault(x => x.Name == userTraining.Name);
193 if (first != default(Trainings))
194 {
195 first.successes.Add(userTraining.Success);
196 first.times.Add(userTraining.Time);
197 first.quizzes.AddQuizList(userTraining.Quizzes);
198 }
199 else
200 {
201 allTrainings.Add(new Trainings()
202 {
203 Name = userTraining.Name,
204 successes = new List<bool>() { userTraining.Success },
205 times = new List<float>() { userTraining.Time },
206 });
207 }
208 }
209
210 }
211 }
212
213
214 foreach (var allTraining in allTrainings)
215 {
216 notzerotimesAll = allTraining.times.Where(x => x != 0.0f);
217 if (!notzerotimesAll.Any())
218 {
219 notzerotimesAll = new List<float>() { 3599.999f };
220 }
221 }
222 }
223
224 private class Trainings : Collector
225 {
226 public List<Collector> quizzes = new List<Collector>();
227 }
228 internal class Collector
229 {
230 public string Name;
231 public List<float> times = new List<float>();
232 public List<bool> successes = new List<bool>();
233
234 public void AddTime(float time)
235 {
236 times.Add(time);
237 }
238 public void AddSuccess(bool value)
239 {
240 successes.Add(value);
241 }
242
243 public static List<Collector> ConvertQuiz(List<UserManager.Quiz> c)
244 {
245 var quiz = new List<Collector>();
246
247 if (c == null || c.Count == 0)
248 {
249 return quiz;
250 }
251
252 foreach (var q in c)
253 {
254 quiz.Add(new Collector()
255 {
256 Name = q.Name,
257 successes = new List<bool>(){q.Success},
258 times = new List<float>() {q.Time}
259 });
260 }
261
262 return quiz;
263 }
264 }
265}
266
267public static class CustomExtensions
268{
269 internal static void SetText(this GameObject go, string text, string value)
270 {
271 go.transform.Find(text)
272 .GetChild(1)
273 .GetComponent<Text>()
274 .text = value;
275 }
276
277 internal static void AddQuizList(this List<UserStatManager.Collector> collectors, List<UserManager.Quiz> value)
278 {
279 if (value == null || collectors == null || value.Count == 0)
280 {
281 return;
282 }
283 foreach (var collector in collectors)
284 {
285 foreach (var quiz in value)
286 {
287 if (collector.Name != quiz.Name) continue;
288 collector.AddTime(quiz.Time);
289 collector.AddSuccess(quiz.Success);
290 }
291 }
292
293 foreach (var quiz in value)
294 {
295 if (collectors.All(x => x.Name != quiz.Name))
296 {
297 collectors.Add(new UserStatManager.Collector()
298 {
299 Name = quiz.Name,
300 successes = new List<bool>(){ quiz.Success},
301 times = new List<float>() {quiz.Time}
302 });
303 }
304 }
305 }
306}
void ShowStats(UserManager.User user, string trainingName)
Transform contentParent
GameObject QuizPrefab
GameObject empty
void GetStats(UserManager.User user, out IEnumerable< float > notzerotimes, out IEnumerable< float > notzerotimesAll, out List< float > recognitionList, out List< float > timeSpendList, out List< float > penaltyList)
GameObject statusWindow
GameObject TrainingPrefab
static UserStatManager Instance