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

@ -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;
}
@ -207,10 +210,12 @@ 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;
std::vector<Chat> chatlog;
while (!endgame) {
using namespace std::chrono;
@ -227,23 +232,22 @@ void Server::Run() {
for (auto& [key, conn] : m_players) {
int deadplayers = 0;
std::vector<char*> lsPck;
Input in;
Sync sync;
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)) {
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.
if (Deserialize(&chat, pck, &bsize))
chatlog.push_back(chat);
break;
default: break;
}
@ -253,6 +257,7 @@ void Server::Run() {
if (conn->m_nsync) {
if (conn->player->AmIDead()) {
++deadplayers;
conn->m_nsync == false;
continue;
}
conn->Run(m_world);
@ -262,12 +267,16 @@ void Server::Run() {
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() {
time_t rawtime;

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);