-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerDataUploader.cs
More file actions
156 lines (130 loc) · 5.33 KB
/
ServerDataUploader.cs
File metadata and controls
156 lines (130 loc) · 5.33 KB
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
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
[System.Serializable]
public class DataClassing
{
[SerializeField] public string conditionInfo = string.Empty;
[SerializeField] public string[] dataTable = new string[1001];
}
public class ServerDataUploader : MonoBehaviour
{
private DataClassing expData = new DataClassing();
// Variables
#region
public ExperimentState expState;
public GameObject[] Objects2Record;
public string setURL = "http://www.bhamxr.com:1234/JoesTaskJson_v1.php";
private List<string> recordDataList = new List<string>();
private float startTime = 0f;
private bool startOfTrial = false;
private int frameNum = 0;
private string conditionPost;
private string userIDPost;
private int trialnum = -1;
#endregion
private void Update()
{
// Test script
if (Input.GetKeyDown(KeyCode.V))
{
string userIDPost = PlayerPrefs.GetString("userID") + "_" +
PlayerPrefs.GetString("age") + "_" +
PlayerPrefs.GetString("gender") + "_" +
PlayerPrefs.GetString("hand");
string groupCondition = "Reward";
conditionPost = "_" + groupCondition + "_" +
(expState.exPonent * 1000f).ToString("F0") + "_" +
expState.actualTrialNum.ToString("F0") + "_" +
(expState.dialGaining * 10).ToString("F0") + "_" +
trialnum.ToString();
//trialNumPost = trialnum.ToString();
//if (expState.uploadData) // end of trial
//{
Debug.Log("Upload Command Sent!!!");
StartCoroutine(Upload2());
// expState.uploadData = false;
//}
}
if (expState.startTrial[0] == 1)
{
recordDataList.Clear();
trialnum++;
startOfTrial = true;
startTime = Time.time;
}
}
private void FixedUpdate()
{
/*---------------------------------------- Trial data collection start -------------------------------------------*/
#region
if (startOfTrial)
{
userIDPost = PlayerPrefs.GetString("userID") + "_" +
PlayerPrefs.GetString("age") + "_" +
PlayerPrefs.GetString("gender") + "_" +
PlayerPrefs.GetString("hand");
string groupCondition = "";
if (expState.group == 0)
{
groupCondition = "NoReward";
}
else
{
groupCondition = "Reward";
}
conditionPost = "_" + groupCondition + "_" +
(expState.exPonent * 1000f).ToString("F0") + "_" +
(expState.dialGaining * 10).ToString("F0") + "_" +
expState.actualTrialNum.ToString("F0") + "_" +
trialnum.ToString();
frameNum = 0;
startTime = Time.time;
startOfTrial = false;
}
// Trial duration
float currTime = Time.time - startTime;
foreach (GameObject obj in Objects2Record)
{
recordDataList.Add(frameNum.ToString() +";"+
obj.name +";"+
obj.transform.position.x.ToString("F3") +";"+
obj.transform.position.y.ToString("F3") + ";" +
obj.transform.position.z.ToString("F3") + ";" +
obj.transform.localEulerAngles.x.ToString("F3") +";"+
obj.transform.localEulerAngles.y.ToString("F3") + ";" +
obj.transform.localEulerAngles.z.ToString("F3") + ";" +
expState.score.ToString() +";"+
currTime.ToString("F3")
);
}
frameNum++;
#endregion
/*---------------------------------------- Trial data collection end ---------------------------------------------*/
if (expState.uploadData) // end of trial
{
StartCoroutine(Upload2());
expState.uploadData = false;
}
}
private IEnumerator Upload2()
{
// Convert to json and send to another site on the server
expData.conditionInfo = userIDPost + conditionPost;
expData.dataTable = recordDataList.ToArray();
WWWForm form2 = new WWWForm();
string jsonString = JsonConvert.SerializeObject(expData, Formatting.Indented);
form2.AddField("postUserID", expData.conditionInfo);
form2.AddField("postJsonData", jsonString);
UnityWebRequest www2 = UnityWebRequest.Post(setURL, form2);
yield return www2.SendWebRequest();
if (www2.isNetworkError || www2.isHttpError)
{
Debug.Log(www2.error);
}
// Empty text fields for next trials (potential for issues with next trial)
recordDataList.Clear();
}
}