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
src
ai
src
main.py
Go to the documentation of this file.
1
7
8
import
sys
9
10
from
tcp_server
import
TCPServer
11
from
custom_functions
import
pinfo, psuccess, perror, pdebug, pwarning
12
from
global_variables
import
GlobalVariables, MAX_PORT_RANGE, MIN_PORT_RANGE
13
14
15
class
Main
:
16
"""
17
This is the main class of the program.
18
This class is responsible for calling the three main steps of the program.
19
These steps are:
20
* Initialisation:
21
* Check arguments
22
* Create listener on a new thread
23
* Create sender on a new thread
24
* Launch mainloop (skipped if error occurs before this step)
25
* When the program ends:
26
* If the reason was an error:
27
* Display the error
28
* Free any allocated ressource that was not freed
29
* Exit with the status contained in GlobalVariables
30
"""
31
32
def
__init__
(self, error: int = 84, success: int = 0) ->
None
:
33
self.
argc
: int = len(sys.argv)
34
self.error: int = error
35
self.success: int = success
36
self.help_options: list[str] = [
37
"-h"
,
"-help"
,
"-?"
,
38
"--h"
,
"--help"
,
"--?"
,
39
"/h"
,
"/help"
,
"/?"
40
]
41
self.
server
: TCPServer =
None
42
self.
global_variables
: GlobalVariables =
None
43
44
def
__del__
(self) -> None:
45
"""
46
Function in charge of unloading the colour library when the class is destroyed.
47
"""
48
if
self.
global_variables
is
None
:
49
return
50
if
self.
global_variables
.colourise_output
is
None
:
51
return
52
self.
global_variables
.colourise_output.display(
53
"0A"
,
54
(),
55
"Unloading ressources\n"
56
)
57
self.
global_variables
.colourise_output.display(
58
"rr"
,
59
(),
60
"End of script\n"
61
)
62
self.
global_variables
.colourise_output.unload_ressources()
63
self.
global_variables
.colourise_error.unload_ressources()
64
65
def
_process_arguments
(self) -> int:
66
"""
67
_summary_
68
This function is used to check the arguments provided by argv.
69
They will be used to define some parameters that user could specify.
70
This function is also the one in charge of displaying the help section when required.
71
"""
72
if
(self.
argc
not
in
(7, 8)):
73
print(
"USAGE: ./zappy_ai -p port -n name -h machine"
)
74
if
self.
argc
== 2
and
sys.argv[1]
in
self.help_options:
75
return
self.success
76
return
self.error
77
78
if
self.
argc
== 7
or
self.
argc
== 8:
79
if
sys.argv[1].lower() ==
"-p"
:
80
if
sys.argv[2].isnumeric()
is
False
:
81
print(
82
f
"Error: The -p flag must be a positive whole number that ranges between {MIN_PORT_RANGE} and {MAX_PORT_RANGE}\nPlease run {sys.argv[0]} --help"
83
)
84
return
self.error
85
port = int(sys.argv[2])
86
if
port < MIN_PORT_RANGE
or
port > MAX_PORT_RANGE:
87
print(
88
f
"Error: The -p flag must be a positive whole number that ranges between {MIN_PORT_RANGE} and {MAX_PORT_RANGE}\nPlease run {sys.argv[0]} --help"
89
)
90
return
self.error
91
else
:
92
print(
93
f
"Error: The -p flag is missing\nPlease run {sys.argv[0]} --help"
94
)
95
return
self.error
96
if
sys.argv[3].lower() ==
"-n"
:
97
name = sys.argv[4]
98
else
:
99
print(
100
f
"Error: The -n flag is missing\nPlease run {sys.argv[0]} --help"
101
)
102
return
self.error
103
if
sys.argv[5].lower() ==
"-h"
:
104
host = sys.argv[6]
105
else
:
106
print(
107
f
"Error: The -h flag is missing\nPlease run {sys.argv[0]} --help"
108
)
109
return
self.error
110
if
self.
argc
== 8
and
sys.argv[7].lower() ==
"-d"
:
111
debug =
True
112
else
:
113
debug =
False
114
print(
115
f
"The only flag allowed in the last position is: -d\nYou entered: {sys.argv[7]}"
116
)
117
return
self.error
118
return
{
"port"
: port,
"name"
: name,
"host"
: host,
"debug"
: debug}
119
return
self.error
120
121
def
_check_team_name
(self) -> None:
122
"""_summary_
123
Send the current team name to the server.
124
"""
125
user_name = self.
global_variables
.user_arguments.name
126
if
user_name ==
""
:
127
pwarning(self.
global_variables
,
"The AI name is empty"
)
128
pdebug(
129
self.
global_variables
,
130
f
"Team name is: {self.global_variables.user_arguments.name}"
131
)
132
133
def
main
(self) -> int:
134
"""
135
_sumary_
136
The main function of the program.
137
This function is the one in charge of:
138
* Parsing the arguments
139
* Creating the threads
140
* Starting the threads
141
"""
142
response = self.
_process_arguments
()
143
if
isinstance(response, int):
144
if
response != self.success:
145
print(
146
f
"One or more errors have occurred\nPlease run {sys.argv[0]} --help for more information"
147
)
148
return
response
149
self.
global_variables
=
GlobalVariables
(
150
error=self.error,
151
success=self.success,
152
ip=response[
"host"
],
153
port=response[
"port"
],
154
name=response[
"name"
],
155
debug=response[
"debug"
]
156
)
157
self.
global_variables
.colourise_output.init_pallet()
158
self.
global_variables
.colourise_error.init_pallet()
159
self.
global_variables
.colourise_output.display(
160
self.
global_variables
.message_colours.DEFAULT,
161
(),
162
""
163
)
164
self.
_check_team_name
()
165
166
pinfo(self.
global_variables
,
"Main class loaded"
)
167
pinfo(self.
global_variables
,
"Loading Server"
)
168
self.
server
=
TCPServer
(self.
global_variables
)
169
psuccess(self.
global_variables
,
"Server is loaded"
)
170
pinfo(self.
global_variables
,
"Starting server"
)
171
status = self.
server
.run()
172
if
status != self.success:
173
perror(
174
self.
global_variables
,
175
f
"Server stopped with status: {status}"
176
)
177
return
status
178
psuccess(self.
global_variables
,
"Server stopped without any errors"
)
179
return
status
180
181
182
if
__name__ ==
"__main__"
:
183
ERROR = 84
184
SUCCESS = 0
185
IP =
"0.0.0.0"
186
PORT = 8080
187
MI =
Main
(
188
error=ERROR,
189
success=SUCCESS
190
)
191
sys.exit(MI.main())
global_variables.GlobalVariables
Definition
global_variables.py:185
main.Main
Definition
main.py:15
main.Main._process_arguments
int _process_arguments(self)
Definition
main.py:65
main.Main._check_team_name
None _check_team_name(self)
Definition
main.py:121
main.Main.global_variables
global_variables
Definition
main.py:127
main.Main.__del__
None __del__(self)
Definition
main.py:44
main.Main.server
server
Definition
main.py:168
main.Main.__init__
None __init__(self, int error=84, int success=0)
Definition
main.py:32
main.Main.argc
argc
Definition
main.py:74
tcp_server.TCPServer
Definition
tcp_server.py:16
main
Definition
main.py:1