1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327 |
- const path = require('./path.json');
- const readline = require('readline');
- var archiver = require('archiver');
- const fs = require('fs');
- const xl = require('xlsx');
- let excelDir = './excel'; // excel配置表目录
- let outDir = './out'; // 导出目录
- let outJsonFolder = 'excel'; // 导出的json目录
- //前端需要在load界面加载的表
- let loadJson = ["audioInfo", "languageLang", "languageClientText", "mathInfo", "packageInfo"]
- const cluster = require('cluster');
- //后端配置
- let serverPath = "../zhetian_backend_kaifa/common/"
- let apiurl = "../zhetian_backend_kaifa/public/api/assets/main.bundle.js"
- // 前后端解析索引的行
- let hang = 3; // 前端3 后端4
- // 表格列表 example: ["iconopen","mainTask"]
- let excelFiles = [];
- // 表名 +分表名
- let excelFen = [];
- // 表格与表单的数据类型 example:{iconopen:{id:string}}
- let excelAndSheets = {};
- var s_xls = require('./s_xls');
- const { exit } = require('process');
- // 检测传入的参数
- var args = process.argv.splice(2)
- var jiexiTarget = args[0] || "" // s:后端 c:前端
- function exeExcelConfig_c() {
- let str = '';
- str += `import GameController from "../GameController"\nimport { gameMethod } from "./gameMethod"\n`
- // 添加类型定义
- excelFiles.forEach((excelName) => {
- str += `// type for ${excelName}.excel\n`;
- for (const sheetName in excelAndSheets[excelName]) {
- str += `export type xls${toUpperCaseByStr(excelName)}${toUpperCaseByStr(sheetName)} = {`;
- //遍历索引存入类型定义
- let keyObjList = excelAndSheets[excelName][sheetName];
- for (const key in keyObjList) {
- str += `${key.split(',')[0]}:${keyObjList[key].value},`;
- }
- // 去掉最后一个逗号 ","
- if (str.charAt(str.length - 1) == ',') {
- str = str.substring(0, str.length - 1);
- }
- str += '}';
- str += '\n';
- }
- });
- str += '\n';
- //导出
- str += `export class XlsConfig {\n`;
- excelFen.forEach((element) => {
- str += ` xls${toUpperCaseByStr(element)}: xls${toUpperCaseByStr(element)}[]\n`;
- });
- // str += ` constructor(callback: Function) {\n`
- // let str111 = [];
- // excelFen.forEach((element) => {
- // str111.push('"' + element + '"');
- // });
- // str += ` }\n`
- str += `
- constructor(bundleName:string, callback: Function, progressCallback?: Function) {
- cc.assetManager.loadBundle(bundleName, (err, bundle) => {
- if (err) {
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- if (gameMethod.isEmpty(bundle) && cc.sys.platform == cc.sys.WECHAT_GAME) {
- //小游戏平台,缓存过期,自动清理缓存,重启游戏
- console.error("加载配置表的bundle失败,清理缓存重新加载")
- cc.assetManager.cacheManager.clearCache()
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- bundle.loadDir("",(finish: number, total: number, item: any)=>{
- if (progressCallback) {
- progressCallback(finish, total);
- }
- }, (err2, assets: cc.JsonAsset[]) => {
- if (err2) {
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- let current = 0;
- assets.forEach(element => {
- this["xls" + this.firstUpCase(element.name)] = element.json;
- current++;
- });
- callback(this)
- })
- })
- }
- // 首字母大写
- private firstUpCase(str: string) {
- return str[0].toUpperCase() + str.substring(1)
- }
- }`;
- if (hang == 3) {
- outDir = path.configPath;
- } else {
- outDir = serverPath;
- }
- // 写入文件
- let outPath = `${outDir}xlsConfig.ts`;
- fs.writeFile(outPath, str, function (err) {
- if (err) {
- console.log('xlsConfig写入失败:', err);
- }
- });
- let pro = '';
- if (hang == 4) {
- pro += 'import { CTX } from "../src/util/master";\n';
- }
- pro += `export default class ConfProxy<T> {
- static getKey(args: string[]): string {
- let out = ""
- args.forEach(element => {
- out += element
- out += "_"
- });
- return out
- }
- static setKey(target: any, args: string[]): string {
- let out = ""
- args.forEach(element => {
- out += target[element]
- out += "_"
- });
- return out
- }
- pool: { [key: string]: T } = {}
- name:string = ""
- constructor(xlsname:string,conf: T[], ...args: string[]) {
- this.name = xlsname
- conf.forEach(element => {
- this.pool[ConfProxy.setKey(element, args)] = element
- });
- }
-
- getItem(...args: string[]): T | null {
- return this.pool[ConfProxy.getKey(args)]
- }`;
- if (hang == 4) {
- pro += `
-
- getItemCtx(ctx:CTX,...args: string[]): T {
- let back = this.pool[ConfProxy.getKey(args)]
- if(back == null){
- console.error("配置错误"+args,this.name)
- ctx.throw("配置错误"+args)
- }
- return back
- }
- `;
- }
- pro += `
- }
- export class ConfListProxy<T> {
- pool: { [key: string]: T[] } = {}
- name:string = ""
- constructor(xlsname:string,conf: T[], ...args: string[]) {
- this.name = xlsname
- conf.forEach(element => {
- let key = ConfProxy.setKey(element, args)
- if (this.pool[key] == null) {
- this.pool[key] = []
- }
- this.pool[key].push(element)
- });
- }
- getItemList(...args: string[]): T[] | null {
- return this.pool[ConfProxy.getKey(args)]
- }
- }`;
- pro += '\n';
- // 写入文件
- let proPath = '';
- if (hang == 3) {
- proPath = `${path.configPath}confProxy.ts`;
- } else {
- proPath = serverPath + `confProxy.ts`;
- }
- fs.writeFile(proPath, pro, function (err) {
- if (err) {
- console.log('ConfProxy写入失败:', err);
- }
- });
- }
- function exeExcelConfig() {
- let str = '';
- // 引入ts文件
- excelFen.forEach((element) => {
- str += `const ${element} = require( "./excel/${element}.json")\n`;
- });
- str += '\n';
- // 添加类型定义
- excelFiles.forEach((excelName) => {
- str += `// type for ${excelName}.excel\n`;
- for (const sheetName in excelAndSheets[excelName]) {
- str += `export type xls${toUpperCaseByStr(excelName)}${toUpperCaseByStr(sheetName)} = {`;
- //遍历索引存入类型定义
- let keyObjList = excelAndSheets[excelName][sheetName];
- for (const key in keyObjList) {
- str += `${key.split(',')[0]}:${keyObjList[key].value},`;
- }
- // 去掉最后一个逗号 ","
- if (str.charAt(str.length - 1) == ',') {
- str = str.substring(0, str.length - 1);
- }
- str += '}';
- str += '\n';
- }
- });
- str += '\n';
- //导出
- str += `export class XlsConfig {\n`;
- excelFen.forEach((element) => {
- str += ` xls${toUpperCaseByStr(element)}: xls${toUpperCaseByStr(element)}[]\n`;
- });
- str += ` constructor() {\n`;
- excelFen.forEach((element) => {
- str += ` this.xls${toUpperCaseByStr(element)} = <xls${toUpperCaseByStr(element)}[]>${element}\n`;
- });
- str += ` }\n`;
- str += '}\n';
- if (hang == 3) {
- outDir = path.configPath;
- } else {
- outDir = serverPath
- }
- // 写入文件
- let outPath = `${outDir}xlsConfig.ts`;
- fs.writeFile(outPath, str, function (err) {
- if (err) {
- console.log('xlsConfig写入失败:', err);
- }
- });
- let pro = '';
- if (hang == 4) {
- pro += 'import { CTX } from "../src/util/master";\n';
- }
- pro += `export default class ConfProxy<T> {
- static getKey(args: string[]): string {
- let out = ""
- args.forEach(element => {
- out += element
- out += "_"
- });
- return out
- }
- static setKey(target: any, args: string[]): string {
- let out = ""
- args.forEach(element => {
- out += target[element]
- out += "_"
- });
- return out
- }
- pool: { [key: string]: T } = {}
- name:string = ""
- constructor(xlsname:string,conf: T[], ...args: string[]) {
- this.name = xlsname
- conf.forEach(element => {
- this.pool[ConfProxy.setKey(element, args)] = this.deepFreeze(element)
- });
- }
-
- getItem(...args: string[]): T | null {
- return this.pool[ConfProxy.getKey(args)]
- }`;
- if (hang == 4) {
- pro += `
-
- getItemCtx(ctx:CTX,...args: string[]): T {
- let back = this.pool[ConfProxy.getKey(args)]
- if(back == null){
- console.error("配置错误"+args,this.name)
- ctx.throw("配置错误"+args)
- }
- return back
- }
- deepFreeze<T>(obj: T): T {
- Object.freeze(obj);
- Object.getOwnPropertyNames(obj).forEach(prop => {
- const value = (obj as Record<string, any>)[prop];
- if (value !== null
- && (typeof value === "object" || typeof value === "function")
- && !Object.isFrozen(value)) {
- this.deepFreeze(value);
- }
- });
- return obj as T;
- }
- `;
- }
- pro += `
- }
- export class ConfListProxy<T> {
- pool: { [key: string]: T[] } = {}
- name:string = ""
- constructor(xlsname:string,conf: T[], ...args: string[]) {
- this.name = xlsname
- conf.forEach(element => {
- let key = ConfProxy.setKey(element, args)
- if (this.pool[key] == null) {
- this.pool[key] = []
- }
- this.pool[key].push(element)
- });
- }
- getItemList(...args: string[]): T[] | null {
- return this.pool[ConfProxy.getKey(args)]
- }`;
- if (hang == 4) {
- pro += `
-
- getItemListCtx(ctx:CTX,...args: string[]): T[] {
- let back = this.pool[ConfProxy.getKey(args)]
- if(back == null){
- console.error("list配置错误"+args,this.name)
- ctx.throw("list配置错误"+args)
- }
- return back
- }
- `;
- }
- pro += `
- }
- `
- pro += '\n';
- // 写入文件
- let proPath = '';
- if (hang == 3) {
- proPath = `${path.configPath}confProxy.ts`;
- } else {
- proPath = serverPath + `confProxy.ts`;
- }
- fs.writeFile(proPath, pro, function (err) {
- if (err) {
- console.log('ConfProxy写入失败:', err);
- }
- });
- }
- // 字符串开头变为大写
- function toUpperCaseByStr(txt) {
- return txt.slice(0, 1).toUpperCase() + txt.slice(1);
- }
- // 分析配置表
- function doExcel(excel) {
- let excelName = excel.split('_')[0];
- if (s_xls.XlsBack[excelName] == null) {
- return
- }
- let fileDir = `${excelDir}/${excel}`;
- let workbook = xl.readFile(fileDir);
- // 获取 Excel 中所有表名
- const sheetNames = workbook.SheetNames; // 返回 ['sheet1', 'sheet2']
- let _sheetNames = [];
- for (const element of sheetNames) {
- if (s_xls.XlsBack[excelName][element] == null) {
- continue
- }
- _sheetNames.push(element);
- }
- if (_sheetNames.length == 0) {
- return;
- }
- console.time("...start excel " + excel)
- // 加到【excelAndSheets】中
- let obj = {};
- excelAndSheets[excelName] = {};
- _sheetNames.forEach((sheetName) => {
- let sheet = workbook.Sheets[sheetName];
- excelAndSheets[excelName][sheetName] = {};
- doSheet(obj, excelName, sheetName, sheet);
- });
- let outDir1 = ""
- let outDir2 = ""
- if (hang == 3) {
- outDir1 = `${path.jsonPath}`;
- if (!fs.existsSync(outDir1)) {
- fs.mkdirSync(outDir1)
- }
- outDir2 = `${path.loadJsonPath}`;
- if (!fs.existsSync(outDir2)) {
- fs.mkdirSync(outDir2)
- }
- } else {
- outDir1 = serverPath + `${outJsonFolder}`;
- if (!fs.existsSync(outDir1)) {
- fs.mkdirSync(outDir1)
- }
- }
- for (const key in obj) {
- let jsonStr = `${geshihua(obj[key])}`;
- if (outDir2 != "" && loadJson.indexOf(key) >= 0) {
- fs.writeFileSync(`${outDir2}/${key}.json`, jsonStr, function (err) {
- if (err) {
- console.log(err);
- }
- });
- } else {
- fs.writeFileSync(`${outDir1}/${key}.json`, jsonStr, function (err) {
- if (err) {
- console.log(err);
- }
- });
- }
- }
- console.timeEnd("...start excel " + excel)
- }
- function geshihua(data) {
- let count = 1;
- let _data = '[\n';
- for (const val of data) {
- if (count == 1) {
- _data += ' ' + JSON.stringify(val);
- } else {
- _data += `,\n`;
- _data += ' ' + JSON.stringify(val);
- }
- count++;
- }
- _data += '\n]';
- return _data;
- }
- //分析表单 obj数据,表名,表单名,表单数据内容
- function doSheet(obj, excelName, sheetName, sheet) {
- let newKey = excelName + toUpperCaseByStr(sheetName);
- excelFen.push(newKey);
- if (obj[newKey] == null) {
- obj[newKey] = [];
- }
- // 索引列表 example:{A:"",B:"id",C:"hp",D:"attack"}
- let keyList = {};
- // 类型列表 example:{A:"number",B:"string",C:"boolean",D:"{}[]"}
- let typeList = {};
- //备注
- let psList = {};
- let _tempObj = {};
- for (const key in sheet) {
- // 单元格定位id example: A25
- const sheetKey = sheet[key];
- // A25->25
- let _hang = key.replace(/[^0-9]+/gi, '');
- // A25->A
- let _lie = key.replace(/[^A-Z]+/gi, '');
- if (_lie == 'A') {
- if (Object.keys(_tempObj).length > 0) {
- //这边检测是否有放空配置的。如果有 , 补上默认类型
- let _tempObj_1 = {};
- for (const key in keyList) {
- if (_tempObj[keyList[key]] == null) {
- if (typeList[key] == 'number') {
- _tempObj_1[keyList[key]] = 0;
- } else if (typeList[key] == 'string') {
- _tempObj_1[keyList[key]] = '';
- } else if (typeList[key] == 'boolean') {
- _tempObj_1[keyList[key]] = false;
- } else if (typeList[key].indexOf('[]') != -1) {
- _tempObj_1[keyList[key]] = null;
- } else if (typeList[key].substring(0, 0 + 1) == '{') {
- _tempObj_1[keyList[key]] = null;
- } else if (typeList[key][0] == "[" && typeList[key][typeList[key].length - 1] == "]") { // 元组
- _tempObj_1[keyList[key]] = null;
- } else {
- console.error('配置表有未知定义', excelName, sheetName, keyList[key], typeList[key]);
- }
- } else {
- _tempObj_1[keyList[key]] = _tempObj[keyList[key]];
- }
- }
- obj[newKey].push(_tempObj_1);
- _tempObj = {};
- }
- }
- if (sheet['A' + _hang] == null) {
- // 第一列没有配置参数,就跳过
- continue;
- }
- if (_hang == 1) {
- psList[_lie] = sheetKey.v;
- }
- // 储存类型
- if (_hang == 2) {
- typeList[_lie] = sheetKey.v;
- }
- // 储存索引
- if (_hang == hang && sheetKey.v && typeList[_lie]) {
- // 正则抽取真正的值(去掉所有的*)
- let realVal = sheetKey.v.replace(/[\*]/g, '');
- // 判断下作为key的字段
- let isKey = 0; // 0:不是key,1:单key,2:列表key
- if (sheetKey.v.slice(0, 2) == '**') {
- isKey = 2;
- } else if (sheetKey.v.slice(0, 1) == '*') {
- isKey = 1;
- }
- keyList[_lie] = realVal.split(',')[0].trim();
- // id:string
- excelAndSheets[excelName][sheetName][keyList[_lie]] = {
- ps: psList[_lie],
- isKey: isKey,
- value: typeList[_lie],
- keys: realVal
- };
- }
- // 储存数据
- if (_hang >= 5 && keyList[_lie] != null && typeList[_lie] != null) {
- // 根据类型转化数据内容,在做储存处理
- let _val;
- if (typeList[_lie] == 'number') {
- if (typeof sheetKey.v != 'number') {
- _val = Number(sheetKey.v);
- if (isNaN(_val)) {
- console.log(
- '\x1b[33m%s\x1b[0m',
- '警告,出现NaN数据类型,表名:' +
- excelName +
- ' 表单:' +
- sheetName +
- ' 索引:' +
- keyList[_lie] +
- ' 表格ID:' +
- key
- );
- }
- _val = 0;
- } else {
- _val = sheetKey.v;
- }
- } else if (typeList[_lie] == 'boolean') {
- if (sheetKey.v == 'true' || sheetKey.v == 'TRUE' || sheetKey.v == true) {
- _val = true;
- } else {
- _val = false;
- }
- } else if (typeList[_lie][0] == '{' || typeList[_lie].slice(-1) == ']') {
- try {
- _val = JSON.parse(sheetKey.v);
- } catch (error) {
- console.log(
- '\x1b[33m%s\x1b[0m',
- '警告:json格式错误,表名:' + excelName + ' 表单:' + sheetName + ' 索引:' + keyList[_lie] + ' 表格ID:' + key
- );
- }
- } else if (typeList[_lie] == 'string') {
- if (typeof sheetKey.v == 'number') {
- _val = Math.floor(sheetKey.v).toString();
- } else {
- _val = sheetKey.v;
- }
- } else {
- _val = sheetKey.v;
- }
- _tempObj[keyList[_lie].trim()] = _val;
- }
- }
- //这边检测是否有放空配置的。如果有 , 补上默认类型
- let _tempObj_1 = {};
- for (const key in keyList) {
- if (_tempObj[keyList[key]] == null) {
- if (typeList[key] == 'number') {
- _tempObj_1[keyList[key]] = 0;
- } else if (typeList[key] == 'string') {
- _tempObj_1[keyList[key]] = '';
- } else if (typeList[key] == 'boolean') {
- _tempObj_1[keyList[key]] = false;
- } else if (typeList[key].indexOf('[]') != -1) {
- _tempObj_1[keyList[key]] = null;
- } else if (typeList[key].substring(0, 0 + 1) == '{') {
- _tempObj_1[keyList[key]] = null;
- } else if (typeList[key][0] == "[" && typeList[key][typeList[key].length - 1] == "]") { // 元组
- _tempObj_1[keyList[key]] = null;
- } else {
- console.error('配置表有未知定义', excelName, sheetName, keyList[key], typeList[key]);
- }
- } else {
- _tempObj_1[keyList[key]] = _tempObj[keyList[key]];
- }
- }
- // 储存最后一个
- obj[newKey].push(_tempObj_1);
- }
- // //--------------------start------------------- 控制台人机交互
- function readSyncByRl(tips) {
- tips = tips || '> ';
- return new Promise((resolve) => {
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- rl.question(tips, (answer) => {
- rl.close();
- resolve(answer.trim());
- });
- });
- }
- /**
- * 后端创建xls对应gameCfg完成脚本
- */
- function createGameCfg_s() {
- let strs = '';
- strs += `import ConfProxy, { ConfListProxy } from "./confProxy"\n`;
- let strHead = `XlsConfig`;
- let count = 2;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- strHead += `, xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}`;
- count++;
- if (count % 9 == 0) {
- strHead += `\n`;
- }
- }
- }
- strs += `import { ${strHead} } from "./xlsConfig"\n\n`;
- strs += `export default class Gamecfg {\n`;
- strs += ` static init() {\n`;
- strs += ` let xls = new XlsConfig()\n`;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- for (const val of s_xls.XlsBack[key1][key2]) {
- let cs = ``;
- for (let index = 1; index < val.length; index++) {
- cs += `,"${val[index]}"`;
- }
- if (val[0] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(key2)} = new ${val[0]}("${key1}${toUpperCaseByStr(key2)}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(key2)}List = new ${val[0]}("${key1}${toUpperCaseByStr(key2)}List",xls.xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- let cs1 = ``;
- for (let index = 2; index < val.length; index++) {
- cs1 += `,"${val[index]}"`;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(val[0])} = new ${val[1]}("${key1}${toUpperCaseByStr(val[0])}",xls.xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}("${key1}${toUpperCaseByStr(
- val[0]
- )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- }
- }
- }
- strs += ` }\n`;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- for (const val of s_xls.XlsBack[key1][key2]) {
- if (val[0] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)} : ${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)} >\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)}List : ${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])} : ${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(
- val[0]
- )}List : ${val[1]}<xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- }
- }
- }
- strs += `}\n`;
- // 写入文件
- let proPath = '';
- if (hang == 3) {
- proPath = `${path.configPath}gameCfg.ts`;
- } else {
- proPath = serverPath + `gameCfg.ts`;
- }
- fs.writeFile(proPath, strs, function (err) {
- if (err) {
- console.log('gameCfg写入失败:', err);
- }
- });
- }
- /**
- * 前端创建xls对应gameCfg完成脚本
- */
- function createGameCfg_c() {
- let strs = '';
- strs += `import ConfProxy, { ConfListProxy } from "./confProxy"\n`;
- let strHead = `XlsConfig`;
- let count = 2;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- strHead += `, xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}`;
- count++;
- if (count % 9 == 0) {
- strHead += `\n`;
- }
- }
- }
- strs += `import { ${strHead} } from "./xlsConfig"\n\n`;
- strs += `export default class Gamecfg {\n`;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- for (const val of s_xls.XlsBack[key1][key2]) {
- if (val[0] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)}:${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)}List:${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])}:${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])}List:${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- }
- }
- }
- strs += `\n`;
- strs += ` static initLoading(loadSuccess: Function) {\n`;
- strs += ` try {\n`;
- strs += ` new XlsConfig("loadJson",(xls: XlsConfig) => {\n`;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- let jsonName = key1 + toUpperCaseByStr(key2)
- if (loadJson.indexOf(jsonName) < 0) {
- continue
- }
- for (const val of s_xls.XlsBack[key1][key2]) {
- let cs = ``;
- for (let index = 1; index < val.length; index++) {
- cs += `,"${val[index]}"`;
- }
- if (val[0] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )} = new ${val[0]}("${key1}${toUpperCaseByStr(
- key2
- )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )}List = new ${val[0]}("${key1}${toUpperCaseByStr(
- key2
- )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- let cs1 = ``;
- for (let index = 2; index < val.length; index++) {
- cs1 += `,"${val[index]}"`;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )} = new ${val[1]}("${key1}${toUpperCaseByStr(
- val[0]
- )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}("${key1}${toUpperCaseByStr(
- val[0]
- )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- }
- }
- }
- strs += `
- loadSuccess(true)
- })
- } catch (error) {
- loadSuccess(false)
- }
- }`;
- strs += `\n`;
- strs += ` static init(loadSuccess: Function, progressCallback: Function) {\n`;
- strs += ` try {\n`;
- strs += ` new XlsConfig("pzwj",(xls: XlsConfig) => {\n`;
- for (const key1 in s_xls.XlsBack) {
- for (const key2 in s_xls.XlsBack[key1]) {
- if (s_xls.XlsBack[key1][key2].length <= 0) {
- continue;
- }
- let jsonName = key1 + toUpperCaseByStr(key2)
- if (loadJson.indexOf(jsonName) >= 0) {
- continue
- }
- for (const val of s_xls.XlsBack[key1][key2]) {
- let cs = ``;
- for (let index = 1; index < val.length; index++) {
- cs += `,"${val[index]}"`;
- }
- if (val[0] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )} = new ${val[0]}("${key1}${toUpperCaseByStr(
- key2
- )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )}List = new ${val[0]}("${key1}${toUpperCaseByStr(
- key2
- )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- let cs1 = ``;
- for (let index = 2; index < val.length; index++) {
- cs1 += `,"${val[index]}"`;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )} = new ${val[1]}("${key1}${toUpperCaseByStr(
- val[0]
- )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}("${key1}${toUpperCaseByStr(
- val[0]
- )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- }
- }
- }
- strs += `
- loadSuccess(true)
- }, progressCallback)
- } catch (error) {
- loadSuccess(false)
- }
- }
- }`;
- // 写入文件
- let proPath = '';
- if (hang == 3) {
- proPath = `${path.configPath}gameCfg.ts`;
- } else {
- proPath = serverPath + `gameCfg.ts`;
- }
- fs.writeFile(proPath, strs, function (err) {
- if (err) {
- console.log('gameCfg写入失败:', err);
- }
- });
- }
- function createXyc() {
- const body = fs.readFileSync(apiurl, "utf-8");
- let arr = body.split('/a/apidoc"}]},')
- arr[1] = '[' + arr[1]
- let arr1 = arr[1].split('}];const');
- let str2 = arr1[0].replace(/!1/g, '0') + '}]';
- let apidoc = `//所有接口定义\n`
- apidoc += `export let apidoc:{[key:string]: {name:string,parms: {[key:string]: {type: string, desc: string}}}} = {\n`
- try {
- let strc = '';
- var jsons = eval('(' + str2 + ')');
- for (const json of jsons) {
- let jsontitle = json.url.split('/');
- let title = toUpperCaseByStr(jsontitle[1]) + toUpperCaseByStr(jsontitle[2]);
- let jsonUrl = json.url.slice(1);
- strc += `//${json.name}\n`;
- strc += `export class ${title}\n`;
- strc += `{\n`;
- strc += ` static url:string = "${jsonUrl}"\n`;
- strc += `}\n`;
- strc += `export interface ${title}Pram{\n`;
- apidoc += ` "/${jsonUrl}":{name:"${json.name}",parms:{`
- let _Parameter = [];
- let hasUuid = 0;
- let hasToken = 0;
- if (json.parameter != null) {
- for (const Parameter of json.parameter.fields.Parameter) {
- let description = Parameter.description;
- let arr11 = description.split('<p>');
- let arr12 = arr11[1].split('</p>');
- let _type = Parameter.type;
- _Parameter.push({
- type: _type,
- field: Parameter.field,
- description: arr12[0]
- });
- if (Parameter.field == 'uuid') {
- hasUuid = 1;
- }
- if (Parameter.field == 'token') {
- hasToken = 1;
- }
- }
- }
-
- for (const _rameter of _Parameter) {
- if (hasUuid == 1 && hasToken == 1) {
- if (_rameter.field == 'uuid' || _rameter.field == 'token') {
- continue;
- }
- }
- strc += ` ${_rameter.field}:${_rameter.type} //${_rameter.description}\n`;
- apidoc += `"${_rameter.field}": {type: "${_rameter.type}", desc: "${_rameter.description}"},`
- }
- strc += `}\n\n`;
- apidoc += `}},\n\n`;
- }
- apidoc += `}`
- if (hang == 3) {
- outDir = path.configPath;
- } else {
- outDir = serverPath
- }
- strc += `
- //前端返回的特殊事件
- export interface keyValue{
- [key:string]:number
- }
- export interface keyValues{
- [key:string]:number[]
- }
- export interface keyValueString{
- [key:string]:string
- }
- export type ZixuanSelectCs = [string,string,number];
- export type Wxmspinzhi = [number,number]
- export type Wxmskeys = [number,string,string];
- export type Wxmslm = [number,string];
- `
- let proPath = `${outDir}Xyc.ts`;
- fs.writeFileSync(proPath, strc, function (err) {
- if (err) {
- console.log('Xyc写入失败:', err);
- }
- });
- let proPath1 = `${outDir}apidoc.ts`;
- fs.writeFileSync(proPath1, apidoc, function (err) {
- if (err) {
- console.log('Xyc写入失败:', err);
- }
- });
- } catch (error) {
- console.log('===', error);
- }
- }
- //拷贝 s_to_proto
- function copy_s_to_proto() {
- fs.copyFile(serverPath + 'Xys.ts', './s_to_proto/Xys.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile(serverPath + 'Xyc.ts', './s_to_proto/Xyc.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile(serverPath + 'gameMethod.ts', './s_to_proto/gameMethod.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile(serverPath + 'fight.ts', './s_to_proto/fight.ts', (err) => {
- console.log('=', err);
- });
- }
- //拷贝 proto
- function copy_proto_to_c() {
- outDir = path.configPath;
- fs.copyFile('./s_to_proto/Xys.ts', outDir + 'Xys.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile('./s_to_proto/Xyc.ts', outDir + 'Xyc.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile('./s_to_proto/gameMethod.ts', outDir + 'gameMethod.ts', (err) => {
- console.log('=', err);
- });
- fs.copyFile('./s_to_proto/fight.ts', outDir + 'fight.ts', (err) => {
- console.log('=', err);
- });
- }
- /**
- * 延迟多少毫秒
- */
- function sleep(ms) {
- if (ms < 1) {
- return;
- }
- return new Promise((reslove, reject) => {
- setTimeout(() => {
- reslove(1);
- }, ms);
- });
- }
- // 开始解析配置表
- function doExe(res) {
- // console.log(res);
- if (res != 'c' && res != 's' && res != 'C' && res != 'S') {
- console.log('非法输入');
- return;
- }
- if (res == 'c' || res == 'C') {
- hang = 3;
- } else {
- hang = 4;
- }
- if (hang == 3) {
- outDir = path.configPath;
- } else {
- outDir = serverPath;
- }
- console.log('\x1b[37m'); // 控制台文字切为白色
- if (hang == 4) {
- fs.rm(serverPath + outJsonFolder, { recursive: true }, (err, data) => {
- if (err) {
- console.log('删除json文件失败' + err);
- }
- console.log('删除json文件成功');
- let sendNum = 0
- let overNum = 0
- // 解析表格
- fs.readdirSync(excelDir).forEach((excelName) => {
- if ((excelName.substr(-4) == '.xls' || excelName.substr(-5) == '.xlsx') && excelName.substr(0, 1) != '~') {
- excelFiles.push(excelName.split('_')[0]);
- const worker = cluster.fork(excelName);
- sendNum += 1
- worker.send({ "hang": 4, "excelName": excelName })
- worker.on('message', function (mes) {
- let mesarr = JSON.parse(mes)
- for (const ar of mesarr.excelFen) {
- excelFen.push(ar)
- }
- for (const key in mesarr.excelAndSheets) {
- excelAndSheets[key] = mesarr.excelAndSheets[key]
- }
- overNum += 1
- console.log(`配置表解析 发送${sendNum}个,完成${overNum}个`)
- })
- }
- });
- const intervalId = setInterval(() => {
- if (sendNum <= overNum) {
- createXyc();
- console.log('*******创建xyc完成*******');
- createGameCfg_s();
- console.log('*******创建xls对应gameCfg完成*******');
- copy_s_to_proto();
- console.log('*******拷贝完成*******');
- exeExcelConfig();
- console.log('*******创建配置文件完成*******');
- console.log("----全部完成----")
- clearInterval(intervalId);
- }
- }, 1000);
- });
- }
- if (hang == 3) {
- fs.unlink(path.excelPath + outJsonFolder + '/excel.zip', (err, data) => {
- if (err) {
- console.log('删除zip文件失败' + err);
- }
- console.log('删除zip文件成功');
- let sendNum = 0
- let overNum = 0
- let xls = {}
- // 解析表格
- fs.readdirSync(excelDir).forEach((excelName) => {
- if ((excelName.substr(-4) == '.xls' || excelName.substr(-5) == '.xlsx') && excelName.substr(0, 1) != '~') {
- excelFiles.push(excelName.split('_')[0]);
- // doExcel(excelName);
- xls[excelName] = 1
- const worker = cluster.fork(excelName);
- sendNum += 1
- worker.send({ "hang": 3, "excelName": excelName })
- worker.on('message', function (mes) {
- let mesarr = JSON.parse(mes)
- for (const ar of mesarr.excelFen) {
- excelFen.push(ar)
- }
- for (const key in mesarr.excelAndSheets) {
- excelAndSheets[key] = mesarr.excelAndSheets[key]
- }
- overNum += 1
- delete xls[excelName]
- console.log(`配置表解析 发送${sendNum}个,完成${overNum}个`)
- worker.destroy();
- })
- }
- });
- const intervalId = setInterval(() => {
- if (sendNum <= overNum) {
- // 生成配置表读取文件(ts)
- exeExcelConfig_c();
- console.log('*******创建配置文件完成*******');
- // createXyc();
- // console.log('*******创建xyc完成*******');
- createGameCfg_c();
- console.log('*******创建xls对应gameCfg完成*******');
- copy_proto_to_c();
- console.log('*******拷贝完成*******');
- console.log("----全部完成----")
- clearInterval(intervalId);
- }
- }, 1000);
- });
- }
- sleep(4000);
- // if (hang == 3) {
- //
- // }
- }
- if (!cluster.isMaster) {
- process.on("message", function (message) {
- hang = message.hang
- doExcel(message.excelName)
- process.send(JSON.stringify({ "excelFen": excelFen, "excelAndSheets": excelAndSheets }))
- if(hang == 4){
- process.exit()
- }
- });
- return
- }
- if (jiexiTarget == "s" || jiexiTarget == "c") {
- doExe(jiexiTarget)
- } else {
- readSyncByRl('exe client->c or server->s:').then((res) => {
- doExe(res)
- });
- }
- // //---------------------end-------------------- 控制台人机交互
|