void GameDatabase::LoadGame(const size_t index, Game &game, const GameObjectEnginePtr &engine, ISerialize& stats) { wxASSERT(mOffsets.size() > index); wxFileInputStream x(FilePath::Data(DataFileGames)); wxDataInputStream games(x); // Seek to this position in the game file. x.SeekI(mOffsets[index]); // Read in the rulesets this game was using. wxUint32 size; games >> size; StringPairArray rulesets; rulesets.reserve(size); for(wxUint32 i = 0; i < size; ++i) { wxString module, ruleset; games >> module; games >> ruleset; rulesets.push_back(StringPair(module, ruleset)); } games >> size; StringPairArray options; options.reserve(size); for(wxUint32 i = 0; i < size; ++i) { wxString module, ruleset; games >> module; games >> ruleset; options.push_back(StringPair(module, ruleset)); } // Fire off the load event for any listeners to deal with it. if(engine) { Controller::get().Transmit(shEventLoadRulesets, rulesets); Controller::get().Transmit(shEventLoadOptions, options); // Immediately start up the game, too. Controller::get().Transmit(shEventStartGame, false); } game.load(games, engine); // Stats must always go at the end so we can ignore them while loading if // desired. stats.load(games); }
size_t GameDatabase::SaveGame(const Game &game, const StringPairArray &rulesets, const StringPairArray& options, const ISerialize& stats, const bool reload) { wxString file = FilePath::Data(DataFileGames); wxString keyfile = FilePath::Data(DataFileGameKeys); // Save the game. wxFile gamedb; gamedb.Open(file, wxFile::write_append); wxFileOutputStream x(gamedb); wxDataOutputStream games(x); // Move to the end. gamedb.SeekEnd(); // Get the starting position wxUint32 pos = gamedb.Tell(); // Write out the rulesets loaded. games << (wxUint32) rulesets.size(); GameDatabase::StringPairArray::const_iterator it, itEnd = rulesets.end(); for(it = rulesets.begin(); it != itEnd; ++it) { games << it->first; games << it->second; } // options data games << (wxUint32) options.size(); itEnd = options.end(); for(it = options.begin(); it != itEnd; ++it) { games << it->first; games << it->second; } // Write out the game. game.save(games); // Stats must always go at the end so we can ignore them while loading if // desired. stats.save(games); // Close the file. gamedb.Close(); // Now update the key file. wxFile keydb; keydb.Open(keyfile, wxFile::write_append); wxFileOutputStream y(keydb); wxDataOutputStream keys(y); // Move to the end keydb.SeekEnd(); // Write the offset keys << pos; wxUint32 keyLength; keyLength = keydb.Length(); // Flush it. keydb.Close(); if(true == reload) { InitializeDatabase(); } // Calculate the number of games. size_t index = (keyLength / sizeof(size_t)) - 1; return index; }
std::string NFCMasterNet_ServerModule::GetServersStatus() { rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType); root.AddMember("code", 0, allocator); root.AddMember("errMsg", "", allocator); root.AddMember("nowTime", pPluginManager->GetNowTime(), allocator); rapidjson::Value master(rapidjson::kArrayType); std::shared_ptr<ServerData> pServerData = mMasterMap.First(); while (pServerData) { rapidjson::Value server(rapidjson::kObjectType); server.AddMember("serverId", pServerData->pData->server_id(), allocator); server.AddMember("servrName", rapidjson::Value(pServerData->pData->server_name().c_str(), allocator), allocator); server.AddMember("ip", rapidjson::Value(pServerData->pData->server_ip().c_str(), allocator), allocator); server.AddMember("port", pServerData->pData->server_port(), allocator); server.AddMember("onlineCount", pServerData->pData->server_cur_count(), allocator); server.AddMember("status", (int)pServerData->pData->server_state(), allocator); /* rapidjson::Value server_info_ext(rapidjson::kArrayType); for (int i = 0; i < pServerData->pData->server_info_list_ext().key_size();i++) { rapidjson::Value extKeyValue(rapidjson::kObjectType); extKeyValue.AddMember(rapidjson::Value(pServerData->pData->server_info_list_ext().key(i).c_str(), allocator), rapidjson::Value(pServerData->pData->server_info_list_ext().value(i).c_str(), allocator), allocator); server_info_ext.PushBack(extKeyValue, allocator); } server.AddMember("info_ext", server_info_ext, allocator); */ master.PushBack(server, allocator); pServerData = mMasterMap.Next(); } root.AddMember("master", master, allocator); rapidjson::Value logins(rapidjson::kArrayType); pServerData = mLoginMap.First(); while (pServerData) { rapidjson::Value server(rapidjson::kObjectType); server.AddMember("serverId", pServerData->pData->server_id(), allocator); server.AddMember("servrName", rapidjson::Value(pServerData->pData->server_name().c_str(), allocator), allocator); server.AddMember("ip", rapidjson::Value(pServerData->pData->server_ip().c_str(), allocator), allocator); server.AddMember("port", pServerData->pData->server_port(), allocator); server.AddMember("onlineCount", pServerData->pData->server_cur_count(), allocator); server.AddMember("status", (int)pServerData->pData->server_state(), allocator); /* rapidjson::Value server_info_ext(rapidjson::kArrayType); for (int i = 0; i < pServerData->pData->server_info_list_ext().key_size();i++) { rapidjson::Value extKeyValue(rapidjson::kObjectType); extKeyValue.AddMember(rapidjson::Value(pServerData->pData->server_info_list_ext().key(i).c_str(), allocator), rapidjson::Value(pServerData->pData->server_info_list_ext().value(i).c_str(), allocator), allocator); server_info_ext.PushBack(extKeyValue, allocator); } server.AddMember("info_ext", server_info_ext, allocator); */ logins.PushBack(server, allocator); pServerData = mLoginMap.Next(); } root.AddMember("logins", logins, allocator); rapidjson::Value worlds(rapidjson::kArrayType); pServerData = mWorldMap.First(); while (pServerData.get()) { rapidjson::Value server(rapidjson::kObjectType); server.AddMember("serverId", pServerData->pData->server_id(), allocator); server.AddMember("servrName", rapidjson::Value(pServerData->pData->server_name().c_str(), allocator), allocator); server.AddMember("ip", rapidjson::Value(pServerData->pData->server_ip().c_str(), allocator), allocator); server.AddMember("port", pServerData->pData->server_port(), allocator); server.AddMember("onlineCount", pServerData->pData->server_cur_count(), allocator); server.AddMember("status", (int)pServerData->pData->server_state(), allocator); /* rapidjson::Value server_info_ext(rapidjson::kArrayType); for (int i = 0; i < pServerData->pData->server_info_list_ext().key_size();i++) { rapidjson::Value extKeyValue(rapidjson::kObjectType); extKeyValue.AddMember(rapidjson::Value(pServerData->pData->server_info_list_ext().key(i).c_str(), allocator), rapidjson::Value(pServerData->pData->server_info_list_ext().value(i).c_str(), allocator), allocator); server_info_ext.PushBack(extKeyValue, allocator); } server.AddMember("info_ext", server_info_ext, allocator); */ worlds.PushBack(server, allocator); pServerData = mWorldMap.Next(); } root.AddMember("worlds", worlds, allocator); rapidjson::Value proxys(rapidjson::kArrayType); pServerData = mProxyMap.First(); while (pServerData.get()) { rapidjson::Value server(rapidjson::kObjectType); server.AddMember("serverId", pServerData->pData->server_id(), allocator); server.AddMember("servrName", rapidjson::Value(pServerData->pData->server_name().c_str(), allocator), allocator); server.AddMember("ip", rapidjson::Value(pServerData->pData->server_ip().c_str(), allocator), allocator); server.AddMember("port", pServerData->pData->server_port(), allocator); server.AddMember("onlineCount", pServerData->pData->server_cur_count(), allocator); server.AddMember("status", (int)pServerData->pData->server_state(), allocator); /* rapidjson::Value server_info_ext(rapidjson::kArrayType); for (int i = 0; i < pServerData->pData->server_info_list_ext().key_size();i++) { rapidjson::Value extKeyValue(rapidjson::kObjectType); extKeyValue.AddMember(rapidjson::Value(pServerData->pData->server_info_list_ext().key(i).c_str(), allocator), rapidjson::Value(pServerData->pData->server_info_list_ext().value(i).c_str(), allocator), allocator); server_info_ext.PushBack(extKeyValue, allocator); } server.AddMember("info_ext", server_info_ext, allocator); */ proxys.PushBack(server, allocator); pServerData = mProxyMap.Next(); } root.AddMember("proxys", proxys, allocator); rapidjson::Value games(rapidjson::kArrayType); pServerData = mGameMap.First(); while (pServerData.get()) { rapidjson::Value server(rapidjson::kObjectType); server.AddMember("serverId", pServerData->pData->server_id(), allocator); server.AddMember("servrName", rapidjson::Value(pServerData->pData->server_name().c_str(), allocator), allocator); server.AddMember("ip", rapidjson::Value(pServerData->pData->server_ip().c_str(), allocator), allocator); server.AddMember("port", pServerData->pData->server_port(), allocator); server.AddMember("onlineCount", pServerData->pData->server_cur_count(), allocator); server.AddMember("status", (int)pServerData->pData->server_state(), allocator); /* rapidjson::Value server_info_ext(rapidjson::kArrayType); for (int i = 0; i < pServerData->pData->server_info_list_ext().key_size();i++) { rapidjson::Value extKeyValue(rapidjson::kObjectType); extKeyValue.AddMember(rapidjson::Value(pServerData->pData->server_info_list_ext().key(i).c_str(), allocator), rapidjson::Value(pServerData->pData->server_info_list_ext().value(i).c_str(), allocator), allocator); server_info_ext.PushBack(extKeyValue, allocator); } server.AddMember("info_ext", server_info_ext, allocator); */ games.PushBack(server, allocator); pServerData = mGameMap.Next(); } root.AddMember("games", games, allocator); rapidjson::StringBuffer jsonBuf; rapidjson::Writer<rapidjson::StringBuffer> jsonWriter(jsonBuf); root.Accept(jsonWriter); return jsonBuf.GetString(); }
void MessageWriter::send_available_games(const std::map<char, std::string> &game_names) { char message_type = LIST_GAMES; socket_->send_buffer(&message_type, sizeof(char)); GamesMessage games(socket_); games.send(game_names); }
void Lobby::handlePackagesForOneUser(User::wPtr p_User) { User::ptr user = p_User.lock(); if (!user) { return; } IConnectionController* con = user->getConnection(); unsigned int numPackages = con->getNumPackages(); for (unsigned int i = 0; i < numPackages; ++i) { Package package = con->getPackage(i); PackageType type = con->getPackageType(package); switch (type) { case PackageType::JOIN_GAME: { const std::string levelName = con->getJoinGameName(package); const std::string username = con->getJoinGameUsername(package); const std::string characterName = con->getJoinGameCharacterName(package); const std::string characterStyle = con->getJoinGameCharacterStyle(package); user->setUsername(username); user->setCharacterName(characterName); user->setCharacterStyle(characterStyle); joinLevel(user, levelName); con->clearPackages(i + 1); return; } case PackageType::REQUEST_GAMES: { std::vector<AvailableGameData> games(m_Levels.size()); for (size_t i = 0; i < games.size(); ++i) { const auto& level = m_Levels[i]; auto& availGame = games[i]; availGame.levelName = level.m_LevelName.c_str(); availGame.waitingPlayers = level.m_JoinedUsers.size(); availGame.maxPlayers = level.m_MaxPlayers; } con->sendGameList(games.data(), games.size()); } break; default: std::string msg("Received unhandled package of type " + std::to_string((uint16_t)type)); Logger::log(Logger::Level::WARNING, msg); break; } } con->clearPackages(numPackages); }