preload.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Settings, { initForm } from "./Settings";
  2. import Twitch from "./TwitchPubSug"
  3. import { ipcRenderer } from "electron"
  4. import WebSocketServer from "./WSServer";
  5. import AlertFront from "./AlertFront";
  6. window.settings = new Settings()
  7. const twitch = new Twitch();
  8. const wss = new WebSocketServer();
  9. wss.start();
  10. let Alerts: AlertFront | undefined;
  11. // TODO Write validator
  12. function isValidToken(token: string): boolean{
  13. return token != ""
  14. }
  15. window.addEventListener("DOMContentLoaded", () => {
  16. initForm(window.settings);
  17. Alerts = new AlertFront(twitch);
  18. Alerts.updateView();
  19. let twitchstartstopButton = document.getElementById("twitchstartstop");
  20. twitch.bindButton(twitchstartstopButton);
  21. let refresher = () => twitch.updateElement();
  22. setInterval(refresher, 1000);
  23. setTimeout(refresher, 0);
  24. let wssstartstopButton = document.getElementById("wssstartstop");
  25. wss.bindButton(wssstartstopButton);
  26. refresher = () => wss.updateElement();
  27. setInterval(refresher, 1000);
  28. setTimeout(refresher, 0);
  29. let getTokenButton = document.getElementById("settGetToken");
  30. getTokenButton.addEventListener("click", e => {
  31. if(isValidToken(window.settings.options.twitch_client_id)){
  32. ipcRenderer.send('twitch-oauth', window.settings.options.twitch_client_id);
  33. }
  34. })
  35. ipcRenderer.on("twitch-oauth", (event, authcode: string)=>{
  36. console.log(authcode);
  37. window.settings.options.twitch_oauth_token = authcode;
  38. window.settings.save();
  39. window.settings.updateView();
  40. });
  41. twitchstartstopButton.addEventListener("click", e => {
  42. console.log("Start stop");
  43. if(twitch.status()){
  44. twitch.stop();
  45. }else{
  46. twitch.start();
  47. }
  48. })
  49. twitch.on("reward", (reward) => {
  50. let al = window.settings.options.alerts;
  51. console.log("Reward");
  52. for(let key in al){
  53. if(al.hasOwnProperty(key)){
  54. if(al[key].rewardtitle === reward.redemption.reward.title){
  55. console.log("Trigering:", al[key].rewardtitle);
  56. wss.sendToAll(JSON.stringify([
  57. {
  58. cmd: "src",
  59. src: al[key].filepath
  60. },
  61. {
  62. cmd: "start"
  63. }
  64. ]));
  65. }
  66. }
  67. }
  68. // if(reward.redemption.id == "testid"){
  69. // wss.sendToAll("src avatar.mp4");
  70. // }else{
  71. // wss.sendToAll("src video.mp4");
  72. // }
  73. // wss.sendToAll("start");
  74. // setTimeout(()=>{
  75. // wss.sendToAll("stop")
  76. // }, 10000)
  77. })
  78. wssstartstopButton.addEventListener("click", e => {
  79. console.log("Start stop");
  80. if(wss.status()){
  81. wss.stop();
  82. }else{
  83. wss.changePort(window.settings.options.port)
  84. wss.start();
  85. }
  86. })
  87. let addAlertButton = document.getElementById("addAlert");
  88. addAlertButton.addEventListener("click", e => {
  89. Alerts.add();
  90. })
  91. });