index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <template>
  2. <view class="container">
  3. <div style="display: flex;flex-direction: column;">
  4. <!-- <div style="display: flex;justify-content: space-between;align-items: center;">
  5. <text class="section-title">输入</text>
  6. <el-button size="small" type="primary" @click="addTargetRow">添加</el-button>
  7. </div> -->
  8. <el-table size="small" border :data="inputData" max-height="calc(100vh - 450px)" empty-text="暂无数据,请添加猫咪势力">
  9. <el-table-column label="顺序" width="55px">
  10. <template #="{ $index }">{{ $index + 1 }}</template>
  11. </el-table-column>
  12. <el-table-column label="目标角色">
  13. <template #header>
  14. <div style="display: flex;justify-content: space-between;">
  15. 目标角色
  16. <el-tooltip content="编辑" placement="top">
  17. <el-button type="primary" link style="margin-left: 0px;" @click="editRow('母', '目标角色')">
  18. <uni-icons style="color: #409eff" size="14" type="compose" />
  19. </el-button>
  20. </el-tooltip>
  21. </div>
  22. </template>
  23. <el-table-column label="公" min-width="60px" prop="targetMale" />
  24. <el-table-column label="母" min-width="60px" prop="targetFemale" />
  25. </el-table-column>
  26. <el-table-column label="备选角色">
  27. <template #header>
  28. <div style="display: flex;justify-content: space-between;">
  29. 备选角色
  30. <el-tooltip content="编辑" placement="top">
  31. <el-button type="primary" link style="margin-left: 0px;" @click="editRow('公', '备选角色')">
  32. <uni-icons style="color: #409eff" size="14" type="compose" />
  33. </el-button>
  34. </el-tooltip>
  35. </div>
  36. </template>
  37. <el-table-column label="公" min-width="60px" prop="backupMale" />
  38. <el-table-column label="母" min-width="60px" prop="backupFemale" />
  39. </el-table-column>
  40. </el-table>
  41. </div>
  42. <div style="display: flex;">
  43. <el-button type="primary" style="width:100%;margin-bottom: 10px;margin-top:10px" @click="switchRoles">
  44. 交换角色
  45. </el-button>
  46. <el-button type="primary" style="width:100%;margin-bottom: 10px;margin-top:10px"
  47. @click="calculateBestMatch">
  48. 计算最佳配对
  49. </el-button>
  50. </div>
  51. <!-- 输出表格 -->
  52. <view class="section" v-if="outputData.length > 0">
  53. <div style="display: flex;justify-content: space-between;" >
  54. <text class="section-title">输出, 计算时间 {{}} ms </text>
  55. <span>差额: {{ powerDifference }}</span>
  56. </div>
  57. <el-table size="small" border :data="outputData">
  58. <el-table-column label="顺序" width="55px">
  59. <template #="{ $index }">{{ $index + 1 === outputData.length ? '合计' : $index + 1}}</template>
  60. </el-table-column>
  61. <el-table-column label="目标角色">
  62. <el-table-column label="性别" min-width="60px" prop="targetGender" />
  63. <el-table-column label="势力" min-width="60px" prop="targetPower" />
  64. </el-table-column>
  65. <el-table-column label="备选角色">
  66. <el-table-column label="性别" min-width="60px" prop="backupGender" />
  67. <el-table-column label="势力" min-width="60px" prop="backupPower" />
  68. </el-table-column>
  69. </el-table>
  70. </view>
  71. <uni-popup ref="batchRef" :is-mask-click="false">
  72. <div
  73. style="display: flex;flex-direction: column;justify-content: center;padding: 10px; width: 95%;background: #fff;border-radius: 10px;padding: 10px;">
  74. <div style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 10px;">
  75. <span></span>
  76. <h4 style="margin-bottom: 10px"> 编辑{{ role }}</h4>
  77. <uni-icons type="closeempty" size="18" @click="batchRef.close()"></uni-icons>
  78. </div>
  79. <span class="dialog-title">猫咪势力(公)</span>
  80. <textarea v-model="malePowers" placeholder="用逗号隔开,如:200,10,100"
  81. style="height: 70px;border: 1px solid #dfe2e5" />
  82. <span class="dialog-title">猫咪势力(母)</span>
  83. <textarea v-model="feMalePowers" placeholder="用逗号隔开,如:200,10,100"
  84. style="height: 70px;border: 1px solid #dfe2e5" />
  85. <button size='mini' type="primary" @click="handleConfirm">保存</button>
  86. </div>
  87. </uni-popup>
  88. </view>
  89. </template>
  90. <script setup>
  91. import { ref, computed } from 'vue';
  92. import { ElMessage } from "element-plus"
  93. const gender = ref("")
  94. const powers = ref("")
  95. const malePowers = ref('')
  96. const feMalePowers = ref('')
  97. const role = ref("")
  98. const type = ref('')
  99. const mockData = [
  100. {
  101. "backupFemale": "180",
  102. "backupMale": "174",
  103. "targetFemale": "121",
  104. "targetMale": "153"
  105. },
  106. {
  107. "backupFemale": "165",
  108. "backupMale": "152",
  109. "targetFemale": "118",
  110. "targetMale": "91"
  111. },
  112. {
  113. "backupFemale": "158",
  114. "backupMale": "147",
  115. "targetFemale": "115",
  116. "targetMale": "77"
  117. },
  118. {
  119. "backupFemale": "154",
  120. "backupMale": "102",
  121. "targetFemale": "86",
  122. "targetMale": "138"
  123. },
  124. {
  125. "backupFemale": "152",
  126. "backupMale": "101",
  127. "targetFemale": "84",
  128. "targetMale": "90"
  129. },
  130. {
  131. "backupFemale": "151",
  132. "backupMale": "98",
  133. "targetFemale": "84",
  134. "targetMale": "80"
  135. },
  136. {
  137. "backupFemale": "150",
  138. "backupMale": "98",
  139. "targetMale": "60",
  140. "targetFemale": "59"
  141. },
  142. {
  143. "backupFemale": "150",
  144. "backupMale": "97",
  145. "targetMale": "54",
  146. "targetFemale": "53"
  147. },
  148. {
  149. "targetMale": "52",
  150. "targetFemale": "51",
  151. "backupMale": "62",
  152. "backupFemale": "102"
  153. },
  154. {
  155. "targetMale": "52",
  156. "backupMale": "60",
  157. "backupFemale": "101"
  158. },
  159. {
  160. "targetMale": "52",
  161. "backupMale": "58",
  162. "backupFemale": "101"
  163. },
  164. {
  165. "targetMale": null,
  166. "targetFemale": null,
  167. "backupMale": "19",
  168. "backupFemale": "93"
  169. },
  170. {
  171. "targetMale": null,
  172. "targetFemale": null,
  173. "backupMale": null,
  174. "backupFemale": "61"
  175. },
  176. {
  177. "targetMale": null,
  178. "targetFemale": null,
  179. "backupMale": null,
  180. "backupFemale": "30"
  181. },
  182. {
  183. "targetMale": null,
  184. "targetFemale": null,
  185. "backupMale": null,
  186. "backupFemale": "30"
  187. },
  188. {
  189. "targetMale": null,
  190. "targetFemale": null,
  191. "backupMale": null,
  192. "backupFemale": "29"
  193. },
  194. {
  195. "targetMale": null,
  196. "targetFemale": null,
  197. "backupMale": null,
  198. "backupFemale": "29"
  199. },
  200. {
  201. "targetMale": null,
  202. "targetFemale": null,
  203. "backupMale": null,
  204. "backupFemale": "29"
  205. },
  206. {
  207. "targetMale": null,
  208. "targetFemale": null,
  209. "backupMale": null,
  210. "backupFemale": "27"
  211. },
  212. {
  213. "targetMale": null,
  214. "targetFemale": null,
  215. "backupMale": null,
  216. "backupFemale": "10"
  217. },
  218. {
  219. "targetMale": null,
  220. "targetFemale": null,
  221. "backupMale": null,
  222. "backupFemale": "10"
  223. }
  224. ]
  225. const inputData = ref(mockData);
  226. const outputData = ref([]);
  227. const batchRef = ref(null)
  228. function editRow(_gender, _role) {
  229. type.value = 'edit'
  230. gender.value = _gender
  231. role.value = _role
  232. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  233. malePowers.value = inputData.value.map(item => item[`${roleProp}Male`]).filter(item => !!item).join(',')
  234. feMalePowers.value = inputData.value.map(item => item[`${roleProp}Female`]).filter(item => !!item).join(',')
  235. batchRef.value.open()
  236. }
  237. function switchRoles(){
  238. // const cloneInputData = inputData.value.map(item => ({ ...item }))
  239. const backups = inputData.value.map(item => ({ male: item.backupMale, female: item.backupFemale }))
  240. const targets = inputData.value.map(item => ({ male: item.targetMale, female: item.targetFemale }))
  241. inputData.value = inputData.value.map((_, index) => ({
  242. backupFemale: targets[index]?.female || null,
  243. backupMale: targets[index]?.male || null,
  244. targetFemale: backups[index]?.female || null,
  245. targetMale: backups[index]?.male || null,
  246. }))
  247. }
  248. const powerDifference = ref(0)
  249. // 目标角色操作
  250. const addTargetRow = () => {
  251. gender.value = ''
  252. powers.value = ''
  253. role.value = ''
  254. batchRef.value.open()
  255. };
  256. function isPositiveInteger(str) {
  257. return /^[1-9]\d*$/.test(str);
  258. }
  259. function generatePowers(_powers, gender){
  260. const keys = ['targetMale', 'targetFemale', 'backupMale', 'backupFemale']
  261. _powers = _powers.replace(/,|、|,/g, ',')
  262. const powerSet = _powers.split(',').map(item => item.trim()).filter(item => item !== '')
  263. const isOk = powerSet.every(num => isPositiveInteger(num))
  264. if (!isOk && powerSet.length !== 0) return false
  265. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  266. const genderProp = gender === '公' ? 'Male' : 'Female'
  267. inputData.value.forEach((_, index) => {
  268. inputData.value[index][`${roleProp}${genderProp}`] = null
  269. })
  270. const prop = `${roleProp}${genderProp}`
  271. const values = inputData.value.map(item => item[prop]).filter(value => value);
  272. if (inputData.value.length === 0) {
  273. for (let index = 0; index < powerSet.length; index++) {
  274. const item = {}
  275. keys.forEach(key => {
  276. if (key === prop) {
  277. item[key] = powerSet[index]
  278. } else {
  279. item[key] = null
  280. }
  281. })
  282. inputData.value.push(item)
  283. }
  284. } else if (values.length < inputData.value.length) {
  285. let pIndex = -1
  286. for (let index = values.length; index < inputData.value.length; index++) {
  287. pIndex += 1
  288. inputData.value[index][prop] = powerSet[pIndex]
  289. }
  290. const calcNum = powerSet.length - (pIndex + 1)
  291. for (let index = 0; index < calcNum; index++) {
  292. pIndex += 1
  293. const item = {}
  294. keys.forEach(key => {
  295. if (key === prop) {
  296. item[key] = powerSet[pIndex]
  297. } else {
  298. item[key] = null
  299. }
  300. })
  301. inputData.value.push(item)
  302. }
  303. }
  304. inputData.value = inputData.value.filter(item => {
  305. if(!item.backupFemale && !item.backupMale && !item.targetFemale && !item.targetMale){
  306. return false
  307. }
  308. return true
  309. })
  310. console.log(inputData.value)
  311. }
  312. function handleConfirm(){
  313. const _malePowers = malePowers.value.trim()
  314. const _feMalePowers = feMalePowers.value.trim()
  315. const isMaleOk = generatePowers(_malePowers, '公')
  316. const isFemakeOk = generatePowers(_feMalePowers, '母')
  317. if(typeof isMaleOk === 'boolean' && !isMaleOk){
  318. ElMessage.warning('势力必须为数字')
  319. return
  320. } else if(typeof isFemakeOk === 'boolean' && !isFemakeOk){
  321. ElMessage.warning('势力必须为数字')
  322. return
  323. }
  324. batchRef.value.close()
  325. }
  326. // 生成数组的所有组合(辅助函数,用于生成指定个数元素的组合情况)
  327. function getCombinations(arr, num) {
  328. const result = [];
  329. if (num === 0) { return [[]]; }
  330. for (let i = 0; i < arr.length; i++) {
  331. const element = arr[i];
  332. const remainingCombos = getCombinations(arr.slice(i + 1), num - 1);
  333. for (const combo of remainingCombos) {
  334. result.push([element].concat(combo));
  335. }
  336. }
  337. return result;
  338. }
  339. function calculateBestMatch() {
  340. outputData.value = []
  341. const targetData = []
  342. const backupData = []
  343. inputData.value.forEach(item => {
  344. if (item.targetMale) { targetData.push({ gender: '公', power: item.targetMale }) }
  345. if (item.targetFemale) { targetData.push({ gender: '母', power: item.targetFemale }) }
  346. if (item.backupMale) { backupData.push({ gender: '公', power: item.backupMale }) }
  347. if (item.backupFemale) { backupData.push({ gender: '母', power: item.backupFemale }) }
  348. })
  349. const targetMale = targetData.filter(item => item.gender === '公').map(item => Number(item.power));
  350. const targetFemale = targetData.filter(item => item.gender === '母').map(item => Number(item.power));
  351. const backupMale = backupData.filter(item => item.gender === '公').map(item => Number(item.power));
  352. const backupFemale = backupData.filter(item => item.gender === '母').map(item => Number(item.power));
  353. // const targetMale = [153, 91, 77, 138, 90, 80, 60, 54, 52, 52, 52];
  354. // const targetFemale = [121, 118, 115, 86, 84, 84, 59, 53, 51];
  355. // const backupMale = [180, 165, 158, 154, 152, 151, 150, 150, 102, 101, 101, 93, 61, 30, 30, 29, 29, 29, 27, 10, 10];
  356. // const backupFemale = [174, 152, 147, 102, 101, 98, 98, 97, 62, 60, 58, 19];
  357. // const targetMale = [238, 90, 90];
  358. // const targetFemale = [86, 84, 84];
  359. // const backupMale = [153, 91, 77];
  360. // const backupFemale = [121, 118, 115];
  361. if (targetFemale.length > backupMale.length && backupMale.length !== 0){
  362. ElMessage.warning('备选角色的公数据长度必须大于或等于目标角色的母数据')
  363. return
  364. }
  365. if (targetMale.length > backupFemale.length && backupFemale.length !== 0) {
  366. ElMessage.warning('备选角色的母数据长度必须或等于大于目标角色的公数据')
  367. return
  368. }
  369. const targetMaleLen = targetMale.length;
  370. const targetFemaleLen = targetFemale.length;
  371. let targetWeight = 0
  372. if (backupMale.length === 0){
  373. targetWeight = targetMale.reduce((acc, val) => acc + val, 0);
  374. } else if(backupFemale.length === 0){
  375. targetWeight = targetFemale.reduce((acc, val) => acc + val, 0)
  376. } else if(backupMale.length !== 0 && backupFemale.length !== 0){
  377. targetWeight = targetMale.reduce((acc, val) => acc + val, 0) + targetFemale.reduce((acc, val) => acc + val, 0);
  378. }
  379. let minDifference = Infinity;
  380. let bestfemaleCombination = null;
  381. let bestMaleCombination = null;
  382. const femaleCombinations = getCombinations(backupFemale, targetMaleLen);
  383. const maleCombinations = getCombinations(backupMale, targetFemaleLen);
  384. if (femaleCombinations.length !== 0 && maleCombinations.length !== 0){
  385. for (const femaleComb of femaleCombinations) {
  386. for (const maleComb of maleCombinations) {
  387. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0) + maleComb.reduce((acc, val) => acc + val, 0);
  388. const difference = Math.abs(totalWeight - targetWeight);
  389. if (difference < minDifference) {
  390. minDifference = difference;
  391. bestfemaleCombination = femaleComb;
  392. bestMaleCombination = maleComb;
  393. if (minDifference == 0) {
  394. break;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. // 备选数据的母为空
  401. if (femaleCombinations.length === 0 && maleCombinations.length !== 0) {
  402. for (const maleComb of maleCombinations) {
  403. const totalWeight = maleComb.reduce((acc, val) => acc + val , 0);
  404. const difference = Math.abs(totalWeight - targetWeight);
  405. if (difference < minDifference) {
  406. minDifference = difference;
  407. bestMaleCombination = maleComb;
  408. if (minDifference == 0) {
  409. break;
  410. }
  411. }
  412. }
  413. }
  414. // 备选数据的公为空
  415. if (femaleCombinations.length !== 0 && maleCombinations.length === 0) {
  416. for (const femaleComb of femaleCombinations) {
  417. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0)
  418. const difference = Math.abs(totalWeight - targetWeight);
  419. if (difference < minDifference) {
  420. minDifference = difference;
  421. bestfemaleCombination = femaleComb;
  422. // bestMaleCombination = maleComb;
  423. if (minDifference == 0) {
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. bestMaleCombination && bestMaleCombination.sort((a, b) => b - a)
  430. bestfemaleCombination && bestfemaleCombination.sort((a, b) => b - a)
  431. targetMale.sort((a, b) => b - a)
  432. targetFemale.sort((a, b) => b - a)
  433. if (!bestMaleCombination && !bestfemaleCombination){ return }
  434. bestfemaleCombination && targetMale.forEach((_, index) => {
  435. outputData.value.push({
  436. targetGender: '公',
  437. targetPower: targetMale[index],
  438. backupGender: '母',
  439. backupPower: bestfemaleCombination[index]
  440. })
  441. })
  442. bestMaleCombination && targetFemale.forEach((_, index) => {
  443. outputData.value.push({
  444. targetGender: '母',
  445. targetPower: targetFemale[index],
  446. backupGender: '公',
  447. backupPower: bestMaleCombination[index]
  448. })
  449. })
  450. outputData.value.push({
  451. targetGender: '',
  452. targetPower: outputData.value.reduce((prev, current) => prev + current.targetPower,0),
  453. backupGender: '',
  454. backupPower: outputData.value.reduce((prev, current) => prev + current.backupPower, 0),
  455. })
  456. powerDifference.value = minDifference
  457. }
  458. </script>
  459. <style>
  460. textarea {
  461. font-size: 13px;
  462. padding: 8px;
  463. }
  464. </style>
  465. <style lang="scss" scoped>
  466. .dialog-title {
  467. line-height: 30px;
  468. padding-left: 10px;
  469. font-size: 14px;
  470. }
  471. .table-wrapper {
  472. font-size: 14px !important;
  473. }
  474. .custom-picker {
  475. border: 1px solid #e3e3e3;
  476. padding: 3px 5px;
  477. text-align: center;
  478. width: 100%;
  479. div {
  480. display: flex;
  481. justify-content: space-between;
  482. text {
  483. overflow: hidden;
  484. white-space: nowrap;
  485. text-overflow: ellipsis;
  486. }
  487. }
  488. }
  489. .container {
  490. padding: 20px;
  491. }
  492. .section {
  493. margin-bottom: 20px;
  494. }
  495. .section-title {
  496. font-size: 18px;
  497. font-weight: bold;
  498. margin-bottom: 10px;
  499. }
  500. .input {
  501. width: 100%;
  502. text-align: center;
  503. border: 1px solid #ddd;
  504. padding: 5px;
  505. }
  506. button {
  507. margin: 5px;
  508. }
  509. .add-btn,
  510. .calculate-btn {
  511. background-color: #007bff;
  512. color: white;
  513. padding: 10px;
  514. font-size: 16px;
  515. text-align: center;
  516. border-radius: 5px;
  517. }
  518. .delete-btn {
  519. background-color: #ff4d4f;
  520. color: white;
  521. padding: 5px;
  522. font-size: 14px;
  523. border-radius: 5px;
  524. }
  525. .summary {
  526. margin-top: 20px;
  527. font-size: 14px;
  528. text-align: right;
  529. font-weight: bold;
  530. }
  531. </style>