Advertisement
Riekelt

livesquid

Apr 6th, 2024
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //pkg index.js --output livesquid
  2. const LiveSplitClient = require('livesplit-client');
  3. const request = require('request');
  4.  
  5. var greenBody = "{'seg': [{'fx': 0, 'col': [[8, 255, 0]]}]}";
  6.  
  7.  
  8. (async () => {
  9.     try {
  10.         // Initialize client with LiveSplit Server's IP:PORT
  11.         const client = new LiveSplitClient('127.0.0.1:16834');
  12.  
  13.         // Connected event
  14.         client.on('connected', () => {
  15.             console.log('Connected!');
  16.         });
  17.  
  18.         // Disconnected event
  19.         client.on('disconnected', () => {
  20.             console.log('Disconnected!');
  21.         });
  22.  
  23.         // Error event
  24.         client.on('error', (err) => {
  25.             console.log(err);
  26.         });
  27.  
  28.         // Some async sleep sugar for this example
  29.         const sleep = (time) => {
  30.             return new Promise((r) => {
  31.                 setTimeout(() => r(), time);
  32.             });
  33.         };
  34.  
  35.         // Connect to the server, Promise will be resolved when the connection will be succesfully established
  36.         await client.connect();
  37.        
  38.         let previousSplitName = '';
  39.  
  40.         // Function to check split name every 2000ms
  41.         setInterval(async () => {
  42.             try {
  43.                 // Get current split name
  44.                 const currentSplitName = await client.getCurrentSplitName();
  45.  
  46.                 // Check if split name has changed
  47.                 if (currentSplitName !== previousSplitName) {
  48.                     // Display split name and delta
  49.                     const delta = await client.getDelta();
  50.                     console.log(`Split Name: ${currentSplitName}, Delta: ${delta}`);
  51.                     // Update previous split name
  52.                     previousSplitName = currentSplitName;
  53.         const info = await client.getAll();
  54.         console.log('Summary:', info);
  55.                 sendPost(greenBody);
  56.                 }
  57.             } catch (error) {
  58.                 console.error('Error:', error);
  59.             }
  60.         }, 2000);
  61.        
  62.         await client.getDelta();
  63.  
  64.  
  65.     } catch (err) {
  66.         console.error(err); // Something went wrong
  67.     }
  68. })();
  69.  
  70. function sendPost(input) {
  71.     request.post({
  72.         url: "http://192.168.2.31/json/state",
  73.         headers: {
  74.             'Content-Type': 'application/json',
  75.             'Accept': "application/json"
  76.         },
  77.         body: input
  78.     }, (error, response, body) => {
  79.         if (error) {
  80.             console.error('Error:', error);
  81.         } else if (response.statusCode !== 200) {
  82.             console.error('Error:', body);
  83.         } else {
  84.             // Do something with the successful response if needed
  85.         }
  86.     });
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement