static std::tuple<uint32_t, uint32_t> find_a_friendly_neighborhood_for_our_new_visitor() { PGresult* result = PQexec(s_postgres, "SELECT idx FROM vault.\"Nodes\" WHERE \"String64_2\"=" " 'Neighborhood' AND \"String64_4\" = '" HOOD_USER_NAME "'" " ORDER BY \"Int32_1\""); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return std::make_tuple<uint32_t, uint32_t>(0, 0); } uint32_t theHoodInfo = 0; for (int i = 0; i < PQntuples(result); ++i) { uint32_t ageInfoId = strtoul(PQgetvalue(result, i, 0), 0, 10); uint32_t owners = v_count_age_owners(ageInfoId); if (owners < HOOD_POPULATION_THRESHOLD) { theHoodInfo = ageInfoId; break; } } PQclear(result); // Need new hood? if (theHoodInfo == 0) { AuthServer_AgeInfo age; age.m_ageId = gen_uuid(); age.m_filename = "Neighborhood"; age.m_instName = HOOD_INSTANCE_NAME; age.m_userName = HOOD_USER_NAME; age.m_description = HOOD_USER_NAME " " HOOD_INSTANCE_NAME; age.m_seqNumber = -1; // Auto-generate theHoodInfo = std::get<1>(v_create_age(age, e_AgePublic)); } // It's important to BCast new hood members, so we'll return the ageOwners folder PostgresStrings<2> parms; parms.set(0, theHoodInfo); parms.set(1, DS::Vault::e_AgeOwnersFolder); result = PQexecParams(s_postgres, "SELECT idx FROM vault.find_folder($1, $2);", 2, 0, parms.m_values, 0, 0, 0); if (PQresultStatus(result) == PGRES_TUPLES_OK) { uint32_t ownersFolder = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); return std::make_tuple(theHoodInfo, ownersFolder); } else { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return std::make_tuple(0, 0); } }
bool dm_vault_init() { PostgresStrings<1> sparm; sparm.set(0, DS::Vault::e_NodeSystem); PGresult* result = PQexecParams(s_postgres, "SELECT \"idx\" FROM vault.\"Nodes\"" " WHERE \"NodeType\"=$1", 1, 0, sparm.m_values, 0, 0, 0); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return false; } int count = PQntuples(result); if (count == 0) { PQclear(result); fputs("[Vault] Initializing empty DirtSand vault\n", stderr); // Create system and global inbox nodes DS::Vault::Node node; node.set_NodeType(DS::Vault::e_NodeSystem); s_systemNode = v_create_node(node); if (s_systemNode == 0) return false; node.set_NodeType(DS::Vault::e_NodeFolder); node.set_Int32_1(DS::Vault::e_GlobalInboxFolder); uint32_t globalInbox = v_create_node(node); if (globalInbox == 0) return false; if (!v_ref_node(s_systemNode, globalInbox, 0)) return false; std::list<AuthServer_AgeInfo> ages = configure_static_ages(); for (auto iter = ages.begin(); iter != ages.end(); ++iter) { if (std::get<0>(v_create_age(*iter, e_AgePublic)) == 0) return false; } } else { DS_DASSERT(count == 1); s_systemNode = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); } return true; }
std::tuple<uint32_t, uint32_t, uint32_t> v_create_player(DS::Uuid acctId, const AuthServer_PlayerInfo& player) { DS::Vault::Node node; node.set_NodeType(DS::Vault::e_NodePlayer); node.set_CreatorUuid(acctId); node.set_Int32_2(player.m_explorer); node.set_Uuid_1(acctId); node.set_String64_1(player.m_avatarModel); node.set_IString64_1(player.m_playerName); uint32_t playerIdx = v_create_node(node); if (playerIdx == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodePlayerInfo); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Uint32_1(playerIdx); node.set_IString64_1(player.m_playerName); uint32_t playerInfoNode = v_create_node(node); if (playerInfoNode == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodePlayerInfoList); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_BuddyListFolder); uint32_t buddyList = v_create_node(node); if (buddyList == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodePlayerInfoList); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_IgnoreListFolder); uint32_t ignoreList = v_create_node(node); if (ignoreList == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_PlayerInviteFolder); uint32_t invites = v_create_node(node); if (invites == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeAgeInfoList); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_AgesIOwnFolder); uint32_t agesNode = v_create_node(node); if (agesNode == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_AgeJournalsFolder); uint32_t journals = v_create_node(node); if (journals == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_ChronicleFolder); uint32_t chronicles = v_create_node(node); if (chronicles == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeAgeInfoList); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_AgesICanVisitFolder); uint32_t visitFolder = v_create_node(node); if (visitFolder == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_AvatarOutfitFolder); uint32_t outfit = v_create_node(node); if (outfit == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_AvatarClosetFolder); uint32_t closet = v_create_node(node); if (closet == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeFolder); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_InboxFolder); uint32_t inbox = v_create_node(node); if (inbox == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodePlayerInfoList); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Int32_1(DS::Vault::e_PeopleIKnowAboutFolder); uint32_t peopleNode = v_create_node(node); if (peopleNode == 0) return std::make_tuple(0, 0, 0); DS::Blob link(reinterpret_cast<const uint8_t*>("Default:LinkInPointDefault:;"), strlen("Default:LinkInPointDefault:;")); node.clear(); node.set_NodeType(DS::Vault::e_NodeAgeLink); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Blob_1(link); uint32_t reltoLink = v_create_node(node); if (reltoLink == 0) return std::make_tuple(0, 0, 0); node.clear(); node.set_NodeType(DS::Vault::e_NodeAgeLink); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Blob_1(link); uint32_t hoodLink = v_create_node(node); if (hoodLink == 0) return std::make_tuple(0, 0, 0); link = DS::Blob(reinterpret_cast<const uint8_t*>("Ferry Terminal:LinkInPointFerry:;"), strlen("Ferry Terminal:LinkInPointFerry:;")); node.clear(); node.set_NodeType(DS::Vault::e_NodeAgeLink); node.set_CreatorUuid(acctId); node.set_CreatorIdx(playerIdx); node.set_Blob_1(link); uint32_t cityLink = v_create_node(node); if (hoodLink == 0) return std::make_tuple(0, 0, 0); AuthServer_AgeInfo relto; relto.m_ageId = gen_uuid(); relto.m_filename = "Personal"; relto.m_instName = "Relto"; relto.m_userName = player.m_playerName + "'s"; relto.m_description = relto.m_userName + " " + relto.m_instName; std::tuple<uint32_t, uint32_t> reltoAge = v_create_age(relto, 0); if (std::get<0>(reltoAge) == 0) return std::make_tuple(0, 0, 0); { PostgresStrings<2> parms; parms.set(0, std::get<1>(reltoAge)); parms.set(1, DS::Vault::e_AgeOwnersFolder); PGresult* result = PQexecParams(s_postgres, "SELECT idx FROM vault.find_folder($1, $2);", 2, 0, parms.m_values, 0, 0, 0); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return std::make_tuple(0, 0, 0); } DS_DASSERT(PQntuples(result) == 1); uint32_t ownerFolder = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); if (!v_ref_node(ownerFolder, playerInfoNode, 0)) return std::make_tuple(0, 0, 0); } std::tuple<uint32_t, uint32_t> hoodAge = find_a_friendly_neighborhood_for_our_new_visitor(); if (std::get<0>(hoodAge) == 0) return std::make_tuple(0, 0, 0); uint32_t cityAge = find_public_age_1("city"); if (cityAge == 0) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, s_systemNode, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, playerInfoNode, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, buddyList, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, ignoreList, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, invites, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, agesNode, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, journals, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, chronicles, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, visitFolder, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, outfit, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, closet, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, inbox, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(playerIdx, peopleNode, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(agesNode, reltoLink, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(agesNode, hoodLink, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(agesNode, cityLink, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(reltoLink, std::get<1>(reltoAge), 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(hoodLink, std::get<0>(hoodAge), 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(cityLink, cityAge, 0)) return std::make_tuple(0, 0, 0); if (!v_ref_node(std::get<0>(reltoAge), agesNode, 0)) return std::make_tuple(0, 0, 0); return std::make_tuple(playerIdx, playerInfoNode, std::get<1>(hoodAge)); }
static uint32_t find_a_friendly_neighborhood_for_our_new_visitor(uint32_t playerInfoId) { PGresult* result = PQexec(s_postgres, "SELECT \"AgeUuid\" FROM game.\"PublicAges\"" " WHERE \"AgeFilename\" = 'Neighborhood'" " AND \"Population\" < 20 AND \"SeqNumber\" <> 0"); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return 0; } std::pair<uint32_t, uint32_t> ageNode; DS::Uuid ageId; if (PQntuples(result) != 0) { ageId = DS::Uuid(PQgetvalue(result, 0, 0)); PQclear(result); PostgresStrings<2> parms; parms.set(0, DS::Vault::e_NodeAgeInfo); parms.set(1, ageId.toString()); result = PQexecParams(s_postgres, "SELECT idx FROM vault.\"Nodes\"" " WHERE \"NodeType\"=$1 AND \"Uuid_1\"=$2", 2, 0, parms.m_values, 0, 0, 0); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return 0; } DS_DASSERT(PQntuples(result) == 1); ageNode.second = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); } else { PQclear(result); AuthServer_AgeInfo age; age.m_ageId = gen_uuid(); age.m_filename = "Neighborhood"; age.m_instName = "Neighborhood"; age.m_userName = "******"; age.m_description = "DS Neighborhood"; age.m_seqNumber = -1; // Auto-generate ageNode = v_create_age(age, e_AgePublic); if (ageNode.second == 0) return 0; } { PostgresStrings<2> parms; parms.set(0, ageNode.second); parms.set(1, DS::Vault::e_AgeOwnersFolder); result = PQexecParams(s_postgres, "SELECT idx FROM vault.find_folder($1, $2);", 2, 0, parms.m_values, 0, 0, 0); } if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return 0; } DS_DASSERT(PQntuples(result) == 1); uint32_t ownerFolder = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); if (!v_ref_node(ownerFolder, playerInfoId, 0)) return 0; { PostgresStrings<1> parms; parms.set(0, ageId.toString()); result = PQexecParams(s_postgres, "UPDATE game.\"PublicAges\"" " SET \"Population\" = \"Population\"+1" " WHERE \"AgeUuid\" = $1", 1, 0, parms.m_values, 0, 0, 0); } if (PQresultStatus(result) != PGRES_COMMAND_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return 0; } PQclear(result); return ageNode.second; }
bool dm_vault_init() { PostgresStrings<1> sparm; sparm.set(0, DS::Vault::e_NodeSystem); PGresult* result = PQexecParams(s_postgres, "SELECT \"idx\" FROM vault.\"Nodes\"" " WHERE \"NodeType\"=$1", 1, 0, sparm.m_values, 0, 0, 0); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "%s:%d:\n Postgres SELECT error: %s\n", __FILE__, __LINE__, PQerrorMessage(s_postgres)); PQclear(result); return false; } int count = PQntuples(result); if (count == 0) { PQclear(result); fprintf(stderr, "[Vault] Initializing empty DirtSand vault\n"); // Create system and global inbox nodes DS::Vault::Node node; node.set_NodeType(DS::Vault::e_NodeSystem); s_systemNode = v_create_node(node); if (s_systemNode == 0) return false; node.set_NodeType(DS::Vault::e_NodeFolder); node.set_Int32_1(DS::Vault::e_GlobalInboxFolder); uint32_t globalInbox = v_create_node(node); if (globalInbox == 0) return false; if (!v_ref_node(s_systemNode, globalInbox, 0)) return false; AuthServer_AgeInfo age; age.m_ageId = gen_uuid(); age.m_filename = "city"; age.m_instName = "Ae'gura"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "Neighborhood02"; age.m_instName = "Kirel"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "Kveer"; age.m_instName = "K'veer"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GreatTreePub"; age.m_instName = "The Watcher's Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GuildPub-Cartographers"; age.m_instName = "The Cartographers' Guild Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GuildPub-Greeters"; age.m_instName = "The Greeters' Guild Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GuildPub-Maintainers"; age.m_instName = "The Maintainers' Guild Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GuildPub-Messengers"; age.m_instName = "The Messengers' Guild Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; age.m_ageId = gen_uuid(); age.m_filename = "GuildPub-Writers"; age.m_instName = "The Writers' Guild Pub"; if (v_create_age(age, e_AgePublic).first == 0) return false; } else { DS_DASSERT(count == 1); s_systemNode = strtoul(PQgetvalue(result, 0, 0), 0, 10); PQclear(result); } return true; }