generated from NiklasEi/bevy_game_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.py
179 lines (119 loc) · 6.04 KB
/
scripts.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
179
path = "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/assets/all_puzzles_clean_ordered.json"
import json
from pathlib import Path
data = json.loads(Path(path).read_text())
# A list of dicts like this:
data[0]
# {'local_filename_map': 'Red Line.png',
# 'name': 'Red Line',
# 'solutions_url': 'http://www.trainyard.ca/solutions/redLine',
# 'city': 'Abbotsford Puzzles',
# 'parsed_map': '00 00 00 00 00 00 00\n00 00 00 Sr_r 00 00 00\n00 00 00 00 00 00 00\n00 00 00 00 00 00 00\n00 00 00 00 00 00 00\n00 00 00 E0010_r 00 00 00\n00 00 00 00 00 00 00',
# 'type_': 'Regular puzzles',
# 'trackCount': 'Best track count: 3+0',
# 'big_image_url': 'http://www.trainyard.ca//system/content/images/blueprintsByID/png/2798688_large.png',
# 'thumb': 'http://s3.amazonaws.com/TrainyardSiteMisc/images/puzzles/redLine_thumb.png'}
# Get all Disstinct values in the "type_" field
type_s = set([d['type_'] for d in data]) # {'Bonus puzzles', 'Express puzzles', 'Regular puzzles'}
# Reorder data like this:
# first, alternate dict of type_ 'Regular puzzles' and 'Express puzzles' (one by one)
# Then, append all 'Bonus puzzles'
data_reordered = []
# Get all 'Regular puzzles':
data_regular = [d for d in data if d['type_'] == 'Regular puzzles']
# Get all 'Express puzzles':
data_express = [d for d in data if d['type_'] == 'Express puzzles']
# Get all 'Bonus puzzles':
data_bonus = [d for d in data if d['type_'] == 'Bonus puzzles']
# Alternate between Regular and Express puzzles:
for i in range(max(len(data_regular), len(data_express))):
if i < len(data_regular):
data_reordered.append(data_regular[i])
if i < len(data_express):
data_reordered.append(data_express[i])
# Append all 'Bonus puzzles':
data_reordered.extend(data_bonus)
len(data_reordered)
names = [d['name'] for d in data_reordered]
###########################################################
########## [ // Lanten: potential exchange // Blue Boys: alternative simple join
# Read all_puzzles_ordered
path = "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/assets/all_puzzles_clean_ordered.json"
import json
from pathlib import Path
data_reordered = json.loads(Path(path).read_text())
string_template_to_format = """
PuzzleData {{
// "local_filename_map": "{local_filename_map}",
name: "{name}".to_string(),
// "solutions_url": "{solutions_url}",
city: "{city}".to_string(),
parsed_map: "{parsed_map}".to_string(),
type_: "{type_}".to_string(),
track_count: "{trackCount}".to_string(),
// "big_image_url": "{big_image_url}",
// "thumb": "{thumb}"
}},"""
new_codes = []
for d in data_reordered:
if d.pop('to_include_in_game'):
# Escape \n as \\n in d['parsed_map']:
d = {k: v.replace("\n", "\\n") for k, v in d.items()}
# Turn trackCount from 'Best track count: 3+0'in '3/0':
d['trackCount'] = d['trackCount'].replace('Best track count: ', '')
new_codes.append(string_template_to_format.format(**d))
new_code = "".join(new_codes)
# Read "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/src/all_puzzles_clean_template.rs" astext file:
path_template = "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/src/all_puzzles_clean_template.rs"
template = Path(path_template).read_text()
# Sobstitute "{/// HEREEEE PUT THEM HERE}" with new_code
template = template.replace("{/// HEREEEE PUT THEM HERE}", new_code)
# {PUT HERE LENGTH} with len(data_reordered)
template = template.replace("{PUT HERE LENGTH}", str(len(new_codes)))
# Write the new file to "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/src/all_puzzles_clean.rs"
path_new_file = "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/src/all_puzzles_clean.rs"
Path(path_new_file).write_text(template)
path_images = "/Users/michele.tasca/My Stuff/Coding stuff/Julia/Trainyard/assets"
import os
import shutil
for i, name in enumerate(names):
print(name)
print(f'')
i_formatted = str(i+1).zfill(3)
# save image named name as path intto "saved_images_ordered" folder, with name i_formatted_name.png
# (e.g. 001_Red Line.png)
output_path = f'{path_images}/saved_images_ordered_2/{i_formatted}_{name}.png'
input_path = f'{path_images}/level_images/{name}.png'
# !cp $input_path $output_path
# Use a library:
# os.system(f'cp {input_path} {output_path}')
# Or:
shutil.copyfile(input_path, output_path)
import json
from pathlib import Path
out_path = "/Users/michele.tasca/My Stuff/Coding stuff/OpenTrainyard/assets/all_puzzles_clean_ordered.json"
Path(out_path).write_text(json.dumps(data_reordered, indent=4))
################################################################
# Get all repeated names:
names_json = [d['name'] for d in data]
len(names_json)
len(names_rust)
def set_diff_stats(**kwargs):
assert len(kwargs) == 2, 'set_diff_stats() takes exactly 2 arguments'
(name_set1, name_set2), (set1, set2) = kwargs.keys(), kwargs.values()
set1, set2 = set(set1), set(set2)
print(f'len({name_set1})={len(set1)}', f'len({name_set2})={len(set2)}')
print(f'len({name_set1}.intersection({name_set2}))={len(set1.intersection(set2))}')
print(f'len({name_set1}.difference({name_set2}))={len(set1.difference(set2))}')
print(f'len({name_set2}.difference({name_set1}))={len(set2.difference(set1))}')
print(f'Fraction of {name_set1} that is in {name_set2}:', len(set1.intersection(set2)) / len(set1))
print(f'Fraction of {name_set2} that is in {name_set1}:', len(set2.intersection(set1)) / len(set2))
print(f'Elements that are in {name_set1} but not in {name_set2}:', set1.difference(set2))
print(f'Elements that are in {name_set2} but not in {name_set1}:', set2.difference(set1))
set_diff_stats(names_rust=names_rust, names_json=names_json)
pd.Series(names_rust)[pd.Series(names_rust).duplicated()]
# For each value in "city", get how many times it appears in the list of dicts:
from collections import Counter
Counter([d['city'] for d in data])
# Get max:
max(Counter([d['city'] for d in data]).values())