warfactoryracine/modules/factory/shared/ModuleBase.h
StillHammer 61ef2293ad Replace engine architecture with modular triple interface system
- Remove old 10-engine system (engines/ directory deleted)
- Implement C++ triple interface architecture:
  * IEngine: Execution coordination (Debug → Production)
  * IModuleSystem: Strategy pattern (Sequential → Threaded → Cluster)
  * IModule: Pure game logic interface (200-300 lines per module)
  * IIO: Communication transport (Intra → Local → Network)

- Add autonomous module structure:
  * modules/factory/: Production logic with autonomous build
  * modules/economy/: Market simulation with autonomous build
  * modules/logistic/: Supply chain with autonomous build
  * Each module: CLAUDE.md + CMakeLists.txt + shared/ + build/

- Benefits for Claude Code development:
  * Ultra-focused contexts (200 lines vs 50K+ lines)
  * Autonomous builds (cmake . from module directory)
  * Hot-swappable infrastructure without logic changes
  * Parallel development across multiple Claude instances

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-20 09:15:03 +08:00

76 lines
1.8 KiB
C++

#pragma once
#include "IModule.h"
#include <nlohmann/json.hpp>
#include <string>
#include <stdexcept>
using json = nlohmann::json;
namespace warfactory {
/**
* @brief Base helper class for modules
*
* Provides common functionality and JSON helpers
* LOCAL COPY - Completely autonomous
*/
class ModuleBase : public IModule {
protected:
std::string module_name;
json module_config;
bool initialized = false;
public:
explicit ModuleBase(const std::string& name) : module_name(name) {}
std::string getName() const override {
return module_name;
}
void initialize(const json& config) override {
module_config = config;
initialized = true;
onInitialize(config);
}
void shutdown() override {
onShutdown();
initialized = false;
}
json getMetadata() const override {
return {
{"name", module_name},
{"version", "1.0.0"},
{"initialized", initialized},
{"type", "game_module"}
};
}
protected:
// Override these in concrete modules
virtual void onInitialize(const json& config) {}
virtual void onShutdown() {}
// JSON helpers
bool hasField(const json& obj, const std::string& field) const {
return obj.contains(field) && !obj[field].is_null();
}
template<typename T>
T getField(const json& obj, const std::string& field, const T& defaultValue = T{}) const {
if (hasField(obj, field)) {
return obj[field].get<T>();
}
return defaultValue;
}
void ensureInitialized() const {
if (!initialized) {
throw std::runtime_error("Module " + module_name + " not initialized");
}
}
};
} // namespace warfactory