GroveEngine/tests/modules/PlayerModule.cpp
StillHammer ddbed30ed7 feat: Add Scenario 11 IO System test & fix IntraIO routing architecture
Implémentation complète du scénario 11 (IO System Stress Test) avec correction majeure de l'architecture de routing IntraIO.

## Nouveaux Modules de Test (Scenario 11)
- ProducerModule: Publie messages pour tests IO
- ConsumerModule: Consomme et valide messages reçus
- BroadcastModule: Test multi-subscriber broadcasting
- BatchModule: Test low-frequency batching
- IOStressModule: Tests de charge concurrents

## Test d'Intégration
- test_11_io_system.cpp: 6 tests validant:
  * Basic Publish-Subscribe
  * Pattern Matching avec wildcards
  * Multi-Module Routing (1-to-many)
  * Low-Frequency Subscriptions (batching)
  * Backpressure & Queue Overflow
  * Thread Safety (concurrent pub/pull)

## Fix Architecture Critique: IntraIO Routing
**Problème**: IntraIO::publish() et subscribe() n'utilisaient PAS IntraIOManager pour router entre modules.

**Solution**: Utilisation de JSON comme format de transport intermédiaire
- IntraIO::publish() → extrait JSON → IntraIOManager::routeMessage()
- IntraIO::subscribe() → enregistre au IntraIOManager::registerSubscription()
- IntraIOManager::routeMessage() → copie JSON pour chaque subscriber → deliverMessage()

**Bénéfices**:
-  Routing centralisé fonctionnel
-  Support 1-to-many (copie JSON au lieu de move unique_ptr)
-  Pas besoin d'implémenter IDataNode::clone()
-  Compatible futur NetworkIO (JSON sérialisable)

## Modules Scenario 13 (Cross-System)
- ConfigWatcherModule, PlayerModule, EconomyModule, MetricsModule
- test_13_cross_system.cpp (stub)

## Documentation
- CLAUDE_NEXT_SESSION.md: Instructions détaillées pour build/test

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-19 11:43:08 +08:00

162 lines
4.3 KiB
C++

#include "PlayerModule.h"
#include <iostream>
namespace grove {
PlayerModule::PlayerModule() {
std::cout << "[PlayerModule] Constructor" << std::endl;
}
PlayerModule::~PlayerModule() {
std::cout << "[PlayerModule] Destructor" << std::endl;
}
void PlayerModule::process(const IDataNode& input) {
// Process incoming messages from IO
if (io && io->hasMessages() > 0) {
auto msg = io->pullMessage();
if (msg.topic.find("config:") == 0) {
handleConfigChange();
}
}
}
void PlayerModule::setConfiguration(const IDataNode& configNode, IIO* ioPtr, ITaskScheduler* schedulerPtr) {
std::cout << "[PlayerModule] setConfiguration called" << std::endl;
this->io = ioPtr;
this->scheduler = schedulerPtr;
// Store config
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
// Subscribe to config changes
if (io) {
io->subscribe("config:gameplay:changed");
}
}
const IDataNode& PlayerModule::getConfiguration() {
if (!config) {
config = std::make_unique<JsonDataNode>("config", nlohmann::json::object());
}
return *config;
}
std::unique_ptr<IDataNode> PlayerModule::getHealthStatus() {
nlohmann::json health = {
{"status", "healthy"},
{"gold", gold},
{"level", level},
{"playerName", playerName}
};
return std::make_unique<JsonDataNode>("health", health);
}
void PlayerModule::shutdown() {
std::cout << "[PlayerModule] Shutdown - Level: " << level << ", Gold: " << gold << std::endl;
}
std::unique_ptr<IDataNode> PlayerModule::getState() {
nlohmann::json inventoryJson = nlohmann::json::array();
for (const auto& item : inventory) {
inventoryJson.push_back(item);
}
nlohmann::json state = {
{"gold", gold},
{"level", level},
{"playerName", playerName},
{"inventory", inventoryJson}
};
return std::make_unique<JsonDataNode>("state", state);
}
void PlayerModule::setState(const IDataNode& state) {
gold = state.getInt("gold", 1000);
level = state.getInt("level", 1);
playerName = state.getString("playerName", "Player1");
// Restore inventory
inventory.clear();
auto stateData = state.getData();
if (stateData && stateData->has("inventory")) {
auto invData = stateData->get("inventory");
if (invData && invData->isArray()) {
size_t size = invData->size();
for (size_t i = 0; i < size; i++) {
auto item = invData->get(i);
if (item && item->isString()) {
inventory.push_back(item->asString());
}
}
}
}
std::cout << "[PlayerModule] State restored - Level: " << level << ", Gold: " << gold << std::endl;
}
void PlayerModule::setDataTree(IDataTree* treePtr) {
this->tree = treePtr;
}
void PlayerModule::handleConfigChange() {
std::cout << "[PlayerModule] Handling config change" << std::endl;
if (!tree) return;
// Read new config
auto configRoot = tree->getConfigRoot();
auto gameplay = configRoot->getChild("gameplay");
if (gameplay) {
double hpMultiplier = gameplay->getDouble("hpMultiplier", 1.0);
std::string difficulty = gameplay->getString("difficulty", "normal");
std::cout << "[PlayerModule] Config updated - Difficulty: " << difficulty
<< ", HP Mult: " << hpMultiplier << std::endl;
}
}
void PlayerModule::savePlayerData() {
if (!tree) return;
auto dataRoot = tree->getDataRoot();
nlohmann::json profileData = {
{"name", playerName},
{"level", level},
{"gold", gold}
};
auto profile = std::make_unique<JsonDataNode>("profile", profileData);
// This would save to data/player/profile
std::cout << "[PlayerModule] Saving player data" << std::endl;
}
void PlayerModule::publishLevelUp() {
if (!io) return;
nlohmann::json data = {
{"event", "level_up"},
{"newLevel", level},
{"goldBonus", 500}
};
auto dataNode = std::make_unique<JsonDataNode>("levelUp", data);
io->publish("player:level_up", std::move(dataNode));
std::cout << "[PlayerModule] Published level up event" << std::endl;
}
} // namespace grove
// Export C API
extern "C" {
grove::IModule* createModule() {
return new grove::PlayerModule();
}
}