2023-09-24 08:45:40 -04:00
|
|
|
#include "server.h"
|
|
|
|
|
2023-10-27 09:51:40 -04:00
|
|
|
Server::Server(LOG_DEST log) {
|
2023-09-24 11:07:03 -04:00
|
|
|
m_log = log;
|
|
|
|
if (log == LOG_DEST::LOGFILE) {
|
2023-09-24 08:45:40 -04:00
|
|
|
m_logfile = std::ofstream("server.log", std::ofstream::out);
|
|
|
|
if (!m_logfile.is_open()) {
|
2023-09-24 11:07:03 -04:00
|
|
|
m_log = LOG_DEST::CONSOLE; // Fallback console.
|
2023-09-24 08:45:40 -04:00
|
|
|
Log("Ouverture fichier log: repli vers console.", true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server() {
|
|
|
|
if (m_logfile.is_open())
|
|
|
|
m_logfile.close();
|
|
|
|
if (m_sock_udp)
|
|
|
|
closesocket(m_sock_udp);
|
|
|
|
if (m_sock_tcp)
|
2023-12-02 09:23:26 -05:00
|
|
|
closesocket(m_sock_tcp);
|
2023-12-05 06:25:48 -05:00
|
|
|
for (const auto& [key, player] : m_conns)
|
2023-12-02 09:23:26 -05:00
|
|
|
closesocket(player->getSock());
|
2023-12-05 06:25:48 -05:00
|
|
|
m_conns.clear();
|
2023-11-28 11:53:21 -05:00
|
|
|
delete m_world;
|
2023-09-24 08:45:40 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int Server::Init() {
|
|
|
|
Log("Initialisation du serveur...", false, false);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &m_wsaData) != 0) { /* Initialisation de l'environnement reseau (Windows only) */
|
|
|
|
Log("Initialisation WinSock.", true, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_sock_udp = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (m_sock_udp == INVALID_SOCKET) { /* Creation du socket UDP */
|
|
|
|
Log("Creation Socket UDP.", true, true);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sock_tcp = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (m_sock_tcp == INVALID_SOCKET) { /* Creation du socket TCP */
|
|
|
|
Log("Creation Socket TCP.", true, true);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creation structure donnes descripteur du socket serveur */
|
|
|
|
sockaddr_in addr;
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(SRV_PORT);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */
|
|
|
|
Log("Association Socket UDP.", true, true);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(m_sock_tcp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket TCP au port */
|
|
|
|
Log("Association Socket TCP.", true, true);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Server::Ready() {
|
2023-12-05 13:44:54 -05:00
|
|
|
int nbrjoueurs = 0,
|
2023-10-18 09:33:56 -04:00
|
|
|
nbrconn = 0;
|
2023-10-26 10:39:08 -04:00
|
|
|
bool readystart = false;
|
2023-10-18 09:33:56 -04:00
|
|
|
do {
|
2023-10-27 09:28:34 -04:00
|
|
|
Log("Entrez la duree de la partie: ", false, false);
|
2023-10-27 09:51:40 -04:00
|
|
|
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
|
2023-12-05 13:44:54 -05:00
|
|
|
try {
|
2023-10-27 10:40:53 -04:00
|
|
|
m_game.countdown = std::stoi(m_buf.ptr);
|
2023-12-05 13:44:54 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
2023-10-27 10:40:53 -04:00
|
|
|
Log(e.what(), true, false);
|
|
|
|
m_game.countdown = 0;
|
|
|
|
}
|
2023-10-18 09:33:56 -04:00
|
|
|
} while (m_game.countdown < 1);
|
2023-12-06 15:37:08 -05:00
|
|
|
m_game.seed = 9370707;
|
|
|
|
/*do {
|
2023-10-18 09:33:56 -04:00
|
|
|
Log("Entrez le seed de la partie: ", false, false);
|
2023-10-27 09:51:40 -04:00
|
|
|
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
|
2023-12-05 13:44:54 -05:00
|
|
|
try {
|
2023-12-06 15:37:08 -05:00
|
|
|
std::stoi(m_buf.ptr);
|
2023-12-05 13:44:54 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
2023-10-27 10:40:53 -04:00
|
|
|
Log(e.what(), true, false);
|
|
|
|
m_game.seed = 0;
|
|
|
|
}
|
2023-12-06 15:37:08 -05:00
|
|
|
} while (m_game.seed < 1);*/
|
2023-10-18 09:33:56 -04:00
|
|
|
do {
|
|
|
|
Log("Entrez le nombre de joueurs: ", false, false);
|
2023-10-27 09:51:40 -04:00
|
|
|
std::cin.getline(m_buf.ptr, BUFFER_LENGTH);
|
2023-12-05 13:44:54 -05:00
|
|
|
try {
|
2023-10-27 10:40:53 -04:00
|
|
|
nbrjoueurs = std::stoi(m_buf.ptr);
|
2023-12-05 13:44:54 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
2023-10-27 10:40:53 -04:00
|
|
|
Log(e.what(), true, false);
|
|
|
|
nbrjoueurs = 0;
|
|
|
|
}
|
|
|
|
if (nbrjoueurs <= 0 || nbrjoueurs > MAX_CONNECTIONS)
|
|
|
|
Log("Nombre de joueurs invalide.", true, false);
|
|
|
|
} while (nbrjoueurs <= 0 || nbrjoueurs > MAX_CONNECTIONS);
|
2023-10-25 10:00:54 -04:00
|
|
|
|
2023-10-18 09:33:56 -04:00
|
|
|
m_game.gameType = 1;
|
|
|
|
|
2023-09-24 11:07:03 -04:00
|
|
|
if (listen(m_sock_tcp, MAX_CONNECTIONS) < 0) {
|
2023-10-26 17:57:42 -04:00
|
|
|
Log("Ecoute sur le port TCP.", true, true);
|
2023-09-24 11:07:03 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-09-28 09:15:39 -04:00
|
|
|
buildIdList(ID_LIST_SIZE);
|
2023-12-05 13:44:54 -05:00
|
|
|
|
2023-10-26 17:57:42 -04:00
|
|
|
Log("A l'ecoute sur le port: " + std::to_string(SRV_PORT), false, false);
|
2023-12-05 13:44:54 -05:00
|
|
|
|
2023-09-24 11:07:03 -04:00
|
|
|
while (!readystart) {
|
2023-09-27 11:24:41 -04:00
|
|
|
sockaddr_in sockad;
|
2023-10-24 07:32:16 -04:00
|
|
|
addrlen_t addrlen = sizeof(sockad);
|
2023-09-27 11:24:41 -04:00
|
|
|
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
|
|
|
|
|
|
|
|
if (sock < 0)
|
|
|
|
Log("Erreur de connexion", true, false);
|
|
|
|
else if (sock > 0) {
|
2023-10-26 17:57:42 -04:00
|
|
|
std::string str = "Nouvelle connexion provenant de: ";
|
2023-10-27 09:51:40 -04:00
|
|
|
str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port));
|
2023-09-27 17:34:25 -04:00
|
|
|
|
2023-10-27 09:51:40 -04:00
|
|
|
if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) {
|
2023-12-09 12:02:04 -05:00
|
|
|
PlayerInfo* play = new PlayerInfo();
|
2023-10-30 17:22:21 -04:00
|
|
|
|
|
|
|
m_buf.len = BUFFER_LENGTH;
|
2023-10-27 09:51:40 -04:00
|
|
|
Packet pck = getPack(&m_buf);
|
2023-10-25 12:16:14 -04:00
|
|
|
if (pck.type != PACKET_TYPE::LOGINF) {
|
|
|
|
Log("Paquet invalide.", true, false);
|
|
|
|
if (pck.type != PACKET_TYPE::ERR)
|
2023-12-05 13:44:54 -05:00
|
|
|
netprot::emptyPack(pck);
|
2023-10-25 12:16:14 -04:00
|
|
|
continue; // Passer au prochain appel si c'est pas un LoginInfo ou un LoginInfo invalide qui rentre.
|
2023-10-30 17:22:21 -04:00
|
|
|
}
|
|
|
|
LoginInfo* log = (LoginInfo*)pck.ptr;
|
2023-10-25 10:00:54 -04:00
|
|
|
|
|
|
|
log->sid = getUniqueId();
|
2023-11-01 06:12:38 -04:00
|
|
|
log->tid = 1145389380; // TODO: À changer si on implemente un mode en equipe.
|
2023-09-28 09:15:39 -04:00
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Log(str.append(" Nom: ").append(log->name), false, false);
|
|
|
|
str.clear();
|
2023-12-02 09:23:26 -05:00
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
|
|
|
|
sendPackTo<LoginInfo>(m_sock_udp, log, &m_buf, &sockad);
|
|
|
|
|
2023-12-09 12:02:04 -05:00
|
|
|
play->id = getUniqueId();
|
|
|
|
play->tid = log->tid;
|
|
|
|
strcpy(play->name, 32, log->name);
|
2023-12-05 13:44:54 -05:00
|
|
|
|
2023-12-09 12:02:04 -05:00
|
|
|
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);
|
|
|
|
play->tid = log->tid;
|
2023-09-28 09:15:39 -04:00
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
sendPackTo<GameInfo>(m_sock_udp, &m_game, &m_buf, &sockad);
|
2023-12-09 12:02:04 -05:00
|
|
|
Connection* conn = new Connection(sock, sockad, log, play);
|
2023-09-28 09:15:39 -04:00
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
m_conns[log->sid] = conn;
|
2023-10-29 14:54:36 -04:00
|
|
|
|
2023-10-25 10:00:54 -04:00
|
|
|
if (++nbrconn >= nbrjoueurs)
|
|
|
|
readystart = true;
|
2023-09-27 11:24:41 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-24 11:07:03 -04:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
for (auto& [keyin, playin] : m_conns) // Not pretty, but it works.
|
|
|
|
for (auto& [keyout, playout] : m_conns) {
|
2023-11-27 13:52:49 -05:00
|
|
|
if (keyin == keyout)
|
2023-12-02 09:23:26 -05:00
|
|
|
continue;
|
2023-11-27 13:52:49 -05:00
|
|
|
sendPackTo<PlayerInfo>(m_sock_udp, playout->getInfo(), &m_buf, playin->getAddr()); // et envoyer les infos des joueurs distants au nouveau joueur.
|
|
|
|
}
|
|
|
|
|
2023-09-24 08:45:40 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::Run() {
|
2023-11-06 16:43:52 -05:00
|
|
|
bool endgame = false;
|
2023-10-26 17:57:42 -04:00
|
|
|
Input in;
|
2023-10-23 17:49:40 -04:00
|
|
|
sockaddr_in sockad;
|
2023-10-26 10:39:08 -04:00
|
|
|
addrlen_t socklen = sizeof(sockad);
|
2023-12-05 13:44:54 -05:00
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Log("Debut de la partie...", false, false);
|
2023-10-23 16:21:15 -04:00
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
int players = m_conns.size();
|
2023-11-06 16:43:52 -05:00
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
m_world = new World();
|
2023-10-29 14:54:36 -04:00
|
|
|
m_world->SetSeed(m_game.seed);
|
2023-10-29 15:07:01 -04:00
|
|
|
m_world->GetChunks().Reset(nullptr);
|
|
|
|
m_world->BuildWorld();
|
2023-11-28 11:38:38 -05:00
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
for (auto& [key, conn] : m_conns) { // Creation des instances de joueurs et premier sync.
|
2023-12-07 15:14:16 -05:00
|
|
|
if (!conn) {
|
|
|
|
m_conns.erase(key);
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-13 13:47:34 -05:00
|
|
|
int x = (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1) - (CHUNK_SIZE_X * WORLD_SIZE_X / 2)) / 16,
|
|
|
|
y = (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1) - (CHUNK_SIZE_Y * WORLD_SIZE_Y / 2)) / 16;
|
2023-12-05 06:25:48 -05:00
|
|
|
conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
|
2023-12-06 13:23:33 -05:00
|
|
|
conn->player->m_username = conn->GetName();
|
2023-12-05 06:25:48 -05:00
|
|
|
m_players[key] = conn->player;
|
2023-10-26 17:57:42 -04:00
|
|
|
Sync sync;
|
2023-12-05 13:44:54 -05:00
|
|
|
sync.position = conn->player->GetPositionAbs();
|
2023-10-29 14:54:36 -04:00
|
|
|
sync.hp = conn->player->GetHP();
|
2023-10-26 17:57:42 -04:00
|
|
|
sync.sid = key;
|
|
|
|
sync.ammo = 0;
|
|
|
|
sync.timestamp = 0;
|
|
|
|
sync.timer = m_game.countdown;
|
2023-12-05 13:44:54 -05:00
|
|
|
sendPackTo<Sync>(m_sock_udp, &sync, &m_buf, conn->getAddr());
|
2023-12-02 09:23:26 -05:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
int timer = m_game.countdown, timer_acc = 0, deadplayers = 0;
|
2023-12-02 09:23:26 -05:00
|
|
|
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
|
|
|
Timestamp last = 0;
|
2023-12-06 13:42:15 -05:00
|
|
|
std::vector<Chat*> chatlog;
|
2023-12-05 06:25:48 -05:00
|
|
|
std::vector<ChunkMod*> chunkdiffs;
|
2023-12-08 04:31:10 -05:00
|
|
|
std::vector<Bullet*> bullets;
|
|
|
|
std::vector<std::vector<Bullet*>::iterator> bullit;
|
2023-12-05 06:25:48 -05:00
|
|
|
std::vector<BulletAdd*> netbull;
|
2023-12-08 04:55:00 -05:00
|
|
|
std::vector<char*> lsPck;
|
2023-12-02 09:23:26 -05:00
|
|
|
|
2023-12-07 13:34:58 -05:00
|
|
|
Chat* startchat = new Chat();
|
2023-12-07 13:05:52 -05:00
|
|
|
startchat->src_id = 0;
|
2023-12-08 05:07:05 -05:00
|
|
|
char startmess[] = "How would -YOU- like to die today, motherf-words?";
|
2023-12-13 16:10:23 -05:00
|
|
|
float endtime = 0.;
|
2023-12-07 13:05:52 -05:00
|
|
|
strcpy(startchat->mess, 140, startmess);
|
|
|
|
|
|
|
|
chatlog.emplace_back(startchat);
|
|
|
|
|
2023-12-13 16:10:23 -05:00
|
|
|
while (!endgame && endtime < 1.) {
|
2023-12-02 09:23:26 -05:00
|
|
|
using namespace std::chrono;
|
|
|
|
Timestamp tstamp = duration_cast<milliseconds>(high_resolution_clock::now() - start).count();
|
|
|
|
|
|
|
|
if (last == 0)
|
|
|
|
last = tstamp;
|
2023-12-13 15:04:08 -05:00
|
|
|
timer_acc += tstamp - last;
|
|
|
|
if (timer_acc >= 1000) {
|
|
|
|
while (timer_acc >= 1000)
|
|
|
|
timer_acc -= 1000;
|
2023-12-13 16:10:23 -05:00
|
|
|
if (!endgame)
|
|
|
|
--timer;
|
2023-12-06 12:25:34 -05:00
|
|
|
std::string str = "Timer: ";
|
2023-12-07 13:34:58 -05:00
|
|
|
Log(str.append(std::to_string(timer)), false, false);
|
2023-12-02 09:23:26 -05:00
|
|
|
}
|
2023-12-06 13:42:15 -05:00
|
|
|
last = tstamp;
|
2023-11-28 11:38:38 -05:00
|
|
|
|
2023-12-07 15:14:16 -05:00
|
|
|
|
|
|
|
for (auto& [key, conn] : m_conns) {
|
2023-12-02 10:04:18 -05:00
|
|
|
|
|
|
|
/* In */
|
|
|
|
|
2023-12-08 04:58:27 -05:00
|
|
|
Input in; Sync sync;
|
2023-12-05 06:25:48 -05:00
|
|
|
|
2023-12-08 04:55:00 -05:00
|
|
|
recvPacks(m_sock_udp, &m_buf, &lsPck);
|
2023-12-02 09:23:26 -05:00
|
|
|
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:
|
2023-12-07 15:14:16 -05:00
|
|
|
if (Deserialize(&in, pck, &bsize)) {
|
|
|
|
if (m_conns.count(in.sid))
|
|
|
|
m_conns[in.sid]->AddInput(in);
|
|
|
|
}
|
2023-12-02 09:23:26 -05:00
|
|
|
break;
|
|
|
|
case SYNC:
|
|
|
|
if (Deserialize(&sync, pck, &bsize)) {}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2023-11-28 11:38:38 -05:00
|
|
|
}
|
2023-12-02 09:23:26 -05:00
|
|
|
lsPck.clear();
|
2023-11-28 11:38:38 -05:00
|
|
|
|
2023-12-02 10:04:18 -05:00
|
|
|
/* Process */
|
|
|
|
|
2023-12-02 09:23:26 -05:00
|
|
|
if (conn->m_nsync) {
|
2023-12-16 13:05:04 -05:00
|
|
|
if (conn->player->m_hit) {
|
|
|
|
std::string str = conn->player->GetUsername();
|
|
|
|
str = str.append(" has been hit: ").append(std::to_string(conn->player->GetHP()));
|
|
|
|
Log(str, false, false);
|
|
|
|
}
|
2023-12-13 13:47:34 -05:00
|
|
|
|
2023-12-05 14:25:21 -05:00
|
|
|
Timestamp tstamp = conn->Run(m_world);
|
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
if (conn->player->AmIDead() && !conn->player->Eulogy) {
|
2023-12-06 13:42:15 -05:00
|
|
|
Chat* chat = new Chat();
|
2023-12-13 13:47:34 -05:00
|
|
|
chat->dest_id = chat->dest_team_id = chat->src_id = 0;
|
2023-12-06 11:16:39 -05:00
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
Player* murderer = m_conns.at(conn->player->Killer)->player;
|
|
|
|
|
2023-12-13 15:21:19 -05:00
|
|
|
if (murderer != conn->player)
|
|
|
|
murderer->addPoint();
|
2023-12-13 15:04:08 -05:00
|
|
|
|
|
|
|
std::string killer = murderer->GetUsername();
|
2023-12-05 14:25:21 -05:00
|
|
|
|
2023-12-06 12:32:26 -05:00
|
|
|
std::string mess = getDeathMessage(conn->player->GetUsername(), killer);
|
2023-12-06 13:23:33 -05:00
|
|
|
|
2023-12-06 13:42:15 -05:00
|
|
|
strcpy(chat->mess, 140, mess.c_str());
|
|
|
|
chatlog.emplace_back(chat);
|
2023-12-02 09:23:26 -05:00
|
|
|
++deadplayers;
|
2023-12-13 15:04:08 -05:00
|
|
|
|
|
|
|
conn->player->Eulogy = true;
|
2023-11-28 11:38:38 -05:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
else {
|
|
|
|
for (auto& chmo : conn->ChunkDiffs)
|
2023-12-07 13:34:58 -05:00
|
|
|
chunkdiffs.emplace_back(std::move(chmo));
|
2023-12-05 06:25:48 -05:00
|
|
|
conn->ChunkDiffs.clear();
|
|
|
|
|
|
|
|
for (auto& bull : conn->Bullets) {
|
|
|
|
bullets.emplace_back(bull);
|
|
|
|
BulletAdd* nbul = new BulletAdd();
|
2023-12-06 11:16:39 -05:00
|
|
|
nbul->pos = bull->getPos();
|
|
|
|
nbul->dir = bull->getVel();
|
2023-12-05 06:25:48 -05:00
|
|
|
nbul->id = key;
|
|
|
|
nbul->tstamp = tstamp;
|
|
|
|
|
2023-12-07 13:34:58 -05:00
|
|
|
netbull.emplace_back(std::move(nbul));
|
2023-12-05 06:25:48 -05:00
|
|
|
}
|
|
|
|
conn->Bullets.clear();
|
|
|
|
}
|
2023-11-24 15:32:53 -05:00
|
|
|
}
|
2023-12-13 14:27:11 -05:00
|
|
|
/* Out */
|
|
|
|
conn->sendPacks(m_sock_udp, m_conns, timer);
|
|
|
|
|
2023-12-13 16:10:23 -05:00
|
|
|
if ((deadplayers == players - 1 && deadplayers != 0) || timer < 0) {
|
|
|
|
if (!endgame) {
|
|
|
|
Chat* gameover = new Chat();
|
|
|
|
gameover->dest_id = gameover->dest_team_id = gameover->src_id = 0;
|
|
|
|
std::string winner, winmess;
|
|
|
|
int score = 0;
|
|
|
|
bool plural = false;
|
|
|
|
for (auto& [key, conn] : m_conns) {
|
|
|
|
if (conn->player->getScore() > score) {
|
|
|
|
winner = conn->player->GetUsername();
|
|
|
|
score = conn->player->getScore();
|
|
|
|
plural = false;
|
|
|
|
}
|
|
|
|
else if (conn->player->getScore() == score) {
|
|
|
|
winner = winner.append(" and ").append(conn->player->GetUsername());
|
|
|
|
plural = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
winmess = "And the winner";
|
|
|
|
if (!plural)
|
|
|
|
winmess = winmess.append(" is ");
|
|
|
|
else winmess = winmess.append("s are ");
|
|
|
|
|
|
|
|
winmess = winmess.append(winner).append(" with ").append(std::to_string(score)).append(" point");
|
|
|
|
|
|
|
|
if (score > 1)
|
|
|
|
winmess = winmess.append("s.");
|
|
|
|
else winmess = winmess.append(".");
|
|
|
|
|
|
|
|
strcpy(gameover->mess, 140, winmess.c_str());
|
|
|
|
|
|
|
|
chatlog.emplace_back(gameover);
|
|
|
|
}
|
2023-12-06 13:46:43 -05:00
|
|
|
endgame = true;
|
2023-12-13 16:10:23 -05:00
|
|
|
endtime += .001;
|
|
|
|
}
|
2023-12-02 09:23:26 -05:00
|
|
|
}
|
2023-12-06 11:16:39 -05:00
|
|
|
|
2023-12-06 15:37:08 -05:00
|
|
|
for (auto& bull : netbull) {
|
2023-12-05 06:25:48 -05:00
|
|
|
for (auto& [key, conn] : m_conns)
|
2023-12-05 14:25:21 -05:00
|
|
|
if (bull->id != conn->GetHash(false)) // Pour pas repitcher au joueur sa propre balle.
|
|
|
|
sendPackTo<BulletAdd>(m_sock_udp, bull, &m_buf, conn->getAddr());
|
2023-12-06 15:37:08 -05:00
|
|
|
delete bull;
|
|
|
|
}
|
2023-12-07 14:03:58 -05:00
|
|
|
netbull.clear();
|
2023-12-08 04:31:10 -05:00
|
|
|
|
|
|
|
for (auto bull = bullets.begin(); bull != bullets.end(); ++bull) {
|
2023-12-05 06:25:48 -05:00
|
|
|
ChunkMod* cmod = nullptr;
|
2023-12-08 04:31:10 -05:00
|
|
|
Bullet* bullet = *bull;
|
2023-12-13 16:29:13 -05:00
|
|
|
if (bullet->Update(m_world, (1. / 60.), 100, m_players, &cmod)) {
|
2023-12-05 06:25:48 -05:00
|
|
|
if (cmod)
|
2023-12-08 04:31:10 -05:00
|
|
|
chunkdiffs.emplace_back(cmod);
|
|
|
|
bullit.push_back(bull);
|
|
|
|
delete bullet;
|
2023-12-05 06:25:48 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-08 04:31:10 -05:00
|
|
|
for (auto& bull: bullit)
|
|
|
|
bullets.erase(bull);
|
|
|
|
bullit.clear();
|
2023-12-07 15:47:31 -05:00
|
|
|
|
2023-12-06 11:16:39 -05:00
|
|
|
for (auto& chat : chatlog) {
|
2023-12-06 13:42:15 -05:00
|
|
|
Log(chat->mess, false, false);
|
2023-12-06 11:16:39 -05:00
|
|
|
for (auto& [key, conn] : m_conns)
|
2023-12-06 13:42:15 -05:00
|
|
|
sendPackTo<Chat>(m_sock_udp, chat, &m_buf, conn->getAddr());
|
|
|
|
delete chat;
|
2023-12-06 11:16:39 -05:00
|
|
|
}
|
2023-12-07 13:34:58 -05:00
|
|
|
chatlog.clear();
|
2023-12-05 06:25:48 -05:00
|
|
|
|
|
|
|
for (auto& chmo : chunkdiffs) {
|
|
|
|
for (auto& [key, conn] : m_conns)
|
|
|
|
sendPackTo<ChunkMod>(m_sock_udp, chmo, &m_buf, conn->getAddr());
|
|
|
|
delete chmo;
|
|
|
|
}
|
|
|
|
chunkdiffs.clear();
|
2023-11-28 11:38:38 -05:00
|
|
|
}
|
2023-12-02 09:23:26 -05:00
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
Chat end;
|
|
|
|
end.src_id = 0;
|
2023-12-07 13:05:52 -05:00
|
|
|
char endmess[] = "Game over, man. Game over.";
|
2023-12-08 04:55:00 -05:00
|
|
|
strcpy(end.mess, 140, endmess);
|
2023-12-05 06:25:48 -05:00
|
|
|
|
2023-12-07 15:47:31 -05:00
|
|
|
for (auto& [key, conn] : m_conns) {
|
|
|
|
std::string str = conn->player->GetUsername();
|
2023-12-13 15:04:08 -05:00
|
|
|
Log(str.append(" ").append(std::to_string(conn->player->getScore())), false, false);
|
2023-12-07 15:47:31 -05:00
|
|
|
}
|
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
for (auto& [key, conn] : m_conns)
|
|
|
|
sendPackTo<Chat>(m_sock_udp, &end, &m_buf, conn->getAddr());
|
|
|
|
|
2023-12-02 09:23:26 -05:00
|
|
|
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
|
2023-12-09 12:02:04 -05:00
|
|
|
|
|
|
|
char* ch = new char[2];
|
|
|
|
std::cout << "Nouvelle partie? [o/N] ";
|
|
|
|
std::cin.getline(ch, 2);
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
m_exit = true;
|
|
|
|
if (ch[0] == 'o' || ch[0] == 'O')
|
|
|
|
m_exit = false;
|
|
|
|
|
|
|
|
delete[] ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::Cleanup() {
|
|
|
|
for (auto& [key, conn] : m_conns)
|
|
|
|
delete conn;
|
|
|
|
|
|
|
|
m_conns.clear();
|
|
|
|
m_players.clear();
|
|
|
|
delete m_world;
|
|
|
|
m_world = nullptr;
|
2023-09-24 08:45:40 -04:00
|
|
|
}
|
|
|
|
|
2023-12-09 12:02:04 -05:00
|
|
|
void Server::DeInit() {
|
|
|
|
if (m_logfile.is_open())
|
|
|
|
m_logfile.close();
|
|
|
|
if (m_sock_udp)
|
|
|
|
closesocket(m_sock_udp);
|
|
|
|
if (m_sock_tcp)
|
|
|
|
closesocket(m_sock_tcp);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Server::NewGameRequested() const { return !m_exit; }
|
|
|
|
|
2023-10-01 11:52:07 -04:00
|
|
|
inline std::string Server::LogTimestamp() {
|
2023-09-24 08:45:40 -04:00
|
|
|
time_t rawtime;
|
2023-10-26 11:17:13 -04:00
|
|
|
tm timeinfo;
|
|
|
|
char buffer[50];
|
2023-09-24 08:45:40 -04:00
|
|
|
|
|
|
|
time(&rawtime);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
localtime_s(&timeinfo, &rawtime);
|
|
|
|
#else
|
|
|
|
localtime_r(&rawtime, &timeinfo);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", &timeinfo);
|
|
|
|
std::string str(buffer);
|
|
|
|
|
|
|
|
return "[" + str + "] ";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
|
|
|
|
switch (m_log) {
|
2023-10-27 12:02:29 -04:00
|
|
|
using enum LOG_DEST; // C++20!
|
2023-12-05 13:44:54 -05:00
|
|
|
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;
|
2023-09-24 08:45:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_fatal) {
|
2023-10-26 17:18:30 -04:00
|
|
|
if (m_logfile.is_open())
|
|
|
|
m_logfile.close();
|
2023-09-24 08:45:40 -04:00
|
|
|
if (m_sock_udp)
|
|
|
|
closesocket(m_sock_udp);
|
|
|
|
if (m_sock_tcp)
|
|
|
|
closesocket(m_sock_tcp);
|
2023-12-05 14:25:21 -05:00
|
|
|
for (const auto& [key, player] : m_conns)
|
2023-10-26 10:39:08 -04:00
|
|
|
closesocket(player->getSock());
|
2023-12-05 14:25:21 -05:00
|
|
|
|
2023-11-28 11:53:21 -05:00
|
|
|
delete m_world;
|
2023-12-05 06:25:48 -05:00
|
|
|
m_conns.clear();
|
2023-09-24 08:45:40 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
2023-10-26 10:47:17 -04:00
|
|
|
exit(-1);
|
2023-09-24 08:45:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 09:15:39 -04:00
|
|
|
void Server::buildIdList(size_t size) {
|
|
|
|
std::set<uint64_t> lst;
|
|
|
|
|
2023-10-26 10:55:36 -04:00
|
|
|
srand(time(NULL));
|
2023-10-29 14:54:36 -04:00
|
|
|
do lst.insert(((uint64_t)rand() << 32 | rand()));
|
2023-09-28 09:15:39 -04:00
|
|
|
while (lst.size() < size);
|
2023-12-05 13:44:54 -05:00
|
|
|
|
2023-09-28 09:15:39 -04:00
|
|
|
m_ids = std::vector<uint64_t>(lst.begin(), lst.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Server::getUniqueId() {
|
|
|
|
uint64_t id = m_ids.back();
|
|
|
|
m_ids.pop_back();
|
|
|
|
return id;
|
|
|
|
}
|
2023-12-05 14:25:21 -05:00
|
|
|
|
2023-12-06 11:16:39 -05:00
|
|
|
std::string Server::getDeathMessage(std::string username, std::string killer) const {
|
2023-12-05 14:25:21 -05:00
|
|
|
std::string mess;
|
|
|
|
std::string temp = DEATHMESSAGES.at(rand() % DEATHMESSAGES.size());
|
|
|
|
size_t ind = temp.find('@');
|
2023-12-06 11:16:39 -05:00
|
|
|
size_t indk = temp.find('$');
|
2023-12-06 13:23:33 -05:00
|
|
|
bool bypass = false;
|
2023-12-06 11:16:39 -05:00
|
|
|
|
2023-12-06 13:23:33 -05:00
|
|
|
if (indk == std::string::npos)
|
|
|
|
bypass = true;
|
|
|
|
|
|
|
|
if (ind < indk || bypass) {
|
2023-12-06 11:16:39 -05:00
|
|
|
mess.append(temp.substr(0, ind));
|
|
|
|
mess.append(username);
|
2023-12-06 13:23:33 -05:00
|
|
|
if (!bypass) {
|
2023-12-09 12:02:04 -05:00
|
|
|
mess.append(temp.substr(ind + 1, indk - 1));
|
2023-12-06 13:23:33 -05:00
|
|
|
mess.append(killer);
|
|
|
|
mess.append(temp.substr(indk + 1));
|
|
|
|
}
|
2023-12-06 14:31:05 -05:00
|
|
|
else mess.append(temp.substr(ind + 1));
|
2023-12-06 11:16:39 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mess.append(temp.substr(0, indk));
|
|
|
|
mess.append(killer);
|
2023-12-09 12:02:04 -05:00
|
|
|
mess.append(temp.substr(indk + 1, ind - 1));
|
2023-12-06 11:16:39 -05:00
|
|
|
mess.append(username);
|
|
|
|
mess.append(temp.substr(ind + 1));
|
|
|
|
}
|
2023-12-05 14:25:21 -05:00
|
|
|
|
|
|
|
return mess;
|
|
|
|
}
|