@smiley-face-game/api

The API package for `@smiley-face-game`. This package is used by the client itself, and is made a public NPM package so bot creators can utilize this package.

Usage no npm install needed!

<script type="module">
  import smileyFaceGameApi from 'https://cdn.skypack.dev/@smiley-face-game/api';
</script>

README

@smiley-face-game/api

The API package for @smiley-face-game. This package is used by the client itself, and is made a public NPM package so bot creators can utilize this package.

Installation

With a Package Manager

Using Yarn

yarn add @smiley-face-game/api

Using NPM

npm i @smiley-face-game/api

With HTML

Using UNPKG to serve JS

<!-- Imports an `sfg` global -->
<script src="https://unpkg.com/@smiley-face-game/api"></script>
<script>
  console.log(sfg);
</script>

Using SkyPack to serve ES Modules

<!-- For modern browsers, recommended. -->
<script type="module">
  import * as sfg from "https://cdn.skypack.dev/@smiley-face-game/api";
  console.log(sfg);
</script>

Examples

Usage in Different Environments

Node

const { useDev } = require("@smiley-face-game/api");
useDev();

HTML Global

<script src="https://unpkg.com/@smiley-face-game/api"></script>
<script>
  sfg.useDev();
</script>

HTML ES Modules

<script type="module">
  import { useDev } from "https://cdn.skypack.dev/@smiley-face-game/api";
  useDev();
</script>

Authentication

import { auth, useDev } from "@smiley-face-game/api";

main().catch(console.error);
async function main() {
  useDev();

  // authenticates as a guest, with the username "wee"
  const guest = await auth({ username: "wee" });

  // logs in using an email and password
  const user = await auth({
    email: "alice@alicenbob.com",
    password: "1234",
  });
}

Connecting to a World

import { auth, useDev } from "@smiley-face-game/api";

main().catch(console.error);
async function main() {
  useDev();
  const client = await auth({ username: "aaa" });

  // generates a *new* dynamic world.
  const newDynamic = await client.connect({
    type: "dynamic",
    name: "Cool new Bot World!",
    width: 25,
    height: 25,
  });

  // connects to an *existing* dynamic world.
  const existingDynamic = await client.connect({
    type: "dynamic",
    id: newDynamic.init.worldId,
  });

  // connects to a *saved* world - these are owned by someone
  const saved = await client.connect({
    type: "saved",
    id: "00000000-0000-0000-0000-000000000000",
  });
}

Chatting

import { auth, useDev } from "@smiley-face-game/api";

main().catch(console.error);
async function main() {
  useDev();
  const client = await auth({ username: "chatbot" });

  const connection = await client.connect({
    type: "dynamic",
    name: "Chat Example",
    width: 10,
    height: 10,
  });

  for await (const message of connection) {
    // if we received a chat message
    if (message.packetId === "SERVER_CHAT") {
      if (message.message === "!hello") {
        // we'll send something back
        connection.chat("hi!!!");
      }
    }
  }
}

Placing Blocks

import { auth, useDev } from "@smiley-face-game/api";

main().catch(console.error);
async function main() {
  useDev();
  const client = await auth({ username: "blockbot" });

  const connection = await client.connect({
    type: "dynamic",
    name: "Blocks Example",
    width: 10,
    height: 10,
  });

  // use `tileJson` to get the numeric id of `basic-red`.
  // for the names of tiles, it's best to consult `tiles.json`
  // you can find a `name` -> `image` mapping at:
  //   `packages/client/src/assets/tiles`
  const redId = connection.tileJson.id("basic-red");

  // fill the world with it
  for (let y = 0; y < connection.init.size.height; y++) {
    for (let x = 0; x < connection.init.size.width; x++) {
      connection.place(redId, { x: x, y: y });
    }
  }
}