<?php
session_start();
include 'cfg.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['voucher_code'])) {
    $voucher_code = $_POST['voucher_code'];
    $opened_index = (int)$_POST['opened_index'];
    $game_over    = (int)$_POST['game_over'];

    $stmt = $pdo->prepare("SELECT opened_circles FROM game_state WHERE voucher_code = ?");
    $stmt->execute([$voucher_code]);
    $row = $stmt->fetch();

    $opened = $row ? array_filter(explode(',', $row['opened_circles'])) : [];
    if (!in_array($opened_index, $opened)) {
        $opened[] = $opened_index;
    }
    $opened_str = implode(',', $opened);

    $update = $pdo->prepare("UPDATE game_state SET opened_circles = ?, game_over = ? WHERE voucher_code = ?");
    $update->execute([$opened_str, $game_over, $voucher_code]);
}
?>