index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 - 150px)" 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. }
  305. function handleConfirm(){
  306. const _malePowers = malePowers.value.trim()
  307. const _feMalePowers = feMalePowers.value.trim()
  308. const isMaleOk = generatePowers(_malePowers, '公')
  309. const isFemakeOk = generatePowers(_feMalePowers, '母')
  310. if(typeof isMaleOk === 'boolean' && !isMaleOk){
  311. ElMessage.warning('势力必须为数字')
  312. return
  313. } else if(typeof isFemakeOk === 'boolean' && !isFemakeOk){
  314. ElMessage.warning('势力必须为数字')
  315. return
  316. }
  317. batchRef.value.close()
  318. }
  319. // 生成数组的所有组合(辅助函数,用于生成指定个数元素的组合情况)
  320. function getCombinations(arr, num) {
  321. const result = [];
  322. if (num === 0) { return [[]]; }
  323. for (let i = 0; i < arr.length; i++) {
  324. const element = arr[i];
  325. const remainingCombos = getCombinations(arr.slice(i + 1), num - 1);
  326. for (const combo of remainingCombos) {
  327. result.push([element].concat(combo));
  328. }
  329. }
  330. return result;
  331. }
  332. function calculateBestMatch() {
  333. outputData.value = []
  334. const targetData = []
  335. const backupData = []
  336. inputData.value.forEach(item => {
  337. if (item.targetMale) { targetData.push({ gender: '公', power: item.targetMale }) }
  338. if (item.targetFemale) { targetData.push({ gender: '母', power: item.targetFemale }) }
  339. if (item.backupMale) { backupData.push({ gender: '公', power: item.backupMale }) }
  340. if (item.backupFemale) { backupData.push({ gender: '母', power: item.backupFemale }) }
  341. })
  342. const targetMale = targetData.filter(item => item.gender === '公').map(item => Number(item.power));
  343. const targetFemale = targetData.filter(item => item.gender === '母').map(item => Number(item.power));
  344. const backupMale = backupData.filter(item => item.gender === '公').map(item => Number(item.power));
  345. const backupFemale = backupData.filter(item => item.gender === '母').map(item => Number(item.power));
  346. // const targetMale = [153, 91, 77, 138, 90, 80, 60, 54, 52, 52, 52];
  347. // const targetFemale = [121, 118, 115, 86, 84, 84, 59, 53, 51];
  348. // const backupMale = [180, 165, 158, 154, 152, 151, 150, 150, 102, 101, 101, 93, 61, 30, 30, 29, 29, 29, 27, 10, 10];
  349. // const backupFemale = [174, 152, 147, 102, 101, 98, 98, 97, 62, 60, 58, 19];
  350. // const targetMale = [238, 90, 90];
  351. // const targetFemale = [86, 84, 84];
  352. // const backupMale = [153, 91, 77];
  353. // const backupFemale = [121, 118, 115];
  354. if (targetFemale.length > backupMale.length && backupMale.length !== 0){
  355. ElMessage.warning('备选角色的公数据长度必须大于或等于目标角色的母数据')
  356. return
  357. }
  358. if (targetMale.length > backupFemale.length && backupFemale.length !== 0) {
  359. ElMessage.warning('备选角色的母数据长度必须或等于大于目标角色的公数据')
  360. return
  361. }
  362. const targetMaleLen = targetMale.length;
  363. const targetFemaleLen = targetFemale.length;
  364. let targetWeight = 0
  365. if (backupMale.length === 0){
  366. targetWeight = targetMale.reduce((acc, val) => acc + val, 0);
  367. } else if(backupFemale.length === 0){
  368. targetWeight = targetFemale.reduce((acc, val) => acc + val, 0)
  369. } else if(backupMale.length !== 0 && backupFemale.length !== 0){
  370. targetWeight = targetMale.reduce((acc, val) => acc + val, 0) + targetFemale.reduce((acc, val) => acc + val, 0);
  371. }
  372. let minDifference = Infinity; // 初始化最小差额为正无穷大
  373. let bestfemaleCombination = null;
  374. let bestMaleCombination = null;
  375. const femaleCombinations = getCombinations(backupFemale, targetMaleLen);
  376. const maleCombinations = getCombinations(backupMale, targetFemaleLen);
  377. if (femaleCombinations.length !== 0 && maleCombinations.length !== 0){
  378. for (const femaleComb of femaleCombinations) {
  379. for (const maleComb of maleCombinations) {
  380. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0) + maleComb.reduce((acc, val) => acc + val, 0);
  381. const difference = Math.abs(totalWeight - targetWeight);
  382. if (difference < minDifference) {
  383. minDifference = difference;
  384. bestfemaleCombination = femaleComb;
  385. bestMaleCombination = maleComb;
  386. if (minDifference == 0) {
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. // 备选数据的母为空
  394. if (femaleCombinations.length === 0 && maleCombinations.length !== 0) {
  395. for (const maleComb of maleCombinations) {
  396. const totalWeight = maleComb.reduce((acc, val) => acc + val , 0);
  397. const difference = Math.abs(totalWeight - targetWeight);
  398. if (difference < minDifference) {
  399. minDifference = difference;
  400. bestMaleCombination = maleComb;
  401. if (minDifference == 0) {
  402. break;
  403. }
  404. }
  405. }
  406. }
  407. // 备选数据的公为空
  408. if (femaleCombinations.length !== 0 && maleCombinations.length === 0) {
  409. for (const femaleComb of femaleCombinations) {
  410. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0)
  411. const difference = Math.abs(totalWeight - targetWeight);
  412. if (difference < minDifference) {
  413. minDifference = difference;
  414. bestfemaleCombination = femaleComb;
  415. if (minDifference == 0) {
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. bestMaleCombination && bestMaleCombination.sort((a, b) => a - b)
  422. bestfemaleCombination && bestfemaleCombination.sort((a, b) => a - b)
  423. targetMale.sort((a, b) => a - b)
  424. targetFemale.sort((a, b) => a - b)
  425. if (!bestMaleCombination && !bestfemaleCombination){ return }
  426. bestfemaleCombination && targetMale.forEach((_, index) => {
  427. outputData.value.push({
  428. targetGender: '公',
  429. targetPower: targetMale[index],
  430. backupGender: '母',
  431. backupPower: bestfemaleCombination[index]
  432. })
  433. })
  434. bestMaleCombination && targetFemale.forEach((_, index) => {
  435. outputData.value.push({
  436. targetGender: '母',
  437. targetPower: targetFemale[index],
  438. backupGender: '公',
  439. backupPower: bestMaleCombination[index]
  440. })
  441. })
  442. outputData.value.push({
  443. targetGender: '',
  444. targetPower: outputData.value.reduce((prev, current) => prev + current.targetPower,0),
  445. backupGender: '',
  446. backupPower: outputData.value.reduce((prev, current) => prev + current.backupPower, 0),
  447. })
  448. powerDifference.value = minDifference
  449. }
  450. </script>
  451. <style>
  452. textarea {
  453. font-size: 13px;
  454. padding: 8px;
  455. }
  456. </style>
  457. <style lang="scss" scoped>
  458. .dialog-title {
  459. line-height: 30px;
  460. padding-left: 10px;
  461. font-size: 14px;
  462. }
  463. .table-wrapper {
  464. font-size: 14px !important;
  465. }
  466. .custom-picker {
  467. border: 1px solid #e3e3e3;
  468. padding: 3px 5px;
  469. text-align: center;
  470. width: 100%;
  471. div {
  472. display: flex;
  473. justify-content: space-between;
  474. text {
  475. overflow: hidden;
  476. white-space: nowrap;
  477. text-overflow: ellipsis;
  478. }
  479. }
  480. }
  481. .container {
  482. padding: 20px;
  483. }
  484. .section {
  485. margin-bottom: 20px;
  486. }
  487. .section-title {
  488. font-size: 18px;
  489. font-weight: bold;
  490. margin-bottom: 10px;
  491. }
  492. .input {
  493. width: 100%;
  494. text-align: center;
  495. border: 1px solid #ddd;
  496. padding: 5px;
  497. }
  498. button {
  499. margin: 5px;
  500. }
  501. .add-btn,
  502. .calculate-btn {
  503. background-color: #007bff;
  504. color: white;
  505. padding: 10px;
  506. font-size: 16px;
  507. text-align: center;
  508. border-radius: 5px;
  509. }
  510. .delete-btn {
  511. background-color: #ff4d4f;
  512. color: white;
  513. padding: 5px;
  514. font-size: 14px;
  515. border-radius: 5px;
  516. }
  517. .summary {
  518. margin-top: 20px;
  519. font-size: 14px;
  520. text-align: right;
  521. font-weight: bold;
  522. }
  523. </style>