Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
scenarios::GamingEventSystem Class Reference

Gaming Event System. More...

Collaboration diagram for scenarios::GamingEventSystem:
Collaboration graph

Classes

struct  GameEvent
 

Public Member Functions

void simulate_gaming_scenario ()
 

Private Member Functions

void process_game_event (const GameEvent &event)
 
void send_achievement_notification (const std::string &player_id, const std::string &achievement)
 
void print_leaderboard ()
 

Private Attributes

std::atomic< int > events_processed_ {0}
 
std::atomic< int > achievements_unlocked_ {0}
 
std::map< std::string, int > player_scores_
 
std::mutex scores_mutex_
 

Detailed Description

Gaming Event System.

Examples
real_world_scenarios.cpp.

Definition at line 314 of file real_world_scenarios.cpp.

Member Function Documentation

◆ print_leaderboard()

void scenarios::GamingEventSystem::print_leaderboard ( )
inlineprivate
Examples
real_world_scenarios.cpp.

Definition at line 457 of file real_world_scenarios.cpp.

457 {
458 std::cout << "\n === Leaderboard ===" << std::endl;
459
460 std::vector<std::pair<std::string, int>> leaderboard;
461 {
462 std::lock_guard<std::mutex> lock(scores_mutex_);
463 for (const auto& entry : player_scores_) {
464 leaderboard.emplace_back(entry.first, entry.second);
465 }
466 }
467
468 std::sort(leaderboard.begin(), leaderboard.end(),
469 [](const auto& a, const auto& b) { return a.second > b.second; });
470
471 for (size_t i = 0; i < std::min(size_t(5), leaderboard.size()); ++i) {
472 std::cout << " " << (i + 1) << ". " << leaderboard[i].first
473 << ": " << leaderboard[i].second << " points" << std::endl;
474 }
475 std::cout << " ===================" << std::endl;
476 }
std::map< std::string, int > player_scores_

References player_scores_, and scores_mutex_.

Referenced by simulate_gaming_scenario().

Here is the caller graph for this function:

◆ process_game_event()

void scenarios::GamingEventSystem::process_game_event ( const GameEvent & event)
inlineprivate
Examples
real_world_scenarios.cpp.

Definition at line 407 of file real_world_scenarios.cpp.

407 {
408 auto container = std::make_shared<value_container>();
409 container->set_source("game_client", event.player_id);
410 container->set_target("game_server", "event_processor");
411 container->set_message_type("game_event");
412
413 container->set("player_id", event.player_id);
414 container->set("event_type", event.event_type);
415 container->set("timestamp", static_cast<int64_t>(
416 std::chrono::duration_cast<std::chrono::milliseconds>(
417 event.timestamp.time_since_epoch()).count()));
418
419 for (const auto& data_pair : event.event_data) {
420 container->set(data_pair.first, data_pair.second);
421 }
422
423 if (event.event_data.find("score") != event.event_data.end()) {
424 int score = std::stoi(event.event_data.at("score"));
425 {
426 std::lock_guard<std::mutex> lock(scores_mutex_);
427 player_scores_[event.player_id] += score;
428 }
429 }
430
431 if (event.event_type == "level_up" && event.event_data.find("new_level") != event.event_data.end()) {
432 int level = std::stoi(event.event_data.at("new_level"));
433 if (level >= 25) {
435 send_achievement_notification(event.player_id, "High Level Achiever");
436 }
437 }
438
439 container->serialize_string(value_container::serialization_format::binary).value();
440 }
void send_achievement_notification(const std::string &player_id, const std::string &achievement)

References achievements_unlocked_, scenarios::GamingEventSystem::GameEvent::event_data, scenarios::GamingEventSystem::GameEvent::event_type, scenarios::GamingEventSystem::GameEvent::player_id, player_scores_, scores_mutex_, send_achievement_notification(), and scenarios::GamingEventSystem::GameEvent::timestamp.

Referenced by simulate_gaming_scenario().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_achievement_notification()

void scenarios::GamingEventSystem::send_achievement_notification ( const std::string & player_id,
const std::string & achievement )
inlineprivate
Examples
real_world_scenarios.cpp.

Definition at line 442 of file real_world_scenarios.cpp.

442 {
443 auto notification = std::make_shared<value_container>();
444 notification->set_source("achievement_system", "unlock_processor");
445 notification->set_target("notification_service", "player_notifier");
446 notification->set_message_type("achievement_unlocked");
447
448 notification->set("player_id", player_id);
449 notification->set("achievement_name", achievement);
450 notification->set("timestamp", static_cast<int64_t>(
451 std::chrono::duration_cast<std::chrono::milliseconds>(
452 std::chrono::system_clock::now().time_since_epoch()).count()));
453
454 std::cout << " ACHIEVEMENT: " << player_id << " unlocked '" << achievement << "'" << std::endl;
455 }

