1 /**********************************************************************
2  *
3  *   freedoko a Doppelkopf-Game
4  *
5  *   Copyright (C) 2001 – 2018 by Diether Knof and Borg Enders
6  *
7  *   This program is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU General Public License as
9  *   published by the Free Software Foundation; either version 2 of
10  *   the License, or (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *   You can find this license in the file 'gpl.txt'.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  *   MA  02111-1307  USA
22  *
23  *  Contact:
24  *    Diether Knof dknof@posteo.de
25  *
26  ********************************************************************/
27 
28 #include "constants.h"
29 
30 #include "game_status.h"
31 
32 // status of the current game
33 GameStatus game_status = GameStatus::programstart;
34 
35 /** -> result
36  **
37  ** @param    game_status   game status
38  **
39  ** @return   name of 'game_status'
40  **/
41 string
to_string(GameStatus const game_status)42 to_string(GameStatus const game_status) noexcept
43 {
44   switch (game_status) {
45   case GameStatus::programstart:
46     (void)_("GameStatus::program start");
47     return "program start";
48   case GameStatus::quit:
49     (void)_("GameStatus::quit");
50     return "quit";
51   case GameStatus::party_new:
52     (void)_("GameStatus::party new");
53     return "party new";
54   case GameStatus::party_initial_loaded:
55     (void)_("GameStatus::party initial loaded");
56     return "party initial loaded";
57   case GameStatus::party_loaded:
58     (void)_("GameStatus::party loaded");
59     return "party loaded";
60   case GameStatus::party_play:
61     (void)_("GameStatus::party play");
62     return "party play";
63   case GameStatus::party_finished:
64     (void)_("GameStatus::party finished");
65     return "party finished";
66   case GameStatus::game_new:
67     (void)_("GameStatus::game new");
68     return "game new";
69   case GameStatus::game_init:
70     (void)_("GameStatus::game init");
71     return "game init";
72   case GameStatus::game_cards_distribution:
73     (void)_("GameStatus::game cards distribution");
74     return "game cards distribution";
75   case GameStatus::game_manual_cards_distribution:
76     (void)_("GameStatus::game manual cards distribution");
77     return "game manual cards distribution";
78   case GameStatus::game_reservation:
79     (void)_("GameStatus::game reservation");
80     return "game reservation";
81   case GameStatus::game_redistribute:
82     (void)_("GameStatus::game redistribute");
83     return "game redistribute";
84   case GameStatus::game_poverty_shift:
85     (void)_("GameStatus::game poverty shift");
86     return "game poverty shift";
87   case GameStatus::game_start:
88     (void)_("GameStatus::game start");
89     return "game start";
90   case GameStatus::game_play:
91     (void)_("GameStatus::game play");
92     return "game play";
93   case GameStatus::game_full_trick:
94     (void)_("GameStatus::game full trick");
95     return "game full trick";
96   case GameStatus::game_trick_taken:
97     (void)_("GameStatus::game trick taken");
98     return "game trick taken";
99   case GameStatus::game_finished:
100     (void)_("GameStatus::game finished");
101     return "game finished";
102   case GameStatus::game_closed:
103     (void)_("GameStatus::game closed");
104     return "game closed";
105   } // switch(game_status)
106 
107   DEBUG_ASSERTION(false, "::to_string(GameStatus):\n"
108                          "  game status not found: "
109                              << static_cast<int>(game_status));
110 
111   return "";
112 } // string to_string(GameStatus game_status)
113 
114 /** -> result
115  **
116  ** @param    game_status   game status
117  **
118  ** @return   translation of the game status
119  **/
120 string
gettext(GameStatus const game_status)121 gettext(GameStatus const game_status)
122 {
123   return gettext("GameStatus::" + to_string(game_status));
124 }
125 
126 /** writes the gamestatus into the stream
127  **
128  ** @param    ostr          output stream
129  ** @param    game_status   game status
130  **
131  ** @return   output stream
132  **/
133 ostream &
operator <<(ostream & ostr,GameStatus const game_status)134 operator<<(ostream &ostr, GameStatus const game_status)
135 {
136   return (ostr << ::to_string(game_status));
137 }
138 
139 /** -> result
140  **
141  ** @param    lhs   left operand
142  ** @param    rhs   left operand, must be party or game
143  **
144  ** @return   whether the gamestatus is in a party or in a game
145  **/
operator &(GameStatus const lhs,GameStatus const rhs)146 bool operator&(GameStatus const lhs, GameStatus const rhs)
147 {
148   if (rhs == GameStatus::party) {
149     return (static_cast<int>(lhs) & static_cast<int>(rhs));
150   } else if (rhs == GameStatus::game) {
151     return (static_cast<int>(lhs) & static_cast<int>(rhs));
152   } else {
153     DEBUG_ASSERTION(false, "GameStatus & GameStatus: right hand side must be "
154                            "party or game but is " +
155                                to_string(rhs));
156   }
157   return false;
158 } // bool operator&(GameStatus lhs, GameStatus rhs)
159 
160 /** @result   whether the status is in a running game
161  **/
162 bool
in_running_game()163 in_running_game() noexcept
164 {
165   switch (::game_status) {
166   case GameStatus::game_play:
167   case GameStatus::game_full_trick:
168   case GameStatus::game_trick_taken:
169     return true;
170   default:
171     return false;
172   }
173 }
174