server/script.js   A
last analyzed

Complexity

Total Complexity 34
Complexity/F 3.4

Size

Lines of Code 156
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 156
rs 9.2
c 1
b 0
f 0
wmc 34
mnd 2
bc 29
fnc 10
bpm 2.9
cpm 3.4
noi 11

6 Functions

Rating   Name   Duplication   Size   Complexity  
A script.js ➔ standBy 0 7 4
B script.js ➔ run 0 35 2
A script.js ➔ last 0 5 3
B script.js ➔ call 0 43 4
A script.js ➔ init 0 4 1
D script.js ➔ speak 0 26 9
1
var extend  = require('extend');
2
3
// ------------------------------------------
4
//  CONSTRUCTOR
5
// ------------------------------------------
6
7
var init = function(){
8
  info('Starting ScriptManager ...');
9
  return ScriptManager;
10
}
11
12
// ------------------------------------------
13
//  RUN / CALL
14
// ------------------------------------------
15
16
var last = function(callback){
17
  if (_name && _last){
18
    run(_name, _last, callback);
19
  }
20
}
21
var _name, _last;
22
var run = function(name, options, callback, backup){
23
  
24
  // Last Backup
25
  if (backup){
26
    _name = name; _last = {};
27
    extend(true, _last, options);
28
    ScriptManager.lastContext = _last; // to be available everywhere
29
  }
30
  
31
  var data = {};
32
  extend(true, data, options);
33
  
34
  // 3. Finish by calling back
35
  var next = function(json){
36
    if (json){ extend(true, data, json); }
37
    if (callback) { callback(data); }
38
  }
39
  
40
  // 2. Dispatch to next script
41
  var dispatch = function(json){
42
    if (json){ extend(true, data, json); }
43
    
44
    // 1.3 Call script
45
    SARAH.RuleEngine.dispatch('after', data);
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
    
47
    // Dispatch
48
    SARAH.RuleEngine.dispatch(name, data, next);
49
  }
50
  
51
  // 1.1 Call before
52
  SARAH.RuleEngine.dispatch('before', data);
53
  
54
  // 1.2 Call script
55
  call(name, data, dispatch);
56
}
57
58
var call = function(name, options, callback){
59
  
60
  // Find Plugin
61
  var plugin = SARAH.find(name);
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
62
  if (!plugin){
63
    warn('call('+name+') ', "Can't find plugin");
64
    if (callback){ callback(); } return;
65
  }
66
  
67
  // Get instance outside of a zone;
68
  var plug = plugin.getInstance(); 
0 ignored issues
show
Unused Code introduced by
The variable plug seems to be never used. Consider removing it.
Loading history...
69
   
70
  // Set callback
71
  var next = function(data){
72
    if (timeout){ clearTimeout(timeout); } else { return; }
73
          
74
    var end = (new Date()).getTime();
75
    info('call('+name+') in ', (end-start)+'ms');
76
    
77
    if (data && data.error){
78
      error('call('+name+') ', data.error);
79
    }
80
    
81
    if (callback){ callback(data); }
82
  }
83
  
84
  // Set timeout
85
  var timeout = setTimeout(function(){
86
    warn('action('+name+') as timeout ! Check plugin\'s callback()');
87
    next();
88
  }, Config.http.timeout);
0 ignored issues
show
Bug introduced by
The variable Config seems to be never declared. If this is a global, consider adding a /** global: Config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
89
  
90
  // Run script
91
  var start  = (new Date()).getTime();
92
  try { 
93
    var instance = plugin.getInstance();
0 ignored issues
show
Unused Code introduced by
The variable instance seems to be never used. Consider removing it.
Loading history...
94
    plugin.getInstance().action(options, next, Config, SARAH);
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
  }
96
  catch(ex){ 
97
    error('call('+name+') ', ex.message);
98
    next();
99
  }
100
}
101
102
// ------------------------------------------
103
//  HOOKS
104
// ------------------------------------------
105
106
var standBy = function(motion, device){
107
  var plugins = SARAH.PluginManager.getList();
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
108
  for (var i = 0 ; i < plugins.length ; i++){
109
    var plugin = plugins[i].getInstance();
110
    if (plugin && plugin.standBy) plugin.standBy(motion, device);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
111
  }
112
}
113
114
var speak = function(tts, async){
115
  if (!tts){ return tts; }
116
  
117
  // Answer
118
  if (tts == 'answer'){ 
119
    var answers = Config.bot.answers.split('|');
0 ignored issues
show
Bug introduced by
The variable Config seems to be never declared. If this is a global, consider adding a /** global: Config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
120
    tts = answers[ Math.floor(Math.random() * answers.length)];
121
  }
122
      
123
  // Dispatch to all plugins
124
  var plugins = SARAH.PluginManager.getList();
0 ignored issues
show
Bug introduced by
The variable SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
125
  for (var i = 0 ; i < plugins.length ; i++){
126
    var plugin = plugins[i].getInstance();
127
    if (plugin && plugin.speak){ 
128
      tts = plugin.speak(tts, async);
129
    }
130
  }
131
  
132
  // Replace Name
133
  if (Profile.last && Profile.last.face && Profile.last.face != "Unknow"){
0 ignored issues
show
Bug introduced by
The variable Profile seems to be never declared. If this is a global, consider adding a /** global: Profile */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
134
    tts = tts.replace("[name]", Profile.last.face);
135
  }
136
  tts = tts.replace('[name]','');
137
  
138
  return tts;
139
}
140
141
// ------------------------------------------
142
//  PUBLIC
143
// ------------------------------------------
144
145
var ScriptManager = {
146
  'init'     : init,
147
  'run'      : run,
148
  'call'     : call,
149
  'last'     : last,
150
  'standBy'  : standBy,
151
  'speak'    : speak,
152
  'lastContext' : {}
153
}
154
155
// Exports Manager
156
exports.init = ScriptManager.init;