🚫 👂 🐆

This commit is contained in:
MarcEricMartel
2023-12-09 12:02:04 -05:00
parent 63d70be488
commit 9aaad6426c
11 changed files with 130 additions and 74 deletions

View File

@@ -4,16 +4,18 @@
Connection::Connection(SOCKET sock,
sockaddr_in sockaddr,
LoginInfo log,
PlayerInfo play) :
LoginInfo *log,
PlayerInfo *play) :
m_sock(sock),
m_addr(sockaddr),
m_loginfo(log),
m_playinfo(play) {
m_loginfo(*log),
m_playinfo(*play) {
}
Connection::~Connection() { closesocket(m_sock); }
Connection::~Connection() {
delete player;
closesocket(m_sock); }
uint64_t Connection::GetHash(bool self) const { return self ? m_loginfo.sid : m_playinfo.id; }
@@ -144,7 +146,7 @@ Timestamp Connection::Run(World* world) {
player->Killer = GetHash(true);
}
out.states.jumping = player->GetIsAirborne(); //abs(player->GetVelocity().y) > .2f;
out.states.jumping = player->GetIsAirborne();
out.states.running = player->GetVelocity().Length() > .5f;
out.states.still = !out.states.running && !out.states.jumping;
out.states.hit = player->m_hit;
@@ -166,7 +168,7 @@ Timestamp Connection::Run(World* world) {
player->GetDirection(),
block, true);
if (cmod)
ChunkDiffs.emplace_back(cmod);
ChunkDiffs.push_back(std::move(cmod));
}
}
else toggle = false;
@@ -178,7 +180,7 @@ Timestamp Connection::Run(World* world) {
else out.states.jumpshot = false;
if (in.keys.shoot && m_shoot_acc <= 0.) {
Bullets.emplace_back(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true)));
Bullets.push_back(std::move(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true))));
m_shoot_acc = BULLET_TIME;
}

View File

@@ -16,8 +16,8 @@ public:
Connection(
SOCKET sock,
sockaddr_in sockaddr,
LoginInfo log,
PlayerInfo play);
LoginInfo *log,
PlayerInfo *play);
~Connection();
Player* player = nullptr;

View File

@@ -3,6 +3,11 @@
int main() {
std::unique_ptr<Server> server = std::make_unique<Server>();
if (server->Init() == 0)
if (server->Ready() == 0)
while (server->Ready() == 0) {
server->Run();
if (!server->NewGameRequested())
break;
server->Cleanup();
}
server->DeInit();
}

View File

@@ -132,7 +132,7 @@ int Server::Ready() {
str.append(inet_ntop(AF_INET, &sockad.sin_addr, m_buf.ptr, m_buf.len)).append(": ").append(std::to_string(sockad.sin_port));
if (recv(sock, m_buf.ptr, m_buf.len, 0) > 0) {
PlayerInfo play;
PlayerInfo* play = new PlayerInfo();
m_buf.len = BUFFER_LENGTH;
Packet pck = getPack(&m_buf);
@@ -153,22 +153,20 @@ int Server::Ready() {
sendPackTo<LoginInfo>(m_sock_udp, log, &m_buf, &sockad);
play.id = getUniqueId();
play.tid = log->tid;
strcpy(play.name, log->name);
play->id = getUniqueId();
play->tid = log->tid;
strcpy(play->name, 32, 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);
play.tid = log->tid;
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;
sendPackTo<GameInfo>(m_sock_udp, &m_game, &m_buf, &sockad);
Connection* conn = new Connection(sock, sockad, *log, play);
Connection* conn = new Connection(sock, sockad, log, play);
m_conns[log->sid] = conn;
delete log;
if (++nbrconn >= nbrjoueurs)
readystart = true;
}
@@ -377,9 +375,44 @@ void Server::Run() {
sendPackTo<Chat>(m_sock_udp, &end, &m_buf, conn->getAddr());
// TODO: Gérer les 2-3 secondes post-game avant le billboard pour pas avoir un whiplash à la fin de la game.
system("pause");
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;
}
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; }
inline std::string Server::LogTimestamp() {
time_t rawtime;
tm timeinfo;
@@ -460,7 +493,7 @@ std::string Server::getDeathMessage(std::string username, std::string killer) co
mess.append(temp.substr(0, ind));
mess.append(username);
if (!bypass) {
mess.append(temp.substr(ind + 1, indk));
mess.append(temp.substr(ind + 1, indk - 1));
mess.append(killer);
mess.append(temp.substr(indk + 1));
}
@@ -469,7 +502,7 @@ std::string Server::getDeathMessage(std::string username, std::string killer) co
else {
mess.append(temp.substr(0, indk));
mess.append(killer);
mess.append(temp.substr(indk + 1, ind));
mess.append(temp.substr(indk + 1, ind - 1));
mess.append(username);
mess.append(temp.substr(ind + 1));
}

View File

@@ -24,6 +24,9 @@ public:
int Init();
int Ready();
void Run();
void Cleanup();
void DeInit();
bool NewGameRequested() const;
private:
@@ -44,7 +47,7 @@ private:
GameInfo m_game;
World* m_world = nullptr;
const bool m_manual_setup = SRV_MANUAL_SETUP;
bool m_exit = true;
std::string LogTimestamp();
void Log(std::string str, bool is_error, bool is_fatal);