presence.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const presence = new Presence({
  2. clientId: "919182644296683520",
  3. });
  4. interface PlaygoundInfo {
  5. playgroundId?: string;
  6. type?: string;
  7. playgroundName?: string;
  8. playgroundDescription?: string;
  9. }
  10. async function getPlaygroundInfo(playgroundId: string): Promise<string> {
  11. const resp = await fetch(
  12. `https://api.gametools.network/bf2042/playground/?playgroundid=${playgroundId}`
  13. );
  14. if (!resp.ok) throw new Error("Failed");
  15. return resp.text();
  16. }
  17. let called = false,
  18. playgroundId: string = null;
  19. const editingStamp = Math.floor(Date.now() / 1000),
  20. info: PlaygoundInfo = {};
  21. presence.on("UpdateData", async () => {
  22. const presenceData: PresenceData = {
  23. largeImageKey:
  24. "https://cdn.rcd.gg/PreMiD/websites/B/Battlefield%20Portal/assets/logo.png",
  25. },
  26. [block, time, buttons, name, description] = await Promise.all([
  27. presence.getSetting<boolean>("block"),
  28. presence.getSetting<boolean>("time"),
  29. presence.getSetting<boolean>("buttons"),
  30. presence.getSetting<boolean>("name"),
  31. presence.getSetting<boolean>("desc"),
  32. ]),
  33. url = document.URL;
  34. if (url.search("=") > 0) playgroundId = url.split("=").pop();
  35. if (
  36. document.cookie.match(new RegExp("(^| )sessionId=([^;]+)"))[1] &&
  37. playgroundId
  38. ) {
  39. info.playgroundId = playgroundId;
  40. if (document.readyState === "complete") {
  41. if (!called) {
  42. called = true;
  43. await getPlaygroundInfo(playgroundId).then(value => {
  44. const json = JSON.parse(value);
  45. if (!json) called = false;
  46. else {
  47. info.type = json.naming.type;
  48. info.playgroundDescription = json.naming.playgroundDescription;
  49. info.playgroundName = json.naming.playgroundName;
  50. presence.info(JSON.stringify(info));
  51. }
  52. });
  53. }
  54. } else called = false;
  55. }
  56. if (info) {
  57. if (name) presenceData.details = `Making ${info.playgroundName}`;
  58. if (description) presenceData.state = info.playgroundDescription;
  59. }
  60. if (block) {
  61. const selected = document.querySelector(".blocklySelected");
  62. if (selected) {
  63. if (
  64. selected.querySelector(".subroutineBlockSubroutineText")
  65. ?.textContent === "SUBROUTINE"
  66. ) {
  67. const blocklyText = selected.querySelectorAll(
  68. "text[class=blocklyText]"
  69. );
  70. for (const element of blocklyText) {
  71. if (
  72. element.parentElement.getAttribute("transform") ===
  73. "translate(126.36666870117188,18)"
  74. )
  75. presenceData.state = `in Subroutine ${element.textContent}`;
  76. }
  77. } else if (
  78. selected.querySelector(".ruleBlockRuleText")?.textContent === "RULE"
  79. ) {
  80. const blocklyText = selected.querySelectorAll(
  81. "text[class=blocklyText]"
  82. );
  83. for (const element of blocklyText) {
  84. if (
  85. element.parentElement.getAttribute("transform") ===
  86. "translate(72.78333282470703,8.5)"
  87. )
  88. presenceData.state = `in RULE ${element.textContent}`;
  89. }
  90. } else if (description) {
  91. //todo: need to implement if a single block is selected
  92. presenceData.state = info.playgroundDescription;
  93. }
  94. }
  95. }
  96. if (time) presenceData.startTimestamp = editingStamp;
  97. if (buttons) presenceData.buttons = [{ label: "View Experience", url }];
  98. presence.setActivity(presenceData);
  99. });