This repository was archived by the owner on Apr 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetCustomAttributes.py
More file actions
74 lines (60 loc) · 2.49 KB
/
getCustomAttributes.py
File metadata and controls
74 lines (60 loc) · 2.49 KB
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
import base64
import json
def encodeObject(obj: dict) -> str:
"""Turns an object into a base64 encoded JSON string for easier transport to
New Relic.
"""
return base64.urlsafe_b64encode(json.dumps(obj).encode()).decode()
def getSnakeData(snake: dict) -> str:
"""Gets some useful information about a snake and encodes it. This is necessary to
keep the custom attribute size under 255bytes.
"""
return encodeObject({
"body": snake["body"],
"head": snake["head"],
"color": snake["customizations"]["color"]
})
def getCustomAttributes(data: dict) -> dict:
game = data["game"]
board = data["board"]
you = data["you"]
attributes = {
"snakeGameId": game["id"],
"snakeRules": game["ruleset"]["name"],
"snakeTurn": data["turn"],
"snakeBoardHeight": board["height"],
"snakeBoardWidth": board["width"],
"snakeBoardFood": encodeObject(board["food"]),
"snakeBoardHazards": encodeObject(board["hazards"]),
"snakeName": you["name"],
"snakeId": you["id"],
"snakeHealth": you["health"],
"snakeLength": you["length"],
"snakeData": getSnakeData(you)
}
opponents = [s for s in board["snakes"] if s["id"] != you["id"]]
for i, snake in enumerate(opponents):
attributes["snakeOpponent_{}_Name".format(i + 1)] = snake["name"]
attributes["snakeOpponent_{}_Id".format(i + 1)] = snake["id"]
attributes["snakeOpponent_{}_Health".format(i + 1)] = snake["health"]
attributes["snakeOpponent_{}_Length".format(i + 1)] = snake["length"]
attributes["snakeOpponent_{}_Data".format(i + 1)] = getSnakeData(snake)
return attributes
def getCustomAttributesEnd(data: dict) -> dict:
game = data["game"]
board = data["board"]
you = data["you"]
attributes = {
"snakeGameId": game["id"],
"snakeRules": game["ruleset"]["name"],
"snakeTurn": data["turn"],
"snakeName": you["name"],
"snakeId": you["id"],
"snakeHealth": you["health"],
"snakeLength": you["length"],
"snakeGameWinnerName": board["snakes"][0]["name"] if len(board["snakes"]) > 0 else None,
"snakeGameWinnerId": board["snakes"][0]["id"] if len(board["snakes"]) > 0 else None,
"snakeGameIsWin": board["snakes"][0]["name"] == you["name"] if len(board["snakes"]) > 0 else False,
"snakeGameReplayLink": "https://play.battlesnake.com/g/{id}".format(id=game["id"]),
}
return attributes