-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimSoccerServer.cpp
More file actions
157 lines (125 loc) · 4.28 KB
/
SimSoccerServer.cpp
File metadata and controls
157 lines (125 loc) · 4.28 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
// SimSoccerServer.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#ifndef WINDOWS
#include "SimSoccerServer.h"
#include "ic_api.h"
#include <stdio.h>
#include <iostream>
#include "SimSoccer/constants.h"
#include "Common/misc/utils.h"
//#include "Common/Time/PrecisionTimer.h"
#include "Common/Game/EntityManager.h"
#include "SimSoccer/SoccerPitch.h"
#include "SimSoccer/SoccerTeam.h"
#include "SimSoccer/Goalkeeper.h"
#include "SimSoccer/FieldPlayer.h"
#include "SimSoccer/FieldGoal.h"
#include "SimSoccer/SteeringBehaviors.h"
#include "Common/json/json.hpp"
#include "SimSoccer/ParamLoader.h"
//#include "Resource.h"
#include "Common/misc/Snapshot.h"
//#include "3rdparty/cpp-httplib/httplib.h"
//#include "3rdparty/picojson/picojson.h"
using json = nlohmann::json;
const int MATCH_DURATION = 10;
const int MATCH_RATE = 6;
const int MILLI_IN_SECOND = 20;
const int MILLI_IN_MINUTE = 60 * 20;
const int SECOND_MAX_VALUE = 60;
const bool LOG_MATCH_OUTPUT = true;
const int SNAPSHOT_RATE = 10;
int mTickCount = 0;
bool mMatchFinished = false;
SoccerPitch* g_SoccerPitch;
Snapshot* g_MatchReplay;
json g_LastSnapshot;
void IncrementTime(int rate)
{
mTickCount += MATCH_RATE * rate;
int minutes = mTickCount / MILLI_IN_MINUTE;
if (minutes >= MATCH_DURATION)
{
mMatchFinished = true;
}
}
std::string GetCurrentTimeString()
{
int seconds = (mTickCount / MILLI_IN_SECOND) % SECOND_MAX_VALUE;
int minutes = mTickCount / MILLI_IN_MINUTE;
// std::ostringstream stringStream;
// stringStream << minutes << " : " << seconds;
std::string time = "";//stringStream.str();
return time;
}
/* ---------------------------------------------------------
Extract a 'std::string" from an incoming CandidTypeText
Respond with an 'std::string' wrapped in a CandidTypeText
*/
void play_match() {
IC_API ic_api(CanisterQuery{std::string(__func__)}, false);
// Get the principal of the caller, as cryptographically verified by the IC
CandidTypePrincipal caller = ic_api.get_caller();
// Get the name, passed as a Candid parameter to this method
uint64_t seed{0};
ic_api.from_wire(CandidTypeNat64{&seed});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//std::cout << "Seed Generated : " << seed << std::endl;
//seed random number generator
srand(seed);
//std::cout << "Creating Pitch..." << std::endl;
PhysicsManager::Instance()->init();
g_SoccerPitch = new SoccerPitch(WindowWidth, WindowHeight);
if (LOG_MATCH_OUTPUT)
{
g_MatchReplay = new Snapshot();
}
//std::cout << "Starting Match..." << std::endl;
mMatchFinished = false;
int updates_count = 0;
while (!mMatchFinished)
{
IncrementTime(1);
//update game states
g_SoccerPitch->Update();
PhysicsManager::Instance()->Update();
g_SoccerPitch->CheckGoal();
if (LOG_MATCH_OUTPUT)
{
updates_count++;
//Don't take snapshot for every move
if (updates_count % SNAPSHOT_RATE == 1 || updates_count == 1)
{
g_LastSnapshot = g_MatchReplay->AddSnapshot(g_SoccerPitch);
}
}
}
json result;
if (LOG_MATCH_OUTPUT)
{
json raw_data = g_MatchReplay->Snapshots();
result["snapshot"] = raw_data.dump();
//std::ofstream o("match_server.json");
//o << std::setw(4) << raw_data.dump() << std::endl;
}
auto score1 = g_SoccerPitch->HomeTeam()->OpponentsGoal()->NumGoalsScored();
auto score2 = g_SoccerPitch->AwayTeam()->OpponentsGoal()->NumGoalsScored();
if (LOG_MATCH_OUTPUT)
{
delete g_MatchReplay;
}
delete g_SoccerPitch;
//result["seed"] = seed;
result["score1"] = score1;
result["score2"] = score2;
//std::cout << "score1 : " << score1 << std::endl;
//std::cout << "score2 : " << score2 << std::endl;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create a msg, to be passed back as Candid over the wire
std::string msg;
msg.append(result.dump());
//msg.append("Your principal is: " + caller.get_text());
// Send the response back
ic_api.to_wire(CandidTypeText{msg});
}
#endif