@@ -38,7 +38,9 @@ void System::ParseSystemConfig(const Json::Value& v)
3838 m_dbPath = v[" gme_sqlite_path" ].asString ();
3939 m_sessionTimeout = size_t (v[" session_timeout" ].asUInt64 ());
4040
41- m_mstConfig.LoadAllTables (v[" mst_root" ].asString ());
41+ const auto mstRoot = v[" mst_root" ].asString ();
42+ m_mstConfig.LoadAllTables (mstRoot);
43+ m_unitMst.LoadFromJson (mstRoot + " /unit_master.json" );
4244}
4345
4446void System::RunMigrations (drogon::orm::DbClientPtr p)
@@ -76,7 +78,8 @@ void System::DevValidateAndSeedUnits(drogon::orm::DbClientPtr db)
7678#if !DEV_SKIP_TUTORIAL
7779 return ;
7880#else
79- const size_t expectedCount = ALL_UNIT_IDS.size ();
81+ const auto & unitIds = m_unitMst.GetAllUnitIds ();
82+ const size_t expectedCount = unitIds.size ();
8083
8184 LOG_INFO << " System::DevValidateAndSeedUnits: checking existing users "
8285 << " (expecting " << expectedCount << " units each)" ;
@@ -110,7 +113,7 @@ void System::DevValidateAndSeedUnits(drogon::orm::DbClientPtr db)
110113 LOG_INFO << " System::DevValidateAndSeedUnits: user " << userId
111114 << " has " << have << " /" << expectedCount << " units - seeding." ;
112115
113- for (const auto & unitId : ALL_UNIT_IDS )
116+ for (const auto & unitId : unitIds )
114117 {
115118 try
116119 {
@@ -135,4 +138,70 @@ void System::DevValidateAndSeedUnits(drogon::orm::DbClientPtr db)
135138#endif
136139}
137140
141+ void System::DevValidateAndSeedItems (drogon::orm::DbClientPtr db)
142+ {
143+ #if !DEV_SKIP_TUTORIAL
144+ return ;
145+ #else
146+ const auto & seedItems = m_mstConfig.GetSeedItems ();
147+ if (seedItems.empty ())
148+ return ;
149+
150+ LOG_INFO << " System::DevValidateAndSeedItems: checking existing users "
151+ << " (expecting " << seedItems.size () << " item types each)" ;
152+
153+ try
154+ {
155+ auto users = db->execSqlSync (" SELECT id FROM users;" );
156+
157+ if (users.empty ())
158+ {
159+ LOG_INFO << " System::DevValidateAndSeedItems: no existing users, nothing to do." ;
160+ return ;
161+ }
162+
163+ for (const auto & row : users)
164+ {
165+ const std::string userId = row[" id" ].as <std::string>();
166+
167+ auto countResult = db->execSqlSync (
168+ " SELECT COUNT(*) FROM user_items WHERE user_id = $1;" , userId);
169+
170+ const size_t have = countResult[0 ][0 ].as <size_t >();
171+
172+ if (have >= seedItems.size ())
173+ {
174+ LOG_DEBUG << " System::DevValidateAndSeedItems: user " << userId
175+ << " already has " << have << " item types, skipping." ;
176+ continue ;
177+ }
178+
179+ LOG_INFO << " System::DevValidateAndSeedItems: user " << userId
180+ << " has " << have << " /" << seedItems.size () << " item types - seeding." ;
181+
182+ for (const auto & item : seedItems)
183+ {
184+ try
185+ {
186+ db->execSqlSync (
187+ " INSERT OR IGNORE INTO user_items (user_id, item_id, quantity) VALUES ($1, $2, $3);" ,
188+ userId, item.id , item.quantity );
189+ }
190+ catch (const drogon::orm::DrogonDbException& e)
191+ {
192+ LOG_WARN << " System::DevValidateAndSeedItems: failed to insert item "
193+ << item.id << " for " << userId << " : " << e.base ().what ();
194+ }
195+ }
196+
197+ LOG_INFO << " System::DevValidateAndSeedItems: finished seeding user " << userId;
198+ }
199+ }
200+ catch (const drogon::orm::DrogonDbException& e)
201+ {
202+ LOG_ERROR << " System::DevValidateAndSeedItems: DB error: " << e.base ().what ();
203+ }
204+ #endif
205+ }
206+
138207System System::m_sys;
0 commit comments