forked from magicksRampage/endless-war
-
Notifications
You must be signed in to change notification settings - Fork 26
/
page_player.py
112 lines (90 loc) · 2.94 KB
/
page_player.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
#!/usr/bin/python3
#
# page_player
# mperron (2019)
#
# Read a player's data from the endless-war database and present an HTML view.
import html
import sys
import ew.backend.core as bknd_core
import ew.static.cfg as ewcfg
from ew.backend.player import EwPlayer
from ew.utils import cmd as cmd_utils
from ew.utils.combat import EwUser
def faction(faction, life_state):
url = "<img src=/img/icons/{}.png style=\"height: 1em;\"> "
if life_state == 2:
if faction == "killers" or faction == "rowdys":
return url.format(faction)
elif life_state == 1:
return url.format("juveniles")
elif life_state == 0:
return url.format("ghost")
return ""
if len(sys.argv) >= 3:
id_server = sys.argv[1]
id_user = sys.argv[2]
else:
print("<p><i>Nothing but dust...</i></p>")
sys.exit(0)
print("<article class=story>")
user_data = EwUser(id_user=id_user, id_server=id_server)
player = EwPlayer(id_user=id_user)
# Floating profile section
print("<div class=profile_float><img src=\"{avatar_url}\" class=profile_avatar>".format(
avatar_url=player.display_avatar.url
))
print("<table>")
print("<tr><td>Faction</td><td>{icon}{faction}</td></tr>".format(
icon=faction(user_data.faction, user_data.life_state),
faction=user_data.faction
))
print("<tr><td>Slime</td><td>{slime:,}</td></tr>".format(slime=user_data.slimes))
print("<tr><td>SlimeCoin</td><td>{slimecoin:,}</td></tr>".format(slimecoin=user_data.slimecoin))
print("<tr><td>Bounty</td><td>{bounty:,}</td></tr>".format(bounty=int((user_data.bounty + 1) / ewcfg.slimecoin_exchangerate)))
print("</table>")
print("</div>")
# Header bar
print("<header><h2>{faction}<a href=player.html?pl={id_user}>{display_name}</a></h2></header>".format(
id_user=id_user,
display_name=html.escape(player.display_name),
faction=faction(user_data.faction, user_data.life_state)
))
# Main body
print("<div>")
print("<p>{}</p>".format(html.escape("".join(cmd_utils.gen_data_text(
id_user=id_user,
id_server=id_server,
display_name=player.display_name
)
))))
print("<h3>Statistics</h3>")
try:
conn_info = bknd_core.databaseConnect()
conn = conn_info.get('conn')
cursor = conn.cursor();
cursor.execute("SELECT {metric}, {value} FROM stats WHERE {id_server} = %s AND {id_user} = %s ORDER BY {metric}".format(
value=ewcfg.col_stat_value,
metric=ewcfg.col_stat_metric,
id_server=ewcfg.col_id_server,
id_user=ewcfg.col_id_user
), (
id_server,
id_user
))
print("<table style=\"display: inline-block;\">")
rows = cursor.fetchall()
for row in rows:
print("<tr><td>{metric}</td><td>{value:,}</td></tr>".format(
metric=row[0],
value=row[1]
))
print("</table>")
except:
print("<p><i>Failed to load stats.</i></p>");
finally:
# Clean up the database handles.
cursor.close()
bknd_core.databaseClose(conn_info)
print("</div>")
print("</article>")