const fs = require("fs");
const ringType = 6;
const YBvoucher = {
  '380': '100',
  '388': '200',
  '389': '500',
  '390': '1000',
  '391': '2000',
  '392': '5000',
  '393': '10000',
  '394': '1',
  '395': '5',
  '396': '10',
  '397': '15',
  '398': '30',
  '399': '50',
  '400': '80'
}
const tpls = {
  SpecialRing: {
    key: "SpecialRing",
    desc: "戒指种类,登记,伤害加倍,元宝花费",
    values: [ringType, 51, 10, 10],
    interval: [0, 1, 10, 10],
    num: 150,
    template: {
      4: `"S2": {
          "attr": [
            { "value": S3, "type": 75 },
            { "value": 428, "type": 76 },
            { "value": 536, "type": 77 },
            { "value": 55, "type": 21 },
            { "value": 55, "type": 23 },
            { "value": 55, "type": 25 },
            { "value": 55, "type": 27 },
            { "value": 5000, "type": 83 }
          ],
          "lv": S2,
          "cost": [
            { "id": 4, "type": 4, "count": S4 },
            { "id": 1018, "type": 0, "count": 1000 }
          ],
          "pos": S1
        }`,
      5: `"S2": {
          "attr": [
            { "value": S3, "type": 75 },
            { "value": 585, "type": 76 },
            { "value": 1115, "type": 77 },
            { "value": 70, "type": 21 },
            { "value": 70, "type": 23 },
            { "value": 70, "type": 25 },
            { "value": 70, "type": 27 },
            { "value": 3400, "type": 79 }
          ],
          "lv": S2,
          "cost": [
              { "id": 1022, "type": 0, "count": 1600 },
              { "id": 4, "type": 4, "count": S4 }
          ],
          "pos": S1
        }`,
      6: `"S2": {
          "attr": [
            { "value": 1000, "type": 5 },
            { "value": 100, "type": 76 },
            { "value": 100, "type": 77 },
            { "value": 2500, "type": 64 },
            { "value": S3, "type": 75 }
          ],
          "lv": S2,
          "cost": [
            { "id": 1037, "type": 0, "count": 180 },
            { "id": 4, "type": 4, "count": S4 }
          ],
          "pos": S1
        }`,
    }[ringType],
  },
  ItemMergeConfig: {
    key: "ItemMergeConfig",
    desc: "ID1,ID2,代金券ID,元宝数",
    values: [7201, 7202,Object.keys(YBvoucher),Object.values(YBvoucher)],
    interval: [2, 2],
    num: Object.keys(YBvoucher).length,
    template: `
    "S1": {
        "circle": 0,
        "compose": { "id": 4, "type": 4, "count": S40 },
        "index": 7,
        "buttontxt10": "合成10次",
        "mergebutton10": 1,
        "text": "兑换",
        "clicklimit": 1,
        "Eid": S1,
        "second_index": 1,
        "table": [{ "id": S3, "type": 0, "count": 1 }],
        "openserverday": 1,
        "level": 1
      },
      "S2": {
        "circle": 0,
        "compose": { "id": 4, "type": 4, "count": S4000 },
        "index": 7,
        "buttontxt10": "合成10次",
        "mergebutton10": 1,
        "text": "兑换",
        "clicklimit": 1,
        "Eid": S2,
        "second_index": 1,
        "table": [{ "id": S3, "type": 0, "count": 100 }],
        "openserverday": 1,
        "level": 1
      }
    `,
  },
};

const scope = [
  // "UpstarConfig",
  "ItemMergeConfig",
].filter(Boolean);
const start = () => {
  const configs = scope.map((key) => {
    return tpls[key];
  });
  configs.forEach((file) => {
    const { key, values, interval, num, template } = file;
    const getStr = (index) => {
      let str = template;
      values.forEach((v, i) => {
        const reg = new RegExp(`S${i + 1}`, "g");
        if (Array.isArray(v)) {
          str = str.replace(reg, v[index]);
        } else str = str.replace(reg, v + index * interval[i]);
      });
      return str;
    };
    const content = Array(num)
      .fill(1)
      .map((e, i) => `${getStr(i)}`)
      .join(",");
    fs.writeFileSync(`./dist/${key}.txt`, content);
  });
};
start();
