-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (131 loc) · 6.08 KB
/
server.js
File metadata and controls
164 lines (131 loc) · 6.08 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
157
158
159
160
161
162
163
164
// Code developed by Finley Lau*, Deepak Gopinath*. Copyright (c) 2020. Argallab. (*) Equal contribution. Based on main.js in nodus-ponens/template
// --------------------------------------------------------------------------------------------------
// Template Experiment 1
// --------------------------------------------------------------------------------------------------
// Participants: <Indicate participant pool information here>
// Date: <Indicate experiment dates here>
// Design: <Indicate names of people who created experimental design>
// Code: <Indicate names of people who wrote the code>
// --------------------------------------------------------------------------------------------------
// Quickstart. To create nodus-ponens experiment:
// i) Use the terminal to navigate to the folder that stores this file
// ii) Type node main.js in the terminal
// The terminal will display experiment information, including a link to whether the experiment can
// be taken and a status of any participants who take the study.
// --------------------------------------------------------------------------------------------------
// Contents
// 1. Setup nodus-ponens framework
// 2. Setup stimuli
// 3. Launch experiment
// --------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------
// 1. Setup nodus-ponens framework
// --------------------------------------------------------------------------------------------------
var path = require("path");
var fs = require('fs');
var cors = require('cors');
var staticDirectory = path.resolve("static"); // Set directory of static HTML+CSS files
var dataDirectory = path.resolve("data"); // Set directory where data will be written
var participantIndex = 0; // Start participant numbering at this value + 1
var np = require("./nodus-ponens")(participantIndex, staticDirectory, dataDirectory);
np.authors = "Authors";
np.experimentName = "Test Experiment";
np.port = 3003;
// --------------------------------------------------------------------------------------------------
// 2. Setup stimuli
// --------------------------------------------------------------------------------------------------
// Note: nodus-ponens has a special function called "loadStimuli" that is intended to take a
// participant's ID number (an integer) as input and return an array of objects representing the
// stimuli catered to that participant. In the code below, "setupStimuli" serves that purpose, and
// it calls a bunch of helper functions to do its job, e.g., "CSVtoJSON" and "assignContents". Hence
// after defining "setupStimuli", "CSVtoJSON", and "assignContents", the code sets np.loadStimuli to
// "setupStimuli".
function setupStimuli(PID)
{
var res = JSON.parse(fs.readFileSync('./Experiment.json').toString()); // read design into CSV string
// shuffle stimuli, if neccessary
if (res.shuffleBlocks)
{
res.blocks = np.randomize(res.blocks)
}
for (var i = 0; i < res.blocks.length; i++)
{
if (res.blocks[i].shuffleTrials)
{
res.blocks[i].trials = np.randomize(res.blocks[i].trials)
}
}
// flatten blocks into list of stimuli
var stimuliList = []
if (res.preExperiment) {
for (var i = 0; i < res.preExperiment.length; i++)
{
stimuliList.push(res.preExperiment[i])
}
}
if (res.blocks) {
for (var i = 0; i < res.blocks.length; i++)
{
var obj = {}
if (res.blocks[i].preTrials){
for (var l = 0; l < res.blocks[i].preTrials.length; l++)
{
obj = res.blocks[i].preTrials[l]
obj["blockName"] = res.blocks[i].blockName
stimuliList.push(obj)
}
}
for (var j = 0; j < res.blocks[i].trials.length; j++)
{
obj = res.blocks[i].trials[j]
obj["blockName"] = res.blocks[i].blockName
stimuliList.push(obj)
}
if (res.blocks[i].postTrials) {
for (var k = 0; k < res.blocks[i].postTrials.length; k++)
{
obj = res.blocks[i].postTrials[k]
obj["blockName"] = res.blocks[i].blockName
stimuliList.push(obj)
}
}
}
}
var design = extendJSON(stimuliList, PID); // add fields to each stimuli object
var stimuli = assignContents(design); // assign contents to the different problems in the design
return stimuli; // To randomize the order of the stimuli, return np.randomize(stimuli)
}
function extendJSON(obj, PID) // This fn imports any CSV "Design" file as a JSON object
{ // and adds some columns for R analyses
var results = obj;
for (var i = 0; i < results.length; i++)
{
results[i]["ParticipantID"] = "P" + PID;
results[i]["ClockTime"] = new Date().toISOString();
results[i]["TrialHeader"] = "Trial";
}
return results;
}
function assignContents(design) // This assigns contents to schematic versions of problems
{
for(i = 0; i < design.length; i++)
{
design[i]["trialIndex"] = i
}
return design;
}
// --------------------------------------------------------------------------------------------------
// 3. Extra routes
// --------------------------------------------------------------------------------------------------
var corsOptions = {
origin: "http://localhost:3001"
}
np.app.use(cors(corsOptions))
np.app.get("/getExperimentName", (req, res) => {
res.json({"name": np.experimentName})
})
// --------------------------------------------------------------------------------------------------
// 4. Launch experiment
// --------------------------------------------------------------------------------------------------
np.loadStimuli = setupStimuli;
np.launchStudy();