From 1ea58e3afaeb50ed3ec42d32e1c7aed29834790b Mon Sep 17 00:00:00 2001 From: Joeri Samson Date: Fri, 4 Aug 2017 11:45:25 +0200 Subject: [PATCH 1/3] First close pizza order if it's already open Note that this should probably only happen if the pizza order is too old, but dates are recorded on closing not on opening, so that requires some other changes --- scripts/pizza.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/pizza.coffee b/scripts/pizza.coffee index 6ec1da7..e0e4808 100644 --- a/scripts/pizza.coffee +++ b/scripts/pizza.coffee @@ -39,6 +39,8 @@ module.exports = (robot) -> ).join() start: -> + if this.isStarted() + this.closeOrder() robot.brain.data.currentPizzaOrder.status = 'open' isStarted: -> From 6718bd7146f251d244296783c9d419c2e045c461 Mon Sep 17 00:00:00 2001 From: Joeri Samson Date: Fri, 4 Aug 2017 11:47:28 +0200 Subject: [PATCH 2/3] Refactor a bit --- scripts/pizza.coffee | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/scripts/pizza.coffee b/scripts/pizza.coffee index e0e4808..d20b0bb 100644 --- a/scripts/pizza.coffee +++ b/scripts/pizza.coffee @@ -24,16 +24,15 @@ module.exports = (robot) -> - robot.brain.data.currentPizzaOrder = - pizzas: {}, - date: null, - status: null robot.brain.data.pizzaOrderHistory = robot.brain.data.pizzaOrderHistory or [] robot.brain.data.pizzaOrderHistoryBackup = robot.brain.data.pizzaOrderHistoryBackup or [] pizzas = + currentOrder: -> + robot.brain.data.currentPizzaOrder + current: -> - obj = robot.brain.data.currentPizzaOrder.pizzas + obj = this.currentOrder().pizzas Object.keys(obj).map((key) -> obj[key] ).join() @@ -41,37 +40,36 @@ module.exports = (robot) -> start: -> if this.isStarted() this.closeOrder() - robot.brain.data.currentPizzaOrder.status = 'open' + this.currentOrder().status = 'open' isStarted: -> - robot.brain.data.currentPizzaOrder.status == 'open' + this.currentOrder().status == 'open' currentQty: -> - Object.keys(robot.brain.data.currentPizzaOrder.pizzas).length + Object.keys(this.currentOrder().pizzas).length currentEaters: -> - Object.keys(robot.brain.data.currentPizzaOrder.pizzas).join() + Object.keys(this.currentOrder().pizzas).join() add: (user, name) -> # only 1 pizza per user atm - robot.brain.data.currentPizzaOrder.pizzas[user] = name + this.currentOrder().pizzas[user] = name remove: (user) -> - delete robot.brain.data.currentPizzaOrder.pizzas[user] + delete this.currentOrder().pizzas[user] true closeOrder: -> if pizzas.currentQty() > 0 - robot.brain.data.currentPizzaOrder.date = new Date() - robot.brain.data.currentPizzaOrder.status = 'closed' - order = robot.brain.data.currentPizzaOrder + order = this.currentOrder() + order.date = new Date() + order.status = 'closed' robot.brain.data.pizzaOrderHistory.push order - pizzas.clearOrder() + pizzas.newOrder() order else - - clearOrder: -> + newOrder: -> robot.brain.data.currentPizzaOrder = {pizzas: {}, date: null, status: null} clearHistory: -> @@ -87,6 +85,8 @@ module.exports = (robot) -> pizza_qty += Object.keys(order.pizzas).length "#{pizza_qty} pizzas ordered in #{order_qty} orders" + pizzas.newOrder() + ## HELP ## robot.respond /pizza help/i, (msg) -> msg.send "Order a pizza: '@hubby pizza me your-pizza-choice'" From 796c1782c72dbaf4fd07bddb123348756125db92 Mon Sep 17 00:00:00 2001 From: Joeri Samson Date: Fri, 4 Aug 2017 11:57:57 +0200 Subject: [PATCH 3/3] Try to not close pizza orders too soon --- scripts/pizza.coffee | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/pizza.coffee b/scripts/pizza.coffee index d20b0bb..6cc0be0 100644 --- a/scripts/pizza.coffee +++ b/scripts/pizza.coffee @@ -38,9 +38,12 @@ module.exports = (robot) -> ).join() start: -> + order = this.currentOrder() if this.isStarted() - this.closeOrder() - this.currentOrder().status = 'open' + unless order.startDate && order.startDate >= new Date(new Date() - 24 * 60 * 60 * 1000) + this.closeOrder() + order.status = 'open' + order.startDate = order.startDate || new Date() isStarted: -> this.currentOrder().status == 'open'