Giter Site home page Giter Site logo

s2client-api-typescript's Introduction

s2client-api-typescript

npm version

This is a package that allows you to communicate with the StarCraft 2 API with a bot written in TypeScript.

It uses protobuf-ts to convert the official protocol files to TypeScript interfaces, enums, and serialization methods.

This project is similar to @node-sc2/core and @node-sc2/proto, which are written in JavaScript and use pbjs to convert the official proto files to JSON (instead of TypeScript).


Usage

This library provides several things:

1) The StarCraft2Client Class

StarCraft 2 exposes its API via a WebSocket server built-in to the game. (It is only available if you launch the game with some specific flags.) The StarCraft2Client class is an abstraction that can be used to easily send messages over the WebSocket connection.

For example:

import { launchStarCraft2, StarCraft2Client } from "s2client-api-typescript";

main().catch((err) => {
  console.error("Failed to run the program:", err);
});

async function main() {
  await launchStarCraft2();

  const client = new StarCraft2Client();
  await client.connect(); // Establishes a WebSocket connection with the game.

  const pingResponse = await client.ping();
  console.log(pingResponse);
}

2) StarCraft 2 API Interfaces and Enums

The official StarCraft 2 protocol files contain the interfaces and enums that are used in the API. These are automatically converted to TypeScript with the protobuf-ts tool and are exported from this library your use.

import { Race } from "s2client-api-typescript";

const MY_RACE = Race.Protoss;

Additionally, this library includes enums that were parsed from the "stableid.json" file (which maps unit/ability/upgrade numbers to names).

3) Helper Functions

When writing your bot, this library offers some helper functions that can get you up and running faster:

  • launchStarCraft2
  • getMapPath

The helper functions are described in more detail in the documentation; see below.

4) CSpell Dictionary

If you use CSpell, then you can import the StarCraft 2 dictionary by adding an "import" line to your "cspell.json" file:

"import": ["s2client-api-typescript/dictionaries/cspell-starcraft2.json"],

Docs

See the automatically generated TypeDoc documentation for every export that this library provides.

s2client-api-typescript's People

Contributors

zamiell avatar

Stargazers

Josh Summerhays avatar Erik Claesson avatar

Watchers

 avatar  avatar

s2client-api-typescript's Issues

`clientPorts` and `options` are required in `joinGame`

without clientPorts set, even to an empty array like clientPorts: [], we get:

Failed to run the program: TypeError: Cannot read properties of undefined (reading 'length')
    at RequestJoinGame$Type.internalBinaryWrite (C:\Users\Nic\Desktop\SC2 AI Arena\typescript-eslint-devcontainer-boilerplate-2022\node_modules\.pnpm\[email protected]\node_modules\s2client-api-typescript\src\proto\sc2api.ts:3895:45)```

via https://github.com/Zamiell/s2client-api-typescript/blob/main/src/proto/sc2api.ts#L3895

without options set, even to an empty object, like options: {}, we get:

 Got a response from StarCraft 2 for the "joinGame" command with the following error: MissingOption

so it seems like the types diverge from the requirements. you can either update the types to make them non-optional, or set a default value for these properties.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.