Better login handling
This commit is contained in:
@@ -152,7 +152,7 @@ void Tangible::serialize(StreamBuffer *sb) {
|
||||
anim_queue_.serialize(sb);
|
||||
id_player_pool_.serialize(sb);
|
||||
print_buffer_.serialize(sb);
|
||||
sb->write_bool(is_controlled_);
|
||||
sb->write_int64(client_id_);
|
||||
sb->write_bool(delete_on_disconnect_);
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ void Tangible::deserialize(StreamBuffer *sb) {
|
||||
anim_queue_.deserialize(sb);
|
||||
id_player_pool_.deserialize(sb);
|
||||
print_buffer_.deserialize(sb);
|
||||
is_controlled_ = sb->read_bool();
|
||||
client_id_ = sb->read_int64();
|
||||
delete_on_disconnect_ = sb->read_bool();
|
||||
update_plane_item();
|
||||
}
|
||||
@@ -216,7 +216,7 @@ Tangible *World::tangible_make(const LuaCoreStack &LS0, LuaSlot database, int64_
|
||||
t.reset(new Tangible(this, id));
|
||||
|
||||
// Set the login flags.
|
||||
t->is_controlled_ = false;
|
||||
t->client_id_ = 0;
|
||||
t->delete_on_disconnect_ = false;
|
||||
|
||||
// AnimQueue initializes itself to a valid default state.
|
||||
@@ -252,6 +252,7 @@ void World::tangible_delete(int64_t id) {
|
||||
if (iter == tangibles_.end()) {
|
||||
return; // Nothing to delete.
|
||||
}
|
||||
Tangible *tan = iter->second.get();
|
||||
|
||||
// Fetch the lua side of the tangible.
|
||||
LS.maketan(database, id);
|
||||
@@ -266,6 +267,9 @@ void World::tangible_delete(int64_t id) {
|
||||
LS.rawset(metatab, "id", id);
|
||||
LS.rawset(metatab, "__metatable", false);
|
||||
|
||||
// If the tangible is in the connections table, remove it.
|
||||
if (tan->client_id_ != 0) connections_.erase(tan->client_id_);
|
||||
|
||||
// Remove the C++ portion from the tangibles table.
|
||||
tangibles_.erase(iter);
|
||||
}
|
||||
@@ -312,7 +316,7 @@ int64_t World::connection_create() {
|
||||
|
||||
// Set the login flags.
|
||||
if (is_authoritative()) {
|
||||
tan->is_controlled_ = true;
|
||||
tan->client_id_ = id;
|
||||
tan->delete_on_disconnect_ = true;
|
||||
}
|
||||
|
||||
@@ -337,11 +341,11 @@ void World::connection_delete(int64_t client_id) {
|
||||
auto iter = connections_.find(client_id);
|
||||
if (iter == connections_.end()) return;
|
||||
int64_t actor_id = iter->second;
|
||||
connections_.erase(iter);
|
||||
Tangible *tan = tangible_get(actor_id);
|
||||
assert(tan != nullptr);
|
||||
assert(tan->is_controlled_);
|
||||
tan->is_controlled_ = false;
|
||||
assert(tan->client_id_ == client_id);
|
||||
connections_.erase(iter);
|
||||
tan->client_id_ = 0;
|
||||
if (tan->delete_on_disconnect_) {
|
||||
util::dprintf("Deleted actor: %lld\n", actor_id);
|
||||
tangible_delete(actor_id);
|
||||
@@ -354,32 +358,22 @@ int64_t World::connection_get_actor(int64_t client_id) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t World::connection_get_client(Tangible *actor) const {
|
||||
if (!actor->is_controlled_) return 0;
|
||||
for (const auto &pair : connections_) {
|
||||
if (pair.second == actor->id()) return pair.first;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
eng::string World::connection_redirect(Tangible *actor1, Tangible *actor2) {
|
||||
if (actor1 == actor2) return "";
|
||||
void World::connection_redirect(Tangible *actor1, Tangible *actor2) {
|
||||
if ((actor1 == nullptr) || (actor2 == nullptr) || (actor1 == actor2)) return;
|
||||
|
||||
if (!actor1->is_controlled_) {
|
||||
return "actor1 is not logged in, cannot redirect";
|
||||
}
|
||||
int64_t client_id1 = actor1->client_id_;
|
||||
if (client_id1 == 0) return;
|
||||
|
||||
// If neccessary, bump actor2. Do not implement delete-on-disconnect,
|
||||
// because actor2 isn't really "disconnected" yet.
|
||||
if ((actor2 != nullptr) && (actor2->is_controlled_)) {
|
||||
int64_t client_id2 = connection_get_client(actor2);
|
||||
actor2->is_controlled_ = false;
|
||||
connections_.erase(client_id2);
|
||||
if ((actor2 != nullptr) && (actor2->client_id_ != 0)) {
|
||||
connections_.erase(actor2->client_id_);
|
||||
actor2->client_id_ = 0;
|
||||
}
|
||||
|
||||
int64_t client_id1 = connection_get_client(actor1);
|
||||
actor1->is_controlled_ = false;
|
||||
connections_.erase(client_id1);
|
||||
connections_.erase(actor1->client_id_);
|
||||
actor1->client_id_ = 0;
|
||||
|
||||
if (actor1->delete_on_disconnect_) {
|
||||
tangible_delete(actor1->id());
|
||||
@@ -387,10 +381,9 @@ eng::string World::connection_redirect(Tangible *actor1, Tangible *actor2) {
|
||||
|
||||
if (actor2 != nullptr) {
|
||||
connection_prepare(actor2);
|
||||
actor2->is_controlled_ = true;
|
||||
actor2->client_id_ = client_id1;
|
||||
connections_[client_id1] = actor2->id();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void World::connection_close_all() {
|
||||
|
||||
Reference in New Issue
Block a user