Advertisement
shadowsdieaway

JS Twitch chat listener voting script:

Apr 6th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. // JS Twitch chat listener voting script:
  2.  
  3. var inputs = [
  4. {"username": "ShadowsDieAway", "input": "12 16"},
  5. {"username": "TheNewGeezer", "input": "5 24"},
  6. {"username": "YoYoYoDude1", "input": "24 16"},
  7. {"username": "ShadowsDieAway", "input": "11 11"},
  8. {"username": "TheNewGeezer", "input": "24 5"},
  9. {"username": "YoYoYoDude1", "input": "lol 16"},
  10. {"username": "ShadowsDieAway", "input": "5 5"},
  11. {"username": "TheNewGeezer", "input": "9 9"},
  12. {"username": "YoYoYoDude1", "input": "2 2"},
  13. {"username": "failure", "input": "lol hahaha"},
  14. {"username": "failure", "input": "3"},
  15. {"username": "failure", "input": "5 7 8"},
  16. {"username": "failure", "input": "nope"}
  17. ];
  18.  
  19. var votes = [];
  20. var votes_x = {};
  21. var votes_y = {};
  22. function range_x(x) { return x >= 1 && x <= 25 };
  23. function range_y(y) { return y >= 1 && y <= 19 };
  24. function top_votes(type) {
  25. return Object.keys(type).sort( function(a,b) {
  26. return type[b] - type[a];
  27. }).map(function(x) {
  28. return x + ": " + type[x];
  29. });
  30. };
  31.  
  32. inputs.forEach(function(input) {
  33. // parse
  34. var username = input["username"];
  35. var vote = input["input"].split(" ").map(function(x) { return parseInt(x,10); });
  36.  
  37. // cull
  38. var usernames = votes.map(function(x) { return x[0] });
  39.  
  40. // validation
  41. if ( !usernames.includes(username) && vote.length == 2 && range_x(vote[0]) && range_y(vote[1]) ) {
  42. votes.push([ username, vote[0], vote[1] ]);
  43. !votes_x[vote[0]] ? votes_x[vote[0]] = 1 : votes_x[vote[0]]++;
  44. !votes_y[vote[1]] ? votes_y[vote[1]] = 1 : votes_y[vote[1]]++;
  45. };
  46. });
  47.  
  48. console.log(votes);
  49. console.log(top_votes(votes_x));
  50. console.log(top_votes(votes_y));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement