# mknv.py out.nv TEAM [games] [fill]  -- makes a MAME battery file with a season for TEAM (index 0..31) after N games;
# 'fill' also gives every team N games of random results (for testing the table screens), otherwise the table is empty
import sys, struct
out, team = sys.argv[1], int(sys.argv[2]); k = int(sys.argv[3]) if len(sys.argv) > 3 else 0
b = bytearray(8192)
blk = bytearray(16 + 32 * 16); blk[0:4] = b'SESN'; struct.pack_into('>HHHHHH', blk, 4, 0, team + 1, k, 82, 0, 0)   # +12 0 = continue (1 = New Season pending), +14 player block not yet
if len(sys.argv) > 4 and sys.argv[4] == 'fill':
    import random
    rnd = random.Random(7); struct.pack_into('>H', blk, 4, k)
    for t in range(32):
        w = l = o = gf = ga = ow = 0
        for g in range(k):
            a, b_ = rnd.randint(0, 6), rnd.randint(0, 6)
            ot = a == b_
            if ot: a += rnd.choice((0, 1)); b_ += 0 if a > b_ else 1
            gf += a; ga += b_
            if a > b_: w += 1; ow += ot
            elif ot: o += 1
            else: l += 1
        struct.pack_into('>8H', blk, 16 + 16 * t, k, w, l, o, gf, ga, 0, ow)
b[0x1000:0x1000 + len(blk)] = blk
s = sum(b[:0x1FFE]) & 0xff; b[0x1FFF] = s; b[0x1FFE] = (~s) & 0xff
nv = bytearray(16384); nv[0::2] = b
open(out, 'wb').write(nv); print('season for team', team, 'game', k)
