-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayerSession.py
126 lines (106 loc) · 4.41 KB
/
PlayerSession.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from ServerConnection import ServerConnection
from Aliases import jsonDict
from Exceptions import BadCommandException, AccessDeniedException, InappropriateGameStateException, TimeoutException, InternalServerErrorException
from Constants import Result
class PlayerSession:
__slots__ = ("name", "password", "connection", "__errorMapping") # class members
def __init__(self, name, password):
self.name = name
self.password = password
self.__errorMapping = {
Result.BAD_COMMAND.value: BadCommandException,
Result.ACCESS_DENIED.value: AccessDeniedException,
Result.INAPPROPRIATE_GAME_STATE.value: InappropriateGameStateException,
Result.TIMEOUT.value: TimeoutException,
Result.INTERNAL_SERVER_ERROR.value: InternalServerErrorException
}
def __enter__(self):
self.connection = ServerConnection()
return self
def __handleResult(self, result):
"""
Handles error codes and returns data if request was successful
:param result: result dict from action request with "resultCode" and "data" keys
:return: result["data"] if the result was okay,
Exception is raised for error types.
"""
code = result["resultCode"]
if code != Result.OKAY:
raise self.__errorMapping[code](result["data"]["error_message"])
return result["data"]
def login(self, data: jsonDict) -> int:
"""
Logs the player to the game server.
:return: player id if login was successful, -1 otherwise
"""
data["name"] = self.name
if self.password:
data["password"] = self.password
result = self.__handleResult(self.connection.login(data))
return int(result["idx"])
def logout(self):
"""
Logs out the player and removes the player's record from the server storage.
"""
self.connection.logout()
def nextTurn(self):
"""
Sends a TURN action, which forces the next turn of the game.
This allows players to play faster and not wait for the game's time slice.
The game's time slice is 10 seconds for test battles and 1 second for final battles. All players and observers
must send the TURN action before the next turn can happen.
"""
return self.__handleResult(self.connection.turn())
def getMapInfo(self) -> jsonDict:
"""
Returns the game map. Map represents static information about the game.
:return: data about the map
"""
return self.__handleResult(self.connection.map())
def getGameActions(self) -> jsonDict:
"""
Gets a list of game actions that happened in the previous turn, representing changes between turns.
:return: data about the game actions
"""
return self.__handleResult(self.connection.game_actions())
def sendChatMessage(self, message):
"""
Do nothing. Just for testing and fun.
:param message: message to be sent
"""
data = dict()
data["message"] = message
self.__handleResult(self.connection.chat(data))
def getGameState(self) -> jsonDict:
"""
Returns the current state of the game. The game state represents dynamic information about the game.
The game state is updated at the end of a turn.
:return: dictionary with game state
"""
return self.__handleResult(self.connection.game_state())
def move(self, data) -> jsonDict:
"""
Changes vehicle position.
:param data:
The server expects to receive the following required values:
vehicle_id - id of vehicle.
target - coordinates of hex.
"""
return self.__handleResult(self.connection.move(data))
def shoot(self, data):
"""
Shoot to target position.
:param data:
The server expects to receive following required values:
vehicle_id - id of vehicle.
target - coordinates of hex.
example: {"vehicle_id":5,"target":{"x":-1,"y":1,"z":0}}
For AT-SPG target means the direction of shooting,
and it must be neighboring the AT-SPG's position hex.
"""
return self.__handleResult(self.connection.shoot(data))
def __exit__(self, *args):
"""
Close connection to the server socket on exit
"""
self.connection.close()