added choising spaceship type for multiplayer and some fix in respawn
This commit is contained in:
1 parent
5216965496
commit
74c2f786a1
6 files changed
+196
-39
No files matched your search
+72
-9
@@ -93,6 +93,9 @@ class Session : public std::enable_shared_from_this<Session> {
|
||||
public:
|
||||
ClientStateInterval timedClientStates;
|
||||
|
||||
std::string nickname = "Player";
|
||||
int shipType = 0;
|
||||
|
||||
explicit Session(tcp::socket&& socket, int id)
|
||||
: ws_(std::move(socket)), id_(id) {
|
||||
}
|
||||
@@ -205,7 +208,7 @@ public:
|
||||
|
||||
return latest;
|
||||
}
|
||||
|
||||
|
||||
void doWrite() {
|
||||
std::lock_guard<std::mutex> lock(writeMutex_);
|
||||
if (is_writing_ || writeQueue_.empty()) {
|
||||
@@ -253,7 +256,6 @@ private:
|
||||
|
||||
void process_message(const std::string& msg) {
|
||||
if (!IsMessageValid(msg)) {
|
||||
// Логируем попытку подмены и просто выходим из обработки
|
||||
std::cout << "[Security] Invalid packet hash. Dropping message: " << msg << std::endl;
|
||||
return;
|
||||
}
|
||||
@@ -266,7 +268,40 @@ private:
|
||||
|
||||
std::string type = parts[0];
|
||||
|
||||
if (type == "UPD") {
|
||||
if (type == "JOIN") {
|
||||
std::string nick = "Player";
|
||||
int sType = 0;
|
||||
if (parts.size() >= 2) nick = parts[1];
|
||||
if (parts.size() >= 3) {
|
||||
try { sType = std::stoi(parts[2]); }
|
||||
catch (...) { sType = 0; }
|
||||
}
|
||||
|
||||
this->nickname = nick;
|
||||
this->shipType = sType;
|
||||
|
||||
std::cout << "Server: Player " << id_ << " joined as [" << nick << "] shipType=" << sType << std::endl;
|
||||
|
||||
std::string info = "PLAYERINFO:" + std::to_string(id_) + ":" + nick + ":" + std::to_string(sType);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_sessions_mutex);
|
||||
for (auto& session : g_sessions) {
|
||||
if (session->get_id() == this->id_) continue;
|
||||
session->send_message(info);
|
||||
}
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_sessions_mutex);
|
||||
for (auto& session : g_sessions) {
|
||||
if (session->get_id() == this->id_) continue;
|
||||
std::string otherInfo = "PLAYERINFO:" + std::to_string(session->get_id()) + ":" + session->nickname + ":" + std::to_string(session->shipType);
|
||||
// Отправляем именно новому клиенту
|
||||
this->send_message(otherInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (type == "UPD") {
|
||||
{
|
||||
std::lock_guard<std::mutex> gd(g_dead_mutex);
|
||||
if (g_dead_players.find(id_) != g_dead_players.end()) {
|
||||
@@ -284,20 +319,48 @@ private:
|
||||
};
|
||||
receivedState.lastUpdateServerTime = uptime_timepoint;
|
||||
receivedState.handle_full_sync(parts, 2);
|
||||
receivedState.nickname = this->nickname;
|
||||
receivedState.shipType = this->shipType;
|
||||
timedClientStates.add_state(receivedState);
|
||||
|
||||
retranslateMessage(cleanMessage);
|
||||
}
|
||||
else if (parts[0] == "RESPAWN") {
|
||||
else if (type == "RESPAWN") {
|
||||
{
|
||||
std::lock_guard<std::mutex> gd(g_dead_mutex);
|
||||
g_dead_players.erase(id_);
|
||||
}
|
||||
|
||||
std::string respawnMsg = "RESPAWN_ACK:" + std::to_string(id_);
|
||||
broadcastToAll(respawnMsg);
|
||||
{
|
||||
auto now_tp = std::chrono::system_clock::now();
|
||||
uint64_t now_ms = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(now_tp.time_since_epoch()).count());
|
||||
|
||||
std::cout << "Server: Player " << id_ << " respawned\n";
|
||||
ClientState st;
|
||||
st.id = id_;
|
||||
st.position = Eigen::Vector3f(0.0f, 0.0f, 45000.0f);
|
||||
st.rotation = Eigen::Matrix3f::Identity();
|
||||
st.currentAngularVelocity = Eigen::Vector3f::Zero();
|
||||
st.velocity = 0.0f;
|
||||
st.selectedVelocity = 0;
|
||||
st.discreteMag = 0.0f;
|
||||
st.discreteAngle = -1;
|
||||
st.lastUpdateServerTime = now_tp;
|
||||
st.nickname = this->nickname;
|
||||
st.shipType = this->shipType;
|
||||
|
||||
timedClientStates.add_state(st);
|
||||
|
||||
std::string respawnMsg = "RESPAWN_ACK:" + std::to_string(id_);
|
||||
broadcastToAll(respawnMsg);
|
||||
|
||||
std::string playerInfo = "PLAYERINFO:" + std::to_string(id_) + ":" + st.nickname + ":" + std::to_string(st.shipType);
|
||||
broadcastToAll(playerInfo);
|
||||
|
||||
std::string eventMsg = "EVENT:" + std::to_string(id_) + ":UPD:" + std::to_string(now_ms) + ":" + st.formPingMessageContent();
|
||||
broadcastToAll(eventMsg);
|
||||
|
||||
std::cout << "Server: Player " << id_ << " respawned, broadcasted RESPAWN_ACK, PLAYERINFO and initial UPD\n";
|
||||
}
|
||||
}
|
||||
else if (parts[0] == "FIRE") {
|
||||
if (parts.size() < 10) return;
|
||||
@@ -344,8 +407,8 @@ private:
|
||||
Eigen::Vector3f(1.5f, 0.9f - 6.f, 5.0f)
|
||||
};
|
||||
|
||||
uint64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
uint64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>((
|
||||
std::chrono::system_clock::now().time_since_epoch())).count();
|
||||
|
||||
std::lock_guard<std::mutex> pl(g_projectiles_mutex);
|
||||
for (int i = 0; i < std::min(shotCount, (int)localOffsets.size()); ++i) {
|
||||
|
||||
Reference in new issue
Block a user