I am very new to coding, and don't know how to to quite word this questions. I am incorporating the CS:GO Game State Integration feature added recently to change Hue Lights based on what is happening in the game. The current part I am working on is a simple script that will change the first light to blue if I am on the 'CT' team. It's working fine besides the fact that it keeps telling me I'm on CT side over and over again. Is there a way I could change my script to only update if the first If statement becomes false?
Code:
setInterval(checkTeam, 250);
function checkTeam() {
fs.readFile('player_team', 'utf8', function(err, teamStatus) {
if (err) throw err;
if(teamStatus === 'CT') {
console.log('CT Side');
if (currentStatus !== 'CT') {
changeLight(1, noLights());
currentStatus = 'CT';
}
if (currentStatus === 'CT') {
changeLight(1, blueLights());
currentStatus = 'CT';
}
}
if (teamStatus === 'T'){
console.log('T Side');
if (currentStatus !== 'T Side') {
changeLight(3, noLights());
}
if (currentStatus === 'T Side') {
changeLight(3, orangeLights());
currentStatus = 'T';
}
if (currentStatus === null) {
changeLight(1, noLights());
}
}
});
}
function changeLight(light, lightType) {
var jsonString = JSON.stringify(lightType);
http.request({
host: HUE_BRIDGE_IP,
path: HUE_PATH + 'lights/' + light + '/state',
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': jsonString.length
}
}, function() {
}).write(jsonString);
}
function redLights() {
return {
'on': true,
'sat': 254,
'bri': 254,
'hue': 0,
'transitiontime': 0
}
}
function blueLights() {
return {
'on': true,
'sat': 254,
'bri': 254,
'hue': 45000,
'transitiontime': 0
}
}
function explodeLights() {
return {
'on': true,
'sat': 254,
'bri': 254,
'hue': 10000,
'transitiontime': 0
}
}
function whiteLights() {
return {
'on': true,
'sat': 0,
'bri': 254,
'hue': 10000
}
}
function noLights() {
return {
'on': false,
'transitiontime': 0
}
}
function orangeLights() {
return {
'on': false,
'transitiontime': 0
}
}
I'm guessing the only part you really need is the top, but just in case you needed the changeLight function, I included more. Basically, I want to get the if(teamStatus === 'CT') {
to only execute once until that either changes or becomes null. If that's not possible, is there a compromise?
Thanks ahead to anyone who attempts to help,
Appreciate it!
Edit:
So I don't think I gave enough info so here is the code for the server getting the info from the game state:
var bodyParser = require('body-parser');
var express = require('express');
var fs = require('fs');
var app = express();
app.use(bodyParser.json()); // for parsing application/json
app.post('/', function(req, res) {
fs.readFile('player_team', 'utf8', function(err, teamStatus) {
var player = 'player' in req.body ? req.body.player : null;
if (player && player.team !== teamStatus) {
var newTeamStatus = req.body.player.team;
if (!newTeamStatus) {
newTeamStatus = '';
}
fs.writeFile('player_team', newTeamStatus);
console.log(newTeamStatus);
}
});
});
var PORT = 8080;
var HOST = '127.0.0.1';
app.listen(PORT, HOST);
console.log('Server is running on ' + HOST + ':' + PORT);
from what I can guess of the logic, you might want to do:
var currentStatus = '';
function checkTeam() {
fs.readFile('player_team', 'utf8', function (err, teamStatus) {
if (err) throw err;
if (teamStatus !== currentStatus) { // only change lights if status has changed
if (teamStatus === 'CT') {
console.log('CT Side');
changeLight(3, noLights()); // turn off 3?
changeLight(1, blueLights());
}
else if (teamStatus === 'T') {
console.log('T Side');
changeLight(1, noLights()); // turn off 1?
changeLight(3, orangeLights());
}
else {
changeLight(1, noLights());
changeLight(3, noLights()); // 1 and 3?
}
currentStatus = teamStatus; // update the current status
}
});
}
The only thing I can't figure out is what the 1
or 3
in changeLight does, so this code may be incomplete, however I believe it does what your original code does without flickering the lights or console.log what team you are on every 250ms
I've added some comments to lines I think may be needed if there are two different lights one of which goes blue or the other goes orange