-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverInteraction.js
141 lines (128 loc) · 3.9 KB
/
serverInteraction.js
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
function saveInitialData() {
subjectID = $('#participantID').val();
if ($('#skill').prop('checked')) {
levelSet = 'S';
} else if ($('#nonskill').prop('checked')) {
levelSet = 'N';
} else {
alert('Error: no level set selected');
return false;
}
for (var i = 1; i < 11; ++i) {
if($('#level' + i).prop('checked')) {
levelList.push(i);
levelQueue.push(i);
}
}
if (levelList.length == 0) {
alert('Error: No levels selected');
return false;
}
chosenLevel = levelQueue.shift();
$('#dataContainer').empty();
return true;
}
function sendDataToServer () {
// Holds information collected during gameplay in csv format
var playDataString = "";
var pair = 0;
var today = new Date();
var sessionID = today.getTime();
var day;
var month;
var year;
if (today.getDate() < 10) {
day = "0" + today.getDate();
} else {
day = "" + today.getDate();
}
if (today.getMonth() < 9) {
month = "0" + (today.getMonth() + 1);
} else {
month = "" + (today.getMonth() + 1);
}
year = today.getFullYear();
headerRow = ['ID', 'Date', 'Timestamp', 'Run',
'Level', 'Contingency', 'Reversed', 'Correct',
'Win', 'Side Chosen', 'Correct Side', 'Winning Side',
'Score', 'Trial Number', '% Stars Hit', '% Bombs Hit'];
playDataString += headerRow.join(',') + "\n";
var originalP = 0;
var level;
var i;
var score;
var fractionStars;
var fractionBombs;
// allLevels is a copy of levelList
var allLevels = levelList.slice(0);
// allScores is a copy of levelScores
var allScores = levelScores.slice(0);
var playDataRow = new Array(16);
for (level = allLevels.shift();
typeof level != 'undefined';
level = allLevels.shift()) {
originalP = successP[level][0];
score = allScores.shift();
if (totalCoinsPerLevel[level] === 0)
{
fractionStars = 0;
} else {
fractionStars = (coinsHitPerLevel[level] /
totalCoinsPerLevel[level]).toString().substring(0, 6);
}
if (totalEnemiesPerLevel[level] === 0)
{
fractionBombs = 0;
} else {
fractionBombs = (enemiesHitPerLevel[level] /
totalEnemiesPerLevel[level]).toString().substring(0, 6);
}
for (i = 0; i < sideChosenRecord[level].length; ++i){
playDataRow[0] = subjectID;
playDataRow[1] = "" + year + month + day;
playDataRow[2] = sessionID;
if (successP[level][i] === originalP) {
playDataRow[3] = pair;
} else {
playDataRow[3] = pair + 1;
}
playDataRow[4] = level;
playDataRow[5] = successP[level][i]
if (reversed[level][i]) {
playDataRow[6] = 1;
} else {
playDataRow[6] = 0;
}
if (sideChosenRecord[level][i] === correctSideRecord[level][i]) {
playDataRow[7] = 1;
} else {
playDataRow[7] = 0;
}
if (sideChosenRecord[level][i] === winLossRecord[level][i]) {
playDataRow[8] = 1;
} else {
playDataRow[8] = 0;
}
playDataRow[9] = sideChosenRecord[level][i];
playDataRow[10] = correctSideRecord[level][i];
playDataRow[11] = winLossRecord[level][i];
playDataRow[12] = score;
playDataRow[13] = i;
playDataRow[14] = fractionStars;
playDataRow[15] = fractionBombs;
playDataString += playDataRow.join(',') + "\n";
}
}
var filename = [year, month, day].join("-");
filename += "_" + sessionID + "_" + subjectID + ".csv";
var playData = { playDataString : playDataString,
filename : filename };
$.post('https://techhouse.org/~johnh/server/postResults.php',
playData,
function (data, textStatus, jqXHR) {
console.log('success');
}).fail(function () {
alert('The data did not save to the remote server. Please save the data below manually.');
$('#dataFallback').text(playDataString);
});
}