-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.js
333 lines (297 loc) · 14.4 KB
/
order.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Order a drink...
//
var lodash = require("lodash");
var DrinkQueue = require('./lib/drink-queue');
var Pourer = require('./lib/pour');
var liquor = "Vodka"; // Default - wil be updated from app config below
var recipes = // recipe: [liquor, cran, sour, orange]
{
"Vodka":
[
{ name: "Cape Cod", recipe: [2, 6, 0, 0]},
{ name: "Cran Codder", recipe: [2, 5, 1, 0]},
{ name: "Cranberry Toad", recipe: [2, 5, 0, 1]},
{ name: "Key West Screwdriver", recipe: [2, 0, 1, 5]},
{ name: "Madras", recipe: [2, 3, 0, 3]},
{ name: "Party Punch", recipe: [2, 2, 2, 2]},
{ name: "Pink Lemonade", recipe: [2, 1, 5, 0]},
{ name: "Ruby Punch", recipe: [2, 4, 1, 1]},
{ name: "Russian Rita", recipe: [2, 0, 5, 1]},
{ name: "Screwdriver", recipe: [2, 0, 0, 6]},
{ name: "Sour Berry", recipe: [2, 3, 3, 0]},
{ name: "Sucker Punch", recipe: [2, 1, 4, 1]},
{ name: "Sunset Punch", recipe: [2, 1, 1, 4]},
{ name: "Sweet-tart", recipe: [2, 0, 3, 0]},
{ name: "Vodka Shot", recipe: [1, 0, 0, 0]},
{ name: "Vodka Sunburst", recipe: [2, 1, 0, 5]},
{ name: "Mix Your Own...", custom: true, recipe: [0, 0, 0, 0]}
],
"Tequila":
[
{ name: "Boat Punch", recipe: [2, 2, 2, 2]},
{ name: "Cactus Punch", recipe: [2, 4, 1, 1]},
{ name: "Cran Rita", recipe: [2, 1, 5, 0]},
{ name: "Cran-Aid", recipe: [2, 5, 0, 1]},
{ name: "Knockout Punch", recipe: [2, 1, 4, 1]},
{ name: "Margarita", recipe: [2, 0, 5, 1]},
{ name: "Orange Worm", recipe: [2, 0, 1, 5]},
{ name: "Prickly Madras", recipe: [2, 3, 0, 3]},
{ name: "South of the Border Screwdriver", recipe: [2, 0, 0, 6]},
{ name: "Souther Breeze", recipe: [2, 1, 1, 4]},
{ name: "Spicy Cran", recipe: [2, 3, 3, 0]},
{ name: "Tequila", recipe: [1, 0, 0, 0]},
{ name: "Tequila Bite", recipe: [2, 6, 0, 0]},
{ name: "Tequila Sour", recipe: [2, 0, 6, 0]},
{ name: "Tequila Sunburst", recipe: [2, 1, 0, 5]},
{ name: "Tropical Rita", recipe: [2, 0, 3, 3]},
{ name: "Wild Thing", recipe: [2, 5, 1, 0]},
{ name: "Mix Your Own...", custom: true, recipe: [0, 0, 0, 0]}
],
"Rum":
[
{ name: "Bermuda Triangle", recipe: [2, 5, 0, 1]},
{ name: "Caribbean Screwdriver", recipe: [2, 0, 0, 6]},
{ name: "Cranberry Kiss", recipe: [2, 5, 1, 0]},
{ name: "Hurricane", recipe: [2, 3, 0, 3]},
{ name: "Hurricane Punch", recipe: [2, 1, 1, 4]},
{ name: "Marina Punch", recipe: [2, 1, 4, 1]},
{ name: "My Pleasure", recipe: [2, 6, 0, 0]},
{ name: "Orange Plunge", recipe: [2, 0, 1, 5]},
{ name: "Picnic Punch", recipe: [2, 4, 1, 1]},
{ name: "Plankwalk", recipe: [2, 1, 5, 0]},
{ name: "Rum", recipe: [1, 0, 0, 0]},
{ name: "Rum Punch", recipe: [2, 2, 2, 2]},
{ name: "Rum Sour", recipe: [2, 0, 6, 0]},
{ name: "Rum Stone Sour", recipe: [2, 0, 5, 1]},
{ name: "Rum Sunburst", recipe: [2, 1, 0, 5]},
{ name: "Suntwist", recipe: [2, 0, 3, 3]},
{ name: "Tangy Cran", recipe: [2, 3, 3, 0]},
{ name: "Mix Your Own...", custom: true, recipe: [0, 0, 0, 0]}
]
};
exports.View =
{
title: "Drynk Order",
elements:
[
{ control: "stackpanel", width: "465", contents: [
{ control: "text", value: "{title}", font: { size: 14, bold: true }, horizontalAlignment: "Center" },
{ control: "text", value: "Order expires in {secondsToStartPour} seconds", horizontalAlignment: "Center", visibility: "eval({state} == 'pending')" },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Your Name", fontsize: 10, verticalAlignment: "Center" },
{ control: "edit", style: "col2Style", placeholder: "enter your name", binding: "username", verticalAlignment: "Center" },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Your Drink", fontsize: 10, verticalAlignment: "Center" },
{ control: "picker", style: "col2Style", margin: { bottom: 10 }, binding: { items: "drinkList", itemContent: "{name}", selection: "selectedDrink", selectionItem: "$data" } },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "{liquor}", fontsize: 10, verticalAlignment: "Center" },
{ control: "slider", style: "col2Style", minimum: 0, maximum: 2, binding: "customRecipe[0]", verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
{ control: "progressbar", style: "col2Style", minimum: 0, maximum: 2, value: "{selectedDrink.recipe[0]}", verticalAlignment: "Center", visibility: "{!selectedDrink.custom}" },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Cranberry", fontsize: 10, verticalAlignment: "Center" },
{ control: "slider", style: "col2Style", minimum: 0, maximum: 6, binding: "customRecipe[1]", verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
{ control: "progressbar", style: "col2Style", minimum: 0, maximum: 6, value: "{selectedDrink.recipe[1]}", verticalAlignment: "Center", visibility: "{!selectedDrink.custom}" },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Sour Mix", fontsize: 10, verticalAlignment: "Center" },
{ control: "slider", style: "col2Style", minimum: 0, maximum: 6, binding: "customRecipe[2]", verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
{ control: "progressbar", style: "col2Style", minimum: 0, maximum: 6, value: "{selectedDrink.recipe[2]}", verticalAlignment: "Center", visibility: "{!selectedDrink.custom}" },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Orange", fontsize: 10, verticalAlignment: "Center"},
{ control: "slider", style: "col2Style", minimum: 0, maximum: 6, binding: "customRecipe[3]", verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
{ control: "progressbar", style: "col2Style", minimum: 0, maximum: 6, value: "{selectedDrink.recipe[3]}", verticalAlignment: "Center", visibility: "{!selectedDrink.custom}" },
] },
{ control: "stackpanel", orientation: "Horizontal", contents: [
{ control: "text", style: "col1Style", value: "Nickname", fontsize: 10, verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
{ control: "edit", style: "col2Style", binding: "customDrinkName", verticalAlignment: "Center", visibility: "{selectedDrink.custom}" },
] },
{ control: "stackpanel", orientation: "Vertical", width: "*", contents: [
{ control: "stackpanel", orientation: "Horizontal", horizontalAlignment: "Center", contents: [
{ control: "button", caption: "{buttonCap}", icon: "local_drink", binding: "order", visibility: "eval({state} == 'ordering')" },
{ control: "button", caption: "Start Pouring", icon: "play_arrow", binding: "startPouring", visibility: "eval({state} == 'pending')" },
{ control: "button", caption: "Cancel Order", icon: "clear", binding: "cancelDrink", visibility: "eval({orderInQueue} && ({state} != 'pouring'))" }, // Also need cancel order in "ordering" if drink in queue
{ control: "button", caption: "Stop Pouring", icon: "stop", binding: "stopPouring", visibility: "eval({state} == 'pouring')" },
] },
] },
] }
]
}
function getDrinkOrder(viewModel)
{
var drinkOrder = { name: viewModel.username || "Unknown" };
drinkOrder.drink = viewModel.selectedDrink.custom ? { name: viewModel.customDrinkName, custom: true, recipe: viewModel.customRecipe } : viewModel.selectedDrink;
return drinkOrder;
}
function moveToPendingState(viewModel)
{
viewModel.state = "pending";
viewModel.title = "You're Up - Pour Away!";
}
function endPourWithMessage(context, viewModel, title, message)
{
viewModel.state = "complete";
viewModel.title = title;
var messageBox =
{
title: "Drynkro",
message: message,
options: [ { label: "Ok", command: "endPour" } ]
}
return Synchro.showMessage(context, messageBox);
}
exports.InitializeViewModel = function (context, session, params)
{
liquor = Synchro.getConfig(context, "LIQUOR") || liquor;
var viewModel =
{
col1Style: { width: 150 },
col2Style: { width: 295, margin: {right: 0} },
liquor: liquor,
drinkList: recipes[liquor],
selectedDrink: recipes[liquor][0],
customDrinkName: "Custom Mix",
customRecipe: [2, 2, 2, 2],
username: "",
title: "Select Your Drink",
buttonCap: "Place Order...",
orderInQueue: false,
state: "ordering", // "pending", "pouring", "complete"
secondsToStartPour: null
}
var drinkQueue = DrinkQueue.getDrinkQueue();
var sessionId = Synchro.getSessionId(context);
var drinkOrder = drinkQueue.getDrinkOrder(sessionId)
if (drinkOrder)
{
viewModel.orderInQueue = true;
if (drinkOrder.drink.custom)
{
viewModel.selectedDrink = lodash.find(recipes[liquor], { name: "Mix Your Own..." });
viewModel.customDrinkName = drinkOrder.drink.name;
viewModel.customRecipe = drinkOrder.drink.recipe;
}
else
{
viewModel.selectedDrink = lodash.find(recipes[liquor], { name: drinkOrder.drink.name });
}
viewModel.username = drinkOrder.name;
viewModel.buttonCap = "Update Order..."
if (drinkQueue.getPosition(sessionId) == 1)
{
// We're up now, go to pouring states
moveToPendingState(viewModel);
}
}
return viewModel;
}
// This isn't really a traditional post-init async loader. We're using this as our async status checking
// loop mechanism (LoadViewModel will run until we get to state "complete", meaning we're exiting).
//
exports.LoadViewModel = function * (context, session, viewModel)
{
var drinkQueue = DrinkQueue.getDrinkQueue();
var sessionId = Synchro.getSessionId(context);
while (Synchro.isActiveInstance(context) && (viewModel.state != "complete"))
{
yield Synchro.interimUpdateAwaitable(context);
yield Synchro.yieldAwaitable(context, function(callback){ setTimeout(callback, 1000) });
var pos = drinkQueue.getPosition(sessionId);
if (viewModel.state == "ordering")
{
if (pos == 1)
{
// We are now the top drink. Move to "pending". This happens when we're on the "order" page looking
// at or modifying details of a previously ordered drink, and while doing that, that drink becomes active.
//
viewModel.secondsToStartPour = Math.round(drinkQueue.getActiveOrderTimeRemaining());
moveToPendingState(viewModel);
}
}
else if (viewModel.state == "pending")
{
if (pos == 0)
{
// Drink expired before pouring began
//
endPourWithMessage(context, viewModel, "Drynk Expired!", "Your drink order has expired");
}
else
{
// Update time remaining for display
viewModel.secondsToStartPour = Math.round(drinkQueue.getActiveOrderTimeRemaining());
}
}
}
}
exports.Commands =
{
order: function(context, session, viewModel, params)
{
var drinkQueue = DrinkQueue.getDrinkQueue();
var sessionId = Synchro.getSessionId(context);
var drinkOrder = getDrinkOrder(viewModel);
drinkQueue.setDrinkOrder(sessionId, drinkOrder);
viewModel.orderInQueue = true;
if (drinkQueue.getPosition(sessionId) == 1)
{
// We're up now, go to pouring states
moveToPendingState(viewModel);
}
else
{
// Back to main to wait for our turn...
Synchro.popTo(context, "main");
}
},
startPouring: function * (context, session, viewModel, params)
{
var drinkQueue = DrinkQueue.getDrinkQueue();
var sessionId = Synchro.getSessionId(context);
var drinkOrder = getDrinkOrder(viewModel);
if (drinkQueue.getPosition(sessionId) == 0)
{
endPourWithMessage(context, viewModel, "Drynk Expired!", "Your drink order has expired");
return;
}
// Prevent drink queue timing this drink out while pouring...
drinkOrder.state = "pouring";
drinkQueue.setDrinkOrder(sessionId, drinkOrder);
viewModel.state = "pouring";
viewModel.title = "Your Drynk is Pouring!";
yield Synchro.interimUpdateAwaitable(context);
var pourer = Pourer.getPourer();
var poured = yield Synchro.yieldAwaitable(context, function(callback)
{
pourer.pourDrink(drinkOrder.drink.recipe, callback)
});
drinkQueue.removeDrinkOrder(sessionId);
if (poured)
{
endPourWithMessage(context, viewModel, "Pour Complete!", "Your drink has been poured");
}
else
{
endPourWithMessage(context, viewModel, "Pour Cancelled!", "Your drink pour was cancelled mid-stream!");
}
},
cancelDrink: function(context, session, viewModel, params)
{
var drinkQueue = DrinkQueue.getDrinkQueue();
var sessionId = Synchro.getSessionId(context);
drinkQueue.removeDrinkOrder(sessionId);
endPourWithMessage(context, viewModel, "Drink Order Cancelled!", "Drink order was cancelled");
},
stopPouring: function(context, session, viewModel, params)
{
var pourer = Pourer.getPourer();
pourer.stopPouring();
},
endPour: function(context, session, viewModel, params)
{
Synchro.popTo(context, "main");
},
}