-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExperimentManager.cs
742 lines (666 loc) · 29.5 KB
/
ExperimentManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UXF;
using Newtonsoft.Json;
using System.Linq;
using System;
using UnityEngine.UI;
using UnityEngine.Events;
using UXF.UI;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace ubco.ovilab.uxf.extensions
{
// TODO: Allow using ppid
// TODO: Allow registering calibration functions from inspector
/// <summary>
/// Extension class with UXF functions that work with the
/// experiment server. The json data is parsed with a
/// <see cref="BlockData"/> or any extension of it. Also provides
/// abstractions to have a calibration phrase before each block.
/// </summary>
public abstract class ExperimentManager<TBlockData> : MonoBehaviour, IExperimentManager<TBlockData> where TBlockData:BlockData
{
private const string UXF_RIG_PATH = "Packages/ubco.ovilab.uxf.extensions/Runtime/UXF/Prefabs/[UXF_Rig].prefab";
[SerializeField] private UIController UXFUIController;
[SerializeField] private StartupMode UXFUIStartupMode;
/// <summary>
/// The study name used when starting session (See Session.Begin).
/// This could be overridden by calling <see cref="SessionBeginParams"/>
/// If UXF UI is used to start a session, the value provided there is taken precedence.
/// </summary>
[Tooltip("The study name used when starting session (See Session.Begin).")]
[SerializeField] public string experimentName = "study";
/// <summary>
/// The sesstion ID used when starting session (See Session.Begin).
/// This could be overridden by calling <see cref="SessionBeginParams"/>.
/// If UXF UI is used to start a session, the value provided there is taken precedence.
/// </summary>
[Tooltip("The sesstion ID used when starting session (See Session.Begin). If UXF UI is used to start a session, the value provided there is taken precedence.")]
[SerializeField] public int sessionNumber = 1;
[Tooltip("Data source being used.")]
public DataSource dataSource;
[Tooltip("Event called when block data is recieved.")]
public UnityEvent<TBlockData> onBlockRecieved = new UnityEvent<TBlockData>();
[Tooltip("(optional) The string to display on `Display Text`")]
[Multiline][SerializeField] private string askPrompt = "When ready ask researcher to proceed with the experiment";
[Tooltip("(optional) The UI button used to move to next/cancel. Note that if not set, `MoveToNextState` has to be called to proceed through experiments.")]
[SerializeField] private Button startNextButton;
[Tooltip("(optional) The text where the logs gets printed.")]
[SerializeField] private TMPro.TMP_Text outputText;
[Tooltip("(optional) The text in the environment where relevant promts will be displayed.")]
[SerializeField] protected TMPro.TMP_Text displayText;
[Tooltip("(optional) The text which shows the current trial/block counts.")]
[SerializeField] private TMPro.TMP_Text countText;
[Tooltip("(optional) The text of the start next button. When `Start Next Button` is set, this would help indicate the current state of the progress on screen.")]
/// <inheritdoc />
public int ParticipantIndex {
get {
Assert.IsNotNull(configSummary, "Config summary not set yet!");
return configSummary.ParticipantIndex;
}
}
/// <inheritdoc />
public int ConfigsLength {
get {
Assert.IsNotNull(configSummary, "Config summary not set yet!");
return configSummary.ConfigsLength;
}
}
#region HIDDEN_VARIABLES
private Settings initialSettings;
private Dictionary<string, object> initialParticipantDetails;
private TMPro.TMP_Text startNextButtonText;
private System.Random random;
private bool blockEnded = true;
private bool sessionStarted = false;
private bool tryingToGetData = false;
private bool waitToGetData = false;
private bool lastBlockCancelled = false;
private ConfigSummaryData configSummary;
private int countDisplay_trialTotal;
private string statusString;
private TBlockData blockData;
// NOTE: If a calibration function is set, when appropriate
// the CalibrationComplete function also should be
// called. Until then the next block will not get called.
private Dictionary<string, Action<TBlockData>> calibrationFunctions = new Dictionary<string, Action<TBlockData>>();
private CalibrationState calibrationState = CalibrationState.none;
private Dictionary<string, object> calibrationParameters;
private int getJsonRetryCounter = 0;
private List<TBlockData> defaultData;
private int currentDefaultDataIndex = 0;
#endregion
#region UNITY_FUNCTIONS
/// <summary>
/// Unity method. Extending classes must call this.
/// </summary>
public virtual void Start()
{
blockEnded = true;
sessionStarted = false;
tryingToGetData = false;
waitToGetData = false;
configSummary = null;
outputText?.SetText("");
OnValidate();
Session session = Session.instance;
session.onSessionBegin.AddListener(OnSessionBeginBase);
session.onBlockBegin.AddListener(OnBlockBeginBase);
session.onBlockEnd.AddListener(OnBlockEndBase);
session.onTrialBegin.AddListener(OnTrialBeginBase);
session.onTrialEnd.AddListener(OnTrialEndBase);
session.onSessionEnd.AddListener(OnSessionEndBase);
session.settingsToLog.AddRange(new List<string>(){ "blockName", "canceled", "calibrationName" });
startNextButton?.onClick.AddListener(MoveToNextState);
startNextButtonText = startNextButton?.GetComponentInChildren<TMPro.TMP_Text>();
startNextButtonText?.SetText("Load session data");
displayText?.SetText(askPrompt);
}
public void OnValidate()
{
if (UXFUIController == null)
{
#if UNITY_EDITOR
UIController[] uiControllerObjs = FindObjectsByType<UIController>(FindObjectsSortMode.None);
bool foundUIControllerObj = false;
foreach (UIController uiControllerObj in uiControllerObjs)
{
if (uiControllerObj != null)
{
UXFUIController = uiControllerObj;
foundUIControllerObj = true;
}
}
if (!foundUIControllerObj)
{
UXFUIController = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(UXF_RIG_PATH)).GetComponentInChildren<UIController>();
}
UXFUIController.experimentName = experimentName;
#else
Debug.LogWarning("The UXF Rig has not been configured?");
#endif
}
if (UXFUIController.startupMode != UXFUIStartupMode)
{
StartCoroutine(DelayedSetUXFUIStartupMode(UXFUIStartupMode));
}
}
/// <summary>
/// Get the data necessary to initiate the session.
/// </summary>
private void InitiateSessionStart(string nextButtonTextOndataRecieved)
{
tryingToGetData = true;
if (dataSource.useLocalData)
{
defaultData = JsonConvert.DeserializeObject<List<TBlockData>>(dataSource.configJsonFile.text);
Debug.Assert(defaultData.Select(d => d.participant_index).Distinct().Count() == 1, "There are more than 1 distinct participant indices in the provided config file");
if (configSummary == null)
{
configSummary = new ConfigSummaryData(defaultData[0].participant_index, defaultData.Count);
}
Debug.Log($"Recieved session data (pp# {configSummary.ParticipantIndex})");
AddToOutpuText($"Recieved session data (pp# {configSummary.ParticipantIndex})");
GetConfig();
startNextButtonText?.SetText(nextButtonTextOndataRecieved);
}
else
{
StartCoroutine(
#if UNITY_EDITOR
/// Making sure to start from the begining when experimentStartFrom0 is true
GetJsonUrl(dataSource.experimentStartFrom0 ? "api/move-to-block/0" : "api/active",
#else
GetJsonUrl("api/active",
#endif
(isActive) =>
{
Debug.Log(isActive);
if (isActive != "false")
{
GetSummaryData(nextButtonTextOndataRecieved);
}
else
{
StartCoroutine(GetJsonUrl("api/move-to-next",
(jsonText) =>
{
GetSummaryData(nextButtonTextOndataRecieved);
},
post: true));
}
},
#if UNITY_EDITOR
post: dataSource.experimentStartFrom0,
#endif
repeatIfFailed: true));
}
}
private void GetSummaryData(string nextButtonTextOndataRecieved)
{
StartCoroutine(GetJsonUrl("api/summary-data", (jsonText) =>
{
ConfigSummaryData data = JsonConvert.DeserializeObject<ConfigSummaryData>(jsonText);
if (configSummary == null)
{
configSummary = data;
}
Assert.AreEqual(data.ParticipantIndex, configSummary.ParticipantIndex,
$"Summary data from server (ppid: {data.ParticipantIndex}) does not match the cached summary data (ppid: {configSummary.ParticipantIndex}).");
Debug.Log($"Recieved session data (pp# {configSummary.ParticipantIndex}): {jsonText}");
AddToOutpuText($"Recieved session data (pp# {configSummary.ParticipantIndex}): {jsonText}");
GetConfig();
startNextButtonText?.SetText(nextButtonTextOndataRecieved);
}));
}
#endregion
#region UFX_FUNCTIONS
/// <summary>
/// Wrapper for <see cref="OnSessionBegin"/>
/// </summary>
private void OnSessionBeginBase(Session session)
{
if (!sessionStarted)
{
sessionStarted = true;
// TODO: Use the ppid endpoints of experiment manager.
Debug.LogWarning($"Starting server from UI? ExperimentManager does not handle/respect ppid set in UI.");
AddToOutpuText("Session started");
InitiateSessionStart("Run calibration");
}
OnSessionBegin(session);
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onSessionBegin">.
/// </summary>
protected abstract void OnSessionBegin(Session session);
/// <summary>
/// Call <see cref="ConfigureBlock"/>. Also update the UI and
/// add the default data from the exeperiment server (i.e.,
/// name, calibrationName), any calibration parameters passed
/// to the <see cref="CalibrationComplete"/> and the canceled
/// (false by default) status. The canceled status will be
/// updated if the block gets canceled.
/// </summary>
private void ConfigureBlockBase(TBlockData el)
{
Debug.Log($"Got {el}");
Block block = Session.instance.CreateBlock();
block.settings.SetValue("blockName", el.name);
block.settings.SetValue("canceled", false); /// by default block is not canceled
block.settings.SetValue("calibrationName", el.calibrationName);
block.settings.SetValue("calibrationParameters", calibrationParameters);
ConfigureBlock(el, block, lastBlockCancelled);
Debug.Log($"Added block with {block.trials.Count} trials");
}
/// <summary>
/// Abstract method that processes the configuration from experiment
/// server. Note that a new block is added each time a new configuration
/// is fetched from the experiment server.
/// </summary>
/// <param name="el">The parsed config data from experiment server.</param>
/// <param name="block">Current block being configured.</param>
/// <param name="lastBlockCancelled">If true, the last block was cancelled.</param>
protected abstract void ConfigureBlock(TBlockData el, Block block, bool lastBlockCancelled);
/// <summary>
/// Call <see cref="OnBlockBegin"/>, update the states and hide the <see cref="displayText"/>
/// </summary>
private void OnBlockBeginBase(Block block)
{
OnBlockBegin(block);
AddToOutpuText("Block: " + block.settings.GetString("blockName"));
AddToCountText(true);
displayText?.gameObject.SetActive(false);
blockEnded = false;
lastBlockCancelled = false;
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onBlockBegin">.
/// </summary>
protected abstract void OnBlockBegin(Block block);
/// <summary>
/// Call <see cref="OnTrialBegin"/> and update the counts on the UI.
/// </summary>
private void OnTrialBeginBase(Trial trial)
{
OnTrialBegin(trial);
AddToCountText();
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onTrialBegin">.
/// </summary>
protected abstract void OnTrialBegin(Trial trial);
/// <summary>
/// Call <see cref="OnTrialEnd"/>
/// </summary>
private void OnTrialEndBase(Trial trial)
{
OnTrialEnd(trial);
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onTrialEnd">.
/// </summary>
protected abstract void OnTrialEnd(Trial trial);
/// <summary>
/// Wrapper for <see cref="OnBlockEnd"/>. Also, save the block
/// settings to the session settings so that the block
/// information is also stored. The block data is stored with
/// key $"Block_{block.number}" Also, updates the states. If
/// The block was not cancelled, get the next block, else get
/// the canceled block again. Also, show the
/// <see cref="displayText"/> and update its text.
/// </summary>
private void OnBlockEndBase(Block block)
{
calibrationState = CalibrationState.none;
OnBlockEnd(block);
// Adding block information to setting to allow it to be logged
Session.instance.settings.SetValue($"Block_{block.number}", block.settings.baseDict);
blockEnded = true;
AddToOutpuText("Ended Block: " + block.settings.GetString("blockName"));
if (displayText != null)
{
displayText.gameObject.SetActive(true);
displayText.text = askPrompt;
}
startNextButtonText?.SetText("Get config");
waitToGetData = true;
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onBlockEnd">.
/// </summary>
protected abstract void OnBlockEnd(Block block);
/// <summary>
/// Wraper for <see cref="OnSessionEnd"/>. Also updates the UI
/// & hides the next button to avoid accidentally restarting
/// the session.
/// </summary>
private void OnSessionEndBase(Session session)
{
OnSessionEnd(session);
Debug.Log($"Ending session");
AddToOutpuText("Ending session");
startNextButton?.gameObject.SetActive(false);
}
/// <summary>
/// Abstract method to be used as a callback for <see cref="UXF.Session.onSessionEnd">.
/// </summary>
protected abstract void OnSessionEnd(Session session);
#endregion
#region HELPER_FUNCTIONS
/// <inheritdoc />
public void SessionBeginParams(string experimentName, int sessionNumber, Dictionary<string, object> participantDetails, Settings settings)
{
if (Session.instance.hasInitialised)
{
throw new InvalidOperationException("SessionBeginParams called after session started.");
}
this.experimentName = experimentName;
this.sessionNumber = sessionNumber;
initialParticipantDetails = participantDetails;
initialSettings = settings;
}
/// <summary>
/// Set which UXF UI Strtup mode to use. If set to manual, session would start when <see cref="MoveToNextState"/> is called.
/// If not the appropriate UXF UI startup mode would be used.
/// </summary>
public void SetUXFUIStartupMode(StartupMode startupMode)
{
UXFUIStartupMode = startupMode;
UXFUIController.startupMode = startupMode;
UXFUIController.LateValidate();
}
private IEnumerator DelayedSetUXFUIStartupMode(StartupMode startupMode)
{
yield return null;
SetUXFUIStartupMode(startupMode);
}
/// <inheritdoc />
public void MoveToNextState()
{
if (!sessionStarted && blockData == null && !tryingToGetData)
{
InitiateSessionStart("Start session");
}
else if (!sessionStarted && !Session.instance.hasInitialised)
{
if (configSummary.ParticipantIndex == -1)
{
AddToOutpuText("participant_index is -1, did't get data?");
return;
}
Session.instance.Begin(experimentName, $"{configSummary.ParticipantIndex}", sessionNumber, initialParticipantDetails, initialSettings);
Session.instance.settings.SetValue("participant_index", configSummary.ParticipantIndex);
// Makes sure the session doesn't get started again
sessionStarted = true;
AddToOutpuText("Session started");
startNextButtonText?.SetText("Run Calibration");
return;
}
else if (!blockEnded)
{
// We want to stop the block now!
// remove all the remaining trials
Session.instance.CurrentBlock.trials
.Where(x => x.status == TrialStatus.NotDone)
.ToList()
.ForEach((trial) => Session.instance.CurrentBlock.trials.Remove(trial));
// Mark as canceled
Session.instance.CurrentBlock.settings.SetValue("canceled", true);
lastBlockCancelled = true;
AddToOutpuText("Cancelled block!");
// Ending current trial would also end block
Session.instance.EndCurrentTrial();
}
else
{
if (blockData == null)
{
if (waitToGetData)
{
GetNextOrCurrentConfig();
}
else
{
AddToOutpuText("Block still not available");
}
}
else
{
// At this point we hopefully got the data for the next block.
// Using that to first process the calibration, then configure the block
string calibrationName = blockData.calibrationName;
switch (calibrationState) {
case CalibrationState.none: {
if (!string.IsNullOrEmpty(calibrationName) && calibrationFunctions.ContainsKey(calibrationName))
{
calibrationState = CalibrationState.started;
AddToOutpuText($"Running calibration - {calibrationName}");
calibrationFunctions[calibrationName].Invoke(blockData);
startNextButtonText?.SetText("...waiting");
}
else
{
AddToOutpuText($"No calibration - {calibrationName}");
calibrationState = CalibrationState.ended;
startNextButtonText?.SetText("Run block");
}
break;
}
case CalibrationState.started: {
AddToOutpuText($"Waiting for calibration to end - {calibrationName}");
break;
}
case CalibrationState.ended: {
ConfigureBlockBase(blockData);
blockData = null;
Session.instance.BeginNextTrial();
startNextButtonText?.SetText("Cancel");
break;
}
}
}
}
}
/// <inheritdoc />
public void CalibrationComplete(Dictionary<string, object> calibrationParameters)
{
// This data will be logged with the block settings
this.calibrationParameters = calibrationParameters;
AddToOutpuText($"Calibration completed");
calibrationState = CalibrationState.ended;
startNextButtonText?.SetText("Run block");
}
/// <inheritdoc />
public void AddCalibrationMethod(String name, Action<TBlockData> action)
{
if (calibrationFunctions.ContainsKey(name))
{
calibrationFunctions[name] = action;
}
calibrationFunctions.Add(name, action);
}
// Copied from UFX.UI.UIController
// Used to get the trial config from the server
IEnumerator GetJsonUrl(string endpoint, System.Action<string> action=null, System.Action<long> errorAction=null, bool post=false, bool repeatIfFailed=false)
{
string url = $"{dataSource.experimentServerUrl}/{endpoint}";
Debug.Log($"Request: {url}");
do
{
using (UnityWebRequest webRequest = post ? UnityWebRequest.PostWwwForm(url, "") : UnityWebRequest.Get(url))
{
webRequest.timeout = 5;
yield return webRequest.SendWebRequest();
bool error;
#if UNITY_2020_OR_NEWER
error = webRequest.result != UnityWebRequest.Result.Success;
#else
#pragma warning disable
error = webRequest.isHttpError || webRequest.isNetworkError;
#pragma warning restore
#endif
if (error)
{
Debug.LogWarning($"Request for {dataSource.experimentServerUrl} failed with: {webRequest.error}");
AddToOutpuText($"Retrying {getJsonRetryCounter++}");
errorAction?.Invoke(webRequest.responseCode);
yield return new WaitForSeconds(5);
continue;
}
getJsonRetryCounter = 0;
if (action != null)
{
action(System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data));
yield break;
}
}
} while (repeatIfFailed);
}
private void GetNextOrCurrentConfig()
{
// Get next only when current block was not cancelled
if (lastBlockCancelled)
{
GetConfig();
}
else
{
GetNextBlock();
}
waitToGetData = false;
startNextButtonText?.SetText("Run calibration");
}
private void GetConfig()
{
if (dataSource.useLocalData)
{
blockData = defaultData[currentDefaultDataIndex];
onBlockRecieved?.Invoke(blockData);
AddToOutpuText($"Got new block: {blockData.name}");
tryingToGetData = false;
}
else
{
StartCoroutine(
GetJsonUrl(
"api/config",
(jsonText) =>
{
blockData = JsonConvert.DeserializeObject<TBlockData>(jsonText);
onBlockRecieved?.Invoke(blockData);
AddToOutpuText($"Got new block: {blockData.name}");
tryingToGetData = false;
},
(errorCode) =>
{
// This happens only if /move-to-next needs to be called
if (errorCode == 406)
{
AddToOutpuText("Server moving to next");
StartCoroutine(GetJsonUrl("api/move-to-next", post:true));
}
},
repeatIfFailed:true));
}
}
private void GetNextBlock()
{
if (dataSource.useLocalData)
{
if(++currentDefaultDataIndex != defaultData.Count)
{
GetConfig();
}
else
{
Session.instance.End();
}
}
else
{
StartCoroutine(GetJsonUrl("api/move-to-next", (jsonText) =>
{
TBlockData el = JsonConvert.DeserializeObject<TBlockData>(jsonText);
if (el.name != "END")
{
GetConfig();
}
else
{
Session.instance.End();
}
}, post: true));
}
}
/// <summary>
/// Print messages and add the message as a line in the <see cref="outputText"/> if it has been set.
/// </summary>
protected void AddToOutpuText(string message)
{
Debug.Log(message);
if (outputText != null)
{
string[] slicedText = outputText.text.Split("\n");
if (slicedText.Length > 5)
{
slicedText = slicedText.Skip(1).ToArray();
}
outputText.text = string.Join("\n", slicedText) + $"\n{message}";
}
}
/// <summary>
/// Add one and update the diplayed counts in <see cref="countText"/>.
/// If <see cref="countText"/> is not set, this does nothing.
/// </summary>
/// <param name="updateTotals">If true, recompute the total counts.</param>
protected void AddToCountText(bool updateTotals=false)
{
if (countText == null)
{
return;
}
Session session = Session.instance;
if (updateTotals)
{
countDisplay_trialTotal = session.Trials.ToList().Count;
if (dataSource.useLocalData)
{
statusString = $"\nppid: {defaultData[0].participant_index} Block: {currentDefaultDataIndex}/{defaultData.Count} Name: {defaultData[currentDefaultDataIndex].name}";
}
else
{
StartCoroutine(GetJsonUrl("api/status-string", (text) =>
{
text = text.Replace(" ", " ").Replace("Participant index", "ppid");
statusString = $"\n{text}";
countText.text = $"{session.currentTrialNum}/{countDisplay_trialTotal} {statusString}";
}));
}
}
else
{
countText.text = $"{session.currentTrialNum}/{countDisplay_trialTotal} {statusString}";
}
}
#endregion
enum CalibrationState {
none, started, ended
}
}
internal class ConfigSummaryData
{
public int ParticipantIndex { get; set; }
public int ConfigsLength { get; set; }
public ConfigSummaryData(int participant_index, int config_length)
{
this.ParticipantIndex = participant_index;
this.ConfigsLength = config_length;
}
}
}