"""Bruecke zum fremden 6-max-Motor CFR-NLHE-6max (dberweger2017, MIT, v0.6.1): Vertrag rein, sein nplayer_serve raus. Sein Wissen haengt an exakten Chipzahlen seines eigenen Menues (min/pot/jam). Darum laeuft eine Schattenhand in seinem Menue mit: jeder echte Raise wird auf seine naechste Groesse (Anteil am Topf, log) abgebildet, seine Antwort auf die echte Hand zurueck. Passt die Schattenhand nicht mehr, spielt sein eigenes Skript 'tight-aggressive' auf der echten Hand. Seine Karten sieht nur er. Aufruf: fremd6.py [policy sha256] """ import json import math import os import subprocess import sys PROTOKOLL = os.environ.get("FREMD_PROTOKOLL") RANG = "23456789TJQKA" class Serve: def __init__(self, a): cmd = [a[0], "serve"] self.agent = "script:tight-aggressive" if len(a) > 3: cmd += ["--card-buckets", a[1], "--policy", "p", a[2], a[3]] self.agent = "policy:p" self.p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1, creationflags=0x4000) self.bereit = json.loads(self.p.stdout.readline()) def frage(self, op, **k): self.p.stdin.write(json.dumps(dict(op=op, **k)) + "\n") self.p.stdin.flush() return json.loads(self.p.stdout.readline()) def karte(k): r = k["rank"] w = ("TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE") r = w.index(r) if r in w else RANG.index(r) return r * 4 + {"c": 0, "d": 1, "h": 2, "s": 3}[str(k["suit"])[0].lower()] def deck(a, n, ich, button): weg = [karte(k) for k in a["heroCards"]] brett = [karte(k) for s in ("FLOP", "TURN", "RIVER") for k in a["board"].get(s, [])] frei = iter([x for x in range(52) if x not in weg and x not in brett]) d = [None] * 52 off = (ich - button - 1) % n d[off], d[n + off] = weg for i, b in enumerate(brett): d[2 * n + i] = b return [x if x is not None else next(frei) for x in d] class Hand: """Eigene kleine Buchfuehrung (Chips, 50/100) fuer die echte Hand.""" def __init__(self, n, button, stack): sb = button if n == 2 else (button + 1) % n self.rest, self.ein, self.ges = list(stack), [0] * n, [0] * n for i, b in ((sb, 50), ((sb + 1) % n, 100)): self.zahle(i, b) def zahle(self, i, ziel): x = min(ziel - self.ein[i], self.rest[i]) self.rest[i] -= x self.ein[i] += x self.ges[i] += x def anteil(ein, ges, i, ziel): h = max(ein) return max((ziel - h) / max(1, sum(ges) + h - ein[i]), 0.01) def antwort(s, a): n = a["seats"] ich, button = a["heroSeat"] - 1, a["buttonSeat"] - 1 f = a["bigBlind"] / 100 def chips(x): return int(round(x / f)) stack = {a["heroSeat"]: chips(a["heroStack"])} for v in a["villains"]: stack[v["seat"]] = chips(v["stack"]) gez, bei, st = {x: 0 for x in range(1, n + 1)}, {}, None for e in a["history"]: if e["street"] != st: for x, y in bei.items(): gez[x] += y bei, st = {}, e["street"] if e.get("amount") and e["action"] not in ("FOLD", "CHECK"): bei[e["seat"]] = chips(e["amount"]) for x, y in bei.items(): gez[x] += y start = [stack[x] + gez[x] for x in range(1, n + 1)] d = deck(a, n, ich, button) echt = Hand(n, button, start) akt_echt, akt_schatten, ok, strasse = [], [], True, "PREFLOP" def hb(acts): return {"stacks": start, "sb": 50, "bb": 100, "button": button, "deck": d, "actions": acts} for e in a["history"]: if e["action"].startswith("POST"): continue if e["street"] != strasse: strasse = e["street"] echt.ein = [0] * n i, ist = e["seat"] - 1, e["action"] h = max(echt.ein) ziel = chips(e["amount"]) if e.get("amount") else 0 if ist == "ALL_IN": ist = "RAISE" if ziel > h else "CALL" if ist in ("BET", "RAISE"): akt_echt.append(["raise", ziel]) if ok: m = s.frage("decide", seat=i, agent="menu", hand=hb(akt_schatten)) r = [(lb, x[1]) for lb, x in zip(m.get("labels") or [], m.get("menu") or []) if x[0] == "raise"] if not m.get("ok") or not r: ok = False else: fr = anteil(echt.ein, echt.ges, i, ziel) jam = ziel >= echt.ein[i] + echt.rest[i] se, sg = [p[1] for p in m["pl"]], [p[2] for p in m["pl"]] best = min(r, key=lambda x: (0 if jam and x[0] == 5 else 1, abs(math.log(anteil(se, sg, i, x[1]) / fr)))) akt_schatten.append(["raise", best[1]]) echt.zahle(i, ziel) elif ist == "CALL": akt_echt.append(["call", 0]) akt_schatten.append(["call", 0]) echt.zahle(i, h) else: x = "fold" if ist == "FOLD" else "check" akt_echt.append([x, 0]) akt_schatten.append([x, 0]) if a["street"] != strasse: echt.ein = [0] * n seed = a.get("seed", 1) r = s.frage("decide", seat=ich, agent=s.agent, seed=seed, hand=hb(akt_schatten)) if ok else {"error": "kein Raise im Schatten"} grund, label = "fremd trainiert=%s" % r.get("trained"), None if r.get("ok") and r.get("street", "").upper() == a["street"]: x = r["action"] label = next((lb for lb, y in zip(r["labels"], r["menu"]) if y == x), None) else: grund = "fremd skript (%s)" % r.get("error") r = s.frage("decide", seat=ich, agent="script:tight-aggressive", seed=seed, hand=hb(akt_echt)) if not r.get("ok"): return {"action": "CHECK" if "CHECK" in a["legalActions"] else "FOLD", "reason": "fremd notfall %s" % r.get("error")} x = r["action"] if x[0] != "raise" or "minRaiseTo" not in a: t = x[0].upper() if x[0] != "raise" else "CALL" if t in ("FOLD", "CALL") and "CHECK" in a["legalActions"]: t = "CHECK" if t == "CHECK" and "CHECK" not in a["legalActions"]: t = "CALL" return {"action": t, "reason": grund} h = max(echt.ein) if label == 3: ziel = chips(a["minRaiseTo"]) elif label == 4: ziel = h + sum(echt.ges) + h - echt.ein[ich] elif label == 5: ziel = chips(a["maxRaiseTo"]) else: ziel = x[1] ziel = min(max(ziel, chips(a["minRaiseTo"])), chips(a["maxRaiseTo"])) return {"action": "RAISE", "size": round(ziel * f, 6), "reason": grund} def main(): s = Serve(sys.argv[1:]) for z in sys.stdin: a = json.loads(z) try: w = antwort(s, a) except Exception as e: # noqa: BLE001 w = {"action": "CHECK" if "CHECK" in a["legalActions"] else "FOLD", "reason": "fremd notfall %r" % e} if PROTOKOLL: open(PROTOKOLL, "a").write(w["reason"].split("(")[0] + "|" + a["street"] + chr(10)) print(json.dumps(w), flush=True) if __name__ == "__main__": main()