MarketStallSelection.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [RequireComponent(typeof(CastingToObject))]
  7. public class MarketStallSelection : MonoBehaviour
  8. {
  9. // Inspector variables
  10. public string logFileName = "";
  11. // Get from LoggingManager
  12. private int ParticipantID;
  13. private string logPathFolder = "";
  14. // Changes on click
  15. private bool sendSelection = false;
  16. private GameObject selectedMarketStall;
  17. private string logPath;
  18. private float time = 0f;
  19. private void Start()
  20. {
  21. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  22. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  23. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  24. logPath = Path.Combine(logPathFolder, logFileName);
  25. using (FileStream stream = File.Open(logPath, FileMode.Create))
  26. {
  27. using (StreamWriter writer = new StreamWriter(stream))
  28. {
  29. writer.WriteLine(GetLogFileHeader());
  30. writer.Flush();
  31. }
  32. }
  33. Debug.Log("Created new Logfile " + logFileName);
  34. }
  35. private string GetLogFileHeader()
  36. {
  37. StringBuilder header = new StringBuilder("ParticipantID");
  38. header.Append(";Time");
  39. header.Append(";Selected Market Stall");
  40. return header.ToString();
  41. }
  42. public void OnSubmitClicked()
  43. {
  44. sendSelection = true;
  45. selectedMarketStall = GameObject.Find(CastingToObject.theFinalObject);
  46. }
  47. private void FixedUpdate()
  48. {
  49. if (sendSelection)
  50. {
  51. if (File.Exists(logPath))
  52. {
  53. try
  54. {
  55. using (StreamWriter writer = new StreamWriter(logPath, true))
  56. {
  57. StringBuilder line = new StringBuilder();
  58. line.Append(ParticipantID);
  59. line.Append(";" + time);
  60. line.Append(";" + selectedMarketStall);
  61. writer.WriteLine(line);
  62. writer.Flush();
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  68. }
  69. }
  70. Debug.Log("Submission successful! Selected Market Stall: " + selectedMarketStall);
  71. sendSelection = false;
  72. }
  73. time += Time.deltaTime;
  74. }
  75. }