Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
global_variables.py
Go to the documentation of this file.
7
8"""
9 _summary_
10 This file (constants.py) is in charge of storing any variable that needs to be shared to different classes and not just be a copy
11"""
12
13from sys import stderr
14from display_tty import Disp, TOML_CONF, OSTRING
15from colourise_output import ColouriseOutput
16
17
18# These define the maximum and minimum port ranges for the tcp protocol
19MAX_PORT_RANGE = 9000
20MIN_PORT_RANGE = 1024
21
22# Disabeling animation
23TOML_CONF['PRETTIFY_OUTPUT'] = False
24TOML_CONF['OUTPUT_MODE'] = OSTRING
25
26
28 """_summary_
29 This is a class containing the translations enum data
30 From command to string and string to command
31 etc
32 """
33
34 def __init__(self) -> None:
36 "Forward": Commands.FORWARD,
37 "Right": Commands.RIGHT,
38 "Left": Commands.LEFT,
39 "Look": Commands.LOOK,
40 "Inventory": Commands.INVENTORY,
41 "Broadcast": Commands.BROADCAST_TEXT,
42 "Connect_nbr": Commands.CONNECT_NBR,
43 "Fork": Commands.FORK,
44 "Eject": Commands.EJECT,
45 "Take": Commands.TAKE_OBJECT,
46 "Set": Commands.SET_OBJECT,
47 "ok": Commands.OK,
48 "ko": Commands.KO,
49 "Incantation": Commands.INCANTATION,
50 "WELCOME": Commands.WELCOME,
51 "Dead": Commands.DEAD
52 }
54 Commands.FORWARD: "Forward",
55 Commands.RIGHT: "Right",
56 Commands.LEFT: "Left",
57 Commands.LOOK: "Look",
58 Commands.INVENTORY: "Inventory",
59 Commands.BROADCAST_TEXT: "Broadcast",
60 Commands.CONNECT_NBR: "Connect_nbr",
61 Commands.FORK: "Fork",
62 Commands.EJECT: "Eject",
63 Commands.TAKE_OBJECT: "Take",
64 Commands.SET_OBJECT: "Set",
65 Commands.INCANTATION: "Incantation",
66 Commands.OK: "Ok",
67 Commands.KO: "Ko",
68 Commands.UNKNOWN: "None",
69 Commands.WELCOME: "Welcome",
70 Commands.DEAD: "Dead"
71 }
72
74 "food": Items.FOOD,
75 "linemate": Items.LINEMATE,
76 "deraumere": Items.DERAUMERE,
77 "sibur": Items.SIBUR,
78 "mendiane": Items.MENDIANE,
79 "phiras": Items.PHIRAS,
80 "thystame": Items.THYSTAME,
81 "player": Items.PLAYER
82 }
83
85 Items.FOOD: "food",
86 Items.LINEMATE: "linemate",
87 Items.DERAUMERE: "deraumere",
88 Items.SIBUR: "sibur",
89 Items.MENDIANE: "mendiane",
90 Items.PHIRAS: "phiras",
91 Items.THYSTAME: "thystame",
92 Items.PLAYER: "player",
93 Items.UNKNOWN: "None"
94 }
95
96
98 """
99 _sumary_
100 This is a python version of an enum.
101 This enum hosts a set of variables referencing the available commands for the exchange between the ai and the server
102 """
103 LEFT: int = 1
104 LOOK: int = 2
105 FORK: int = 3
106 RIGHT: int = 4
107 EJECT: int = 5
108 EMPTY: int = 6
109 FORWARD: int = 7
110 INVENTORY: int = 8
111 CONNECT_NBR: int = 9
112 SET_OBJECT: int = 10
113 TAKE_OBJECT: int = 11
114 INCANTATION: int = 12
115 BROADCAST_TEXT: int = 13
116 OK: int = 14
117 KO: int = 15
118 UNKNOWN: int = 16
119 WELCOME: int = 17
120 CLIENT_NUMBER: int = 18
121 GRAPHIC_MODE: int = 19
122 DEAD: int = 20
123
124
125class Items:
126 """
127 _summary_
128 This is a python version of an enum.
129 This enum hosts a set of variables referencing the available types of objects that can be gathered from the field
130 """
131 FOOD: int = 1
132 LINEMATE: int = 2
133 DERAUMERE: int = 3
134 SIBUR: int = 4
135 MENDIANE: int = 5
136 PHIRAS: int = 6
137 THYSTAME: int = 7
138 PLAYER: int = 8
139 UNKNOWN: int = 9
140
141
143 """
144 _summary_
145 This is the class in charge of storing data referring the information that the server needs in order to function
146 """
147
148 def __init__(self, ip: str = "0.0.0.0", port: int = 8080) -> None:
149 self.ip: str = ip
150 self.port: int = port
151 self.buffer_size: int = 1024*4
152 self.timeout: float = None # None
153 self.make_tcp_wait: bool = False
154 # f"AI land is live at {self.ip}:{self.port}"
155 self.startup_message: str = f"AI client is connecting to {self.ip}:{self.port}"
156
157
159 """
160 _summary_
161 This is the class in charge of storing the different colours that will be used for the messages
162 """
163 END = 'rr'
164 INFO = '0B'
165 ERROR = '0C'
166 DEBUG = '0D'
167 SUCCESS = '0A'
168 WARNING = '0E'
169 DEFAULT = "0F"
170
171
173 """
174 _summary_
175 This is the class in charge of storing the arguments passed by the user
176 """
177
178 def __init__(self, ip: str = "0.0.0.0", port: int = 8080, name: str = "No name", debug: bool = False) -> None:
179 self.host: str = ip
180 self.port: int = port
181 self.name: str = name
182 self.debug: bool = debug
183
184
186 """
187 _summary_
188 This is the class in charge of storing the variables that are to be globally synced throughout the program
189
190 Args:
191 success (int, optional): _description_. Defaults to 0.
192 error (int, optional): _description_. Defaults to 84.
193 ip (str, optional): _description_. Defaults to "0.0.0.0".
194 port (int, optional): _description_. Defaults to 8080.
195 name (str, optional): _description_. Defaults to "".
196 """
197
198 def __init__(self, success: int = 0, error: int = 84, ip: str = "0.0.0.0", port: int = 8080, name: str = "", debug: bool = False) -> None:
199 # Heartbeat variable, if it is set to false, the program stops
200 self.continue_running: bool = True
201
202 # Classes acting as enums
203 self.items: Items = Items()
204 self.commands: Commands = Commands()
205
206 # Bundle of variables relevant to the sections of the program containing the same name
207 self.server_data: ServerData = ServerData(
208 ip=ip,
209 port=port
210 ) # "0.0.0.0"
211 self.user_arguments = UserArguments(ip, port, name, debug)
212
213 # Status tracking
214 self.error: int = error
215 self.success: int = success
216 self.current_status: int = self.success
217
218 # server monitoring
219 self.tcp_ready: bool = False
220 self.tcp_status: int = self.success
221
222 # ai monitoring
223 self.ai_ready: bool = False
224 self.ai_status: int = self.success
225
226 # Handshake monitoring
227 self.initialisation_complete: bool = False
228
229 # Stall if response for the look command is expected
230 self.wait_look: bool = False
231
232 # The converted income from the server
233 self.current_buffer: list[dict[Commands, any]] = []
234 self.response_buffer: list[dict[Commands, any]] = []
235
236 # Message customisation desing
237 self.message_colours: MessageColours = MessageColours()
238 self.colourise_output: ColouriseOutput = ColouriseOutput()
239 self.colourise_error: ColouriseOutput = ColouriseOutput(stderr)
240 self.beautify: Disp = Disp(
241 toml_content=TOML_CONF,
242 save_to_file=False
243 )
244
245 # Translation reference
246 self.translation_reference: TranslationReference = TranslationReference()
None __init__(self, int success=0, int error=84, str ip="0.0.0.0", int port=8080, str name="", bool debug=False)
None __init__(self, str ip="0.0.0.0", int port=8080)
None __init__(self, str ip="0.0.0.0", int port=8080, str name="No name", bool debug=False)