index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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" @click="calculateBestMatch">
  47. 计算最佳配对
  48. </el-button>
  49. </div>
  50. <!-- 输出表格 -->
  51. <view class="section" v-if="outputData.length > 0">
  52. <div style="display: flex;justify-content: space-between;">
  53. <text class="section-title">输出 </text>
  54. 计算时间 {{timer}} ms
  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
  81. v-model="malePowers"
  82. placeholder="用逗号隔开,如:200,10,100"
  83. style="height: 70px;border: 1px solid #dfe2e5" />
  84. <span class="dialog-title">猫咪势力(母)</span>
  85. <textarea
  86. v-model="feMalePowers"
  87. placeholder="用逗号隔开,如:200,10,100"
  88. style="height: 70px;border: 1px solid #dfe2e5" />
  89. <button size='mini' type="primary" @click="handleConfirm">保存</button>
  90. </div>
  91. </uni-popup>
  92. </view>
  93. </template>
  94. <script setup>
  95. import { ref, computed } from 'vue';
  96. import { ElMessage } from "element-plus"
  97. const gender = ref("")
  98. const powers = ref("")
  99. const malePowers = ref('')
  100. const feMalePowers = ref('')
  101. const role = ref("")
  102. const type = ref('')
  103. // 性别选项
  104. const genderOptions = ['公', '母'];
  105. const roleOptions = ['目标角色', '备选角色'];
  106. const timer = ref(0)
  107. const mockData = [
  108. {
  109. "backupFemale": "121",
  110. "backupMale": "153",
  111. "targetFemale": "86",
  112. "targetMale": "138"
  113. },
  114. {
  115. "backupFemale": "118",
  116. "backupMale": "91",
  117. "targetFemale": "84",
  118. "targetMale": "90"
  119. },
  120. {
  121. "backupFemale": "115",
  122. "backupMale": "77",
  123. "targetFemale": "84"
  124. },
  125. {
  126. "backupFemale": null,
  127. "backupMale": null,
  128. "targetFemale": "59"
  129. },
  130. {
  131. "backupFemale": null,
  132. "backupMale": null,
  133. "targetFemale": "53"
  134. },
  135. {
  136. "backupFemale": null,
  137. "backupMale": null,
  138. "targetFemale": "51"
  139. },
  140. {
  141. "backupFemale": null,
  142. "backupMale": null
  143. },
  144. {
  145. "backupFemale": null,
  146. "backupMale": null
  147. }
  148. ]
  149. const inputData = ref(mockData);
  150. const outputData = ref([]);
  151. const batchRef = ref(null)
  152. function editRow(_gender, _role) {
  153. type.value = 'edit'
  154. gender.value = _gender
  155. role.value = _role
  156. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  157. malePowers.value = inputData.value.map(item => item[`${roleProp}Male`]).filter(item => !!item).join(',')
  158. feMalePowers.value = inputData.value.map(item => item[`${roleProp}Female`]).filter(item => !!item).join(',')
  159. batchRef.value.open()
  160. }
  161. function switchRoles(){
  162. const cloneInputData = inputData.value.map(item => ({ ...item }))
  163. const backups = inputData.value.map(item => ({ male: item.backupMale, female: item.backupFemale }))
  164. const targets = inputData.value.map(item => ({ male: item.targetMale, female: item.targetFemale }))
  165. inputData.value = inputData.value.map((_, index) => ({
  166. backupFemale: targets[index]?.female || null,
  167. backupMale: targets[index]?.male || null,
  168. targetFemale: backups[index]?.female || null,
  169. targetMale: backups[index]?.male || null,
  170. }))
  171. }
  172. // 计算属性
  173. const totalTargetPower = computed(() => {
  174. return outputData.value.reduce((sum, item) => (sum + item.targetGender ? (Number(item.targetPower) || 0) : 0), 0);
  175. });
  176. const totalBackupPower = computed(() => {
  177. return outputData.value.reduce((sum, item) => sum + (item.backupGender ? (Number(item.backupPower) || 0) : 0), 0);
  178. });
  179. const powerDifference = computed(() => {
  180. return totalTargetPower.value - totalBackupPower.value
  181. });
  182. // 目标角色操作
  183. const addTargetRow = () => {
  184. gender.value = ''
  185. powers.value = ''
  186. role.value = ''
  187. batchRef.value.open()
  188. };
  189. function isPositiveInteger(str) {
  190. return /^[1-9]\d*$/.test(str);
  191. }
  192. function generatePowers(_powers, gender){
  193. const keys = ['targetMale', 'targetFemale', 'backupMale', 'backupFemale']
  194. _powers = _powers.replace(/,/g, ',')
  195. const powerSet = _powers.split(',')
  196. const isOk = powerSet.every(num => isPositiveInteger(num))
  197. if(!isOk) return false
  198. // if(!isOk){
  199. // ElMessage.warning('势力必须为正整数')
  200. // return
  201. // }
  202. const roleProp = role.value === '目标角色' ? 'target' : 'backup'
  203. const genderProp = gender === '公' ? 'Male' : 'Female'
  204. inputData.value.forEach((_, index) => {
  205. inputData.value[index][`${roleProp}${genderProp}`] = null
  206. })
  207. const prop = `${roleProp}${genderProp}`
  208. const values = inputData.value.map(item => item[prop]).filter(value => value);
  209. if (inputData.value.length === 0) {
  210. for (let index = 0; index < powerSet.length; index++) {
  211. const item = {}
  212. keys.forEach(key => {
  213. if (key === prop) {
  214. item[key] = powerSet[index]
  215. } else {
  216. item[key] = null
  217. }
  218. })
  219. inputData.value.push(item)
  220. }
  221. } else if (values.length < inputData.value.length) {
  222. let pIndex = -1
  223. for (let index = values.length; index < inputData.value.length; index++) {
  224. pIndex += 1
  225. inputData.value[index][prop] = powerSet[pIndex]
  226. }
  227. const calcNum = powerSet.length - (pIndex + 1)
  228. for (let index = 0; index < calcNum; index++) {
  229. pIndex += 1
  230. const item = {}
  231. keys.forEach(key => {
  232. if (key === prop) {
  233. item[key] = powerSet[pIndex]
  234. } else {
  235. item[key] = null
  236. }
  237. })
  238. inputData.value.push(item)
  239. }
  240. }
  241. }
  242. function handleConfirm(){
  243. const _malePowers = malePowers.value.trim()
  244. const _feMalePowers = feMalePowers.value.trim()
  245. const isMaleOk = generatePowers(_malePowers, '公')
  246. const isFemakeOk = generatePowers(_feMalePowers, '母')
  247. if(typeof isMaleOk === 'boolean' && !isMaleOk){
  248. ElMessage.warning('势力必须为数字')
  249. return
  250. } else if(typeof isFemakeOk === 'boolean' && !isFemakeOk){
  251. ElMessage.warning('势力必须为数字')
  252. return
  253. }
  254. batchRef.value.close()
  255. }
  256. async function calculateBestMatch() {
  257. let startTime = new Date().getTime()
  258. let endTime = null
  259. outputData.value = []
  260. const targetData = []
  261. const backupData = []
  262. inputData.value.forEach(item => {
  263. if(item.targetMale){ targetData.push({ gender: '公', power: item.targetMale }) }
  264. if(item.targetFemale){ targetData.push({ gender: '母', power: item.targetFemale }) }
  265. if(item.backupMale){ backupData.push({ gender: '公', power: item.backupMale }) }
  266. if(item.backupFemale){ backupData.push({ gender: '母', power: item.backupFemale }) }
  267. })
  268. if (targetData.length === 0) {
  269. ElMessage.warning('至少添加一条目标角色数据')
  270. return
  271. }
  272. if (backupData.length === 0) {
  273. ElMessage.warning('至少添加一条备选角色数据')
  274. return
  275. }
  276. let target = null
  277. let backup = null
  278. let isTansfer = false
  279. if(targetData.length > backupData.length){
  280. isTansfer = true
  281. target = backupData
  282. backup = targetData
  283. } else {
  284. target = targetData
  285. backup = backupData
  286. }
  287. const result = [];
  288. // 按性别分类
  289. const targetMale = target.filter(item => item.gender === '公');
  290. const targetFemale = target.filter(item => item.gender === '母');
  291. const backupMale = backup.filter(item => item.gender === '公');
  292. const backupFemale = backup.filter(item => item.gender === '母');
  293. // 计算匹配成本矩阵(power 差的绝对值)
  294. const costMatrixMaleFemale = [];
  295. targetMale.forEach(t => {
  296. const row = [];
  297. backupFemale.forEach(b => { row.push(Math.abs(t.power - b.power)); });
  298. costMatrixMaleFemale.push(row);
  299. });
  300. const costMatrixFemaleMale = [];
  301. targetFemale.forEach(t => {
  302. const row = [];
  303. backupMale.forEach(b => {
  304. row.push(Math.abs(t.power - b.power));
  305. });
  306. costMatrixFemaleMale.push(row);
  307. });
  308. // 匹配函数:尽量匹配并返回匹配结果
  309. function matchBestPairs(costMatrix, targetGroup, backupGroup) {
  310. const result = [];
  311. while (targetGroup.length > 0 && backupGroup.length > 0) {
  312. // 找到最小的 cost
  313. let minDiff = Infinity;
  314. let minTargetIdx = -1;
  315. let minBackupIdx = -1;
  316. for (let i = 0; i < targetGroup.length; i++) {
  317. for (let j = 0; j < backupGroup.length; j++) {
  318. if (costMatrix[i][j] < minDiff) {
  319. minDiff = costMatrix[i][j];
  320. minTargetIdx = i;
  321. minBackupIdx = j;
  322. }
  323. }
  324. }
  325. console.log(targetGroup[minTargetIdx], backupGroup[minBackupIdx])
  326. // 记录最小差值的匹配并移除对应元素
  327. result.push({ target: targetGroup[minTargetIdx], backup: backupGroup[minBackupIdx] });
  328. targetGroup.splice(minTargetIdx, 1);
  329. backupGroup.splice(minBackupIdx, 1);
  330. }
  331. return result;
  332. }
  333. // 匹配公对母,母对公
  334. const maleFemaleMatches = matchBestPairs(costMatrixMaleFemale, targetMale, backupFemale);
  335. const femaleMaleMatches = matchBestPairs(costMatrixFemaleMale, targetFemale, backupMale);
  336. // 合并匹配结果
  337. result.push(...maleFemaleMatches, ...femaleMaleMatches);
  338. let totalTargetPower = 0
  339. let totalBackupPower = 0
  340. result.forEach(item => {
  341. outputData.value.push({
  342. targetGender: isTansfer ? item.backup.gender : item.target.gender,
  343. targetPower: isTansfer ? item.backup.power : item.target.power,
  344. backupGender:isTansfer ? item.target.gender : item.backup.gender,
  345. backupPower:isTansfer ? item.target.power : item.backup.power,
  346. })
  347. })
  348. outputData.value = outputData.value.filter(item => {
  349. if (item.targetGender === '公' && item.backupGender === '公') return false
  350. if (item.targetGender === '母' && item.backupGender === '母') return false
  351. return true
  352. })
  353. outputData.value.forEach(item => {
  354. totalTargetPower = totalTargetPower + Number(item.targetPower)
  355. totalBackupPower = totalBackupPower + Number(item.backupPower)
  356. })
  357. outputData.value.push({
  358. targetGender: '',
  359. targetPower: totalTargetPower,
  360. backupGender: '',
  361. backupPower: totalBackupPower,
  362. })
  363. endTime = new Date().getTime()
  364. const calc = endTime - startTime
  365. timer.value = calc
  366. }
  367. </script>
  368. <style>
  369. textarea {
  370. font-size: 13px;
  371. padding: 8px;
  372. }
  373. </style>
  374. <style lang="scss" scoped>
  375. .dialog-title {
  376. line-height: 30px;
  377. padding-left: 10px;
  378. font-size: 14px;
  379. }
  380. .table-wrapper {
  381. font-size: 14px !important;
  382. }
  383. .custom-picker {
  384. border: 1px solid #e3e3e3;
  385. padding: 3px 5px;
  386. text-align: center;
  387. width: 100%;
  388. div {
  389. display: flex;
  390. justify-content: space-between;
  391. text {
  392. overflow: hidden;
  393. white-space: nowrap;
  394. text-overflow: ellipsis;
  395. }
  396. }
  397. }
  398. .container {
  399. padding: 20px;
  400. }
  401. .section {
  402. margin-bottom: 20px;
  403. }
  404. .section-title {
  405. font-size: 18px;
  406. font-weight: bold;
  407. margin-bottom: 10px;
  408. }
  409. .input {
  410. width: 100%;
  411. text-align: center;
  412. border: 1px solid #ddd;
  413. padding: 5px;
  414. }
  415. button {
  416. margin: 5px;
  417. }
  418. .add-btn,
  419. .calculate-btn {
  420. background-color: #007bff;
  421. color: white;
  422. padding: 10px;
  423. font-size: 16px;
  424. text-align: center;
  425. border-radius: 5px;
  426. }
  427. .delete-btn {
  428. background-color: #ff4d4f;
  429. color: white;
  430. padding: 5px;
  431. font-size: 14px;
  432. border-radius: 5px;
  433. }
  434. .summary {
  435. margin-top: 20px;
  436. font-size: 14px;
  437. text-align: right;
  438. font-weight: bold;
  439. }
  440. </style>