# ตัวอย่างคัดลอกวาง: index.html ไฟล์เดียว

หน้านี้คือไฟล์เดียวที่คุณวาง รัน zip และอัปโหลดได้โดยไม่ต้องแก้อะไร เป็นเกมที่เล็กที่สุดที่ใช้ทุกส่วนของ SDK ที่การส่งครั้งแรกต้องการ: `init`, สัญญาณการโหลด, `gameplayStart` และ `gameplayStop`, โฆษณา rewarded หนึ่งรายการพร้อมทางสำรอง และการเซฟ ใช้เวลาประมาณ 10 นาที บทช่วยสอนสองไฟล์ที่ยาวกว่าซึ่งมีโฆษณา midgame และ `happytime` คือ[เกมขั้นต่ำใน 15 นาที](https://docs.zokogames.com/th/getting-started/first-game) ส่วนเส้นทางแบบมีลำดับเลขที่รวมคอนโซลคือ[เกมแรกของคุณเปิดให้เล่นใน 30 นาที](https://docs.zokogames.com/th/getting-started/quick-start)

## 1. บันทึกเป็น index.html

สร้างโฟลเดอร์ว่างชื่อ `coin-tap` แล้วบันทึกไฟล์ในนั้นเป็น `index.html` ไม่ต้องมีอย่างอื่นอีก

```html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Coin Tap</title>
  <!-- The real SDK script. It must come before your own code. -->
  <script src="https://sdk.zokogames.com/zokogames-sdk-v1.js"></script>
  <style>
    html, body { margin: 0; height: 100%; background: #14161f; color: #fff; font: 18px sans-serif; text-align: center; }
    main { padding-top: 12vh; }
    button { font: inherit; padding: 12px 24px; margin: 6px; }
    #coin { width: 120px; height: 120px; border-radius: 50%; border: 0; background: #ffc531; touch-action: manipulation; }
    [hidden] { display: none !important; }
  </style>
</head>
<body>
  <main>
    <h1>Coin Tap</h1>
    <p>Score <b id="score">0</b> | Time <b id="time">10</b> | Best <b id="best">0</b></p>
    <p><button id="coin" aria-label="Coin" hidden></button></p>
    <p id="msg">Loading...</p>
    <button id="play" disabled>Play</button>
    <button id="bonus" hidden>Watch an ad for +5 seconds</button>
  </main>
  <script>
    var SDK = window.ZokoGames && window.ZokoGames.SDK; // undefined if the script was blocked
    var $ = function (id) { return document.getElementById(id); };
    var usable = false, running = false, score = 0, best = 0, left = 10, extra = 0, timer = 0;

    // Every SDK call goes through here: if the SDK is missing, disabled or a call fails, the game still runs.
    // SDK errors are plain { code, message } objects, not Error instances.
    function zoko(call) {
      if (!usable) return Promise.resolve(undefined);
      return Promise.resolve().then(call).catch(function (e) {
        console.warn("SDK:", e && e.code, e && e.message);
      });
    }

    function boot() {
      var ready = SDK ? SDK.init() : Promise.resolve(); // init() never rejects
      return ready
        .then(function () { usable = !!SDK && SDK.environment !== "disabled"; })
        .then(function () { return zoko(function () { return SDK.game.loadingStart(); }); })
        .then(function () {
          if (usable) { // saves are synchronous and throw on error, so keep them inside try
            try { best = Number(SDK.data.getItem("best")) || 0; } catch (e) { console.warn("SDK:", e.code); }
          }
          $("best").textContent = best;
          $("bonus").hidden = !usable;
          return zoko(function () { return SDK.game.loadingStop(); }); // the first screen is ready
        })
        .then(function () { $("msg").textContent = "Tap the coin as often as you can."; $("play").disabled = false; });
    }

    function setRunning(on) {
      if (on === running) return;
      running = on;
      clearInterval(timer);
      if (on) timer = setInterval(tick, 1000);
      zoko(function () { return on ? SDK.game.gameplayStart() : SDK.game.gameplayStop(); });
    }

    function startRound() {
      score = 0; left = 10 + extra; extra = 0;
      $("play").hidden = $("bonus").hidden = true;
      $("coin").hidden = false;
      $("msg").textContent = "";
      render();
      setRunning(true); // input is live now: gameplayStart
    }

    function tick() { left -= 1; render(); if (left <= 0) endRound(); }

    function endRound() {
      setRunning(false); // round over: gameplayStop
      $("coin").hidden = true;
      $("play").hidden = false;
      $("bonus").hidden = !usable;
      $("msg").textContent = "Round over: " + score;
      if (score > best) {
        best = score;
        $("best").textContent = best;
        if (usable) {
          try { SDK.data.setItem("best", String(best)); } catch (e) { console.warn("SDK:", e.code); }
        }
      }
    }

    // Rewarded ad with a fallback: the reward is granted only when the ad finished; every other outcome just carries on.
    $("bonus").addEventListener("click", function () {
      var finished = false;
      $("bonus").disabled = $("play").disabled = true;
      zoko(function () {
        return SDK.ad.requestAd("rewarded", {
          adFinished: function () { finished = true; },
          adError: function (e) { console.info("No ad:", e.code); } // adsDisabled before full launch, unfilled, adblock...
        });
      }).then(function () {
        $("bonus").disabled = $("play").disabled = false;
        if (finished) { extra = 5; $("msg").textContent = "+5 seconds next round."; }
        else { $("msg").textContent = "No ad right now. Play on."; } // the fallback
      });
    });

    $("coin").addEventListener("pointerdown", function (e) { e.preventDefault(); if (running) { score += 1; render(); } });
    $("play").addEventListener("click", startRound);
    window.addEventListener("blur", function () { if (running) setRunning(false); });  // pause when focus leaves
    window.addEventListener("focus", function () { if (!$("coin").hidden) setRunning(true); });
    function render() { $("score").textContent = score; $("time").textContent = left; }
    boot();
  </script>
</body>
</html>
```

เกมยังทำงานต่อได้เมื่อ SDK ถูกบล็อกหรือ `SDK.environment` เป็น `"disabled"`: `zoko()` ข้ามทุกการเรียก และปุ่มที่ต้องใช้พอร์ทัลจะยังซ่อนอยู่ URL ของสคริปต์ SDK คือของจริง `https://sdk.zokogames.com/zokogames-sdk-v1.js` อย่าคัดลอกไฟล์ลงในบิลด์ของคุณ

## 2. แต่ละส่วนทำอะไร

| ส่วน | การเรียก SDK | เหตุผลที่มี |
| --- | --- | --- |
| แท็กสคริปต์ใน `<head>` | ไม่มี | กำหนด `window.ZokoGames.SDK` รายงานการอัปโหลดเตือน `no_sdk` หากไม่มี |
| `boot()` | `SDK.init()` แล้ว `SDK.environment` | `init()` ต้องเรียกก่อนและไม่เคย reject ความล้มเหลวแสดงเป็น `"disabled"` |
| การโหลด | `SDK.game.loadingStart()`, `SDK.game.loadingStop()` | แยกเวลาโหลดออกจากเวลาเล่น เรียก `loadingStop` เมื่อหน้าจอแรกเล่นได้ |
| **เล่น** และตอนจบรอบ | `SDK.game.gameplayStart()`, `SDK.game.gameplayStop()` | `gameplayStart` คือการเรียกแรกที่รายการตรวจของ Preview รอ (ภายใน 20 วินาทีหลังโหลด) ส่วน `gameplayStop` ปิดรอบ และทำงานเมื่อหน้าต่างเสียโฟกัสด้วย |
| ปุ่มโบนัส | `SDK.ad.requestAd("rewarded", { adFinished, adError })` | ให้รางวัลเฉพาะใน `adFinished` ในการเปิดตัวขั้นพื้นฐาน พอร์ทัลตอบ `adsDisabled` ดังนั้นสาขาทางสำรองคือสิ่งที่คุณจะเห็นก่อน |
| คะแนนสูงสุด | `SDK.data.getItem`, `SDK.data.setItem` | ซิงโครนัสและใช้สตริงเท่านั้น โยน `{ code, message }` เมื่อเกิดข้อผิดพลาด จึงอยู่ใน `try` |

## 3. รันบนเครื่องของคุณ

บน `localhost` SDK ตอบทุกการเรียกเอง: ไม่ต้องมีพอร์ทัล ไม่ต้องมีบัญชี เปิดโฟลเดอร์ผ่าน HTTP (`file://` ไม่นับเป็นในเครื่อง) แล้วเปิดที่อยู่พร้อม `?sdk_debug=true`

```bash
cd coin-tap
npx http-server . -p 8080
# or, without Node.js:
python3 -m http.server 8080
# open http://localhost:8080/?sdk_debug=true
```

คุณควรเห็นบรรทัด `[ZokoGames SDK] Local mode: ...` หนึ่งบรรทัดเมื่อเปิดหน้า, `[ZokoGames SDK] call game.loadingStart` และ `call game.loadingStop` ก่อนที่ **เล่น** จะใช้งานได้ จากนั้น `call game.gameplayStart` เมื่อกด **เล่น** และ `call game.gameplayStop` หลังผ่านไปสิบวินาที การกด **Watch an ad for +5 seconds** จะคลุมหน้าไว้สามวินาทีด้วยตัวแทนที่ติดป้าย `Local ad (rewarded)` จากนั้นข้อความเปลี่ยนเป็น "+5 seconds next round." การพิมพ์ `ZokoGames.SDK.environment` ในคอนโซลจะพิมพ์ `"local"` เพิ่ม `&ad_response=unfilled` ต่อท้ายที่อยู่เพื่อดูทางสำรอง ("No ad right now. Play on.") และ `&ad_response=adsDisabled` เพื่อดูว่าการเปิดตัวขั้นพื้นฐานเป็นอย่างไร สวิตช์ทุกตัวอยู่ใน[การทดสอบ](https://docs.zokogames.com/th/getting-started/local-testing)

จากนั้นเปิด[เครื่องมือ Preview](https://www.zokogames.com/th/preview) พิมพ์ `http://localhost:8080/` แล้วกด **โหลดเกม** เล่นหนึ่งรอบแล้วกดปุ่มโบนัส: รายการตรวจจะติ๊ก **เริ่มต้น SDK แล้ว (เรียก init)**, **เรียก loadingStop แล้ว**, **เรียก gameplayStart แล้ว**, **เรียก gameplayStop แล้ว**, **ขอโฆษณาแบบมีรางวัลแล้ว** และ **ใช้โมดูล Data แล้ว** และบรรทัดเวลาแสดงเวลาถึง `gameplayStart` ครั้งแรกที่ไม่ถึง 20 วินาที รายการสำหรับโฆษณา midgame และ `happytime` ยังเป็นสีเทา ซึ่งไม่บล็อกการส่งครั้งแรก

## 4. Zip

`index.html` ต้องอยู่ที่รากของอาร์ไคฟ์ โฟลเดอร์ระดับบนสุดหนึ่งโฟลเดอร์ก็ยอมรับเช่นกัน และไฟล์ระบบเช่น `__MACOSX` และ `.DS_Store` จะถูกละเลย

| ระบบ | คำสั่งหรือการกระทำ | ผลลัพธ์ |
| --- | --- | --- |
| macOS หรือ Linux | `cd coin-tap && zip ../coin-tap.zip index.html` | `coin-tap.zip` ข้างโฟลเดอร์ |
| Windows 10 หรือ 11 | ในโฟลเดอร์ รัน `tar -a -c -f ..\coin-tap.zip index.html` | `coin-tap.zip` ข้างโฟลเดอร์ |
| ระบบใดก็ได้ ด้วยตัวจัดการไฟล์ | เลือกไฟล์ภายในโฟลเดอร์ (ไม่ใช่ตัวโฟลเดอร์) แล้วบีบอัด | zip ที่มี `index.html` ที่ราก |

ตรวจด้วย `unzip -l coin-tap.zip` (macOS, Linux) หรือ `tar -tf coin-tap.zip` (Windows): รายการต้องแสดง `index.html` โดยไม่มีอะไรนำหน้า Windows PowerShell รุ่นเก่าอาจเขียนแบ็กสแลชลงในชื่อรายการ zip ด้วย `Compress-Archive` ตัวตรวจสอบปฏิเสธรายการเหล่านั้นเป็น `unsafe_path` จึงควรใช้ `tar` หรือตัวจัดการไฟล์

## 5. อัปโหลด

ทำตามขั้นตอนที่ 6 และ 7 ของ[เกมแรกของคุณเปิดให้เล่นใน 30 นาที](https://docs.zokogames.com/th/getting-started/quick-start#6-upload-and-read-the-report-5-minutes): วาง `coin-tap.zip` ที่ **วางไฟล์ zip ของเกมที่นี่** ในขั้นตอน **บิลด์** คุณควรเห็น "บิลด์ v1 ผ่านการตรวจสอบและเผยแพร่แล้ว" โดยตรวจพบ SDK และไม่มีข้อผิดพลาด หน้าเกมยังต้องมีคำอธิบาย การควบคุม แท็ก และปกของคุณเองก่อน **ส่งตรวจสอบ**

## ขั้นตอนถัดไป

- [เกมขั้นต่ำใน 15 นาที](https://docs.zokogames.com/th/getting-started/first-game): แนวคิดเดียวกันพร้อมโฆษณา midgame, `happytime` และการตั้งค่าปิดเสียงของพอร์ทัล
- [บทนำ SDK](https://docs.zokogames.com/th/sdk/introduction): `init` สภาพแวดล้อม และรหัสข้อผิดพลาดทุกรหัส
- [การแก้ไขปัญหา](https://docs.zokogames.com/th/guides/troubleshooting): เมื่อคอนโซลเงียบหรืออัปโหลดถูกปฏิเสธ
