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.
 
 
 
 
 
 

203 lines
5.6 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. const NOBODY = '***NOBODY';
  6. function groupSizeChanged(val){
  7. let nbParticipants = document.getElementsByClassName('participant').length;
  8. let meetsPerRound = val - 1;
  9. let maxRounds = Math.floor(nbParticipants / meetsPerRound);
  10. document.getElementById('nbRounds').max = maxRounds
  11. }
  12. var participants;
  13. function loadParticipantsFileChooser(){
  14. document.getElementById('filechooser').click()
  15. }
  16. function loadTest(){
  17. addParticipants(TESTS_LIST)
  18. }
  19. function loadParticipantsFromFile(file){
  20. let reader = new FileReader();
  21. let contents = reader.readAsText(file);
  22. reader.onload = function(event) {
  23. let contents = reader.result;
  24. let lines = contents.split('\n');
  25. addParticipants(lines)
  26. };
  27. }
  28. function addParticipants(list){
  29. list.forEach(function(item) {
  30. if(item.trim() !== '')
  31. addParticipant(item)
  32. });
  33. }
  34. function addParticipant(name){
  35. var newInput = document.createElement('input');
  36. newInput.setAttribute('class','participant');
  37. newInput.setAttribute('placeholder','participant');
  38. if(name)
  39. newInput.value = name;
  40. document.getElementById('participants').appendChild(newInput)
  41. //outch...
  42. document.getElementById('nbPerGroup').disabled = false;
  43. document.getElementById('nbRounds').disabled = false;
  44. document.getElementById('btn-do-it').disabled = false;
  45. }
  46. function russellize(){
  47. let k = document.getElementById('nbPerGroup').value
  48. let nbRounds = document.getElementById('nbRounds').value
  49. let nbGroups = Math.ceil(participants.length / k)
  50. let rounds = new Array()
  51. let missingParticipants = nbGroups * k - participants.length
  52. for(let n = 0; n < missingParticipants; n++)
  53. participants.push(NOBODY + n);
  54. let availablesPerParticipants = new Map()
  55. participants.forEach(function (p) {
  56. availablesPerParticipants.set(p,participants.slice().filter(el => el !== p))
  57. });
  58. for(let i = 1; i <= nbRounds; i++){
  59. let round = new Array()
  60. let availables = participants.slice()
  61. for(let j = 0; j < nbGroups ; j++){
  62. let currentGroup = new Array()
  63. let toTest = availables.slice()
  64. while(currentGroup.length < k){
  65. let random = Math.floor(Math.random()*toTest.length)
  66. let randomParticipant = toTest[random]
  67. let ok = true
  68. toTest.splice(random, 1);
  69. // console.log(randomParticipant + '/' + random)
  70. currentGroup.forEach(function (p) {
  71. if(availablesPerParticipants.get(p).indexOf(randomParticipant)<0){
  72. ok = false
  73. return
  74. }
  75. })
  76. if(!ok && toTest.length === 0){
  77. return false
  78. }
  79. if(ok){
  80. let randomEq = availables.indexOf(randomParticipant)
  81. availables.splice(randomEq, 1);
  82. // console.log(availables)
  83. currentGroup.forEach(function (p) {
  84. let l = availablesPerParticipants.get(p)
  85. let index = l.indexOf(randomParticipant)
  86. l.splice(index,1)
  87. })
  88. currentGroup.push(randomParticipant)
  89. }
  90. }
  91. round.push(currentGroup)
  92. }
  93. rounds.push(round)
  94. }
  95. return rounds
  96. }
  97. function run(){
  98. doIt()
  99. }
  100. function doIt(){
  101. participants = new Array()
  102. var inputs = document.getElementsByClassName('participant')
  103. for(var i = 0; i < inputs.length; i++){
  104. participants.push(inputs[i].value)
  105. }
  106. let rounds = false
  107. let attempts = 0
  108. while(!rounds){
  109. attempts ++
  110. rounds = russellize()
  111. }
  112. console.log(rounds)
  113. console.log("Attempts = " + attempts)
  114. let stats = new Map()
  115. participants.forEach(function (p) {
  116. let l = new Array()
  117. stats.set(p,l)
  118. rounds.forEach(function (round) {
  119. round.forEach(function (group) {
  120. if(group.indexOf(p)>=0){
  121. group.forEach(function (p2) {
  122. if(p !== p2){
  123. l.push(p2)
  124. }
  125. })
  126. }
  127. })
  128. })
  129. })
  130. document.getElementById('rounds').innerHTML=''
  131. let view = renderRounds(rounds)
  132. document.getElementById('rounds').appendChild(view)
  133. }
  134. function renderRounds(rounds){
  135. let x = 1
  136. let roundsBlock = document.createElement('div')
  137. rounds.forEach(function (round) {
  138. let roundBlockWrap = document.createElement('div')
  139. let roundBlock = document.createElement('div')
  140. let roundTitle = document.createElement('h2')
  141. let roundTitleText = document.createTextNode('Round ' + x)
  142. roundTitle.appendChild(roundTitleText)
  143. roundBlockWrap.appendChild(roundTitle)
  144. roundBlock.classList.add('round')
  145. let y = 1
  146. round.forEach(function (group) {
  147. let groupBlock = document.createElement('div')
  148. let groupTitle = document.createElement('h4')
  149. let groupTitleText = document.createTextNode('Group ' + y)
  150. groupTitle.append(groupTitleText)
  151. groupBlock.append(groupTitle)
  152. roundBlock.append(groupBlock)
  153. let list = document.createElement('ul')
  154. groupBlock.appendChild(list)
  155. groupBlock.classList.add('group')
  156. group.forEach(function (p) {
  157. let emptySlot = p.indexOf(NOBODY) >= 0
  158. let item = document.createElement('li')
  159. if(emptySlot)
  160. item.classList='empty-slot'
  161. let pTxt = emptySlot ? '' : p;
  162. item.appendChild(document.createTextNode(pTxt))
  163. list.appendChild(item)
  164. })
  165. y ++
  166. })
  167. x++
  168. roundBlockWrap.append(roundBlock)
  169. roundsBlock.appendChild(roundBlockWrap)
  170. })
  171. return roundsBlock
  172. }