diff options
author | Stijn Buys <ingar@osirion.org> | 2010-09-22 21:32:34 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2010-09-22 21:32:34 +0000 |
commit | a6f9773c358dd7d091ff64cbda504ab8d8066dd3 (patch) | |
tree | 226e23c4656957e908623ccda9d3d1c50240a0b4 /src/core/netconnection.cc | |
parent | bbb43d1c15f2858573f5abb595aa62f8224e4d76 (diff) |
full trading support for networked games
Diffstat (limited to 'src/core/netconnection.cc')
-rw-r--r-- | src/core/netconnection.cc | 92 |
1 files changed, 90 insertions, 2 deletions
diff --git a/src/core/netconnection.cc b/src/core/netconnection.cc index 39bed62..11e23c9 100644 --- a/src/core/netconnection.cc +++ b/src/core/netconnection.cc @@ -435,6 +435,17 @@ void NetConnection::send_info_request(Info *info) info->set_timestamp(timestamp()); } +// send an inventory update request +void NetConnection::send_inventory_request(Entity *entity) +{ + if (!entity || !entity->inventory()) + return; + + std::ostringstream msg; + msg << "inv " << entity->id() << " " << entity->inventory()->timestamp() << "\n"; + this->send_raw(msg.str()); +} + // parse incoming client messages /** * The following incoming messages are parsed; @@ -452,7 +463,8 @@ void NetConnection::send_info_request(Info *info) * sup <id> <typeid> * pif <id> * pid <id> - * inf <id> <typelabel> <label> + * inf <id> + * inv <id> <timestamp> * zone */ void NetConnection::parse_incoming_message(const std::string & message) @@ -787,6 +799,30 @@ void NetConnection::parse_incoming_message(const std::string & message) con_warn << "Received invalid info record message!" << std::endl; return; } + + if (id == 0) { + // special case: info type constructor + size_t s; + + if (!(msgstream >> s)) { + con_warn << "Received invalid info type message!" << std::endl; + return; + } + + for (size_t i = 0; i < s; i++) { + // read infotype label + n.clear(); + while ((msgstream.get(c)) && (c != '"')); + while ((msgstream.get(c)) && (c != '"')) + n +=c; + + if (n.size() && !InfoType::find(n)) { + // add new infotype + new InfoType(n.c_str()); + } + } + return; + } // read type label n.clear(); @@ -834,9 +870,61 @@ void NetConnection::parse_incoming_message(const std::string & message) info->set_id(id); info->receive_server_update(msgstream); - info->clear_timestamp(); + //info->clear_timestamp(); + info->set_timestamp(timestamp()); //con_debug << "Received info for " << info->id() << " " << info->type()->label() << ":" << info->label() << std::endl; + + } else if (command.compare("inv") == 0) { + + // received inventory update + unsigned int id = 0; + unsigned long server_timestamp; + + // read id + if (!(msgstream >> id >> server_timestamp)) { + con_warn << "Received invalid inventory update message!" << std::endl; + return; + } + + Entity *entity = Entity::find(id); + if (!entity) { + con_warn << "Received inventory update for non-existing entity " << id << "!" << std::endl; + return; + } + if (!entity->inventory()) { + con_warn << "Received inventory update for entity " << id << " without inventory!" << std::endl; + return; + } + + //con_debug << "CLIENT received inv message for entity " << id << " server timestamp " << server_timestamp << " client timestamp " << entity->inventory()->timestamp() << std::endl; + + + if (server_timestamp < entity->inventory()->timestamp()) + return; + + size_t nbitems = 0; + if (!(msgstream >> nbitems)) + nbitems = 0; + + //con_debug << "CLIENT number of items: " << nbitems << std::endl; + for (size_t i = 0; i < nbitems; i++) { + if (!(msgstream >> id)) { + con_warn << "Received inventory update without info id for existing entity " << id << "!" << std::endl; + return; + } + + Info *info = core::game()->request_info(id); + Item *item = entity->inventory()->find(info); + if (!item) { + item = new Item(info); + entity->inventory()->add(item); + } + item->receive_server_update(msgstream); + } + + entity->inventory()->set_timestamp(server_timestamp); + entity->inventory()->recalculate(); } } |