Compare commits

...

3 Commits

Author SHA1 Message Date
Will Webberley
4eeb573e3e updated logger URL and tidied up scripts 2016-03-30 20:50:48 +00:00
Will Webberley
e272016176 ensure app copes with undefined elements 2016-03-30 20:11:27 +00:00
Will Webberley
3983b781a4 changes re. Dave for Sherlock-SAM exp 2016-03-30 21:02:24 +01:00

View File

@ -5,15 +5,14 @@ var submitted_statements = [];
var scored_cards = []; var scored_cards = [];
var space_pressed = 0; var space_pressed = 0;
var last_space_pressed = 0; var last_space_pressed = 0;
var forbid_input = false;
var multiplayer;
var last_successful_request = 0; var last_successful_request = 0;
var latest_latitude = null; var latest_latitude = null;
var latest_longitude = null; var latest_longitude = null;
var latest_card = null;
var logging_configs = [ var logging_configs = [
{url: 'http://logger.cenode.io/cards/sherlock', logged_cards: []}, {url: 'http://logger.cenode.io/cards/sherlockSam', logged_cards: []},
{url: 'http://logger2.cenode.io/cards/sherlock', logged_cards: []} {url: 'http://logger2.cenode.io/cards/sherlockSam', logged_cards: []}
]; ];
var SHERLOCK_CORE = [ var SHERLOCK_CORE = [
@ -30,7 +29,7 @@ var SHERLOCK_CORE = [
"conceptualise the object O ~ resides in ~ the room R", "conceptualise the object O ~ resides in ~ the room R",
"conceptualise the room R ~ contains ~ the fruit F and has the character C as ~ contents ~ and has the object O as ~ additional contents ~", "conceptualise the room R ~ contains ~ the fruit F and has the character C as ~ contents ~ and has the object O as ~ additional contents ~",
"conceptualise the fruit F ~ is eaten by ~ the character C", "conceptualise the fruit F ~ is eaten by ~ the character C",
"conceptualise the sport S ~ is played by ~ the character C", "conceptualise the sport S ~ is played by ~ the character C and ~ is in ~ the room R",
"conceptualise a ~ question ~ Q that has the value V as ~ text ~ and has the value W as ~ value ~ and has the value X as ~ relationship ~", "conceptualise a ~ question ~ Q that has the value V as ~ text ~ and has the value W as ~ value ~ and has the value X as ~ relationship ~",
"conceptualise the question Q ~ concerns ~ the sherlock thing C", "conceptualise the question Q ~ concerns ~ the sherlock thing C",
@ -104,7 +103,6 @@ var SHERLOCK_CORE = [
"there is an object named 'gorilla'", "there is an object named 'gorilla'",
"there is an object named 'dinosaur'", "there is an object named 'dinosaur'",
"there is an object named 'robot'", "there is an object named 'robot'",
// "there is an object named 'elephant'",
"there is an object named 'ghost'", "there is an object named 'ghost'",
"there is an object named 'balloon'", "there is an object named 'balloon'",
@ -144,50 +142,6 @@ var SHERLOCK_CORE = [
"there is a question named 'q52' that has 'What sport does Giraffe play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Giraffe'", "there is a question named 'q52' that has 'What sport does Giraffe play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Giraffe'",
"there is a question named 'q53' that has 'Where is Hippopotamus?' as text and has 'is in' as relationship and concerns the sherlock thing 'Hippopotamus'", "there is a question named 'q53' that has 'Where is Hippopotamus?' as text and has 'is in' as relationship and concerns the sherlock thing 'Hippopotamus'",
"there is a question named 'q54' that has 'What sport does Hippopotamus play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Hippopotamus'" "there is a question named 'q54' that has 'What sport does Hippopotamus play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Hippopotamus'"
/*
"there is a question named 'q1' that has 'What character eats pineapples?' as text and has 'is eaten by' as relationship and concerns the sherlock thing 'pineapple'",
"there is a question named 'q2' that has 'What sport does Zebra play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Dr Finch'",
"there is a question named 'q3' that has 'What character eats apples?' as text and has 'is eaten by' as relationship and concerns the sherlock thing 'apple'",
"there is a question named 'q4' that has 'What colour shirt is Prof Crane wearing?' as text and has 'shirt colour' as value and concerns the sherlock thing 'Prof Crane'",
"there is a question named 'q6' that has 'Where is Col Robin?' as text and has 'is in' as relationship and concerns the sherlock thing 'Col Robin'",
"there is a question named 'q7' that has 'What colour shirt is Sgt Stork wearing?' as text and has 'shirt colour' as value and concerns the sherlock thing 'Sgt Stork'",
"there is a question named 'q8' that has 'Where is Sgt Stork?' as text and has 'is in' as relationship and concerns the sherlock thing 'Sgt Stork'",
"there is a question named 'q9' that has 'Which character is in the emerald room?' as text and has 'contents' as value and concerns the sherlock thing 'Emerald Room'",
"there is a question named 'q12' that has 'What character eats bananas?' as text and has 'is eaten by' as relationship and concerns the sherlock thing 'banana'",
"there is a question named 'q13' that has 'What character is in the sapphire room?' as text and has 'contents' as value and concerns the sherlock thing 'Sapphire Room'",
"there is a question named 'q17' that has 'What sport does Prof Crane play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Prof Crane'",
"there is a question named 'q18' that has 'What character is wearing a red shirt?' as text and has 'is worn by' as relationship and concerns the sherlock thing 'red'",
"there is a question named 'q19' that has 'What character plays rugby?' as text and has 'is played by' as relationship and concerns the sherlock thing 'rugby'",
"there is a question named 'q20' that has 'What fruit does Rev Hawk eat?' as text and has 'eats' as relationship and concerns the sherlock thing 'Rev Hawk'",
"there is a question named 'q23' that has 'What fruit does Col Robin eat?' as text and has 'eats' as relationship and concerns the sherlock thing 'Col Robin'",
"there is a question named 'q24' that has 'What colour shirt is Dr Finch wearing?' as text and has 'shirt colour' as value and concerns the sherlock thing 'Dr Finch'",
"there is a question named 'q25' that has 'Where is the apple?' as text and has 'is in' as relationship and concerns the sherlock thing 'apple'",
"there is a question named 'q26' that has 'What character is wearing a yellow shirt?' as text and has 'is worn by' as relationship and concerns the sherlock thing 'yellow'",
"there is a question named 'q28' that has 'What fruit is in the silver room?' as text and has 'contains' as relationship and concerns the sherlock thing 'Silver Room'",
"there is a question named 'q30' that has 'What character is wearing a black shirt?' as text and has 'is worn by' as relationship and concerns the sherlock thing 'black'",
"there is a question named 'q31' that has 'What character eats lemons?' as text and has 'is eaten by' as relationship and concerns the sherlock thing 'lemon'",
"there is a question named 'q33' that has 'What fruit does Prof Crane eat?' as text and has 'eats' as relationship and concerns the sherlock thing 'Prof Crane'",
"there is a question named 'q34' that has 'What character plays baseball?' as text and has 'is played by' as relationship and concerns the sherlock thing 'baseball'",
"there is a question named 'q35' that has 'What character plays soccer?' as text and has 'is played by' as relationship and concerns the sherlock thing 'soccer'",
"there is a question named 'q36' that has 'What sport does Sgt Stork play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Sgt Stork'",
"there is a question named 'q37' that has 'What character is in the ruby room?' as text and has 'contents' as value and concerns the sherlock thing 'Ruby Room'",
"there is a question named 'q39' that has 'What character plays golf?' as text and has 'is played by' as relationship and concerns the sherlock thing 'golf'",
"there is a question named 'q40' that has 'What character eats oranges?' as text and has 'is eaten by' as relationship and concerns the sherlock thing 'orange'",
"there is a question named 'q41' that has 'What colour shirt is Capt Falcon wearing?' as text and has 'shirt colour' as value and concerns the sherlock thing 'Capt Falcon'",
"there is a question named 'q45' that has 'What character is in the amber room?' as text and has 'contents' as value and concerns the sherlock thing 'Amber Room'",
"there is a question named 'q47' that has 'Where is Prof Crane?' as text and has 'is in' as relationship and concerns the sherlock thing 'Prof Crane'",
"there is a question named 'q48' that has 'Where is the pear?' as text and has 'is in' as relationship and concerns the sherlock thing 'pear'",
"there is a question named 'q50' that has 'What fruit does Sgt Stork eat?' as text and has 'eats' as relationship and concerns the sherlock thing 'Sgt Stork'",
"there is a question named 'q52' that has 'What sport does Col Robin play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Col Robin'",
"there is a question named 'q53' that has 'Where is Capt Falcon?' as text and has 'is in' as relationship and concerns the sherlock thing 'Capt Falcon'",
"there is a question named 'q54' that has 'What sport does Capt Falcon play?' as text and has 'plays' as relationship and concerns the sherlock thing 'Capt Falcon'"
*/
];
var SHERLOCK_NODE = [
"there is an agent named 'Mycroft' that has 'http://mycroft.cenode.io' as address",
"there is a tell policy named 'p2' that has 'true' as enabled and has the agent 'Mycroft' as target",
"there is a listen policy named 'p4' that has 'true' as enabled and has the agent 'Mycroft' as target"
]; ];
var settings = { var settings = {
@ -222,8 +176,7 @@ var ui = {
main_user_id : null, main_user_id : null,
text : null, text : null,
guess : null, guess : null,
autofill : null, autofill : null
multiplayer: null
}, },
overlays : { overlays : {
login : null, login : null,
@ -247,14 +200,10 @@ function initialize_ui(){
ui.inputs.text = document.getElementById("text"); ui.inputs.text = document.getElementById("text");
ui.inputs.guess = document.getElementById("guess"); ui.inputs.guess = document.getElementById("guess");
ui.inputs.autofill = document.getElementById("autofill"); ui.inputs.autofill = document.getElementById("autofill");
ui.inputs.multiplayer = document.getElementById("multiplayer");
ui.overlays.login = document.getElementById("login_overlay"); ui.overlays.login = document.getElementById("login_overlay");
ui.overlays.moira = document.getElementById("moira_overlay"); ui.overlays.moira = document.getElementById("moira_overlay");
ui.overlays.dashboard = document.getElementById("dashboard_overlay");
ui.info.cards = document.getElementById("cards"); ui.info.cards = document.getElementById("cards");
ui.info.questions = document.getElementById("questions");
ui.info.login_error = document.getElementById("login_error"); ui.info.login_error = document.getElementById("login_error");
ui.info.score = document.getElementById("score");
ui.info.online_status = document.getElementById("online_status"); ui.info.online_status = document.getElementById("online_status");
ui.view_changers = document.getElementsByClassName("change_view"); ui.view_changers = document.getElementsByClassName("change_view");
} }
@ -265,7 +214,7 @@ function bind_listeners(){
ui.inputs.text.onkeyup = key_up; ui.inputs.text.onkeyup = key_up;
ui.inputs.text.onkeydown = key_down; ui.inputs.text.onkeydown = key_down;
for(var i = 0; i < ui.view_changers.length; i++){ for(var i = 0; i < ui.view_changers.length; i++){
ui.view_changers[i].onclick = function(e){change_view(e.target.getAttribute("data-view"));}; ui.view_changers[i].onclick = function(e){change_view(e.target.getAttribute("data-view"));};
} }
} }
@ -282,21 +231,14 @@ function login(e){
} }
user.id = ui.inputs.login_user_id.value.charAt(0).toUpperCase() + ui.inputs.login_user_id.value.slice(1); user.id = ui.inputs.login_user_id.value.charAt(0).toUpperCase() + ui.inputs.login_user_id.value.slice(1);
user.id = user.id.trim(); user.id = user.id.trim();
multiplayer = ui.inputs.multiplayer.checked == true;
if(user.id == null || user.id == ""){ if(user.id == null || user.id == ""){
ui.info.login_error.style.display = "block"; ui.info.login_error.style.display = "block";
return; return;
} }
if(multiplayer){ node = new CENode(MODELS.CORE, SHERLOCK_CORE);
node = new CENode(MODELS.CORE, SHERLOCK_CORE, SHERLOCK_NODE); ui.info.online_status.style.display = "none";
ui.info.online_status.style.display = "block";
check_online();
}
else{
node = new CENode(MODELS.CORE, SHERLOCK_CORE);
ui.info.online_status.style.display = "none";
}
node.agent.set_name(user.id+" agent"); node.agent.set_name(user.id+" agent");
window.setTimeout(function(){ window.setTimeout(function(){
node.add_sentence("there is a tell card named 'msg_{uid}' that is from the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and is to the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and has the timestamp '{now}' as timestamp and has 'there is an agent named \\'"+node.agent.get_name().replace(/'/g, "\\\'")+"\\'' as content"); node.add_sentence("there is a tell card named 'msg_{uid}' that is from the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and is to the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and has the timestamp '{now}' as timestamp and has 'there is an agent named \\'"+node.agent.get_name().replace(/'/g, "\\\'")+"\\'' as content");
@ -322,10 +264,6 @@ function add_sentence(t){
} }
function key_down(e){ function key_down(e){
if(forbid_input){
e.preventDefault();
return false;
}
if(e.keyCode == 9){ if(e.keyCode == 9){
e.preventDefault(); e.preventDefault();
return false; return false;
@ -344,10 +282,6 @@ function key_down(e){
} }
function key_up(e){ function key_up(e){
if(forbid_input){
e.preventDefault();
return false;
}
if(e.keyCode == 13){ if(e.keyCode == 13){
log.recording_presses = false; log.recording_presses = false;
log.end_time = parseInt(new Date().getTime()); log.end_time = parseInt(new Date().getTime());
@ -400,7 +334,13 @@ function send(){
var sentence = input.replace(/'/g, "\\'"); var sentence = input.replace(/'/g, "\\'");
var card; var card;
if(sentence.toLowerCase().trim() == 'show anomalies'){ if(latest_card && sentence.toLowerCase().trim() == 'y' || sentence.toLowerCase().trim() == 'yes'){
confirm_card(latest_card.name, latest_card.content);
}
else if(latest_card && sentence.toLowerCase().trim() == 'n' || sentence.toLowerCase().trim() == 'no'){
unconfirm_card(latest_card.name);
}
else if(sentence.toLowerCase().trim() == 'show anomalies'){
add_card_simple(sentence, 'user'); add_card_simple(sentence, 'user');
var objects = node.concepts.object.instances; var objects = node.concepts.object.instances;
for(var i = 0; i < objects.length; i++){ for(var i = 0; i < objects.length; i++){
@ -413,10 +353,6 @@ function send(){
add_card_simple(input, 'user'); add_card_simple(input, 'user');
} }
else{ else{
if(submitted_statements.indexOf(input.toLowerCase()) > -1 ){
add_card_simple("I cannot accept duplicate information from the same user.", 'friend');
return window.alert("The input is invalid or you've already entered this information!");
}
submitted_statements.push(input.toLowerCase()); submitted_statements.push(input.toLowerCase());
card = "there is an nl card named 'msg_{uid}' that has '"+sentence+"' as content and is to the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and is from the individual '"+user.id+"' and has the timestamp '{now}' as timestamp"; card = "there is an nl card named 'msg_{uid}' that has '"+sentence+"' as content and is to the agent '"+node.agent.get_name().replace(/'/g, "\\'")+"' and is from the individual '"+user.id+"' and has the timestamp '{now}' as timestamp";
@ -426,14 +362,14 @@ function send(){
} }
function confirm_card(id, content){ function confirm_card(id, content){
document.getElementById("confirm_"+id).style.display = "none"; try{
document.getElementById("unconfirm_"+id).style.display = "none"; document.getElementById("confirm_"+id).style.display = "none";
forbid_input = false; document.getElementById("unconfirm_"+id).style.display = "none";
if(submitted_statements.indexOf(content.toLowerCase()) > -1){
add_card_simple("I cannot accept duplicate information from the same user.", 'friend');
return window.alert("You have already entered or conifirmed this statement.");
} }
catch(err){
console.log(err);
}
submitted_statements.push(content.toLowerCase()); submitted_statements.push(content.toLowerCase());
add_card_simple("Yes.", 'user'); add_card_simple("Yes.", 'user');
@ -445,11 +381,8 @@ function confirm_card(id, content){
card+=" and has '"+latest_latitude+"' as latitude"; card+=" and has '"+latest_latitude+"' as latitude";
card+=" and has '"+latest_longitude+"' as longitude"; card+=" and has '"+latest_longitude+"' as longitude";
} }
node.add_sentence(card); node.add_sentence(card);
/*setTimeout(function(){ ui.inputs.text.focus();
ask_question_based_on_input(content);
}, 1500);*/
} }
function unconfirm_card(id){ function unconfirm_card(id){
@ -457,19 +390,16 @@ function unconfirm_card(id){
document.getElementById("unconfirm_"+id).style.display = "none"; document.getElementById("unconfirm_"+id).style.display = "none";
add_card_simple("No.", 'user'); add_card_simple("No.", 'user');
add_card_simple("OK.", 'friend'); add_card_simple("OK.", 'friend');
forbid_input = false; ui.inputs.text.focus();
} }
function update_ui(){ function update_ui(){
if(settings.logged_in == true){ if(settings.logged_in == true){
ui.overlays.login.style.display = "none"; ui.overlays.login.style.display = "none";
ui.info.score.innerHTML = user.score+' points';
if(user.selected_screen == "moira"){ if(user.selected_screen == "moira"){
ui.overlays.moira.style.display = "block"; ui.overlays.moira.style.display = "block";
ui.overlays.dashboard.style.display = "none";
} }
else if(user.selected_screen == "dashboard"){ else if(user.selected_screen == "dashboard"){
ui.overlays.dashboard.style.display = "block";
ui.overlays.moira.style.display = "none"; ui.overlays.moira.style.display = "none";
} }
} }
@ -487,6 +417,7 @@ function add_card_simple(text, user){
} }
function add_card(card){ function add_card(card){
latest_card = card;
var content = card.content; var content = card.content;
var id = card.name; var id = card.name;
var tos = card.is_tos.map(function(to){ var tos = card.is_tos.map(function(to){
@ -517,7 +448,6 @@ function add_card(card){
if(card_type != null && card_type.name == "confirm card"){ if(card_type != null && card_type.name == "confirm card"){
c+='<button id="confirm_'+id+'" class="confirm" onclick="confirm_card(\''+id+'\', \''+content.replace(/'/g, "\\'")+'\')">Yes</button>'; c+='<button id="confirm_'+id+'" class="confirm" onclick="confirm_card(\''+id+'\', \''+content.replace(/'/g, "\\'")+'\')">Yes</button>';
c+='<button id="unconfirm_'+id+'" class="unconfirm" onclick="unconfirm_card(\''+id+'\')">No</button>'; c+='<button id="unconfirm_'+id+'" class="unconfirm" onclick="unconfirm_card(\''+id+'\')">No</button>';
forbid_input = true;
} }
if(linked_content != null){ if(linked_content != null){
c+='<img src="'+linked_content+'" alt="Attachment" />'; c+='<img src="'+linked_content+'" alt="Attachment" />';
@ -607,12 +537,12 @@ function poll_for_instances(){
} }
} }
} }
ui.info.questions.innerHTML = ""; // ui.info.questions.innerHTML = "";
user.score = 0; user.score = 0;
var ratios = {}; var ratios = {};
for(var i = 0; i < user.questions.length ; i++){ for(var i = 0; i < user.questions.length ; i++){
var state = get_question_state(user.questions[i]); var state = get_question_state(user.questions[i]);
ui.info.questions.innerHTML += '<li onclick="alert(\''+user.questions[i].text+'\');" class="response question '+state+'">'+(i+1)+'</li>'; // ui.info.questions.innerHTML += '<li onclick="alert(\''+user.questions[i].text+'\');" class="response question '+state+'">'+(i+1)+'</li>';
if(state == 'answered'){ if(state == 'answered'){
user.score++; user.score++;
} }
@ -621,12 +551,15 @@ function poll_for_instances(){
} }
ratios[state]++; ratios[state]++;
} }
var bars = document.getElementById('dashboard_indicator').getElementsByTagName('div'); // var bars = document.getElementById('dashboard_indicator').getElementsByTagName('div');
for(var i = 0; i < bars.length; i++){ // for(var i = 0; i < bars.length; i++){
bars[i].style.width = "0%"; // bars[i].style.width = "0%";
} // }
for(var type in ratios){ for(var type in ratios){
document.getElementById(type).style.width = Math.floor(ratios[type] * 100 / parseFloat(user.questions.length))+'%'; var typeElem = document.getElementById(type);
if (typeElem) {
typeElem.style.width = Math.floor(ratios[type] * 100 / parseFloat(user.questions.length))+'%';
}
} }
update_ui(); update_ui();
poll_for_instances(); poll_for_instances();
@ -707,24 +640,33 @@ var record_position = function(position){
} }
window.onresize = function(event) { window.onresize = function(event) {
ui.info.cards.style.height = (window.innerHeight - 200)+'px'; ui.info.cards.style.height = (window.innerHeight - 350)+'px';
ui.info.cards.scrollTop = ui.info.cards.scrollHeight; ui.info.cards.scrollTop = ui.info.cards.scrollHeight;
document.getElementById('wrapper').style.height = window.innerHeight+'px';
var content = document.getElementById("main-content");
content.style.height = (window.innerHeight - 50) + "px";
} }
window.onbeforeunload = function() { window.onbeforeunload = function() {
return "Quitting Sherlock may mean you can't resume from where you left off."; return "Quitting Sherlock may mean you can't resume from where you left off.";
}; };
window.onload = function(){ var loadChat = function() {
initialize_ui(); initialize_ui();
bind_listeners(); bind_listeners();
ui.overlays.moira.style.display = "none"; ui.overlays.moira.style.display = "none";
ui.overlays.dashboard.style.display = "none";
if (navigator.geolocation) { if (navigator.geolocation) {
navigator.geolocation.watchPosition(record_position); navigator.geolocation.watchPosition(record_position);
} }
ui.inputs.text.focus(); ui.inputs.text.focus();
ui.info.cards.style.height = (window.innerHeight - 200)+'px'; ui.info.cards.style.height = (window.innerHeight - 350)+'px';
document.getElementById('wrapper').style.height = window.innerHeight+'px';
var content = document.getElementById("main-content");
if(content){
content.style.height = (window.innerHeight - 50) + "px";
}
}
window.onload = function(){
loadChat();
}; };