everything = {
async function addPlayer ({userId, gameId, role }) {
await new Promise(resolve => setTimeout(resolve, 1000))
console.log('Adding player', {userId, gameId, role })
const getFormData = object => Object.keys(object).reduce((formData, key) => {
formData.append(key, object[key]);
return formData;
}, new FormData());
const data = new URLSearchParams(getFormData({formType:'addPlayerToGame',
userId, gameId, role}))
await fetch("/admin/games", {
method: "post",
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
body: data
})
}
async function assignTeamToGame({team, role, gameId}) {
for (const p of team.players) { await addPlayer({userId: p.userId, role, gameId}) }
}
function assignPairsToGames({pairs, gameIds}) {
const zip = (l, r) => l.map((e, i) => [e, r[i]])
return zip(pairs, gameIds).map(([pair, gameId]) => [{gameId, team: pair[0], role: 'ATTACKER'}, {gameId, team: pair[1], role: 'DEFENDER'}]).flat()
}
async function stageAssignments(assignments) {
for (const assignment of assignments) {
await assignTeamToGame(assignment)
}
}
function randomlyPairTeams({teams}) {
const rand = teams.sort(() => Math.random() > .5)
const zip = (l, r) => l.map((e, i) => [e, r[i]])
console.log('Remember to save these pairs!')
return zip(rand.slice(0, teams.length/2), rand.slice(teams.length/2, teams.length))
}
function readStagedGames() {
return [...document.querySelectorAll('#table-staged-games tbody tr td:nth-child(2)')].map(e => e.textContent)
}
}