add asynch function

This commit is contained in:
FilipKudlacz 2019-06-03 13:19:28 +02:00
parent 5369e2c57c
commit 6be0b47f9e
5 changed files with 87 additions and 38 deletions

View File

@ -2,19 +2,24 @@ class Flower {
constructor(type) {
this.type = type;
this.maturity = 0;
this.maxMaturity;
this.maxMaturity = 0;
this.grown = false;
this.ordered = false;
}
grow() {
if(!this.isReady()) {
this.maturity++;
if(this.grown == false && this.maturity == this.maxMaturity) {
this.grown == true;
if(this.grown == false && this.maturity >= this.maxMaturity) {
this.grown = true;
}
}
}
isOrdered() {
return this.ordered;
}
isReady() {
return this.grown;
}

View File

@ -11,6 +11,7 @@ class Forklift {
this.currentSection = 0;
this.cargo = null;
this.queue = new PriorityQueue();
this.busy = false;
}
draw() {
@ -32,7 +33,7 @@ class Forklift {
} else {
// Final destination reached
magazineState[this.currentTarget].push(this.cargo);
this.removeCargo();
//this.removeCargo();
this.end = true;
this.targetId = 0;
@ -62,6 +63,65 @@ class Forklift {
}
}
async findPath(targetSection) {
let data = {
graph: magazineToGraph(),
start_node: forklift.currentSection,
dest_node: int(targetSection),
};
httpPost(
serverUrl + '/shortestPath',
data,
response => {
let path = response.split('').map(Number);
this.currentTarget = path[0];
this.setPath(path);
going = true;
},
error => {
console.log(error);
},
);
}
completeOrder(order) {
this.waitUntilJobIsDone()
.then(() => this.findPath(order.from))
.then(() => this.waitUntilJobIsDone())
.then(() => {
this.setCargo(order.what)
magazineState[order.from] = magazineState[order.from].filter((flower) => {
return flower != order.what;
})
this.findPath(order.to);
})
.then(() => this.waitUntilJobIsDone())
.then(() => {
magazineState[order.to].push(order.what);
order.what.setMaxMaturity(5 + 2*order.to);
console.log(order.message);
this.busy = false;
});
}
waitUntilJobIsDone() {
return new Promise(resolve => {
let interval = setInterval(() => {
if (!going) {
clearInterval(interval);
resolve();
}
}, 50);
});
}
completeOrders() {
while(!this.queue.isEmpty()) {
let order = queue.dequeue();
this.completeOrder(order);
}
}
setCargo(cargo) {
this.cargo = cargo;
}

View File

@ -5,7 +5,7 @@ class PriorityQueue {
}
enqueue(element) {
if(this.collection.isEmpty()) {
if(this.isEmpty()) {
this.collection.push(element);
}else {
let added = false;

View File

@ -4,7 +4,7 @@ let roads;
let packageClaim;
let going = false;
let forklift;
let magazineState = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
let magazineState = {0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
// This runs once at start
function setup() {
@ -14,7 +14,7 @@ function setup() {
createMagazineLayout();
select('#button').mousePressed(getIrisType);
select('#button').mousePressed(addOrder());
sepalWidth = select('#sepalWidth');
sepalLength = select('#sepalLength');
petalWidth = select('#petalWidth');
@ -23,6 +23,8 @@ function setup() {
// Create a forklift instance
forklift = new Forklift(sections[0].x, sections[0].y);
setInterval(ageFlowers, 5000)
select('#completeOrdersButton').mousePressed(forklift.completeOrders());
}
// This runs every frame
@ -68,7 +70,7 @@ function drawMagazine() {
}
}
function getIrisType() {
function addOrder() {
let sw = select('#sepalWidth').value();
let sl = select('#sepalLength').value();
let pw = select('#petalWidth').value();
@ -79,34 +81,13 @@ function getIrisType() {
petalWidth: pw,
petalLength: pl,
};
console.log(data)
httpPost(serverUrl + '/classify', data, response => {
forklift.setCargo(new Flower(response))
deliver(response);
magazineState[0].push(new Flower(response));
forklift.completeOrder(new Order(0, magazineState[0][0], magazineState[0][0].type, 'cycki'));
});
}
function deliver(targetSection) {
let data = {
graph: magazineToGraph(),
start_node: forklift.currentSection,
dest_node: int(targetSection),
};
console.log(data);
httpPost(
serverUrl + '/shortestPath',
data,
response => {
path = response.split('').map(Number);
forklift.currentTarget = path[0];
forklift.setPath(path);
going = true;
},
error => {
console.log(error);
},
);
}
function createMagazineLayout() {
unit = width / 6;
@ -156,7 +137,7 @@ function ageFlowers() {
for (let key of Object.keys(magazineState)) {
for (let flower of magazineState[key]) {
flower.grow();
if(flower.isReady()) {
if(flower.isReady() && !flower.isOrdered()) {
let order;
let priority;
if(int(key) == 0) {
@ -170,6 +151,7 @@ function ageFlowers() {
priority = 2;
}
forklift.queue.enqueue([order, priority]);
flower.ordered = true;
}
}
}

View File

@ -7,6 +7,7 @@
<script src="{% static 'magazine/flower.js' %}"></script>
<script src="{% static 'magazine/sketch.js' %}"></script>
<script src="{% static 'magazine/priorityQueue.js' %}"></script>
<script src="{% static 'magazine/order.js' %}"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
@ -44,14 +45,15 @@
<div class="package">
<h3 style="margin-top: 0;">Package description</h1>
<label >Sepal Width</label>
<input type="number" id="sepalWidth">
<input type="number" id="sepalWidth" value="1">
<label >Sepal Length</label>
<input type="number" id="sepalLength">
<input type="number" id="sepalLength" value="1">
<label >Petal Width</label>
<input type="number" id="petalWidth">
<input type="number" id="petalWidth" value="1">
<label >Petal Length</label>
<input type="number" id="petalLength">
<input type="number" id="petalLength" value="1">
<button id="button">Send Package</button>
<button id="completeOrdersButton">Complete Queued Orders</button>
</div>
<div id="canvas" style="margin: 10px;"></div>
<div class="legend">