Skip to content

ghlin/node-ygocore

Repository files navigation

node-ygocore

WIP node bindings for ygopro-core (the OCG script engine)

NOTE This is not Ygopro the game!!!

Install

npm install ygocore

How to use

To use this package, you need to know how ocgcore works.

Note

I'm not the author/contributer of ygopro, if my understandings on ocgcore's API are not correct, please fire me an issue. Thanks!

import { engine } from 'ygopro';

this project is writen in Typescript, each method is type-annotated.

Prepare a duel

create a duel instance

ocgapi: create_duel()

const duel = engine.createDuel(/* any random seed */ 0);

set duelist's LP/cards to draw

ocgapi: set_player_info()

engine.setPlayerInfo(duel, { /* ... */ });

add card to the field

ocgapi: new_card()

engine.newCard(duel, { /* ... */ });

Duel!

start the duel

ocgapi: start_duel()

engine.startDuel(duel, options);

process for one step

ocgapi: process(), get_message()

const { flags, data } = engine.process(duel);

data is a buffer (of type Buffer) contains messages returned from ocgcore, to deserialize it, you can use ygocore-interface's parseMessage (see below)

write player's response

ocgapi: set_responsei(), set_responseb()

engine.setResponse(duel, response);

End the duel

ocgapi: end_duel()

engine.endDuel(duel);

Query the game field

query cards by location

ocgapi: query_field_card

const data = engine.queryFieldCard(duel, { /* player, location, ... */ });

Again, to deserialize the message, you can use ygocore-interface's parseFieldCardQueryResult.

query single card

ocgapi: query_card

const data = engine.queryCard(duel, { /* player, location, sequence, ... */ });

Again and again, to deserialize the message, you can use ygocore-interface's parseCardQueryResult.

query card count

ocgapi: query_field_count

const howManyCards = engine.queryFieldCount(duel, { /* ... */ });

ygocore-interface

const { parseMessage } from 'ygocore-interface';

const messages = parseMessage(data /* from engine.process */);

for (const message of messages) {
  if (message.msgtype == 'MSG_SELECT_IDLECMD') {
    // message is not of type 'MsgSelectIdlecmd'
    // handle MsgSelectIdlecmd here.

    // refresh M zone:
    const mzone = parseFieldCardQueryResult(engine.queryFieldCard(duel, {
      player, location, queryFlags, useCache
    });

    for (const card of mzone) {
      // play with card...
      // type card.
      //          ^
      //          |
      //      nice code completion!
    }
  }

  // handle other messages here.
}

Constants in common.h are also exported:

import { LOCATION, QUERY } from 'ygocore-interface';

/* .... */

const location = LOCATION.HAND;
const queryFlags = /* ... + ... + */ QUERY.LSCALE + QUERY.RSCALE + QUERY.STATUS;
const hand = parseFieldCardQueryResult(engine.queryFieldCard(duel, {
  player, location, queryFlags, useCache
});

About

[WIP] node bindings for ygopro-core (https://github.com/moecube/ygopro-core)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages