From 02f0c8e82cbe7f162cbb5477bea5a5120af5ef94 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Mon, 21 Dec 2015 04:10:25 -0600 Subject: [PATCH] Add githooks/build-indexes This is the search index build script from the Gen 6 Learnsets repository, slightly rewritten for the PS client environment. It expects a checkout of the server respository in `data/Pokemon-Showdown`. --- .gitignore | 1 + .htaccess | 1 + githooks/build-indexes | 105 +++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100755 githooks/build-indexes diff --git a/.gitignore b/.gitignore index bca957afb..bd968bc51 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /js/config.js /index.php /index.html +/data/Pokemon-Showdown/ node_modules/ eslint-cache/ .DS_Store diff --git a/.htaccess b/.htaccess index 2a66c3b39..f2888f903 100644 --- a/.htaccess +++ b/.htaccess @@ -98,6 +98,7 @@ RewriteRule ^~~([^:/]*):([0-9]*)(/.*)?$ http://$1-$2.psim.us$3 [R=301,L] RewriteRule ^backup/ - [F] RewriteRule ^\.git/ - [F] +RewriteRule ^data/Pokemon-Showdown/ - [F] RewriteRule ^lib/ - [F] RewriteRule ^githooks/ - [F] diff --git a/githooks/build-indexes b/githooks/build-indexes new file mode 100755 index 000000000..c90318ced --- /dev/null +++ b/githooks/build-indexes @@ -0,0 +1,105 @@ +#!/usr/bin/env node +'use strict'; + +require('sugar'); +global.toId = function (text) { + if (text && text.id) { + text = text.id; + } else if (text && text.userid) { + text = text.userid; + } + if (typeof text !== 'string' && typeof text !== 'number') return ''; + return ('' + text).toLowerCase().replace(/[^a-z0-9]+/g, ''); +}; +const Tools = require('../data/Pokemon-Showdown/tools').includeData(); +const fs = require("fs"); + +/********************************************************* + * Build search-index.js + *********************************************************/ + +{ + process.stdout.write("Building `data/search-index.js`... "); + + var index = []; + + index = index.concat(Object.keys(Tools.data.Pokedex).map(x => x + ' pokemon')); + index = index.concat(Object.keys(Tools.data.Movedex).map(x => x + ' move')); + index = index.concat(Object.keys(Tools.data.Items).map(x => x + ' item')); + index = index.concat(Object.keys(Tools.data.Abilities).map(x => x + ' ability')); + index = index.concat(Object.keys(Tools.data.TypeChart).map(x => toId(x) + ' type')); + index = index.concat(['physical', 'special', 'status'].map(x => toId(x) + ' category')); + index = index.concat(['monster', 'water1', 'bug', 'flying', 'field', 'fairy', 'grass', 'humanlike', 'water3', 'mineral', 'amorphous', 'water2', 'ditto', 'dragon', 'undiscovered'].map(x => toId(x) + ' egggroup')); + + index.sort(); + + // manually rearrange + index[index.indexOf('grass type')] = 'grass egggroup'; + index[index.indexOf('grass egggroup')] = 'grass type'; + + index[index.indexOf('fairy type')] = 'fairy egggroup'; + index[index.indexOf('fairy egggroup')] = 'fairy type'; + + index[index.indexOf('flying type')] = 'flying egggroup'; + index[index.indexOf('flying egggroup')] = 'flying type'; + + index[index.indexOf('dragon type')] = 'dragon egggroup'; + index[index.indexOf('dragon egggroup')] = 'dragon type'; + + index[index.indexOf('bug type')] = 'bug egggroup'; + index[index.indexOf('bug egggroup')] = 'bug type'; + + index[index.indexOf('psychic type')] = 'psychic move'; + index[index.indexOf('psychic move')] = 'psychic type'; + + index[index.indexOf('ditto pokemon')] = 'ditto egggroup'; + index[index.indexOf('ditto egggroup')] = 'ditto pokemon'; + + + let BattleSearchIndex = index.map(x => x.split(' ')[0]); + let BattleSearchIndexType = index.map(x => x.split(' ')[1]); + + let BattleSearchIndexOffset = BattleSearchIndex.map((id, i) => { + var name = ''; + switch (BattleSearchIndexType[i]) { + case 'pokemon': name = Tools.getTemplate(id).species; break; + case 'move': name = Tools.getMove(id).name; break; + case 'item': name = Tools.getItem(id).name; break; + case 'ability': name = Tools.getAbility(id).name; break; + } + var res = ''; + var nonAlnum = 0; + for (var i = 0, j = 0; i < id.length; i++, j++) { + while (!/[a-zA-Z0-9]/.test(name[j])) { + j++; + nonAlnum++; + } + res += nonAlnum; + } + if (nonAlnum) return res; + return ''; + }); + + let BattleSearchCountIndex = {}; + for (const type in Tools.data.TypeChart) { + BattleSearchCountIndex[type + ' move'] = Object.keys(Tools.data.Movedex).filter(id => (Tools.data.Movedex[id].type === type)).length; + } + + for (const type in Tools.data.TypeChart) { + BattleSearchCountIndex[type + ' pokemon'] = Object.keys(Tools.data.Pokedex).filter(id => (Tools.data.Pokedex[id].types.indexOf(type) >= 0)).length; + } + + var buf = '// automatically built with githooks/build-indexes\n\n'; + + buf += 'exports.BattleSearchIndex = ' + JSON.stringify(BattleSearchIndex) + ';\n\n'; + + buf += 'exports.BattleSearchIndexType = ' + JSON.stringify(BattleSearchIndexType) + ';\n\n'; + + buf += 'exports.BattleSearchIndexOffset = ' + JSON.stringify(BattleSearchIndexOffset) + ';\n\n'; + + buf += 'exports.BattleSearchCountIndex = ' + JSON.stringify(BattleSearchCountIndex) + ';\n\n'; + + fs.writeFileSync('data/search-index.js', buf); +} + +console.log("DONE"); diff --git a/package.json b/package.json index 96fffcd73..3f489322e 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ }, "dependencies": {}, "devDependencies": { - "eslint": "~1.10.3" + "eslint": "~1.10.3", + "sugar": "1.4.1" }, "private": true }