-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence.py
178 lines (147 loc) · 6 KB
/
presence.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
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
import time, argparse, json, configparser, pprint
from rapidfuzz import fuzz, process
from pypresence import Presence
# Functions
def lower_if_exists(lie_argument):
if lie_argument:
lie_output = lie_argument.lower()
else:
lie_output = lie_argument
return lie_output
def fuzzy_error(fe_string, fe_list, fe_thresh):
if fe_string != None:
return process.extractOne(fe_string, fe_list, score_cutoff=fe_thresh) is None
def presence_gen():
# Setup default values
pg_presencedict = {}
pg_presencedict["party_size"] = []
pg_presencedict["start"] = start_time
pg_formatdict = {}
pg_formatdict["map_image"] = ""
pg_formatdict["map_hf"] = ""
pg_formatdict["difficulty_hf"] = ""
pg_formatdict["variation_hf"] = ""
pg_formatdict["variation_image"] = ""
pg_formatdict["icon"] = "icon"
pg_coop = coop
if map:
pg_map = process.extractOne(map, list(assets["maps"].keys()))[0]
pg_formatdict["map_image"] = assets["maps"][pg_map]["image"]
pg_formatdict["map_hf"] = assets["maps"][pg_map]["name_hf"]
if difficulty:
pg_difficulty = difficulty
pg_formatdict["difficulty_hf"] = assets["difficulties"][pg_difficulty][
"name_hf"
]
if variation:
pg_variation = process.extractOne(
variation, list(assets["difficulties"][difficulty]["variations"].keys())
)[0]
pg_formatdict["variation_hf"] = assets["difficulties"][difficulty][
"variations"
][pg_variation]["name_hf"]
pg_formatdict["variation_image"] = assets["difficulties"][difficulty][
"variations"
][pg_variation]["image"]
else:
pg_variation = "standard"
pg_formatdict["variation_hf"] = assets["difficulties"][difficulty][
"variations"
][pg_variation]["name_hf"]
pg_formatdict["variation_image"] = assets["difficulties"][difficulty][
"variations"
][pg_variation]["image"]
for pg_field in list(config["No Map, No Difficulty"].keys()):
pg_presencedict[pg_field] = config["No Map, No Difficulty"][pg_field].format(
**pg_formatdict
)
if map and difficulty:
for pg_field in list(config["Map, Difficulty"].keys()):
pg_presencedict[pg_field] = config["Map, Difficulty"][pg_field].format(
**pg_formatdict
)
if map and not difficulty:
for pg_field in list(config["Map, No Difficulty"].keys()):
pg_presencedict[pg_field] = config["Map, No Difficulty"][pg_field].format(
**pg_formatdict
)
if pg_coop:
for pg_field in list(config["Co-Op"].keys()):
pg_presencedict[pg_field] = config["Co-Op"][pg_field].format(
**pg_formatdict
)
pg_presencedict["party_size"] = pg_coop
pg_presencedict["start"] = None
for pg_field in list(pg_presencedict.keys()):
if pg_presencedict[pg_field] == "" or pg_presencedict[pg_field] == []:
pg_presencedict[pg_field] = None
return pg_presencedict
# Parsers
# Parse the arguments. Note that -m and -v do not have to be exact.
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--map", help="Map you are playing.")
parser.add_argument("-d", "--difficulty", help="Difficulty you are playing at.")
parser.add_argument("-v", "--variation", help="Difficulty variation.")
parser.add_argument("-c", "--coop", help="Number of players in your co-op game.")
args = parser.parse_args()
# Parse the config file.
config = configparser.ConfigParser()
config.read("config.ini")
# Parse the json file.
assets = json.load(open("assets.json", "r"))
# If the argument exists, then make it lowercase to normalize.
map = lower_if_exists(args.map)
difficulty = lower_if_exists(args.difficulty)
variation = lower_if_exists(args.variation)
if args.coop:
coop = [int(args.coop), 4]
else:
coop = args.coop
if args.coop:
coop = [int(args.coop), 4]
else:
coop = args.coop
# Load the data for each map and difficulty from the json.
assets = json.load(open("assets.json", "r"))
# Set up some basic errors if there are invalid inputs.
if difficulty and (map == None): # Cannot have a difficulty without a map
parser.error("--difficulty requires --map.")
if variation and (difficulty == None): # Cannot have a variation without a difficulty
parser.error("--variation requires --difficulty.")
if coop and (map == None):
parser.error("--coop requires --map.") # Cannot have Co-Op without a map
# For map and variation, fuzzywuzzy will check if the closest match in the list has a score of lower than 75.
# If it does, it will return an error.
if fuzzy_error(map, list(assets["maps"].keys()), 75):
parser.error("invalid map.")
if difficulty:
if difficulty not in list(assets["difficulties"].keys()):
parser.error("invalid difficulty.")
if fuzzy_error(
variation, list(assets["difficulties"][difficulty]["variations"].keys()), 75
):
parser.error("invalid variation.")
if coop:
if not (1 <= coop[0] <= 4):
parser.error("invalid co-op player count.")
# Setting up the rich presence.
client_id = config["Rich Presence"]["client_id"]
RPC = Presence(client_id)
RPC.connect()
RPC.update(large_image="icon", large_text="Bloons TD 6", details="In Menu")
start_time = time.time()
# Check through different cases and run the one that fits.
pvars = presence_gen()
pprint.pprint(pvars)
while True:
RPC.update(
large_image=pvars["large_image"],
large_text=pvars["large_text"],
small_image=pvars["small_image"],
small_text=pvars["small_text"],
details=pvars["details"],
state=pvars["state"],
party_size=pvars["party_size"],
start=pvars["start"],
)
time.sleep(5)