Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
feat(mods): add incursion t3 chest mods
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jun 6, 2018
1 parent eb9e8e8 commit 674ff4b
Show file tree
Hide file tree
Showing 11 changed files with 2,747 additions and 2 deletions.
42 changes: 42 additions & 0 deletions scripts/itemClassIdType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ts = require('typescript');

const { apiEndpoint } = require('./util');

// creates a union type for all possible string values of `item_class`

itemClasses().then(item_classes => {
const ids = item_classes.map(({ id }) => id);
const type = ts.createTypeAliasDeclaration(
[],
[],
'ItemClassId',
[],
ts.createUnionTypeNode(ids.map(id => ts.createLiteral(id))),
);

// print ts source
const source_file = ts.createSourceFile(
'schema.ts',
'',
ts.ScriptTarget.Latest,
false,
ts.ScriptKind.TS,
);
const printer = ts.createPrinter();
const source = printer.printNode(ts.EmitHint.Unspecified, type, source_file);

console.log(source);
});

async function itemClasses(options = {}) {
const {
api_root = 'http://localhost:3000/',
auth_token = 'allowme',
} = options;

const endpoint = '/find/ItemClasses/';
const params = { page_size: Number.MAX_SAFE_INTEGER };

const body = await apiEndpoint(endpoint, { api_root, auth_token, params });
return body.result;
}
74 changes: 74 additions & 0 deletions scripts/util/apiEndpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const fs = require('fs');
const http = require('http');
const { URL } = require('url');
const { promisify } = require('util');

const writeFile = promisify(fs.writeFile);

module.exports = async function apiEndpoint(endpoint, options = {}) {
const {
api_root = 'http://localhost:3000/',
auth_token = 'allowme',
params = {},
} = options;

const api_url = new URL(endpoint, api_root);
setParams(api_url, params);

const body = await httpGet(api_url, {
headers: { Authorization: auth_token },
});

const json = JSON.parse(body);
return json;
};

function identity(arg) {
return arg;
}

function setParams(url, params) {
if (params === null || typeof params !== 'object') {
throw new Error('no object given');
}

for (const key of Object.keys(params)) {
url.searchParams.set(key, params[key]);
}

return url;
}

/**
*
* @param {URL} url
* @param {*} options
*/
function httpGet(url, options = {}) {
return new Promise((resolve, reject) => {
let data = '';

http
.get(
{
host: url.hostname,
port: url.port,
path: `${url.pathname}${url.search}`,
headers: {
Authorization: 'allowme',
},
},
res => {
res.on('data', chunk => (data += chunk));
res.on('end', () => {
if (res.statusCode === 200) {
resolve(data);
} else {
reject(data);
}
});
},
)
.on('error', e => reject(e));
});
}
3 changes: 3 additions & 0 deletions scripts/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const apiEndpoint = require('./apiEndpoint');

module.exports = { apiEndpoint };
8 changes: 6 additions & 2 deletions src/generators/Orb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Container from '../containers/Container';
import { ModProps } from '../schema';
import { ModProps, SpawnWeightProps } from '../schema';
import { Mod } from '../mods';
import { Flags, anySet } from '../util/Flags';
import { choose } from '../util/rng';
Expand Down Expand Up @@ -69,7 +69,7 @@ export default abstract class Orb<C extends Container<any>> extends Generator<
no_matching_tags: false,
spawnweight_zero: false,
};
const spawnweight = mod.spawnweightPropsFor(container);
const spawnweight = this.spawnweightFor(mod, container);

if (spawnweight == null) {
// at first glance this shouldn't be happening
Expand All @@ -83,6 +83,10 @@ export default abstract class Orb<C extends Container<any>> extends Generator<
return spawnable_flags;
}

public spawnweightFor(mod: Mod, container: C): SpawnWeightProps | undefined {
return mod.spawnweightPropsFor(container);
}

public modsFor(
container: C,
whitelist: string[] = [],
Expand Down
2 changes: 2 additions & 0 deletions src/generators/ViewOnlyOrb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Orb from './Orb';
import Item from '../containers/item';
import { Flags } from '../util';

export { SpawnableFlags, SpawnableFlag } from './Orb';

export interface ApplicableFlags extends Flags {
not_applicable: boolean;
}
Expand Down

0 comments on commit 674ff4b

Please sign in to comment.