Skip to content

Commit

Permalink
feat: command line data dragon
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysomallos committed Jul 19, 2024
1 parent aec1b05 commit 13a1293
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# lor-deck-codes - Legends of Runeterra - Deck Codes

## 1.2.0 - 2024-07-19

### Features

- Add [Data Dragon](https://developer.riotgames.com/docs/lor#data-dragon) to generate deck information.
- Added a command line for direct usage or testing.

## 1.1.0 - 2022-05-19

### Updates

- Add new faction 'Runeterra'
- Add new faction 'Runeterra'.

### Fixes

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Expected result:
*/
```

## Command line

Use `yarn cli <code>` to show code output, with `--out-file` you can write additional deck information from [Data Dragon](https://developer.riotgames.com/docs/lor#data-dragon) into a file (html or json), optional you can change the `--language` (default is `en_us`).

Example `yarn cli CEAAECABAQJRWHBIFU2DOOYIAEBAMCIMCINCILJZAICACBANE4VCYBABAILR2HRL --out-file test.hml`.

## Test

To call the test first check out project and run `yarn install`.
Expand Down
25 changes: 16 additions & 9 deletions cli.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import camelCase from 'camelcase';
import fs from 'node:fs/promises';
import minimist from 'minimist';
import stringify from 'json-stringify-pretty-compact';
import {Deck, generateDataDragon} from './index.mjs';

const parameters = process.argv.slice(2);
const [code, ...parameters] = process.argv.slice(2);

if (parameters[0] === 'show') {
console.log(Deck.fromCode(parameters[1]));
}
if (parameters[0] === 'json') {
await fs.writeFile(parameters[1], stringify(await generateDataDragon().fetchData(parameters[2], parameters[3]), {indent: 2, maxLength: 180}));
}
if (parameters[0] === 'html') {
await fs.writeFile(parameters[1], await generateDataDragon().generatePageFromCode(parameters[2], parameters[3]));
console.log(Deck.fromCode(code));

if (parameters?.length) {
const dragon = generateDataDragon();

const {style, language, outFile} = Object.fromEntries(Object.entries(minimist(parameters)).map(([k, v]) => [camelCase(k), v]));

const data =
style === 'html' || outFile.endsWith('.html')
? await dragon.generatePageFromCode(code, language)
: stringify(await dragon.fetchData(code, language), {indent: 2, maxLength: 180});

await fs.writeFile(outFile, data);
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "lor-deck-encoder",
"version": "1.1.1",
"version": "1.2.0",
"description": "Legends of Runeterra Deck Encoder",
"exports": "./index.mjs",
"author": "Chrysomallos",
"repository": "https://github.com/chrysomallos/lor-deck-encoder.git",
"license": "MIT",
"type": "module",
"dependencies": {
"camelcase": "^8.0.0",
"json-stringify-pretty-compact": "^4.0.0",
"minimist": "^1.2.8",
"object-hash": "^3.0.0"
},
"devDependencies": {
Expand Down
26 changes: 23 additions & 3 deletions src/data-dragon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import hash from 'object-hash';
import request from '../utils/request.mjs';
import Deck from './deck.mjs';

export const LANGUAGES = {
en_us: 'English',
ko_kr: 'Korean',
ja_jp: 'Japanese',
es_es: 'Spanish',
es_mx: 'Mexico',
fr_fr: 'French',
de_de: 'German',
it_it: 'Italian',
pl_pl: 'Polish',
pt_br: 'Brazil',
ru_ru: 'Russian',
tr_tr: 'Turkish',
zh_cn: 'Chinese',
};

/**
* Base URL for data grabbing, see [Data Dragon](https://developer.riotgames.com/docs/lor#data-dragon).
*
Expand Down Expand Up @@ -74,11 +90,15 @@ export default class DataDragon {

/**
* Initializes the DataDragon instance by fetching core data and card data for the specified language.
* @param {string} language The language code (e.g. 'en_US') to fetch data for.
* @param {string} language The language code (e.g. 'en_us') to fetch data for.
* @returns {Promise<void>}
*/
async initialize(language) {
if (this.cardsByCode) return;
if (!LANGUAGES[language]) {
language = Object.keys(LANGUAGES).find(key => key === language || key.split('_').includes(language));
if (!language) language = 'en_us';
}
const tempFile = path.join(os.tmpdir(), `lor-data-dragon-temp-data-${language}.json`);

if (fs.existsSync(tempFile)) {
Expand Down Expand Up @@ -125,7 +145,7 @@ export default class DataDragon {
* @param {string} [language] The language code (e.g. 'en_us') to fetch data for.
* @returns {Promise<FetchedData>} The fetched data, including the deck and matching cards.
*/
async fetchData(code, language = 'en_US') {
async fetchData(code, language) {
await this.initialize(language);
const deck = Deck.fromCode(code);
return {
Expand Down Expand Up @@ -157,7 +177,7 @@ export default class DataDragon {
* @param {string} [language] The language code (e.g. 'en_us') to generate for.
* @returns {Promise<string>} The generated HTML content.
*/
async generatePageFromCode(code, language = 'en_US') {
async generatePageFromCode(code, language) {
const {deck, cardTypes, matchedCards, matchedRegions} = await this.fetchData(code, language);
return `<html>
<head>
Expand Down

0 comments on commit 13a1293

Please sign in to comment.