Skip to content

Commit

Permalink
Adding tutorials 7 - 8 + using new naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Salaün committed Sep 21, 2015
1 parent 2992ae8 commit 3607820
Show file tree
Hide file tree
Showing 20 changed files with 919 additions and 51 deletions.
14 changes: 7 additions & 7 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@
});
$('#run').prop('disabled', true);
$('#submitsub').prop('disabled', true);
createSimulation(TUTO_OWNER, TUTO_PROJECT, TUTO_BRANCH, function(instanceId) {
createInstance(TUTO_OWNER, TUTO_PROJECT, TUTO_BRANCH, function(instanceId) {
console.log('Instance', instanceId, 'created');
doWS();
registerActions(function() {
createEntity(TUTO_FOLDER + '/agent_0.bt', {}, function(responseText) {
createAgent(TUTO_FOLDER + '/agent_0.bt', {}, function(responseText) {
var proceed = function() {
updateGlobalKnowledge('capitals', countries, function() {
updateInstanceKnowledge('capitals', countries, function() {
$('#stop').prop('disabled', false);
$('#submitsub').prop('disabled', false);
doUpdate();
});
};
createEntity(TUTO_FOLDER + '/agent_1.bt', {}, proceed, function() {
createAgent(TUTO_FOLDER + '/agent_1.bt', {}, proceed, function() {
console.log('second agent has not been instantiated');
proceed();
});
Expand All @@ -193,9 +193,9 @@
messages.innerHTML = '';
capitalsID.innerHTML = '';
matchedCapitals = [];
loading.innerHTML = 'no simulation';
loading.innerHTML = 'no instance';
clearInterval(idUpdate);
destroySimulation();
destroyInstance();
ws.close();
}

Expand Down Expand Up @@ -234,7 +234,7 @@
</div>
</div>
<div class="col-md-12 col-sm-12 col-xs-12" style="bottom: 1px; position: fixed;">
<div class="label label-info" id="loading">no simulation</div>
<div class="label label-info" id="loading">no instance</div>
</div>
</div>
</div>
Expand Down
26 changes: 13 additions & 13 deletions app/static/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ function registerActions(onloadCB) {
recursiveReg();
}

function Say(requestID, entityID, params) {
messages.innerHTML += '<br/>Agent ' + entityID + ' says: ' + params.message;
function Say(requestID, agentID, params) {
messages.innerHTML += '<br/>Agent ' + agentID + ' says: ' + params.message;
$('#results').animate({scrollTop: document.getElementById('messages').offsetHeight}, 1000);
sendSuccess(requestID);
}

function GetFrontPage(requestID, entityID, params) {
function GetFrontPage(requestID, agentID, params) {
loading.innerHTML = 'loading...';
console.log('requesting front page');
$.getJSON('http://www.reddit.com/r/{0}/new/.json?limit=16&after=t3_{1}&show=all&sr_detail&jsonp=?'.format(params.subreddit, params.after), function(data) {
Expand Down Expand Up @@ -62,7 +62,7 @@ function GetFrontPage(requestID, entityID, params) {
}, true);
}

function ShowPic(requestID, entityID, params) {
function ShowPic(requestID, agentID, params) {
for (var i = 0; i < params.front.length; ++i) {
var kb = params.front[i];
loading.innerHTML = 'refreshing...';
Expand All @@ -75,7 +75,7 @@ function ShowPic(requestID, entityID, params) {
sendSuccess(requestID);
}

function GetCapitals(requestID, entityID, params) {
function GetCapitals(requestID, agentID, params) {
function matchCapital(name) {
var matching = '';
countries.some(function(i) {
Expand Down Expand Up @@ -109,7 +109,7 @@ function GetCapitals(requestID, entityID, params) {
sendSuccess(requestID, '{"capitals":' + JSON.stringify(cityList) + '}');
}

function GetFirstElement(requestID, entityID, params) {
function GetFirstElement(requestID, agentID, params) {
if (params.array === null) {
sendFailure(requestID);
}
Expand All @@ -122,7 +122,7 @@ function GetFirstElement(requestID, entityID, params) {
}
}

function GetCityWeather(requestID, entityID, params) {
function GetCityWeather(requestID, agentID, params) {
function getWeather(city) {
$.getJSON('http://api.openweathermap.org/data/2.5/weather?q=' + city, function(data) {
if (data.weather !== undefined) {
Expand All @@ -145,7 +145,7 @@ function GetCityWeather(requestID, entityID, params) {
getWeather(params.cityName);
}

function DisplayCityWeather(requestID, entityID, params) {
function DisplayCityWeather(requestID, agentID, params) {
function displayOnMap(city, weather) {
geocoder.geocode({'address': city}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
Expand All @@ -172,13 +172,13 @@ function DisplayCityWeather(requestID, entityID, params) {
sendSuccess(requestID);
}

function DisplayCityThumbnail(requestID, entityID, params) {
function DisplayCityThumbnail(requestID, agentID, params) {
capitalsID.innerHTML += '<div class="col-md-6"><div class="thumbnail capitalThumb" style="height: 160px; overflow: hidden;"><a href="{1}" target="_blank"><img src="{2}" style="max-height: 100px;"></a><div class="caption" style="text-align:center;"><strong>{0}</strong></div></div></div>'.format(params.city.name, params.city.url, params.city.pic);
$('#capitals').animate({scrollTop: document.getElementById('capitalsID').offsetHeight}, 1000);
sendSuccess(requestID);
}

function EndOfSub(requestID, entityID, params) {
function EndOfSub(requestID, agentID, params) {
$('#next').prop('disabled', true);
$('#nextFooter').prop('disabled', true);
messages.innerHTML += '<div class="col-md-3 col-sm-6"><div class="thumbnail endOfSub" style="height: 200px;"><img src="http://ipadwatcher.com/wp-content/uploads/2010/05/dead-end.jpg" style="height: 120px;"><div class="caption" style="text-align:center;"><h4>{0}</h4></div></div></div>'.format(params.message);
Expand All @@ -187,12 +187,12 @@ function EndOfSub(requestID, entityID, params) {
sendSuccess(requestID);
}

function Debug(requestID, entityID, params) {
console.log('message from agent', entityID + ':', params.message);
function Debug(requestID, agentID, params) {
console.log('message from agent', agentID + ':', params.message);
sendSuccess(requestID);
}

function Prompt(requestID, entityID, params) {
function Prompt(requestID, agentID, params) {
var succeeds = function() {
$('#prompt').off('hidden.bs.modal', succeeds);
sendSuccess(requestID, '{"answer":' + JSON.stringify($('#promptinput').val()) + '}');
Expand Down
61 changes: 39 additions & 22 deletions app/static/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ String.prototype.format = function() {
return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match; });
};

var simID;
var entityID;
var instanceID;
var agentID;
console.log(window.location.href);
var httpURL;
var wsURL;

function createSimulation(user, project, version, onloadCB) {
function createInstance(user, project, version, onloadCB) {
httpURL = 'https://' + RUNTIME_URL + '/api/v1/' + user + '/' + project + '/' + version;
wsURL = 'wss://' + RUNTIME_URL + '/api/v1/' + user + '/' + project + '/' + version;
var oReq = new XMLHttpRequest();
Expand All @@ -19,20 +19,20 @@ function createSimulation(user, project, version, onloadCB) {
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
oReq.setRequestHeader('X-Craft-Ai-App-Secret', TUTO_APP_SECRET);
oReq.onload = function() {
simID = JSON.parse(oReq.responseText).instance.instance_id;
onloadCB(simID);
instanceID = JSON.parse(oReq.responseText).instance.instance_id;
onloadCB(instanceID);
};
oReq.onerror = function() {
alert('error while creating simulation');
alert('error while creating instance');
};
oReq.send();
}

function destroySimulation(onloadCB) {
function destroyInstance(onloadCB) {
console.log('delete');
//clearInterval( idUpdate );
var oReq = new XMLHttpRequest();
oReq.open('DELETE', httpURL + '/' + simID, true);
oReq.open('DELETE', httpURL + '/' + instanceID, true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -41,9 +41,9 @@ function destroySimulation(onloadCB) {
oReq.onload = onloadCB;
}

function createEntity(behavior, knowledge, onloadCB, onErrorCB) {
function createAgent(behavior, knowledge, onloadCB, onErrorCB) {
var oReq = new XMLHttpRequest();
oReq.open('PUT', httpURL + '/' + simID + '/entities', true);
oReq.open('PUT', httpURL + '/' + instanceID + '/agents', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -62,7 +62,7 @@ function createEntity(behavior, knowledge, onloadCB, onErrorCB) {

function update() {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + simID + '/update', true);
oReq.open('POST', httpURL + '/' + instanceID + '/update', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -75,13 +75,13 @@ function update() {
oReq.send('{"time":0.5,"ts":' + new Date().getTime() + '}');
}

function cancel(requestID, entityID, params) {
function cancel(requestID, agentID, params) {
sendCancel(requestID);
}

function getEntityKnowledge(entityID, onloadCB) {
function getAgentKnowledge(agentID, onloadCB) {
var oReq = new XMLHttpRequest();
oReq.open('GET', httpURL + '/' + simID + '/entities/' + entityID + '/knowledge', true);
oReq.open('GET', httpURL + '/' + instanceID + '/agents/' + agentID + '/knowledge', true);
oReq.setRequestHeader('Content-type', 'application/json');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
oReq.setRequestHeader('X-Craft-Ai-App-Secret', TUTO_APP_SECRET);
Expand All @@ -96,9 +96,26 @@ function getEntityKnowledge(entityID, onloadCB) {

}

function updateEntityKnowledge(entityID, destination, value, onloadCB) {
function updateAgentKnowledge(agentID, destination, value, onloadCB) {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + simID + '/entities/' + entityID + '/knowledge', true);
oReq.open('POST', httpURL + '/' + instanceID + '/agents/' + agentID + '/knowledge', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
oReq.setRequestHeader('X-Craft-Ai-App-Secret', TUTO_APP_SECRET);

oReq.onload = function() {
onloadCB();
};
var j = {};
j[destination] = value;
oReq.send(JSON.stringify(j));

}

function updateInstanceKnowledge(destination, value, onloadCB) {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + instanceID + '/instanceKnowledge', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand Down Expand Up @@ -132,7 +149,7 @@ function updateGlobalKnowledge(destination, value, onloadCB) {

function sendSuccess(requestID, jsonString) {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + simID + '/actions/' + requestID + '/success', true);
oReq.open('POST', httpURL + '/' + instanceID + '/actions/' + requestID + '/success', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -143,7 +160,7 @@ function sendSuccess(requestID, jsonString) {

function sendFailure(requestID, jsonString) {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + simID + '/actions/' + requestID + '/failure', true);
oReq.open('POST', httpURL + '/' + instanceID + '/actions/' + requestID + '/failure', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -153,7 +170,7 @@ function sendFailure(requestID, jsonString) {

function sendCancel(requestID) {
var oReq = new XMLHttpRequest();
oReq.open('POST', httpURL + '/' + simID + '/actions/' + requestID + '/cancelation', true);
oReq.open('POST', httpURL + '/' + instanceID + '/actions/' + requestID + '/cancelation', true);
oReq.setRequestHeader('content-type', 'application/html; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -163,7 +180,7 @@ function sendCancel(requestID) {

function registerAction(jsonString, onloadCB) {
var oReq = new XMLHttpRequest();
oReq.open('PUT', httpURL + '/' + simID + '/actions', true);
oReq.open('PUT', httpURL + '/' + instanceID + '/actions', true);
oReq.setRequestHeader('content-type', 'application/json; charset=utf-8');
oReq.setRequestHeader('accept', '');
oReq.setRequestHeader('X-Craft-Ai-App-Id', TUTO_APP_ID);
Expand All @@ -183,7 +200,7 @@ function doUpdate() {
}

function doWS() {
var wsUrlRoute = wsURL + '/' + simID + '/websockets';
var wsUrlRoute = wsURL + '/' + instanceID + '/websockets';
console.log('WS Connexion on', wsUrlRoute + ':');
if (wsUrlRoute) {
console.log('requesting WS connexion...');
Expand All @@ -192,7 +209,7 @@ function doWS() {
if (evt.data != 'ping') {
var jsonEvt = JSON.parse(evt.data);
console.log('WS data:', evt.data);
window[jsonEvt.call](jsonEvt.requestId , jsonEvt.entityId, jsonEvt.input);
window[jsonEvt.call](jsonEvt.requestId , jsonEvt.agentID, jsonEvt.input);
}
else {
console.log('ping');
Expand Down
Loading

0 comments on commit 3607820

Please sign in to comment.