-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsgohome_info.py
More file actions
371 lines (321 loc) · 11.7 KB
/
csgohome_info.py
File metadata and controls
371 lines (321 loc) · 11.7 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import math
from pathlib import Path
from PIL import Image, ImageDraw
from gsuid_core.logger import logger
from gsuid_core.utils.image.convert import convert_img
from gsuid_core.utils.image.image_tools import easy_paste, draw_pic_with_ring
from .utils import save_img
from .csgo_path import TEXTURE
from ..utils.csgo_api import pf_api
from ..utils.csgo_font import csgo_font_20, csgo_font_30, csgo_font_42
from ..utils.api.models import UserFall, UserHomedetailData
from ..utils.error_reply import not_msg, get_error
# TEXTURE = Path(__file__).parent / "texture2d"
FONT_PATH = Path(__file__).parent / "font/萝莉体 第二版.ttf"
async def get_csgohome_info_img(uid: str, friend: bool = False):
data = await pf_api.get_csgohomedetail(uid)
fall = await pf_api.get_fall(uid)
logger.debug(fall)
logger.debug(data)
if isinstance(data, int):
return get_error(data)
if isinstance(fall, int):
return get_error(fall)
if friend:
return data["data"]["friendCode"]
if data["data"]["hotMaps"] is None or len(data["data"]["hotMaps"]) == 0:
return not_msg
return await draw_csgohome_info_img(data["data"], fall["result"])
async def draw_csgohome_info_img(
detail: UserHomedetailData, fall: UserFall
) -> bytes | str:
if not detail:
return "token已过期"
# 提取用户信息
name = detail["nickName"]
uid = f"{detail['steamId'][:4]}********{detail['steamId'][12:]}"
avatar = detail["avatar"]
# 背景图
img = Image.open(TEXTURE / "base" / "bg.jpg")
img_bg = Image.open(TEXTURE / "bg" / "2.jpg").resize((1000, 2400))
new_alpha = Image.new("L", img_bg.size, 90)
img_bg_out = Image.merge("RGBA", img_bg.split()[:3] + (new_alpha,))
img.paste(img_bg_out, (0, 0), img_bg_out)
# 处理标题
titel_img = Image.open(TEXTURE / "base" / "title_bg.png")
head = await save_img(avatar, "avatar")
round_head = await draw_pic_with_ring(head, 100)
# 合成标题图像
easy_paste(titel_img, round_head, (112, 108), "cc")
await draw_title_info(titel_img, name, uid)
rank_scoce = detail["rank"]
# rank = await assign_rank(rank_scoce) # 如果需要处理排名
img.paste(titel_img, (0, 55), titel_img)
# 基础信息表
main1_img = Image.open(TEXTURE / "base" / "banner.png")
main1_draw = ImageDraw.Draw(main1_img)
main1_draw.text((50, 10), "基础", (255, 255, 255, 255), csgo_font_42)
img.paste(main1_img, (0, 350), main1_img)
# 主信息
main2_img = Image.open(TEXTURE / "base" / "basehome_bg.png")
main2_draw = ImageDraw.Draw(main2_img)
main2_draw.text(
(260, 80),
f"{detail['hours']:.1f}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(415, 80),
f"{detail['rating']:.2f}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(570, 80),
f"{detail['rws']:.2f}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(725, 80),
f"{detail['adr']:.2f}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(880, 80),
f"{detail['entryKillRatio'] * 100:.2f}%",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(105, 230), str(rank_scoce), (255, 255, 255, 255), csgo_font_42, "mm"
)
main2_draw.text(
(260, 230),
str(detail["cnt"]),
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(415, 230),
f"{detail['winRate'] * 100:.1f}%",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(570, 230),
f"{detail['kd']:.2f}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(725, 230),
f"{detail['headShotRatio'] * 100:.1f}%",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
main2_draw.text(
(880, 230),
f"{detail['awpKillRatio']}",
(255, 255, 255, 255),
csgo_font_42,
"mm",
)
img.paste(main2_img, (0, 410), main2_img)
# 地图信息表
main1_img = Image.open(TEXTURE / "base" / "banner.png")
main1_draw = ImageDraw.Draw(main1_img)
main1_draw.text((50, 10), "地图", (255, 255, 255, 255), csgo_font_42)
img.paste(main1_img, (0, 780), main1_img)
# 地图
for i in range(min(4, len(detail["hotMaps"]))):
usr_map = detail["hotMaps"][i]
# map_layer = Image.new("RGBA", (480, 180), (0, 0, 0, 0))
# 750*480
map_img = (await save_img(usr_map["mapImage"], "map")).resize((470, 180))
new_alpha = Image.new("L", map_img.size, 128)
map_img = Image.merge("RGBA", (map_img.split()[:3] + (new_alpha,)))
map_logo = (await save_img(usr_map["mapLogo"], "map")).resize((50, 50))
easy_paste(map_img, map_logo, (40, 60), "cc")
rank_png = f"{usr_map['rank'] if usr_map['rank'] is not None else '0'}.png"
rank_img = (
Image.open(TEXTURE / "rank_gp" / rank_png).resize((100, 40)).convert("RGBA")
)
easy_paste(map_img, rank_img, (70, 120), "cc")
map_draw = ImageDraw.Draw(map_img)
map_draw.text(
(110, 60),
usr_map["mapName"],
(255, 255, 255, 255),
csgo_font_20,
"mm",
)
map_draw.text(
(180, 80),
str(usr_map["totalMatch"]),
(255, 255, 255, 255),
csgo_font_30,
"mm",
)
map_draw.text((180, 120), "场次", (255, 255, 255, 255), csgo_font_20, "mm")
map_draw.text(
(260, 80),
f"{usr_map['winCount'] / usr_map['totalMatch'] * 100:.1f}%",
(255, 255, 255, 255),
csgo_font_30,
"mm",
)
map_draw.text((260, 120), "胜率", (255, 255, 255, 255), csgo_font_20, "mm")
avg_rat = usr_map["ratingSum"] / usr_map["totalMatch"]
avg_map_adr = usr_map["totalAdr"] / usr_map["totalMatch"]
map_draw.text(
(380, 80),
f"{avg_rat:.2f} / {avg_map_adr:.0f}",
(255, 255, 255, 255),
csgo_font_30,
"mm",
)
map_draw.text((380, 120), "RT/ADR", "white", csgo_font_20, "mm")
if i % 2 == 0:
site_x = 20
else:
site_x = 520
site_y = 1090 + 200 * (i // 2 - 1)
img.paste(map_img, (site_x, site_y), map_img)
# 武器信息表
main3_img = Image.open(TEXTURE / "base" / "banner.png")
main3_draw = ImageDraw.Draw(main3_img)
main3_draw.text((50, 10), "武器", (255, 255, 255, 255), csgo_font_42)
img.paste(main3_img, (0, 1300), main3_img)
# 武器
for i in range(min(8, len(detail["hotWeapons"]))):
usr_weapon = detail["hotWeapons"][i]
base_img = Image.open(TEXTURE / "base" / "weapon_bg.png").resize((500, 110))
weapon_img = await save_img(usr_weapon["weaponImage"], "weapon")
weapon_img = weapon_img.resize(
(int(weapon_img.size[0] * 0.2), int(weapon_img.size[1] * 0.2))
)
easy_paste(base_img, weapon_img, (100, 70), "cc")
weapon_draw = ImageDraw.Draw(base_img)
weapon_draw.text(
(77, 17),
usr_weapon["weaponName"],
(255, 255, 255, 255),
csgo_font_20,
"mm",
)
weapon_draw.text(
(204, 60),
str(usr_weapon["weaponKill"]),
(255, 255, 255, 255),
csgo_font_20,
"mm",
)
# weapon_draw.text(
# (375, 60),
# f"{usr_weapon['avgTimeToKill']}ms",
# (255, 255, 255, 255),
# csgo_font_20,
# "mm",
# )
# if usr_weapon['sprayAccuracy'] is None:
# weapon_draw.text(
# (285, 60),
# f"{usr_weapon['firstShotAccuracy']* 100:.2f}%",
# (255, 255, 255, 255),
# csgo_font_20,
# "mm",
# )
# else:
# weapon_draw.text(
# (285, 60),
# f"{usr_weapon['sprayAccuracy']* 100 :.2f}%",
# (255, 255, 255, 255),
# csgo_font_20,
# "mm",
# )
avg_heat = usr_weapon["weaponHeadShot"] / usr_weapon["weaponKill"]
hdr = f"{avg_heat * 100:.2f}" if usr_weapon["weaponKill"] is not None else 0
weapon_draw.text((430, 31), f"{hdr}", "white", csgo_font_20, "mm")
if i % 2 == 0:
site_x = 0
else:
site_x = 500
site_y = 1510 + 120 * (i // 2 - 1)
img.paste(base_img, (site_x, site_y), base_img)
# 天梯段位
main4_img = Image.open(TEXTURE / "base" / "banner.png")
main4_draw = ImageDraw.Draw(main4_img)
main4_draw.text((50, 10), "分数曲线", (255, 255, 255, 255), csgo_font_42)
img.paste(main4_img, (0, 1880), main4_img)
score_list = detail["historyRatings"]
if len(score_list) > 0:
score_list = score_list[:10]
# 获取最大值和最小值
max_score = max(score_list)
min_score = min(score_list)
# 向下取整和向上取整到十的倍数
y_start = (min_score // 10) * 10 # 向下取整
y_end = ((max_score // 10) + 1) * 10 # 向上取整
# 图像设置
width = 700
height = 480
padding = 100
img_line = Image.new("RGBA", (width, height), (255, 255, 255, 0))
draw = ImageDraw.Draw(img_line)
scale = (height - 2 * padding) / (y_end - y_start)
# 绘制坐标轴
draw.line(
[(padding, height - padding), (width - padding, height - padding)],
fill="black",
)
draw.line([(padding, padding), (padding, height - padding)], fill="black")
# 标注 Y 轴
for i in range(math.ceil(y_start), math.ceil(y_end) + 1, 10):
y = height - padding - (i - y_start) * scale
draw.line([(padding - 5, y), (padding, y)], fill="black")
draw.text((padding - 50, y - 15), str(i), fill="white", font=csgo_font_20)
# 标注 X 轴
for i in range(len(score_list)):
x = padding + i * (width - 2 * padding) / (len(score_list) - 1)
draw.line(
[(x, height - padding), (x, height - padding + 5)],
fill="black",
)
draw.text(
(x - 10, height - padding + 10),
str(i),
fill="white",
font=csgo_font_20,
)
# 绘制折线和数据点
points = []
for i, score in enumerate(reversed(score_list)):
x = padding + i * (width - 2 * padding) / (len(score_list) - 1)
y = height - padding - (score - y_start) * scale
points.append((x, y))
draw.ellipse((x - 6, y - 6, x + 6, y + 6), fill="blue") # 使用蓝色圆形点
draw.text((x - 15, y - 30), str(score), fill="white", font=csgo_font_20)
# 绘制折线
draw.line(points, fill="yellow", width=3)
img.paste(img_line, (0, 1900), img_line)
img_up = Image.open(TEXTURE / "base" / "footer.png")
img.paste(img_up, (0, 2340), img_up)
return await convert_img(img)
async def draw_title_info(titel_img: Image.Image, name: str, uid: str) -> None:
"""在标题图像上绘制用户信息"""
head_draw = ImageDraw.Draw(titel_img)
head_draw.text((250, 50), name, (255, 255, 255, 255), csgo_font_42)
head_draw.text((250, 100), uid, (255, 255, 255, 255), csgo_font_30)
head_draw.line([(250, 150), (550, 150)], fill="white", width=2)