-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
96 lines (84 loc) · 2.68 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var meshblu = require('meshblu');
console.log("hello");
var MAX_SENTIMENT = 30;
var MIN_SENTIMENT = 0;
var currentSentiment = 15;
var page = document.getElementById("page");
page.addEventListener("click", function() {
page.webkitRequestFullscreen()
}, false);
var conn = meshblu.createConnection({});
function setMood(mood) {
document.getElementById("face").className="face " + mood;
document.getElementById("lefteye").className="eyes left " + mood;
document.getElementById("righteye").className="eyes right " + mood;
document.getElementById("mouth").className="mouth " + mood;
document.getElementById("face").className="face " + mood;
document.getElementById("leftbrow").className="brows left " + mood;
document.getElementById("rightbrow").className="brows right " + mood;
};
function addSentiment(inputScore){
if (inputScore > 1) {
document.getElementById("text").className="textBox " + "ecstatic";
}
else if(inputScore < -1) {
document.getElementById("text").className="textBox " + "angry";
}
else {
document.getElementById("text").className="textBox " + "mellow";
}
if(currentSentiment + inputScore <= MAX_SENTIMENT
&& currentSentiment + inputScore >= MIN_SENTIMENT) {
currentSentiment += inputScore;
}
else if(currentSentiment + inputScore > MAX_SENTIMENT) {
currentSentiment = 30;
}
else if(currentSentiment + inputScore < MIN_SENTIMENT) {
currentSentiment = 0;
}
};
function decideFace() {
if (currentSentiment >= 0 && currentSentiment <= 6) {
return 'angry';
}
else if (currentSentiment >= 7 && currentSentiment <= 13) {
return 'upset';
}
else if (currentSentiment >= 14 && currentSentiment <= 16) {
return 'mellow';
}
else if (currentSentiment >= 17 && currentSentiment <= 23) {
return 'happy';
}
else if (currentSentiment >= 24 && currentSentiment <= 30) {
return 'ecstatic';
}
};
function showCurrentSentimentInConsole() {
console.log(currentSentiment);
};
function inputText(text) {
if(text){
if(text.length > 30) {
document.getElementById("text").innerHTML = text.replace(/</g,'').replace(/>/g, '').substring(0, 30) + '…';
} else {
document.getElementById("text").innerHTML = text.replace(/</g,'').replace(/>/g, '');
}
}
};
// function textColorDecider()
conn.on('ready', function(data){
console.log("ready", data);
conn.subscribe({ uuid: "c5e963f1-f7de-4e54-b7b9-29f14033c16b", types: ['broadcast']}, function(err){
console.log('subscribed', err);
});
});
conn.on('message', function(data){
console.log('message', data);
addSentiment(data.sentiment.score);
decideFace();
setMood(decideFace());
inputText(data.payload);
showCurrentSentimentInConsole();
});