This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathformatter.py
131 lines (104 loc) · 4.13 KB
/
formatter.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
127
128
129
130
131
import logging as log
import os
import urllib
import credentials
atk_dur_template = "{atk}/{dur}"
subtype_template = " {subType}"
desc_template = " | {desc}"
extDesc_template = "[{text}] \n"
card_template = ("* **[{name}]({cdn})** {class} {type} {rarity} {set} {std} "
"^[HP](https://www.hearthpwn.com/cards/{hpwn}), "
"^[TD](https://www.hearthstonetopdecks.com/cards/{head}/), "
"^[W](https://hearthstone.gamepedia.com/{wiki}) \n"
"{cost}/{atk_dur}{subtype}{desc} \n{extDesc}")
signature = ("\n^(Call/)^[PM](https://www.reddit.com/message/compose/?to={bot})"
" ^( me with up to 7 [[cardname]]. )"
"^[About.](https://www.reddit.com/message/compose/"
"?to={bot}&message=Tell%20me%20more%20[[info]]&subject=hi)") \
.format(bot=credentials.username)
duplicate_header_templ = ("You've posted a comment reply in [{title}]({url}) "
"containing cards I already explained. "
"To reduce duplicates, your cards are here:\n\n")
# Standard legal icon
# 2023 Year of the Wolf
STD_ICON = '\U0001F43A'
# unreleased sleep
NEXT_STD_ICON = '\U0001F4A4'
# crossed swords
DUELS_ICON = '\U00002694'
def createCardText(card, constants):
""" formats a single card to text """
cardSet = card['set']
cardSetData = constants.sets[constants.setIds[cardSet]]
cardSetCode = cardSetData.get('code')
cost = card['cost']
atk = card['atk']
dur = card['hp']
if cost is None: cost = '-'
if atk is None: atk = '-'
if dur is None: dur = '-'
atk_dur = atk_dur_template.format(atk=atk, dur=dur)
cardDesc = card['desc']
extDesc = card.get('extDesc', '')
if extDesc:
extDesc = ''.join(extDesc_template.format(text=desc) for desc in extDesc)
local_card = {
'name': card['name'],
'type': card['type'],
'class': card['class'],
'rarity': card['rarity'],
'set': cardSetCode or cardSet,
'cost': cost,
'desc': desc_template.format(desc=cardDesc) if cardDesc else '',
'extDesc': extDesc,
'hpwn': card['hpwn'],
'head': card['head'],
'wiki': urllib.parse.quote(card['name'].replace(' ', '_')),
'cdn': card['cdn'],
'atk_dur': atk_dur,
'subtype': subtype_template.format(subType=card['subType']) \
if card['subType'] else '',
'std': DUELS_ICON if cardSetData.get('duels') else \
STD_ICON if cardSetData.get('std') else \
NEXT_STD_ICON if cardSetData.get('unreleased') else ''
}
return card_template.format(**local_card)
def createAnswer(cardDB, cards):
"""gets card formatted card text and signature and joins them"""
comment_text = ''
for card in cards:
log.debug('adding card to text: %s', card)
comment_text += cardDB[card]
if comment_text:
comment_text += signature
return comment_text
def createDuplicateMsg(title, url):
"""message header for duplicate comment requests"""
return duplicate_header_templ.format(title=title, url=url)
def loadInfoTempl(specials=[], alts=[], tokens=[], duelsPrefix='d!', vanillaPrefix='v!', *, infoMsgTmpl='data/info_msg.templ'):
""" reads and prepares [[info]] template,
{user} will remain for later formatting
"""
if not os.path.isfile(infoMsgTmpl):
return ''
rawTemplate = ''
with open(infoMsgTmpl, 'r', encoding="utf8") as file:
rawTemplate = file.read()
# sets to list and sort them all
specials = list(specials)
specials.sort()
alts = list(alts)
alts.sort()
tokens = list(tokens)
tokens.sort()
# join lists together
comma = ', '
specialText = comma.join(specials)
altsText = comma.join(alts)
tokensText = comma.join(tokens)
return rawTemplate.format(user='{user}',
alts=altsText,
tokens=tokensText,
special=specialText,
duelsPrefix=duelsPrefix,
vanillaPrefix=vanillaPrefix)