As89q98123678

Untitled

Oct 10th, 2019
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. class KahootBot {
  2. constructor(gamePin, name) {
  3. this.gamePin = gamePin
  4. this.name = name
  5. this.id = 1
  6. this.joined = false
  7. }
  8.  
  9. setSessionToken(token) {
  10. this.sessionToken = token
  11. }
  12.  
  13. sendConnectPacket() {
  14. this.id += 1
  15. let connectPayload = {
  16. "id": ""+this.id,
  17. "channel": "/meta/connect",
  18. "connectionType": "websocket",
  19. "advice": {
  20. "timeout": 0
  21. },
  22. "clientId": this.clientId
  23. }
  24.  
  25. this.socket.send(JSON.stringify(connectPayload))
  26. }
  27.  
  28. sendControllerPacket() {
  29. this.id += 1
  30. let controllerPayload = {
  31. "id": ""+this.id,
  32. "channel": "/service/controller",
  33. "data": {
  34. "type": "login",
  35. "gameid": this.gamePin,
  36. "host": "kahoot.it",
  37. "name": this.name
  38. },
  39. "clientId": this.clientId
  40. }
  41.  
  42. this.socket.send(JSON.stringify(controllerPayload))
  43. }
  44.  
  45. setup() {
  46. let handshakePayload = {
  47. "id": ""+this.id,
  48. "version": "1.0",
  49. "minimumVersion": "1.0",
  50. "channel": "/meta/handshake",
  51. "supportConnectionType": ["websocket"],
  52. "advice": {
  53. "timeout": 60000,
  54. "interval": 0
  55. }
  56. }
  57.  
  58. try {
  59. this.socket = new WebSocket("wss://kahoot.it/cometd/"+this.gamePin+"/"+this.sessionToken)
  60. } catch (err) {
  61. console.log(err)
  62. this.start()
  63. }
  64.  
  65. this.socket.onopen = e => {
  66. this.socket.send(JSON.stringify(handshakePayload))
  67. }
  68.  
  69. this.socket.onmessage = event => {
  70. let jsonData = JSON.parse(event.data)
  71.  
  72. if (jsonData[0]["channel"] == "/meta/handshake") {
  73. console.log("Handshake response recieved")
  74. if (jsonData[0]["successful"]) {
  75. this.clientId = jsonData[0]["clientId"]
  76. console.log("Handshake successful")
  77. this.sendConnectPacket()
  78. } else {
  79. console.log("Failed")
  80. }
  81. } else if (jsonData[0]["channel"] == "/meta/connect") {
  82. if (this.joined) {
  83. this.sendConnectPacket()
  84. } else {
  85. console.log("Connect response recieved")
  86. console.log(jsonData)
  87. if (jsonData[0]["successful"]) {
  88. this.sendControllerPacket()
  89. } else {
  90. console.log("Failed")
  91. console.log(jsonData)
  92. }
  93. }
  94. } else if (jsonData[0]["channel"] == "/service/controller") {
  95. if (jsonData[0]["successful"]) {
  96. console.log("Controller payload success!!")
  97. this.joined = true
  98. this.sendConnectPacket()
  99. } else if (jsonData[0]["data"]["type"] == "loginResponse") {
  100. console.log("Recieved game informations")
  101. } else {
  102. console.log("Controller payload failed")
  103. console.log(jsonData)
  104. }
  105. }
  106. }
  107.  
  108. this.socket.onclose = function(event) {
  109. if (event.wasClean) {
  110. console.log("Connection closed cleanly")
  111. } else {
  112. console.log("Connection died :(")
  113. }
  114. }
  115.  
  116. this.socket.onerror = event => {
  117. this.socket.close(1000, "Connection error")
  118. console.log("Error -> will attempt to generate a new session token")
  119.  
  120. }
  121. }
  122.  
  123. start() {
  124. fetch("/createSessionToken", {
  125. method: "POST",
  126. headers: {
  127. "Content-Type": "application/x-www-form-urlencoded"
  128. },
  129. body: "pin="+this.gamePin
  130. })
  131. .then(res => {
  132. res.json()
  133. .then(data => {
  134. this.setSessionToken(data["tokens"][0])
  135. this.setup()
  136. })
  137. })
  138. }
  139. }
  140.  
  141. function beginFlood() {
  142. let gamePin = document.getElementById("pin").value;
  143. let name = document.getElementById("name").value;
  144. let amount = document.getElementById("amount").value;
  145. let bots = []
  146. let tokens = []
  147.  
  148. for (let i=0; i<amount; i++) {
  149. bots[i] = new KahootBot(gamePin, name+i)
  150. }
  151.  
  152. for (let i=0; i<amount; i++) {
  153. bots[i].start()
  154. }
  155.  
  156. return false;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment