-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
212 lines (168 loc) · 6.7 KB
/
application.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "statemachine.h"
#include "game.h"
#include "port.h"
#include "client.h"
#include "statemachine.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <fstream>
#include <algorithm>
#include <random>
#include <fstream>
#include <cstdio>
#include "params.h"
int main() {
// Load previous parameters
Port port( 1234, 100 );
if ( !port.open )
return 0;
std::cout << "Connected to port " << 1234 << std::endl;
std::cout << "Waiting for clients" << std::endl;
Client* client1 = port.WaitForClient();
std::cout << "Waiting for second client" << std::endl;
Client* client2 = port.WaitForClient();
// Hyperscanning Application Base Compatible Version
std::string version = "1.0";
// Check that versions are compatible
if ( client1->version != client2->version ) {
std::cout << "Client Versions Do Not Match" << std::endl;
std::cout << "Client 1 Version: " << client1->version << std::endl;
std::cout << "Client 2 Version: " << client2->version << std::endl;
std::cout << "Please use clients with matching versions to ensure bug free usage" << std::endl;
return -1;
}
if ( client1->version != version ) {
std::cout << "Client versions are incompatible with this version of the backend" << std::endl;
std::cout << "Client Version: " << client1->version << std::endl;
std::cout << "Backend Version: " << version << std::endl;
std::cout << "Please use clients and backend with compatible versions to ensure bug free usage" << std::endl;
return -1;
}
if ( client1->sid != client2->sid ) {
std::cout << "Client Session IDs do not match" << std::endl;
std::cout << "Client 1 sID: " << client1->sid << std::endl;
std::cout << "Client 2 sID: " << client2->sid << std::endl;
std::cout << "Please use matching session IDs" << std::endl;
return -1;
}
if ( client1->sid.size() == 0 && client2->sid.size() == 0 ) {
std::cout << "Session ID not provided by clients" << std::endl;
return -1;
}
if ( client1->paramsPath != client2->paramsPath ) {
std::cout << "Client ServerParameterPath do not match" << std::endl;
std::cout << "Client 1 ServerParameterPath: " << client1->paramsPath << std::endl;
std::cout << "Client 2 ServerParameterPath: " << client2->paramsPath << std::endl;
std::cout << "Please use matching ServerParameterPath" << std::endl;
return -1;
}
if ( client1->paramsPath.size() == 0 && client2->paramsPath.size() == 0 ) {
std::cout << "ServerParameterPath not provided by clients" << std::endl;
return -1;
}
// Load Parameter File
Params params = Params( client1->paramsPath );
std::cout << "Loading existing params" << std::endl;
//bool swaped = false;
//Params clientparms = Params( client1->id + "-" + client2->id + ".prm" );
//if ( clientparms.contents.size() <= 0 ) {
// clientparms = Params( client2->id + "-" + client1->id + ".prm" );
// if ( clientparms.contents.size() ) swaped = true;
//
//}
std::cout << "Client1 SID: " << client1->sid << std::endl;
Params clientparms = Params( client1->sid + ".prm" );
std::string stimuliSequence;
int trials;
if ( clientparms.contents.size() > 0 ) {
std::cout << "Found existing game" << std::endl;
params.contents += clientparms.contents;
stimuliSequence = clientparms.GetParam( "StimuliSequence" )->line;
trials = clientparms.GetParam( "StimuliSequence" )->length;
}
else {
// Generate Random Sequence
std::cout << "Did not find existing game" << std::endl;
std::cout << "Generating new values..." << std::endl;
Param* stimmat = params.GetParam( "StimuliMatrix" );
std::cout << "Got stimuli matrix" << std::endl;
std::vector<int> order = std::vector<int>( stimmat->width );
for ( int i = 0; i < stimmat->width; i++ )
order[ i ] = i;
std::cout << "Width: " << stimmat->width << std::endl;
trials = stimmat->width;
std::random_device rd;
auto rng = std::default_random_engine( rd() );
std::shuffle( std::begin( order ), std::end( order ), rng );
std::cout << "First: " << order[ 0 ] << std::endl;
stimuliSequence = "\nApplication:Sequence intlist StimuliSequence= ";
stimuliSequence += std::to_string( stimmat->width );
stimuliSequence += " ";
for ( int i = 0; i < stimmat->width; i++ ) {
stimuliSequence += std::to_string( order[ i ] );
stimuliSequence += " ";
}
stimuliSequence += "% % % //Random Stimuli Sequence";
std::cout << stimuliSequence << std::endl;
params.AddParam( stimuliSequence );
params.AddParam( "Application int InitialTrialNumber= 0 % % % // trial number" );
}
params.contents.push_back( 0 );
std::cout << "Size: " << params.contents.size();
if ( clientparms.contents.size() <= 0 )
std::cout << "Could not find existing parameters" << std::endl;
// Initialize Game
Game game = Game( port, params.contents );
// Connect first client
std::ifstream indatfile( client1->sid + ".dat" );
std::string firstclient;
indatfile >> firstclient;
if ( firstclient == client1->id ) {
game.Connect( client1 );
std::cout << "Connected to first client: " << client1->id << std::endl;
game.Connect( client2 );
std::cout << "Connected to second client: " << client2->id << std::endl;
} else {
game.Connect( client2 );
std::cout << "Connected to first client: " << client2->id << std::endl;
game.Connect( client1 );
std::cout << "Connected to second client: " << client1->id << std::endl;
//Swap Clients
auto c = client2;
client2 = client1;
client1 = c;
}
// Run game loop
StateMachine out_states = game.Loop();
std::cout << "All done!" << std::endl;
std::string InitialTrialNumber = out_states.GetState( "TrialNumber" );
std::cout << "Trial Number: " << InitialTrialNumber << std::endl;
if ( InitialTrialNumber.size() == 0 ) InitialTrialNumber = "\1";
std::cout << "Saving Trial Number: " << ( int ) *InitialTrialNumber.c_str() - 1 << std::endl;
if ( ( int ) *InitialTrialNumber.c_str() < trials ) {
Params outparams = Params();
outparams.AddParam( "Application", "int", "InitialTrialNumber", std::to_string( ( int )*InitialTrialNumber.c_str() - 1 ) );
outparams.AddParam( stimuliSequence );
//if ( !swaped ) {
// std::ofstream egof( client1->id + "-" + client2->id + ".prm" );
// egof << outparams.contents << std::endl;
//}
//else {
// std::ofstream egof( client2->id + "-" + client1->id + ".prm" );
// egof << outparams.contents << std::endl;
//}
std::ofstream egof( client1->sid + ".prm" );
egof << outparams.contents << std::endl;
egof.close();
std::ofstream datfile( client1->sid + ".dat" );
datfile << client1->id << std::endl;
datfile << client2->id << std::endl;
datfile.close();
} else {
std::cout << "Trials finished, so not saving game" << std::endl;
std::remove( ( client1->sid + ".prm" ).c_str() );
std::remove( ( client1->sid + ".dat" ).c_str() );
}
std::cout << "All done!" << std::endl;
}