MarketStallSelection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. [RequireComponent(typeof(CastingToObject))]
  8. public class MarketStallSelection : MonoBehaviour
  9. {
  10. // Inspector variables
  11. public string logFileName = "";
  12. // Get from LoggingManager
  13. private int ParticipantID;
  14. private string logPathFolder = "";
  15. // Changes on click
  16. private bool sendSelection = false;
  17. private List<int>[][] selectedMarketStall;
  18. private List<float>[][] selectedMarketStallTime;
  19. private string[] marketStallNames;
  20. private string logPath;
  21. private float time = 0f;
  22. private float saveTime = 0f;
  23. private string[] buffer;
  24. private int index = 0;
  25. private void Start()
  26. {
  27. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  28. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  29. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  30. logPath = Path.Combine(logPathFolder, logFileName);
  31. using (FileStream stream = File.Open(logPath, FileMode.Create))
  32. {
  33. using (StreamWriter writer = new StreamWriter(stream))
  34. {
  35. writer.WriteLine(GetLogFileHeader());
  36. writer.Flush();
  37. }
  38. }
  39. Debug.Log("Created new Logfile " + logFileName);
  40. buffer = new string[1000];
  41. }
  42. private string GetLogFileHeader()
  43. {
  44. StringBuilder header = new StringBuilder("ParticipantID");
  45. header.Append(";Time Submit clicked");
  46. header.Append(";Market Stalls");
  47. header.Append(";Most Visited Market Stall");
  48. header.Append(";Low Waiting Time Market Stall");
  49. header.Append(";High Waiting Time Market Stall");
  50. header.Append(";Selection Time Most Visited ");
  51. header.Append(";Selection Time Low Waiting Time");
  52. header.Append(";Selection Time High Waiting Time");
  53. return header.ToString();
  54. }
  55. public void OnSubmitClicked()
  56. {
  57. sendSelection = true;
  58. selectedMarketStall = CastingToObject.selectedMarketStalls;
  59. selectedMarketStallTime = CastingToObject.selectedMarketStallsTimes;
  60. marketStallNames = CastingToObject.marketStallNames;
  61. saveTime = time;
  62. time = 0f;
  63. }
  64. private void FixedUpdate()
  65. {
  66. if (sendSelection)
  67. {
  68. for (int i = 0; i < selectedMarketStall.Length; i++)
  69. {
  70. StringBuilder line = new StringBuilder();
  71. line.Append(ParticipantID);
  72. line.Append(";" + saveTime);
  73. line.Append(";" + marketStallNames[i]);
  74. line.Append(";" + string.Join("|", selectedMarketStall[i][0]));
  75. line.Append(";" + string.Join("|", selectedMarketStall[i][1]));
  76. line.Append(";" + string.Join("|", selectedMarketStall[i][2]));
  77. line.Append(";" + string.Join("|", selectedMarketStallTime[i][0]));
  78. line.Append(";" + string.Join("|", selectedMarketStallTime[i][1]));
  79. line.Append(";" + string.Join("|", selectedMarketStallTime[i][2]));
  80. buffer[index++] = line.ToString();
  81. if (index > buffer.Length - 1)
  82. writeFromBuffer();
  83. }
  84. Debug.Log("Submission successful!");
  85. sendSelection = false;
  86. }
  87. time += Time.deltaTime;
  88. }
  89. private void writeFromBuffer()
  90. {
  91. if (File.Exists(logPath))
  92. {
  93. try
  94. {
  95. using (StreamWriter writer = new StreamWriter(logPath, true))
  96. {
  97. for (int i = 0; i < buffer.Length; i++)
  98. {
  99. if (buffer[i] != null)
  100. {
  101. writer.WriteLine(buffer[i]);
  102. writer.Flush();
  103. }
  104. }
  105. buffer = new string[1000];
  106. index = 0;
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  112. }
  113. }
  114. }
  115. private void OnApplicationQuit()
  116. {
  117. writeFromBuffer();
  118. }
  119. }