added sync projectiles/boxes

This commit is contained in:
Vlad committed 2026-01-27 19:38:18 +06:00
1 parent cacc18dc7e
commit e8fb14b809
7 files changed
+338 -192

No files matched your search

+61 -18
View File
@@ -56,33 +56,76 @@ class Session : public std::enable_shared_from_this<Session> {
void process_message(const std::string& msg) {
auto now_server = std::chrono::system_clock::now();
auto parts = split(msg, ':');
if (parts.size() < 16)
{
throw std::runtime_error("Unknown message type received, too small");
if (parts.empty()) {
std::cerr << "Empty message received\n";
return;
}
uint64_t clientTimestamp = std::stoull(parts[1]);
std::string type = parts[0];
ClientState receivedState;
receivedState.id = id_;
std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast<std::chrono::system_clock::time_point::duration>(std::chrono::milliseconds(clientTimestamp)) };
receivedState.lastUpdateServerTime = uptime_timepoint;
if (parts[0] == "UPD") {
if (type == "UPD") {
if (parts.size() < 16) {
std::cerr << "Invalid UPD message: too few parts (" << parts.size() << ")\n";
return;
}
uint64_t clientTimestamp = std::stoull(parts[1]);
ClientState receivedState;
receivedState.id = id_;
std::chrono::system_clock::time_point uptime_timepoint{
std::chrono::milliseconds(clientTimestamp)
};
receivedState.lastUpdateServerTime = uptime_timepoint;
receivedState.handle_full_sync(parts, 2);
retranslateMessage(msg);
timedClientStates.add_state(receivedState);
}
else
{
throw std::runtime_error("Unknown message type received: " + parts[0]);
}
else if (parts[0] == "FIRE") {
if (parts.size() < 8) {
std::cerr << "Invalid FIRE: too few parts\n";
return;
}
timedClientStates.add_state(receivedState);
uint64_t clientTime = std::stoull(parts[1]);
Eigen::Vector3f pos{
std::stof(parts[2]), std::stof(parts[3]), std::stof(parts[4])
};
Eigen::Vector3f dir{
std::stof(parts[5]), std::stof(parts[6]), std::stof(parts[7])
};
int shotCount = 1;
if (parts.size() >= 9) {
try {
shotCount = std::stoi(parts[8]);
}
catch (...) {
shotCount = 1;
}
}
std::string broadcast = "PROJECTILE:" +
std::to_string(id_) + ":" +
std::to_string(clientTime) + ":" +
std::to_string(pos.x()) + ":" +
std::to_string(pos.y()) + ":" +
std::to_string(pos.z()) + ":" +
std::to_string(dir.x()) + ":" +
std::to_string(dir.y()) + ":" +
std::to_string(dir.z()) + ":" +
std::to_string(shotCount);
std::lock_guard<std::mutex> lock(g_sessions_mutex);
std::cout << "Player " << id_ << " fired " << shotCount << " shots → broadcasting\n";
for (auto& session : g_sessions) {
session->send_message(broadcast);
}
}
else {
std::cerr << "Unknown message type: " << type << "\n";
}
}
void retranslateMessage(const std::string& msg)