Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
game_instance_manager.h
1//
2// Created by Manuel on 29.01.2021.
3//
4// The game_instance_manager only exists on the server side. It stores all currently active games and offers
5// functionality to retrieve game instances by id and adding players to games.
6// If a new player requests to join a game but no valid game_instance is available, then this class
7// will generate a new game_instance and add it to the unordered_map of (active) game instances.
8
9#ifndef WIZARD_GAME_INSTANCE_MANAGER_H
10#define WIZARD_GAME_INSTANCE_MANAGER_H
11
12#include <string>
13#include <shared_mutex>
14#include <unordered_map>
15
16#include "game_instance.h"
17
19
20private:
21
22 inline static std::shared_mutex games_lut_lock;
23 static std::unordered_map<std::string, game_instance*> games_lut;
24
25 static game_instance* create_new_game();
26 static game_instance* find_joinable_game_instance();
27
28public:
29
30 // returns true if the desired game_instance 'game_id' was found or false otherwise.
31 // The found game instance is written into game_instance_ptr.
32 static bool try_get_game_instance(const std::string& game_id, game_instance*& game_instance_ptr);
33 // returns true if the desired player 'player_id' was found and is connected to a game_instance.
34 // The found player and game_instance will be written into 'player' and 'game_instance_ptr'
35 static bool try_get_player_and_game_instance(const std::string& player_id, player*& player, game_instance*& game_instance_ptr, std::string& err);
36
37 // Try to add 'player' to any game. Returns true if 'player' is successfully added to a game_instance.
38 // The joined game_instance will be written into 'game_instance_ptr'.
39 static bool try_add_player_to_any_game(player* player, game_instance*& game_instance_ptr, std::string& err);
40 // Try to add 'player' to the provided 'game_instance_ptr'. Returns true if success and false otherwise.
41 static bool try_add_player(player* player, game_instance*& game_instance_ptr, std::string& err);
42
43
44 static bool try_remove_player(player* player, const std::string& game_id, std::string& err);
45 static bool try_remove_player(player* player, game_instance*& game_instance_ptr, std::string& err);
46
47};
48
49
50#endif //WIZARD_GAME_INSTANCE_MANAGER_H
Definition game_instance_manager.h:18
Definition game_instance.h:17
Represents a player in the game.
Definition player.h:18