From 6be0b47f9eb1854a4e30e575de474e5a3878e7b4 Mon Sep 17 00:00:00 2001 From: FilipKudlacz Date: Mon, 3 Jun 2019 13:19:28 +0200 Subject: [PATCH] add asynch function --- magazine/static/magazine/flower.js | 11 ++-- magazine/static/magazine/forklift.js | 62 ++++++++++++++++++++++- magazine/static/magazine/priorityQueue.js | 2 +- magazine/static/magazine/sketch.js | 40 ++++----------- magazine/templates/magazine/index.html | 10 ++-- 5 files changed, 87 insertions(+), 38 deletions(-) diff --git a/magazine/static/magazine/flower.js b/magazine/static/magazine/flower.js index 11df5b8..e3e6215 100644 --- a/magazine/static/magazine/flower.js +++ b/magazine/static/magazine/flower.js @@ -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; } diff --git a/magazine/static/magazine/forklift.js b/magazine/static/magazine/forklift.js index 02535f6..ffcb194 100644 --- a/magazine/static/magazine/forklift.js +++ b/magazine/static/magazine/forklift.js @@ -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; } diff --git a/magazine/static/magazine/priorityQueue.js b/magazine/static/magazine/priorityQueue.js index f3dca9f..58cff2d 100644 --- a/magazine/static/magazine/priorityQueue.js +++ b/magazine/static/magazine/priorityQueue.js @@ -5,7 +5,7 @@ class PriorityQueue { } enqueue(element) { - if(this.collection.isEmpty()) { + if(this.isEmpty()) { this.collection.push(element); }else { let added = false; diff --git a/magazine/static/magazine/sketch.js b/magazine/static/magazine/sketch.js index a10036c..e614739 100644 --- a/magazine/static/magazine/sketch.js +++ b/magazine/static/magazine/sketch.js @@ -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,15 +14,17 @@ function setup() { createMagazineLayout(); - select('#button').mousePressed(getIrisType); + select('#button').mousePressed(addOrder()); sepalWidth = select('#sepalWidth'); sepalLength = select('#sepalLength'); petalWidth = select('#petalWidth'); petalLength = select('#petalLength'); - + // 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; } } } diff --git a/magazine/templates/magazine/index.html b/magazine/templates/magazine/index.html index 9f2e5c3..073bbca 100644 --- a/magazine/templates/magazine/index.html +++ b/magazine/templates/magazine/index.html @@ -7,6 +7,7 @@ +