Skip to content

Commit 8f877f6

Browse files
committed
ybot support on the converter script
1 parent ef5692b commit 8f877f6

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Some sample replays are available on the `replays/` folder.
2222

2323
```bash
2424
# from replaybot to plain text
25-
python converter.py myreplay.replay txt myreplay.txt
25+
python converter.py replaybot myreplay.replay txt myreplay.txt
2626
# txt back to replaybot
27-
python converter.py myreplay.txt replaybot myreplay.replay
27+
python converter.py txt myreplay.txt replaybot myreplay.replay
2828
```
2929

3030
## TODO

converter.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from struct import pack, unpack
33
import sys
44
import os
5+
import json
56

67
@dataclass
78
class Action:
@@ -44,6 +45,17 @@ def parse_zbot(data: bytes) -> Replay:
4445
actions.append(Action(x, hold == 0x31, player_1 != 0x31))
4546
return Replay(fps, actions)
4647

48+
def parse_ybot(data: bytes) -> Replay:
49+
data = json.loads(data)
50+
levels = list(data.keys())
51+
level = input(f'Choose a level {levels}: ')
52+
macro = data[level]
53+
fps = 1 / macro['delta_override']
54+
actions = []
55+
for inst in macro['instructions']:
56+
actions.append(Action(inst['x'], inst['press'], inst['p2']))
57+
return Replay(fps, actions)
58+
4759
def dump_replaybot(replay: Replay) -> bytearray:
4860
data = bytearray()
4961
data.extend(pack('f', replay.fps))
@@ -77,44 +89,50 @@ def dump_txt(replay: Replay) -> str:
7789
final += f'{action.x} {int(action.hold)} {int(action.player_2)}\n'
7890
return final[:-1]
7991

80-
if len(sys.argv) != 4:
81-
print('''\
82-
Usage: python converter.py (from) format (to)
83-
format can be either zbot, replaybot or txt
92+
FORMATS = {'txt', 'replaybot', 'zbot', 'ybot'}
93+
94+
if len(sys.argv) != 5:
95+
print(f'''\
96+
Usage: python converter.py format (from) format (to)
97+
format can be either {", ".join(FORMATS)}
8498
Example:
8599
Converts from zBot to ReplayBot
86-
python converter.py "Sonic Wave.zbot" replaybot "Sonic Wave.replay"
100+
python converter.py zbot "Sonic Wave.zbot" replaybot "Sonic Wave.replay"
87101
88102
Converts from zBot to txt
89-
python converter.py "Tartarus.zbot" txt "tartarus.txt"
103+
python converter.py zbot "Tartarus.zbot" txt "tartarus.txt"
90104
''')
91105
exit(1)
92106

93-
if os.path.exists(sys.argv[3]):
94-
print(f'{sys.argv[3]} already exists')
107+
if os.path.exists(sys.argv[4]):
108+
print(f'{sys.argv[4]} already exists')
95109
exit(1)
96110

97-
FORMATS = {'txt', 'replaybot', 'zbot'}
111+
from_format = sys.argv[1]
98112

99-
from_format = os.path.splitext(sys.argv[1])[1][1:]
113+
to = sys.argv[3]
100114

101-
to = sys.argv[2]
102-
103-
if to not in FORMATS:
115+
if to not in FORMATS or from_format not in FORMATS:
104116
print(f'format can only be {FORMATS}')
105117
exit(1)
106118

107-
with open(sys.argv[1], 'rb') as file:
119+
if to == 'ybot':
120+
print('cant convert to ybot (yet)')
121+
exit(1)
122+
123+
with open(sys.argv[2], 'rb') as file:
108124
data = file.read()
109125

110126
if from_format == 'zbot':
111127
replay = parse_zbot(data)
112128
elif from_format == 'txt':
113129
replay = parse_txt(data)
114-
else:
130+
elif from_format == 'ybot':
131+
replay = parse_ybot(data)
132+
elif from_format == 'replaybot':
115133
replay = parse_replaybot(data)
116134

117-
with open(sys.argv[3], 'wb') as file:
135+
with open(sys.argv[4], 'wb') as file:
118136
if to == 'zbot':
119137
file.write(dump_zbot(replay))
120138
elif to == 'txt':

0 commit comments

Comments
 (0)