]> git.localhorst.tv Git - blank.git/blob - src/shared/CLIContext.hpp
impersonate command
[blank.git] / src / shared / CLIContext.hpp
1 #ifndef BLANK_SHARED_CLICONTEXT_HPP_
2 #define BLANK_SHARED_CLICONTEXT_HPP_
3
4 #include <string>
5
6
7 namespace blank {
8
9 class Player;
10 class Entity;
11
12 class CLIContext {
13
14 public:
15         /// Create context with optional player and entity
16         /// entity defaults to the player's if given
17         /// Associated player or entity can be changed during
18         /// the context's lifetime and will assume the original
19         /// values when reset.
20         explicit CLIContext(Player *p = nullptr, Entity *e = nullptr);
21
22         /// get a best name for this context
23         std::string Name() const;
24
25         /// check if this context associates a player
26         bool HasPlayer() const noexcept { return effective_player; }
27         /// get the player responsible for all this
28         /// only valid if HasPlayer() returns true
29         Player &GetPlayer() noexcept { return *effective_player; }
30         const Player &GetPlayer() const noexcept { return *effective_player; }
31         /// change the effective player of this context
32         /// note that this will *not* change the effective entity
33         void SetPlayer(Player &p) noexcept { effective_player = &p; }
34
35         /// check if this context associates an entity
36         bool HasEntity() const noexcept { return effective_entity; }
37         /// get the entity on which operations should be performed
38         /// only valid if HasPlayer() returns true
39         Entity &GetEntity() noexcept { return *effective_entity; }
40         const Entity &GetEntity() const noexcept { return *effective_entity; }
41         /// change the effective player of this context
42         void SetEntity(Entity &e) noexcept { effective_entity = &e; }
43
44         /// reset effective player and entity to their original values
45         void Reset() noexcept {
46                 effective_player = original_player;
47                 effective_player = original_player;
48         }
49
50         /// an error has happened and the player should be notified
51         virtual void Error(const std::string &) = 0;
52
53         /// return to sender
54         /// use this for output concerning the originator of a command
55         virtual void Message(const std::string &) = 0;
56
57         /// send a status message to all players
58         /// use this to announce stuff which may be interesting to anyone
59         virtual void Broadcast(const std::string &) = 0;
60
61 private:
62         Player *original_player;
63         Player *effective_player;
64         Entity *original_entity;
65         Entity *effective_entity;
66
67 };
68
69 }
70
71 #endif