Referenced by process_game_event().

Here is the caller graph for this function:

◆ simulate_gaming_scenario()

void scenarios::GamingEventSystem::simulate_gaming_scenario ( )
inline
Examples
real_world_scenarios.cpp.

Definition at line 329 of file real_world_scenarios.cpp.

329 {
330 std::cout << "\n=== Gaming Event System Scenario ===" << std::endl;
331
332 const int num_players = 10;
333 const int events_per_player = 50;
334 std::vector<std::thread> player_threads;
335
336 std::queue<GameEvent> event_queue;
337 std::mutex event_mutex;
338 std::condition_variable event_cv;
339 std::atomic<bool> game_active{true};
340
341 std::thread event_processor([&]() {
342 while (game_active || !event_queue.empty()) {
343 std::unique_lock<std::mutex> lock(event_mutex);
344 event_cv.wait(lock, [&]() { return !event_queue.empty() || !game_active; });
345
346 if (!event_queue.empty()) {
347 GameEvent event = event_queue.front();
348 event_queue.pop();
349 lock.unlock();
350
351 process_game_event(event);
353 }
354 }
355 });
356
357 std::random_device rd;
358 for (int player_id = 0; player_id < num_players; ++player_id) {
359 player_threads.emplace_back([&, player_id]() {
360 std::mt19937 gen(rd());
361 std::uniform_int_distribution<> action_dist(0, 4);
362 std::uniform_int_distribution<> score_dist(10, 500);
363 std::uniform_int_distribution<> level_dist(1, 50);
364
365 std::vector<std::string> actions = {"kill", "death", "level_up", "item_collected", "quest_completed"};
366
367 for (int event_count = 0; event_count < events_per_player; ++event_count) {
368 GameEvent event;
369 event.player_id = "player_" + std::to_string(player_id);
370 event.event_type = actions[action_dist(gen)];
371 event.timestamp = std::chrono::system_clock::now();
372
373 if (event.event_type == "kill") {
374 event.event_data["target"] = "player_" + std::to_string((player_id + 1) % num_players);
375 event.event_data["score"] = std::to_string(score_dist(gen));
376 } else if (event.event_type == "level_up") {
377 event.event_data["new_level"] = std::to_string(level_dist(gen));
378 }
379
380 {
381 std::lock_guard<std::mutex> lock(event_mutex);
382 event_queue.push(event);
383 }
384 event_cv.notify_one();
385
386 std::this_thread::sleep_for(std::chrono::milliseconds(20));
387 }
388 });
389 }
390
391 for (auto& thread : player_threads) {
392 thread.join();
393 }
394
395 game_active = false;
396 event_cv.notify_all();
397 event_processor.join();
398
400
401 std::cout << "Gaming simulation completed:" << std::endl;
402 std::cout << " Events processed: " << events_processed_.load() << std::endl;
403 std::cout << " Achievements unlocked: " << achievements_unlocked_.load() << std::endl;
404 }
void process_game_event(const GameEvent &event)

References achievements_unlocked_, scenarios::GamingEventSystem::GameEvent::event_type, events_processed_, scenarios::GamingEventSystem::GameEvent::player_id, print_leaderboard(), and process_game_event().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ achievements_unlocked_

std::atomic<int> scenarios::GamingEventSystem::achievements_unlocked_ {0}
private
Examples
real_world_scenarios.cpp.

Definition at line 324 of file real_world_scenarios.cpp.

324{0};

Referenced by process_game_event(), and simulate_gaming_scenario().

◆ events_processed_

std::atomic<int> scenarios::GamingEventSystem::events_processed_ {0}
private
Examples
real_world_scenarios.cpp.

Definition at line 323 of file real_world_scenarios.cpp.

323{0};

Referenced by simulate_gaming_scenario().

◆ player_scores_

std::map<std::string, int> scenarios::GamingEventSystem::player_scores_
private
Examples
real_world_scenarios.cpp.

Definition at line 325 of file real_world_scenarios.cpp.

Referenced by print_leaderboard(), and process_game_event().

◆ scores_mutex_

std::mutex scenarios::GamingEventSystem::scores_mutex_
private
Examples
real_world_scenarios.cpp.

Definition at line 326 of file real_world_scenarios.cpp.

Referenced by print_leaderboard(), and process_game_event().


The documentation for this class was generated from the following file: