File size: 15,451 Bytes
fe1a02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Poker AI — mesa</title>
<style>

  :root{

    --bg:#0d1117; --panel:#161b22; --line:#2a313c; --text:#e6edf3; --muted:#8b949e;

    --felt:#14613b; --felt2:#0d4a2c; --rail:#3b2414; --gold:#e3b341; --accent:#3fb950; --red:#f85149; --blue:#58a6ff;

  }

  *{box-sizing:border-box}

  body{margin:0;background:var(--bg);color:var(--text);font:14px/1.4 system-ui,-apple-system,Segoe UI,Roboto,sans-serif;height:100vh;display:flex;flex-direction:column}

  header{display:flex;gap:14px;align-items:center;padding:10px 16px;border-bottom:1px solid var(--line);background:var(--panel);flex-wrap:wrap}

  header h1{font-size:16px;margin:0 8px 0 0;letter-spacing:.3px}

  header h1 span{color:var(--gold)}

  label{color:var(--muted);font-size:12px;display:flex;align-items:center;gap:6px}

  input[type=number]{width:64px}

  input,select,button{font:inherit;color:var(--text);background:#0d1117;border:1px solid var(--line);border-radius:6px;padding:5px 8px}

  button{cursor:pointer;background:#21262d}

  button:hover{border-color:var(--muted)}

  button.primary{background:#238636;border-color:#2ea043}

  button.danger{background:#6e1c1c;border-color:#a32b2b}

  button:disabled{opacity:.4;cursor:default}

  .stats{margin-left:auto;display:flex;gap:16px;font-size:13px}

  .stats b{font-variant-numeric:tabular-nums}

  main{flex:1;display:flex;min-height:0}

  #tablewrap{flex:1;position:relative;min-width:0}

  #table{position:absolute;left:50%;top:46%;transform:translate(-50%,-50%);width:min(78%,1000px);aspect-ratio:2.05/1;border-radius:50%/50%;

    background:radial-gradient(ellipse at center,var(--felt) 0%,var(--felt2) 75%);border:14px solid var(--rail);box-shadow:0 0 0 3px #1f130a,0 20px 60px rgba(0,0,0,.6), inset 0 0 60px rgba(0,0,0,.45)}

  #center{position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);text-align:center}

  #board{display:flex;gap:6px;justify-content:center;min-height:76px}

  #pot{margin-top:8px;font-weight:600;color:#fff;background:rgba(0,0,0,.35);display:inline-block;padding:3px 12px;border-radius:20px}

  #street{color:#cfe8d6;font-size:12px;margin-bottom:6px;letter-spacing:1px;text-transform:uppercase}

  .card{width:50px;height:72px;border-radius:7px;background:#fdfdfd;color:#111;display:flex;flex-direction:column;align-items:center;justify-content:center;

    font-weight:700;box-shadow:0 2px 6px rgba(0,0,0,.5);font-size:20px;line-height:1}

  .card .s{font-size:22px}

  .card.red{color:#d1242f}

  .card.back{background:repeating-linear-gradient(45deg,#1f4f9a 0 6px,#173d78 6px 12px);border:2px solid #fff}

  .card.small{width:34px;height:48px;font-size:14px;border-radius:5px}

  .card.small .s{font-size:15px}

  .card.tiny{width:27px;height:38px;font-size:12px;border-radius:4px}

  .card.tiny .s{font-size:12px}

  .seat.compact .cards{min-height:38px}

  .seat.compact .box{padding:2px 4px}

  .seat.compact .nm,.seat.compact .stk{font-size:11px}

  .seat{position:absolute;transform:translate(-50%,-50%);width:126px;text-align:center;transition:opacity .2s}

  .seat .box{background:rgba(13,17,23,.92);border:2px solid var(--line);border-radius:10px;padding:4px 6px}

  .seat.acting .box{border-color:var(--gold);box-shadow:0 0 14px rgba(227,179,65,.6)}

  .seat.hero .box{border-color:var(--blue)}

  .seat.hero.acting .box{border-color:var(--gold)}

  .seat.folded{opacity:.38}

  .seat.winner .box{border-color:var(--accent);box-shadow:0 0 18px rgba(63,185,80,.7)}

  .seat .nm{font-weight:600;font-size:12px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}

  .seat .stk{font-variant-numeric:tabular-nums;color:var(--gold);font-size:12px}

  .seat .cards{display:flex;gap:3px;justify-content:center;margin-bottom:3px;min-height:48px}

  .seat .act{font-size:11px;color:#cfe8d6;min-height:15px;white-space:nowrap}

  .seat .hn{font-size:11px;color:var(--accent)}

  .seat .profit{font-size:10px;color:var(--muted)}

  .chip{position:absolute;transform:translate(-50%,-50%);background:#f0f0f0;color:#111;border-radius:12px;padding:1px 8px;font-size:12px;font-weight:700;

    border:3px dashed #c0392b;white-space:nowrap}

  .dealer{display:inline-block;background:#fff;color:#000;border-radius:50%;width:17px;height:17px;font-size:10px;line-height:17px;font-weight:800;margin-left:4px}

  .tag{font-size:9px;color:var(--muted);margin-left:3px}

  #controls{position:absolute;left:50%;bottom:14px;transform:translateX(-50%);display:flex;gap:8px;align-items:center;background:var(--panel);

    border:1px solid var(--line);border-radius:12px;padding:10px 12px;flex-wrap:wrap;justify-content:center;max-width:95%}

  #controls .big{font-size:15px;padding:9px 16px;min-width:96px}

  #amt{width:80px}

  #slider{width:160px}

  aside{width:340px;border-left:1px solid var(--line);background:var(--panel);display:flex;flex-direction:column;min-height:0}

  aside h2{font-size:13px;text-transform:uppercase;letter-spacing:1px;color:var(--muted);margin:14px 14px 8px}

  #think{padding:0 14px 6px}

  .bar{display:flex;align-items:center;gap:8px;margin:4px 0;font-size:12px}

  .bar .lbl{width:120px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}

  .bar .track{flex:1;height:14px;background:#0d1117;border-radius:4px;overflow:hidden;border:1px solid var(--line)}

  .bar .fill{height:100%;background:var(--blue)}

  .bar.chosen .fill{background:var(--gold)}

  .bar.chosen .lbl{color:var(--gold);font-weight:700}

  .bar .pct{width:42px;text-align:right;font-variant-numeric:tabular-nums}

  .eq{font-size:12px;color:var(--muted);margin-top:6px}

  #log{flex:1;overflow:auto;padding:0 14px 14px;font-size:12px;font-family:ui-monospace,Consolas,monospace;color:#c9d1d9}

  #log div{padding:1px 0}

  #log .sep{color:var(--gold);margin-top:6px}

  #msg{position:absolute;left:50%;top:12px;transform:translateX(-50%);background:rgba(0,0,0,.7);padding:6px 14px;border-radius:8px;display:none}

</style>
</head>
<body>
<header>
  <h1>♠ Poker <span>AI</span></h1>
  <label>Jogadores <input id="players" type="number" min="2" max="23" value="6"></label>
  <label>Stack (bb) <input id="stack" type="number" min="5" max="1000" value="100"></label>
  <label>Modo <select id="mode"><option value="play">Jogar contra a IA</option><option value="watch">Assistir IA vs IA</option></select></label>
  <label><input id="reveal" type="checkbox"> Mostrar cartas das IAs</label>
  <label><input id="hint" type="checkbox"> Dica da IA na minha vez</label>
  <label>Velocidade <input id="speed" type="range" min="100" max="2500" value="900" step="100" style="width:90px"></label>
  <button class="primary" id="newgame">Novo jogo</button>
  <div class="stats">
    <div>Mãos: <b id="hands">0</b></div>
    <div id="pstat">Seu resultado: <b id="profit">0</b> bb</div>
    <div id="bbstat">bb/100: <b id="bb100">—</b></div>
  </div>
</header>
<main>
  <div id="tablewrap">
    <div id="table"><div id="center"><div id="street"></div><div id="board"></div><div id="pot"></div></div></div>
    <div id="seats"></div>
    <div id="msg"></div>
    <div id="controls"></div>
  </div>
  <aside>
    <h2>🧠 Cérebro da IA</h2>
    <div id="think"><div class="eq">A última decisão da IA aparece aqui.</div></div>
    <h2>Histórico</h2>
    <div id="log"></div>
  </aside>
</main>
<script>

const $ = id => document.getElementById(id);

let S = null, busy = false, timer = null;

const SUIT = {c:'♣', d:'♦', h:'♥', s:'♠'};



async function api(path, body={}) {

  body.hint = $('hint').checked;

  const r = await fetch(path, {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(body)});

  const j = await r.json();

  if (j.error) { flash('Erro: ' + j.error); throw new Error(j.error); }

  return j;

}

function flash(t){ const m=$('msg'); m.textContent=t; m.style.display='block'; setTimeout(()=>m.style.display='none',2500); }



function cardHTML(c, small) {

  const sz = small === 'tiny' ? 'tiny' : (small ? 'small' : '');

  if (!c) return `<div class="card back ${sz}"></div>`;

  const r = c[0] === 'T' ? '10' : c[0], s = c[1];

  return `<div class="card ${sz} ${(s==='h'||s==='d')?'red':''}"><div>${r}</div><div class="s">${SUIT[s]}</div></div>`;

}



function seatPos(i, n, hero) {

  // hero (or seat 0) at the bottom centre, others clockwise around the ellipse

  const base = hero >= 0 ? hero : 0;

  const k = (i - base + n) % n;

  const ang = Math.PI/2 + 2*Math.PI*k/n;       // start at bottom

  return {x: 50 + 44*Math.cos(ang), y: 46 + 39*Math.sin(ang), cx: 50 + 27*Math.cos(ang), cy: 46 + 23*Math.sin(ang)};

}



function render() {

  if (!S) return;

  const wrap = $('tablewrap').getBoundingClientRect();

  const tb = $('table').getBoundingClientRect();

  $('street').textContent = S.done ? 'Fim da mão' : S.street;

  $('board').innerHTML = S.board.map(c => cardHTML(c)).join('') + Array(5 - S.board.length).fill('<div class="card" style="opacity:.08"></div>').join('');

  $('pot').textContent = `Pote: ${S.pot} bb`;

  const n = S.n, small = n > 10, csz = n > 10 ? 'tiny' : true;

  let html = '';

  const winners = new Set(S.result ? S.result.winners : []);

  S.seats.forEach(s => {

    const p = seatPos(s.seat, n, S.hero);

    // map ellipse percentages to table rect (seats slightly outside the felt)

    const x = tb.left - wrap.left + tb.width * (0.5 + (p.x-50)/100*(small?1.16:1.12));

    const y = tb.top - wrap.top + tb.height * (0.5 + (p.y-46)/100*1.25);

    const cls = ['seat', small?'compact':'', s.hero?'hero':'', s.folded?'folded':'', (S.cur===s.seat)?'acting':'', winners.has(s.seat)?'winner':''].join(' ');

    const cards = s.folded && !s.cards ? '' : (s.cards ? s.cards.map(c=>cardHTML(c,csz)).join('') : cardHTML(null,csz)+cardHTML(null,csz));

    const hn = S.result && S.result.hands[s.seat] ? `<div class="hn">${S.result.hands[s.seat]}</div>` : '';

    const delta = S.result ? S.result.delta[s.seat] : null;

    const w = n > 16 ? 84 : (small ? 96 : 126);

    html += `<div class="${cls}" style="left:${x}px;top:${y}px;width:${w}px">

      <div class="cards">${cards}</div>

      <div class="box">

        <div class="nm">${s.name}${s.button?'<span class="dealer">D</span>':''}${s.sb?'<span class="tag">SB</span>':''}${s.bb?'<span class="tag">BB</span>':''}</div>

        <div class="stk">${s.allin && !S.done ? 'ALL-IN' : s.stack + ' bb'}</div>

        <div class="act">${delta!==null && Math.abs(delta)>0.001 ? (delta>0?'+':'')+delta+' bb' : (s.folded?'fold':s.last)}</div>

        ${hn}

      </div></div>`;

    if (s.bet > 0 && !S.done) {

      const cx = tb.left - wrap.left + tb.width * (0.5 + (p.cx-50)/100);

      const cy = tb.top - wrap.top + tb.height * (0.5 + (p.cy-46)/100);

      html += `<div class="chip" style="left:${cx}px;top:${cy}px">${s.bet}</div>`;

    }

  });

  $('seats').innerHTML = html;

  // stats

  $('hands').textContent = S.hands;

  if (S.hero >= 0) {

    const pr = S.seats[S.hero].profit;

    $('profit').textContent = (pr>0?'+':'') + pr.toFixed(1);

    $('profit').style.color = pr >= 0 ? 'var(--accent)' : 'var(--red)';

    $('bb100').textContent = S.hands ? (100*pr/S.hands).toFixed(0) : '—';

    $('pstat').style.display = $('bbstat').style.display = '';

  } else { $('pstat').style.display = $('bbstat').style.display = 'none'; }

  // brain panel

  const T = S.hint || S.think;

  if (T) {

    const title = S.hint ? `O que a IA faria no seu lugar (${T.street})` : `${T.name} pensou (${T.street}):`;

    $('think').innerHTML = `<div style="margin-bottom:6px;font-weight:600">${title}</div>` +

      T.options.map(o => `<div class="bar ${o.chosen?'chosen':''}"><div class="lbl">${o.label}</div><div class="track"><div class="fill" style="width:${(o.p*100).toFixed(1)}%"></div></div><div class="pct">${(o.p*100).toFixed(0)}%</div></div>`).join('') +

      `<div class="eq">Equidade estimada vs mãos aleatórias dos oponentes vivos: ${(T.equity*100).toFixed(0)}%</div>`;

  }

  $('log').innerHTML = S.log.map(l => `<div class="${l.startsWith('—')?'sep':''}">${l}</div>`).join('');

  $('log').scrollTop = 1e9;

  controls();

}



function controls() {

  const c = $('controls');

  if (S.done) {

    c.innerHTML = `<button class="primary big" id="next">Próxima mão ▶</button>`;

    $('next').onclick = nextHand;

    return;

  }

  if (S.cur !== S.hero || !S.hero_opts) { c.innerHTML = `<span style="color:var(--muted)">${S.seats[S.cur].name} está pensando…</span>`; return; }

  const o = S.hero_opts;

  const callTxt = o.to_call > 0 ? `Call ${o.to_call}` : 'Check';

  let h = `${o.can_fold ? '<button class="danger big" id="bf">Fold</button>' : ''}<button class="big" id="bc">${callTxt}</button>`;

  if (o.can_raise) {

    const potTo = x => Math.min(o.max_to, Math.max(o.min_to, +(o.max_bet + x*(o.pot + o.to_call)).toFixed(1)));

    h += `<input id="slider" type="range" min="${o.min_to}" max="${o.max_to}" step="0.5" value="${o.min_to}">

          <input id="amt" type="number" step="0.5" min="${o.min_to}" max="${o.max_to}" value="${o.min_to}">

          <button data-f="0.5" class="ps">½ pote</button><button data-f="0.75" class="ps">¾</button><button data-f="1" class="ps">Pote</button>

          <button class="primary big" id="br">${o.bet_word}</button><button class="big" id="ba">All-in ${o.max_to}</button>`;

    c.innerHTML = h;

    $('slider').oninput = e => $('amt').value = e.target.value;

    $('amt').oninput = e => $('slider').value = e.target.value;

    document.querySelectorAll('.ps').forEach(b => b.onclick = () => { const v = potTo(+b.dataset.f); $('amt').value = v; $('slider').value = v; });

    $('br').onclick = () => act('raise', +$('amt').value);

    $('ba').onclick = () => act('allin');

  } else c.innerHTML = h;

  if (o.can_fold) $('bf').onclick = () => act('fold');

  $('bc').onclick = () => act('call');

}



async function act(kind, amount) { if (busy) return; busy = true; try { S = await api('/api/act', {kind, amount}); render(); } finally { busy = false; } loop(); }

async function nextHand() { clearTimeout(timer); S = await api('/api/next_hand'); render(); loop(); }



function loop() {

  clearTimeout(timer);

  if (!S) return;

  const speed = +$('speed').value;

  if (S.done) { if (S.hero < 0) timer = setTimeout(nextHand, speed * 2.2); return; }

  if (S.cur !== S.hero) timer = setTimeout(async () => { S = await api('/api/ai_step'); render(); loop(); }, speed);

}



$('newgame').onclick = async () => {

  clearTimeout(timer);

  S = await api('/api/new_game', {players: +$('players').value, stack: +$('stack').value, mode: $('mode').value, reveal: $('reveal').checked});

  $('think').innerHTML = '<div class="eq">A última decisão da IA aparece aqui.</div>';

  render(); loop();

};

$('hint').onchange = async () => { if (S) { S = await api('/api/state'); render(); } };

window.onresize = render;

// optional URL params: ?players=9&stack=100&mode=watch&reveal=1&hint=1&speed=500

const Q = new URLSearchParams(location.search);

['players','stack','speed'].forEach(k => { if (Q.has(k)) $(k).value = Q.get(k); });

if (Q.has('mode')) $('mode').value = Q.get('mode');

if (Q.get('reveal') === '1') $('reveal').checked = true;

if (Q.get('hint') === '1') $('hint').checked = true;

$('newgame').click();

</script>
</body>
</html>