JsMind
From Hobby'dev
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jsM</title>
<link rel="stylesheet" href="style.css">
<style>
body {
background: #333;
color: #fff;
font-family: Calibri;
}
</style>
<script src="js/logic.js"></script>
<script src="js/rivescript.js"></script>
</head>
<body>
<h1>jsM</h1>
<script>
var Idle = Symbol("Idle"),
Running = Symbol("Running"),
Success = Symbol("Sucess"),
Failure = Symbol("Failure");
var node = {
tick: {
"select": function() {
if (this.status == Idle) {
this.current = 0;
this.status = Running;
}
if (this.children[this.current].status == Success) {
this.children[this.current].status = Idle;
this.status = Success;
return;
}
if (this.children[this.current].status == Failure) {
this.children[this.current].status = Idle;
this.current++;
if (this.current == this.children.length) {
this.status = Failure;
}
return;
}
this.children[this.current].tick();
},
"sequence": function() {
if (this.status == Idle) {
this.current = 0;
this.status = Running;
}
if (this.children[this.current].status == Success) {
this.children[this.current].status = Idle;
this.current++;
if (this.current == this.children.length) {
this.status = Success;
}
return;
}
if (this.children[this.current].status == Failure) {
this.children[this.current].status = Idle;
this.status = Failure;
return;
}
this.children[this.current].tick();
},
"selectMaxUtility": function() {
if (this.children.length == 0) return;
var max = 0,
best = 0,
u;
for (var c=0; c<this.children.length; c++) {
u = this.children[c].utility ? this.children[c].utility() : 0;
if (u > max) {
max = u;
best = c;
}
}
this.children[best].tick();
this.status = this.children[best].status;
},
"print": function() {
console.log(this.text);
this.status = Success;
},
"constantUtility": function() {
console.log("utility "+this.u);
this.child.tick();
this.status = this.child.status;
},
"fail": function() {
this.status = Failure;
}
},
utility: {
"constantUtility": function() {
if (typeof this.u == "function")
return this.u();
else
return this.u;
}
}
};
var b = {
"select": function() {
return {
type: "select",
status: Idle,
current: 0,
children: Array.from(arguments),
tick: node.tick.select
};
},
"sequence": function() {
return {
type: "sequence",
status: Idle,
current: 0,
children: Array.from(arguments),
tick: node.tick.sequence
};
},
"selectMaxUtility": function() {
return {
type: "selectMaxUtility",
status: Idle,
children: Array.from(arguments),
tick: node.tick.selectMaxUtility
};
},
"print": function() {
return {
type: "print",
status: Idle,
text: Array.from(arguments).join(''),
tick: node.tick.print
};
},
"constantUtility": function(util, child) {
return {
type: "constantUtility",
status: Idle,
u: util,
child: child,
utility: node.utility.constantUtility,
tick: node.tick.constantUtility
};
},
"fail": function() {
return {
type: "fail",
status: Idle,
tick: node.tick.fail
};
}
};
var bt = b.sequence(
b.print("1 foo"),
b.select(
b.fail(),
b.fail(),
b.print("2 foo"),
b.fail(),
b.print("3 foo")
),
b.selectMaxUtility(
b.constantUtility(4, b.print("4 foo")),
b.constantUtility(function(){return 5}, b.print("5 foo"))
)
);
while ((bt.status != Success) && (bt.status != Failure)) {
bt.tick();
console.log(bt.type+": "+bt.status.toString());
}
var or = logic.or,
and = logic.and,
eq = logic.eq,
run = logic.run,
lvar = logic.lvar,
between = logic.between
//creates two unique logic variables
var x = lvar(),
y = lvar()
//creates a 'goal'
g1 = or(
and(eq(x,2), eq(y,3)),
and(eq(x,y), eq(y,'dog'))
)
//runs goal asking for the possible values of x and y
console.log(run(g1, x)); //[2, 'dog']
console.log(run(g1, y)); //[3, 'dog']
console.log(run(g1, [x,y])); //[ [2, 3], ['dog', 'dog'] ]
var rs = new RiveScript({
});
rs.stream(`
+ hey *
- hello
- hi there <star>
+ *
- what?
`,function(err){console.log(err);});
rs.sortReplies();
rs.reply("user", "some foo stuff").then(function(reply) {
console.log("bot> "+reply);
});
setTimeout(function() {
rs.stream(`
+ * foo *
- you're talking about fooooo
`,function(err){console.log(err);});
rs.sortReplies();
rs.reply("user", "some foo stuff").then(function(reply) {
console.log("bot> "+reply);
});
}, 1000);
</script>
</body>
</html>