This commit is contained in:
MarcEricMartel 2023-12-02 09:23:26 -05:00
parent 37ea09aaf3
commit c44d1453ae
2 changed files with 87 additions and 80 deletions

View File

@ -19,7 +19,7 @@ Server::~Server() {
if (m_sock_tcp)
closesocket(m_sock_tcp);
for (const auto& [key, player] : m_players)
closesocket(player->getSock());
closesocket(player->getSock());
m_players.clear();
delete m_world;
#ifdef _WIN32
@ -77,7 +77,8 @@ int Server::Ready() {
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
try {
m_game.countdown = std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
}
catch (const std::exception& e) {
Log(e.what(), true, false);
m_game.countdown = 0;
}
@ -87,7 +88,8 @@ int Server::Ready() {
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
try {
m_game.seed = 9370707;//std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
}
catch (const std::exception& e) {
Log(e.what(), true, false);
m_game.seed = 0;
}
@ -97,7 +99,8 @@ int Server::Ready() {
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
try {
nbrjoueurs = std::stoi(m_buf.ptr);
} catch(const std::exception& e) {
}
catch (const std::exception& e) {
Log(e.what(), true, false);
nbrjoueurs = 0;
}
@ -154,8 +157,8 @@ int Server::Ready() {
strcpy(play.name, log->name);
Log(str.append(play.name).append(" SID: [").append(std::to_string(log->sid)).append("]")
.append(" ID: [").append(std::to_string(play.id)).append("]")
.append(" TID: [").append(std::to_string(play.tid)).append("]"), false, false);
.append(" ID: [").append(std::to_string(play.id)).append("]")
.append(" TID: [").append(std::to_string(play.tid)).append("]"), false, false);
play.tid = log->tid;
sendPackTo<GameInfo>(m_sock_udp, &m_game, &m_buf, &sockad);
@ -207,66 +210,72 @@ void Server::Run() {
sync.timestamp = 0;
sync.timer = m_game.countdown;
sendPackTo<Sync>(m_sock_udp, &sync, &m_buf, conn->getAddr());
}
int timer = m_game.countdown, sync_acc = 0;
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
Timestamp last = 0;
int timer = m_game.countdown, sync_acc = 0;
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
Timestamp last = 0;
std::vector<Chat> chatlog;
while (!endgame) {
using namespace std::chrono;
Timestamp tstamp = duration_cast<milliseconds>(high_resolution_clock::now() - start).count();
while (!endgame) {
using namespace std::chrono;
Timestamp tstamp = duration_cast<milliseconds>(high_resolution_clock::now() - start).count();
if (last == 0)
last = tstamp;
sync_acc += tstamp - last;
if (sync_acc >= 1000) {
sync_acc -= 1000;
--timer;
}
for (auto& [key, conn] : m_players) {
int deadplayers = 0;
std::vector<char*> lsPck;
Input in;
Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf);
for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
switch (netprot::getType(pck, 1)) {
using enum netprot::PACKET_TYPE;
case INPUT:
if (Deserialize(&in, pck, &bsize)) {
m_players[in.sid]->AddInput(in);
}
break;
case SYNC:
if (Deserialize(&sync, pck, &bsize)) {}
break;
case CHAT:
// TODO: Pitcher les chats à tous le monde.
break;
default: break;
}
}
lsPck.clear();
if (conn->m_nsync) {
if (conn->player->AmIDead()) {
++deadplayers;
continue;
}
conn->Run(m_world);
conn->sendPacks(m_sock_udp, m_players, timer);
}
if (deadplayers == players - 1 || timer <= 0)
endgame = true;
}
if (last == 0)
last = tstamp;
sync_acc += tstamp - last;
if (sync_acc >= 1000) {
sync_acc -= 1000;
--timer;
}
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
for (auto& [key, conn] : m_players) {
int deadplayers = 0;
std::vector<char*> lsPck;
Input in; Chat chat; Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf);
for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
switch (netprot::getType(pck, 1)) {
using enum netprot::PACKET_TYPE;
case INPUT:
if (Deserialize(&in, pck, &bsize))
m_players[in.sid]->AddInput(in);
break;
case SYNC:
if (Deserialize(&sync, pck, &bsize)) {}
break;
case CHAT:
if (Deserialize(&chat, pck, &bsize))
chatlog.push_back(chat);
break;
default: break;
}
}
lsPck.clear();
if (conn->m_nsync) {
if (conn->player->AmIDead()) {
++deadplayers;
conn->m_nsync == false;
continue;
}
conn->Run(m_world);
conn->sendPacks(m_sock_udp, m_players, timer);
}
if (deadplayers == players - 1 || timer <= 0)
endgame = true;
}
for (auto& chat : chatlog)
for (auto& [key, conn] : m_players)
sendPackTo<Chat>(m_sock_udp, &chat, &m_buf, conn->getAddr());
chatlog.clear();
}
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
}
inline std::string Server::LogTimestamp() {
@ -291,13 +300,13 @@ inline std::string Server::LogTimestamp() {
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
switch (m_log) {
using enum LOG_DEST; // C++20!
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE: [[fallthrough]]; // Pour dire que c'est voulu que ça traverse vers le case en dessous (C++17!)
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case LOGFILE:
m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case CONSOLE: [[fallthrough]]; // Pour dire que c'est voulu que ça traverse vers le case en dessous (C++17!)
default:
std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
}
if (is_fatal) {

View File

@ -1339,8 +1339,6 @@ void Engine::Render(float elapsedTime) {
if (diff.y < 1.)
diff.y = 0;
//std::cout << sync.timestamp << " ms: " << comp.position << " - " << sync.position << " Diff: " << diff.Length() << std::endl;
if (diff.Length() > 1.5) {
diff.Normalize();
m_player.Move(-diff);