You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
4.9 KiB

  1. const TESTS_LIST = ['Charles', 'Baruch','Frédéric', 'John', 'Dan',
  2. 'Linus', 'Nina', 'Mikhaïl', 'Russel', 'Françoise',
  3. 'Albert', 'Beth', 'Erykah', 'Aretha', 'Lucio',
  4. 'Bartleby','Zach','Patti','Aesop','Vernon']
  5. var participants;
  6. function loadParticipantsFileChooser(){
  7. document.getElementById('filechooser').click()
  8. }
  9. function loadTest(){
  10. addParticipants(TESTS_LIST)
  11. }
  12. function loadParticipantsFromFile(file){
  13. let reader = new FileReader();
  14. let contents = reader.readAsText(file);
  15. reader.onload = function(event) {
  16. let contents = reader.result;
  17. let lines = contents.split('\n');
  18. addParticipants(lines)
  19. };
  20. }
  21. function addParticipants(list){
  22. list.forEach(function(item) {
  23. if(item.trim() !== '')
  24. addParticipant(item)
  25. });
  26. }
  27. function addParticipant(name){
  28. var newInput = document.createElement('input');
  29. newInput.setAttribute('class','participant');
  30. newInput.setAttribute('placeholder','participant');
  31. if(name)
  32. newInput.value = name;
  33. document.getElementById('participants').appendChild(newInput)
  34. }
  35. function russellize(){
  36. let availablesPerParticipants = new Map()
  37. participants.forEach(function (p) {
  38. availablesPerParticipants.set(p,participants.slice().filter(el => el !== p))
  39. });
  40. let k = document.getElementById('nbPerGroup').value
  41. let nbRounds = document.getElementById('nbRounds').value
  42. let nbGroups = Math.ceil(participants.length / k)
  43. let rounds = new Array()
  44. for(let i = 1; i <= nbRounds; i++){
  45. let round = new Array()
  46. let availables = participants.slice()
  47. for(let j = 0; j < nbGroups ; j++){
  48. let currentGroup = new Array()
  49. let toTest = availables.slice()
  50. while(currentGroup.length < k){
  51. let random = Math.floor(Math.random()*toTest.length)
  52. let randomParticipant = toTest[random]
  53. let ok = true
  54. toTest.splice(random, 1);
  55. // console.log(randomParticipant + '/' + random)
  56. currentGroup.forEach(function (p) {
  57. if(availablesPerParticipants.get(p).indexOf(randomParticipant)<0){
  58. ok = false
  59. return
  60. }
  61. })
  62. if(!ok && toTest.length === 0){
  63. return false
  64. }
  65. if(ok){
  66. let randomEq = availables.indexOf(randomParticipant)
  67. availables.splice(randomEq, 1);
  68. // console.log(availables)
  69. currentGroup.forEach(function (p) {
  70. let l = availablesPerParticipants.get(p)
  71. let index = l.indexOf(randomParticipant)
  72. l.splice(index,1)
  73. })
  74. currentGroup.push(randomParticipant)
  75. }
  76. }
  77. round.push(currentGroup)
  78. }
  79. rounds.push(round)
  80. }
  81. return rounds
  82. }
  83. function run(){
  84. doIt()
  85. }
  86. function doIt(){
  87. participants = new Array()
  88. var inputs = document.getElementsByClassName('participant')
  89. for(var i = 0; i < inputs.length; i++){
  90. participants.push(inputs[i].value)
  91. }
  92. let rounds = false
  93. let attempts = 0
  94. while(!rounds){
  95. attempts ++
  96. rounds = russellize()
  97. }
  98. console.log(rounds)
  99. console.log("Attempts = " + attempts)
  100. let stats = new Map()
  101. participants.forEach(function (p) {
  102. let l = new Array()
  103. stats.set(p,l)
  104. rounds.forEach(function (round) {
  105. round.forEach(function (group) {
  106. if(group.indexOf(p)>=0){
  107. group.forEach(function (p2) {
  108. if(p !== p2){
  109. l.push(p2)
  110. }
  111. })
  112. }
  113. })
  114. })
  115. })
  116. document.getElementById('rounds').innerHTML=''
  117. let view = renderRounds(rounds)
  118. document.getElementById('rounds').appendChild(view)
  119. }
  120. function renderRounds(rounds){
  121. let x = 1
  122. let roundsBlock = document.createElement('div')
  123. rounds.forEach(function (round) {
  124. let roundBlockWrap = document.createElement('div')
  125. let roundBlock = document.createElement('div')
  126. let roundTitle = document.createElement('h2')
  127. let roundTitleText = document.createTextNode('Round ' + x)
  128. roundTitle.appendChild(roundTitleText)
  129. roundBlockWrap.appendChild(roundTitle)
  130. roundBlock.classList.add('round')
  131. let y = 1
  132. round.forEach(function (group) {
  133. let groupBlock = document.createElement('div')
  134. let groupTitle = document.createElement('h4')
  135. let groupTitleText = document.createTextNode('Group ' + y)
  136. groupTitle.append(groupTitleText)
  137. groupBlock.append(groupTitle)
  138. roundBlock.append(groupBlock)
  139. let list = document.createElement('ul')
  140. groupBlock.appendChild(list)
  141. groupBlock.classList.add('group')
  142. group.forEach(function (p) {
  143. let item = document.createElement('li')
  144. item.appendChild(document.createTextNode(p))
  145. list.appendChild(item)
  146. })
  147. y ++
  148. })
  149. x++
  150. roundBlockWrap.append(roundBlock)
  151. roundsBlock.appendChild(roundBlockWrap)
  152. })
  153. return roundsBlock
  154. }