index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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">输出, 计算时间 {{ time }} 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="targetMale" />
  63. <el-table-column label="母" min-width="60px" prop="targetFemale" />
  64. </el-table-column>
  65. <el-table-column label="备选角色">
  66. <el-table-column label="公" min-width="60px" prop="backupMale" />
  67. <el-table-column label="母" min-width="60px" prop="backupFemale" />
  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 time = ref("")
  96. const malePowers = ref('')
  97. const feMalePowers = ref('')
  98. const role = ref("")
  99. const type = ref('')
  100. const mockData = [
  101. ]
  102. const inputData = ref(mockData);
  103. const outputData = ref([]);
  104. const batchRef = ref(null)
  105. function editRow(_gender, _role) {
  106. type.value = 'edit'
  107. gender.value = _gender
  108. role.value = _role
  109. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  110. malePowers.value = inputData.value.map(item => item[`${roleProp}Male`]).filter(item => !!item).join(',')
  111. feMalePowers.value = inputData.value.map(item => item[`${roleProp}Female`]).filter(item => !!item).join(',')
  112. batchRef.value.open()
  113. }
  114. function switchRoles(){
  115. outputData.value = []
  116. // const cloneInputData = inputData.value.map(item => ({ ...item }))
  117. const backups = inputData.value.map(item => ({ male: item.backupMale, female: item.backupFemale }))
  118. const targets = inputData.value.map(item => ({ male: item.targetMale, female: item.targetFemale }))
  119. inputData.value = inputData.value.map((_, index) => ({
  120. backupFemale: targets[index]?.female || null,
  121. backupMale: targets[index]?.male || null,
  122. targetFemale: backups[index]?.female || null,
  123. targetMale: backups[index]?.male || null,
  124. }))
  125. }
  126. const powerDifference = ref(0)
  127. // 目标角色操作
  128. const addTargetRow = () => {
  129. gender.value = ''
  130. powers.value = ''
  131. role.value = ''
  132. batchRef.value.open()
  133. };
  134. function isPositiveInteger(str) {
  135. return /^[1-9]\d*$/.test(str);
  136. }
  137. function generatePowers(_powers, gender){
  138. const keys = ['targetMale', 'targetFemale', 'backupMale', 'backupFemale']
  139. _powers = _powers.replace(/,|、|,/g, ',')
  140. const powerSet = _powers.split(',').map(item => item.trim()).filter(item => item !== '')
  141. const isOk = powerSet.every(num => isPositiveInteger(num))
  142. if (!isOk && powerSet.length !== 0) return false
  143. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  144. const genderProp = gender === '公' ? 'Male' : 'Female'
  145. inputData.value.forEach((_, index) => {
  146. inputData.value[index][`${roleProp}${genderProp}`] = null
  147. })
  148. const prop = `${roleProp}${genderProp}`
  149. const values = inputData.value.map(item => item[prop]).filter(value => value);
  150. if (inputData.value.length === 0) {
  151. for (let index = 0; index < powerSet.length; index++) {
  152. const item = {}
  153. keys.forEach(key => {
  154. if (key === prop) {
  155. item[key] = powerSet[index]
  156. } else {
  157. item[key] = null
  158. }
  159. })
  160. inputData.value.push(item)
  161. }
  162. } else if (values.length < inputData.value.length) {
  163. let pIndex = -1
  164. for (let index = values.length; index < inputData.value.length; index++) {
  165. pIndex += 1
  166. inputData.value[index][prop] = powerSet[pIndex]
  167. }
  168. const calcNum = powerSet.length - (pIndex + 1)
  169. for (let index = 0; index < calcNum; index++) {
  170. pIndex += 1
  171. const item = {}
  172. keys.forEach(key => {
  173. if (key === prop) {
  174. item[key] = powerSet[pIndex]
  175. } else {
  176. item[key] = null
  177. }
  178. })
  179. inputData.value.push(item)
  180. }
  181. }
  182. inputData.value = inputData.value.filter(item => {
  183. if(!item.backupFemale && !item.backupMale && !item.targetFemale && !item.targetMale){
  184. return false
  185. }
  186. return true
  187. })
  188. console.log(inputData.value)
  189. }
  190. function handleConfirm(){
  191. const _malePowers = malePowers.value.trim()
  192. const _feMalePowers = feMalePowers.value.trim()
  193. const isMaleOk = generatePowers(_malePowers, '公')
  194. const isFemakeOk = generatePowers(_feMalePowers, '母')
  195. if(typeof isMaleOk === 'boolean' && !isMaleOk){
  196. ElMessage.warning('势力必须为数字')
  197. return
  198. } else if(typeof isFemakeOk === 'boolean' && !isFemakeOk){
  199. ElMessage.warning('势力必须为数字')
  200. return
  201. }
  202. batchRef.value.close()
  203. }
  204. // 生成数组的所有组合(辅助函数,用于生成指定个数元素的组合情况)
  205. function getCombinations(arr, num) {
  206. const result = [];
  207. if (num === 0) { return [[]]; }
  208. for (let i = 0; i < arr.length; i++) {
  209. const element = arr[i];
  210. const remainingCombos = getCombinations(arr.slice(i + 1), num - 1);
  211. for (const combo of remainingCombos) {
  212. result.push([element].concat(combo));
  213. }
  214. }
  215. return result;
  216. }
  217. function calculateBestMatch() {
  218. let startTime = new Date().getTime()
  219. let endTime = null
  220. outputData.value = []
  221. const targetData = []
  222. const backupData = []
  223. inputData.value.forEach(item => {
  224. if (item.targetMale) { targetData.push({ gender: '公', power: item.targetMale }) }
  225. if (item.targetFemale) { targetData.push({ gender: '母', power: item.targetFemale }) }
  226. if (item.backupMale) { backupData.push({ gender: '公', power: item.backupMale }) }
  227. if (item.backupFemale) { backupData.push({ gender: '母', power: item.backupFemale }) }
  228. })
  229. const targetMale = targetData.filter(item => item.gender === '公').map(item => Number(item.power));
  230. const targetFemale = targetData.filter(item => item.gender === '母').map(item => Number(item.power));
  231. const backupMale = backupData.filter(item => item.gender === '公').map(item => Number(item.power));
  232. const backupFemale = backupData.filter(item => item.gender === '母').map(item => Number(item.power));
  233. // const targetMale = [153, 91, 77, 138, 90, 80, 60, 54, 52, 52, 52];
  234. // const targetFemale = [121, 118, 115, 86, 84, 84, 59, 53, 51];
  235. // const backupMale = [180, 165, 158, 154, 152, 151, 150, 150, 102, 101, 101, 93, 61, 30, 30, 29, 29, 29, 27, 10, 10];
  236. // const backupFemale = [174, 152, 147, 102, 101, 98, 98, 97, 62, 60, 58, 19];
  237. // const targetMale = [238, 90, 90];
  238. // const targetFemale = [86, 84, 84];
  239. // const backupMale = [153, 91, 77];
  240. // const backupFemale = [121, 118, 115];
  241. if(targetFemale.length + targetFemale.length > 20){
  242. ElMessage.warning('目标角色公数据与母数据的和不能超过20条')
  243. return
  244. }
  245. if(backupMale.length > 20){
  246. ElMessage.warning('备选角色的公数据不能超过20条')
  247. return
  248. }
  249. if(backupFemale.length > 20){
  250. ElMessage.warning('备选角色的母数据不能超过20条')
  251. return
  252. }
  253. if (targetFemale.length > backupMale.length && backupMale.length !== 0){
  254. ElMessage.warning('备选角色的公数据长度必须大于或等于目标角色的母数据')
  255. return
  256. }
  257. if (targetMale.length > backupFemale.length && backupFemale.length !== 0) {
  258. ElMessage.warning('备选角色的母数据长度必须或等于大于目标角色的公数据')
  259. return
  260. }
  261. const targetMaleLen = targetMale.length;
  262. const targetFemaleLen = targetFemale.length;
  263. let targetWeight = 0
  264. if (backupMale.length === 0){
  265. targetWeight = targetMale.reduce((acc, val) => acc + val, 0);
  266. } else if(backupFemale.length === 0){
  267. targetWeight = targetFemale.reduce((acc, val) => acc + val, 0)
  268. } else if(backupMale.length !== 0 && backupFemale.length !== 0){
  269. targetWeight = targetMale.reduce((acc, val) => acc + val, 0) + targetFemale.reduce((acc, val) => acc + val, 0);
  270. }
  271. let minDifference = Infinity;
  272. let bestfemaleCombination = null;
  273. let bestMaleCombination = null;
  274. const femaleCombinations = getCombinations(backupFemale, targetMaleLen);
  275. const maleCombinations = getCombinations(backupMale, targetFemaleLen);
  276. if (femaleCombinations.length !== 0 && maleCombinations.length !== 0){
  277. for (const femaleComb of femaleCombinations) {
  278. for (const maleComb of maleCombinations) {
  279. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0) + maleComb.reduce((acc, val) => acc + val, 0);
  280. const difference = Math.abs(totalWeight - targetWeight);
  281. if (difference < minDifference) {
  282. minDifference = difference;
  283. bestfemaleCombination = femaleComb;
  284. bestMaleCombination = maleComb;
  285. if (minDifference == 0) {
  286. break;
  287. }
  288. }
  289. }
  290. }
  291. }
  292. // 备选数据的母为空
  293. if (femaleCombinations.length === 0 && maleCombinations.length !== 0) {
  294. for (const maleComb of maleCombinations) {
  295. const totalWeight = maleComb.reduce((acc, val) => acc + val , 0);
  296. const difference = Math.abs(totalWeight - targetWeight);
  297. if (difference < minDifference) {
  298. minDifference = difference;
  299. bestMaleCombination = maleComb;
  300. if (minDifference == 0) {
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. // 备选数据的公为空
  307. if (femaleCombinations.length !== 0 && maleCombinations.length === 0) {
  308. for (const femaleComb of femaleCombinations) {
  309. const totalWeight = femaleComb.reduce((acc, val) => acc + val, 0)
  310. const difference = Math.abs(totalWeight - targetWeight);
  311. if (difference < minDifference) {
  312. minDifference = difference;
  313. bestfemaleCombination = femaleComb;
  314. // bestMaleCombination = maleComb;
  315. if (minDifference == 0) {
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. bestMaleCombination && bestMaleCombination.sort((a, b) => b - a)
  322. bestfemaleCombination && bestfemaleCombination.sort((a, b) => b - a)
  323. targetMale.sort((a, b) => b - a)
  324. targetFemale.sort((a, b) => b - a)
  325. if (!bestMaleCombination && !bestfemaleCombination){ return }
  326. bestfemaleCombination && targetMale.forEach((_, index) => {
  327. outputData.value.push({
  328. targetMale: targetMale[index],
  329. targetFemale: null,
  330. backupMale: null,
  331. backupFemale: bestfemaleCombination[index]
  332. })
  333. })
  334. const ml = targetMale.length
  335. const fl = targetFemale.length
  336. for(let index = 0; index < fl; index ++){
  337. if(outputData.value[index]){
  338. outputData.value[index].targetFemale = targetFemale[index]
  339. outputData.value[index].backupMale = bestMaleCombination[index]
  340. } else {
  341. outputData.value.push({
  342. targetMale: null,
  343. targetFemale: targetFemale[index],
  344. backupMale: bestMaleCombination[index],
  345. backupFemale: null
  346. })
  347. }
  348. }
  349. outputData.value.push({
  350. targetMale: outputData.value.reduce((prev, current) => prev + current.targetMale,0),
  351. targetFemale: outputData.value.reduce((prev, current) => prev + current.targetFemale,0),
  352. backupMale: outputData.value.reduce((prev, current) => prev + current.backupMale,0),
  353. backupFemale: outputData.value.reduce((prev, current) => prev + current.backupFemale,0)
  354. })
  355. powerDifference.value = minDifference
  356. endTime = new Date().getTime()
  357. time.value = endTime - startTime
  358. }
  359. </script>
  360. <style>
  361. textarea {
  362. font-size: 13px;
  363. padding: 8px;
  364. }
  365. </style>
  366. <style lang="scss" scoped>
  367. .dialog-title {
  368. line-height: 30px;
  369. padding-left: 10px;
  370. font-size: 14px;
  371. }
  372. .table-wrapper {
  373. font-size: 14px !important;
  374. }
  375. .custom-picker {
  376. border: 1px solid #e3e3e3;
  377. padding: 3px 5px;
  378. text-align: center;
  379. width: 100%;
  380. div {
  381. display: flex;
  382. justify-content: space-between;
  383. text {
  384. overflow: hidden;
  385. white-space: nowrap;
  386. text-overflow: ellipsis;
  387. }
  388. }
  389. }
  390. .container {
  391. padding: 20px;
  392. }
  393. .section {
  394. margin-bottom: 20px;
  395. }
  396. .section-title {
  397. font-size: 18px;
  398. font-weight: bold;
  399. margin-bottom: 10px;
  400. }
  401. .input {
  402. width: 100%;
  403. text-align: center;
  404. border: 1px solid #ddd;
  405. padding: 5px;
  406. }
  407. button {
  408. margin: 5px;
  409. }
  410. .add-btn,
  411. .calculate-btn {
  412. background-color: #007bff;
  413. color: white;
  414. padding: 10px;
  415. font-size: 16px;
  416. text-align: center;
  417. border-radius: 5px;
  418. }
  419. .delete-btn {
  420. background-color: #ff4d4f;
  421. color: white;
  422. padding: 5px;
  423. font-size: 14px;
  424. border-radius: 5px;
  425. }
  426. .summary {
  427. margin-top: 20px;
  428. font-size: 14px;
  429. text-align: right;
  430. font-weight: bold;
  431. }
  432. </style>