Created
August 6, 2023 06:08
-
-
Save motsu0/260f4aa64e50a18865bbe78ab34b8ff0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#players-input { | |
box-sizing: border-box; | |
width: 100%; | |
max-width: 600px; | |
height: 200px; | |
padding: 4px; | |
resize: vertical; | |
} | |
#pair-select { | |
height: 2em; | |
} | |
.settings { | |
display: flex; | |
flex-direction: column; | |
row-gap: 16px; | |
margin: 12px 0; | |
} | |
.settings__row { | |
display: flex; | |
gap: 12px; | |
} | |
#result { | |
display: flex; | |
flex-direction: column; | |
row-gap: 12px; | |
margin: 24px 0; | |
} | |
.result__block { | |
padding: 12px; | |
border: 1px solid #aaa; | |
border-radius: 8px; | |
overflow-x: auto; | |
} | |
.result-table { | |
margin-top: 4px; | |
border-collapse: collapse; | |
} | |
.result-table__cell { | |
padding: 4px 8px; | |
border: 1px solid #71ab09; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea | |
id="players-input" | |
placeholder="Aさん | |
Bさん | |
Cさん | |
Dさん | |
Eさん | |
Fさん" | |
></textarea> | |
<div class="settings"> | |
<div class="settings__row"> | |
<select id="pair-select"> | |
<option value="once">同じ組み合わせのペアを許可する</option> | |
<option value="all" selected> | |
同じ組み合わせのペアを許可しない | |
</option> | |
</select> | |
</div> | |
<div class="settings__row"> | |
<button id="submit-bt">ペア分けを決める</button> | |
<button id="reset-bt">リセット</button> | |
</div> | |
</div> | |
<div id="result"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nowloading = new nowLoading(); | |
nowloading.start(); | |
const playersInput = document.getElementById('players-input'); | |
const pairSelect = document.getElementById('pair-select'); | |
const result = document.getElementById('result'); | |
let roundNumber = 1; | |
init(); | |
function init() { | |
const submitBt = document.getElementById('submit-bt'); | |
const resetBt = document.getElementById('reset-bt'); | |
playersInput.addEventListener('input', () => { | |
roundNumber = 1; | |
result.textContent = ''; | |
}); | |
pairSelect.addEventListener('change', () => { | |
roundNumber = 1; | |
result.textContent = ''; | |
}); | |
submitBt.addEventListener('click', submitHandle); | |
resetBt.addEventListener('click', () => { | |
roundNumber = 1; | |
result.textContent = ''; | |
}); | |
// | |
playersInput.value = | |
'Aさん\nBさん\nCさん\nDさん\nEさん\nFさん\nGさん\nHさん'; | |
nowloading.stop(); | |
} | |
function submitHandle() { | |
nowloading.start(); | |
if (pairSelect.value === 'once') { | |
listUpOnce(); | |
} else if (pairSelect.value === 'all') { | |
result.textContent = ''; | |
listUpAll(); | |
} | |
nowloading.stop(); | |
} | |
function getPlayersList() { | |
return playersInput.value | |
.split(/\n/) | |
.map((v) => v.trim()) | |
.filter((v) => v !== ''); | |
} | |
function writeResultBlock(pairList, roundIndex) { | |
const block = document.createElement('div'); | |
block.classList.add('result__block'); | |
const title = document.createElement('div'); | |
title.classList.add('result__block__title'); | |
title.textContent = `ペア分け結果 ${roundIndex}回目`; | |
block.appendChild(title); | |
const table = document.createElement('table'); | |
table.classList.add('result-table'); | |
pairList.forEach((pair) => { | |
const tr = document.createElement('tr'); | |
pair.forEach((player) => { | |
const td = document.createElement('td'); | |
td.classList.add('result-table__cell'); | |
td.textContent = player || ''; | |
tr.appendChild(td); | |
}); | |
table.appendChild(tr); | |
}); | |
block.appendChild(table); | |
return block; | |
} | |
function listUpOnce() { | |
const players = getPlayersList(); | |
if (players.length === 0) return; | |
shuffle(players); | |
if (players.length % 2 === 1) players.push(undefined); | |
const pairList = splitArray(players, 2); | |
const block = writeResultBlock(pairList, roundNumber); | |
result.prepend(block); | |
roundNumber++; | |
// | |
function splitArray(array, size) { | |
return array.flatMap((_, i, a) => | |
i % size ? [] : [array.slice(i, i + size)] | |
); | |
} | |
} | |
function listUpAll() { | |
const players = getPlayersList(); | |
if (players.length === 0) return; | |
shuffle(players); | |
if (players.length % 2 === 1) { | |
players.push(undefined); | |
} | |
const playerLength = players.length; | |
const roundMax = playerLength - 1; | |
const pairMax = Math.ceil(playerLength / 2); | |
for (let round = 0; round < roundMax; round++) { | |
const lastChild = players.slice(-1); | |
const roundPlayerList = players | |
.slice(round, playerLength - 1) | |
.concat(players.slice(0, round)) | |
.concat(lastChild); | |
const pairList = []; | |
for (let pIndex = playerLength / 2 - 1; pIndex >= 0; pIndex--) { | |
const pair = [ | |
roundPlayerList[pIndex], | |
roundPlayerList[playerLength - 1 - pIndex], | |
]; | |
pairList.push(pair); | |
} | |
const block = writeResultBlock(pairList, round + 1); | |
result.appendChild(block); | |
} | |
} | |
function randomMN(m, n) { | |
const d = Math.max(m, n) - Math.min(m, n); | |
return Math.floor(Math.random() * (d + 1) + Math.min(m, n)); | |
} | |
function shuffle(array) { | |
for (let i = array.length - 1; i >= 1; i--) { | |
const j = randomMN(0, i); | |
[array[j], array[i]] = [array[i], array[j]]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment