# مثال للنسخ واللصق: ملف index.html واحد {#copy-paste-example-one-index-html}

هذه الصفحة عبارة عن ملف واحد يمكنك لصقه وتشغيله وضغطه ورفعه دون تغيير أي شيء. إنها أصغر لعبة تستخدم كل جزء من SDK يحتاجه أول إرسال: `init` وإشارات التحميل و`gameplayStart` و`gameplayStop` وإعلانًا مكافِئًا واحدًا مع مسار بديل وحفظًا. تستغرق نحو 10 دقائق. أما الدرس الأطول من ملفين، مع إعلان أثناء اللعب و`happytime`، فهو [لعبة بسيطة في 15 دقيقة](https://docs.zokogames.com/ar/getting-started/first-game)؛ والمسار المرقَّم الذي يشمل وحدة التحكم هو [لعبتك الأولى على الهواء خلال 30 دقيقة](https://docs.zokogames.com/ar/getting-started/quick-start).

## 1. احفظ هذا باسم index.html {#1-save-this-as-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()` كل استدعاء وتبقى الأزرار التي تحتاج إلى البوابة مخفية. عنوان سكربت SDK هو العنوان الحقيقي، `https://sdk.zokogames.com/zokogames-sdk-v1.js`؛ لا تنسخ الملف إلى البناء.

## 2. ما يفعله كل جزء {#2-what-each-part-does}

| الجزء | استدعاء SDK | سبب وجوده |
| --- | --- | --- |
| وسم script في `<head>` | لا شيء | يعرّف `window.ZokoGames.SDK`. يحذّر تقرير الرفع بـ `no_sdk` من دونه. |
| `boot()` | `SDK.init()` ثم `SDK.environment` | `init()` مطلوب أولًا ولا يُرفض أبدًا؛ ويظهر الفشل بالقيمة `"disabled"`. |
| التحميل | `SDK.game.loadingStart()` و`SDK.game.loadingStop()` | يفصل زمن التحميل عن زمن اللعب. استدعِ `loadingStop` عندما تصبح الشاشة الأولى قابلة للعب. |
| **Play** ونهاية الجولة | `SDK.game.gameplayStart()` و`SDK.game.gameplayStop()` | `gameplayStart` هو أول استدعاء تنتظره قائمة تحقق المعاينة (خلال 20 ثانية من التحميل)؛ و`gameplayStop` يغلق الجولة ويعمل أيضًا عندما تفقد النافذة التركيز. |
| زر المكافأة | `SDK.ad.requestAd("rewarded", { adFinished, adError })` | المكافأة في `adFinished` فقط. في الإطلاق الأساسي ترد البوابة بـ `adsDisabled`، فالفرع البديل هو أول ما سترى. |
| أفضل نتيجة | `SDK.data.getItem` و`SDK.data.setItem` | متزامنان ويقبلان السلاسل النصية فقط؛ يرميان `{ code, message }` عند الخطأ، لذا يوضعان داخل `try`. |

## 3. شغّلها على جهازك {#3-run-it-on-your-machine}

على `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` قبل أن يصبح **Play** فعّالًا، ثم `call game.gameplayStart` عند الضغط على **Play** و`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/ar/getting-started/local-testing).

ثم افتح [أداة المعاينة](https://www.zokogames.com/ar/preview) واكتب `http://localhost:8080/` واضغط **تحميل اللعبة**، والعب جولة واحدة واضغط زر المكافأة: تؤشّر قائمة التحقق على **تمت تهيئة SDK (استدعاء init)** و**تم استدعاء loadingStop** و**تم استدعاء gameplayStart** و**تم استدعاء gameplayStop** و**طُلب إعلان مكافِئ** و**استُخدمت وحدة البيانات**، ويُظهر سطر التوقيت الزمن حتى أول `gameplayStart` أقل من 20 ث. وتبقى بنود الإعلانات أثناء اللعب و`happytime` رمادية، وهذا لا يمنع أول إرسال.

## 4. اضغطها في zip {#4-zip-it}

يجب أن يكون `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` دون أي شيء قبله. قد تكتب إصدارات PowerShell الأقدم في Windows خطوطًا مائلة عكسية في أسماء إدخالات zip عند استخدام `Compress-Archive`؛ ويرفضها المدقِّق بالرمز `unsafe_path`، فاستخدم `tar` أو مدير الملفات.

## 5. ارفعها {#5-upload-it}

اتبع الخطوتين 6 و7 من [لعبتك الأولى على الهواء خلال 30 دقيقة](https://docs.zokogames.com/ar/getting-started/quick-start#6-upload-and-read-the-report-5-minutes): اسحب `coin-tap.zip` إلى "أفلت ملف zip الخاص بلعبتك هنا" في خطوة **الـ build**. ينبغي أن ترى "اجتاز الـ build v1 التحقق ونُشر" مع اكتشاف SDK ودون أخطاء. وما تزال صفحة اللعبة تحتاج إلى وصفك وتعليمات التحكم والوسوم والأغلفة الخاصة بك قبل **أرسل للمراجعة**.

## الخطوات التالية {#next-steps}

- [لعبة بسيطة في 15 دقيقة](https://docs.zokogames.com/ar/getting-started/first-game): الأفكار نفسها مع إعلان أثناء اللعب و`happytime` وإعداد كتم الصوت في البوابة.
- [المقدمة والتهيئة والأخطاء](https://docs.zokogames.com/ar/sdk/introduction): `init` والبيئات وكل رموز الأخطاء.
- [استكشاف الأخطاء وإصلاحها](https://docs.zokogames.com/ar/guides/troubleshooting): عندما تصمت وحدة التحكم أو يُرفض الرفع.
