Skip to content

Commit

Permalink
📖 Release!
Browse files Browse the repository at this point in the history
- Updated todo.md
- Simplified/Condensed Code
- Added quantity in add to cart message.
- Flask output is now hidden by default
- Coupon & Cart Handling Improved
- Format fix for JS
  • Loading branch information
KingPr0o7 committed Feb 27, 2023
1 parent 140b049 commit f984007
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 100 deletions.
Binary file modified __pycache__/functions.cpython-310.pyc
Binary file not shown.
35 changes: 18 additions & 17 deletions functions.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
item_types = []
current_sandwich = ''

# Checks JS outputs from server.py and returns outputs for JS.
class Output_Check():
def __init__(self, javascript_output, javascript_output_type):
global cart
global current_sandwich

self.chicken_typecases = ['c', 'chicken']
self.beef_typecases = ['b', 'beef']
self.tofu_typecases = ['t', 'tofu']
self.small_typecases = ['s', 'small']
self.medium_typecases = ['m', 'medium']
current_sandwich = ''
self.large_typecases = ['l', 'large']
if javascript_output_type == 'Sandwich Selection' or javascript_output_type == 'Drink Size Selection':
self.js_output = str(javascript_output).lower().strip()
Expand Down Expand Up @@ -43,7 +42,7 @@ def check(self):
return self.python_output, self.python_output_type
elif self.js_output_type == 'Sandwich Amount':
self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility = cart.add('Regular', 'Sandwich', current_sandwich, self.js_output)
self.python_output = quantity_fixer(self.item_quantity, current_sandwich.lower(), str(self.cart_types[0]).lower())
self.python_output = quantity_fixer(self.item_quantity, current_sandwich.lower(), 'regular', str(self.cart_types[0]).lower())
self.python_output_type = 'Sandwich Cart Addition'
return self.python_output, self.python_output_type, self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility
elif self.js_output_type == 'Sandwich Cart Addition':
Expand All @@ -67,7 +66,7 @@ def check(self):
return self.python_output, self.python_output_type
elif self.js_output_type == 'Drink Amount':
self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility = cart.add(current_drink_size, 'Drink', 'Fountain', self.js_output)
self.python_output = quantity_fixer(self.item_quantity, 'fountain', 'drink')
self.python_output = quantity_fixer(self.item_quantity, 'fountain', str(current_drink_size).lower(), 'drink')
self.python_output_type = 'Drink Cart Addition'
return self.python_output, self.python_output_type, self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility
elif self.js_output_type == 'Drink Cart Addition':
Expand Down Expand Up @@ -101,7 +100,7 @@ def check(self):
return self.python_output, self.python_output_type
elif self.js_output_type == 'Fries Amount':
self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility = cart.add(current_fries_size, 'Fries', 'French', self.js_output)
self.python_output = quantity_fixer(self.item_quantity, 'french', 'fries')
self.python_output = quantity_fixer(self.item_quantity, 'french', str(current_fries_size).lower(), 'fries')
self.python_output_type = 'Fries Cart Addition'
return self.python_output, self.python_output_type, self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility
elif self.js_output_type == 'Fries Cart Addition':
Expand All @@ -114,7 +113,7 @@ def check(self):
return self.python_output, self.python_output_type
elif self.js_output_type == 'Ketchup Amount':
self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility = cart.add('Regular', 'Packet', 'Ketchup', self.js_output)
self.python_output = quantity_fixer(self.item_quantity, 'ketchup', 'packet')
self.python_output = quantity_fixer(self.item_quantity, 'ketchup', 'regular', 'packet')
self.python_output_type = 'Ketchup Cart Addition'
return self.python_output, self.python_output_type, self.cart_sizes, self.cart_types, self.cart_names, self.cart_prices, self.cart_total, self.item_quantity, self.coupon_eligibility
elif self.js_output_type == 'Ketchup Cart Addition':
Expand All @@ -126,29 +125,31 @@ def check(self):
self.python_output_type = 'Done!'
return self.python_output, self.python_output_type

def quantity_fixer(quantity, item, type):
def quantity_fixer(quantity, item, size, type):
if int(quantity) <=1:
if type == 'sandwich':
return f'Okay! Added a {item} {type} to the cart!'
return f'Okay! Added a {size} {item} {type} to the cart!'
elif type == 'drink':
return f'Okay! Added a {item} {type} to the cart!'
return f'Okay! Added a {size} {item} {type} to the cart!'
elif type == 'fries':
return f'Okay! Added {item} {type} to the cart!'
return f'Okay! Added {size} {item} {type} to the cart!'
elif type == 'packet':
return f'Okay! Added a {item} {type} to the cart!'
return f'Okay! Added a {size} {item} {type} to the cart!'
else:
if type == 'sandwich':
return f'Okay! Added {quantity} {item} {type}es to the cart!'
return f'Okay! Added {quantity} {size} {item} {type}es to the cart!'
elif type == 'drink':
return f'Okay! Added {quantity} {item} {type}s to the cart!'
return f'Okay! Added {quantity} {size} {item} {type}s to the cart!'
elif type == 'fries':
return f'Okay! Added {quantity} {item} {type} to the cart!'
return f'Okay! Added {quantity} {size} {item} {type} to the cart!'
elif type == 'packet':
return f'Okay! Added {quantity} {item} {type}s to the cart!'
return f'Okay! Added {quantity} {size} {item} {type}s to the cart!'

def clear_item_types():
global item_types
item_types = []
global item_types
global current_sandwich
item_types = []
current_sandwich = ''

# Stores and sends variables for the cart.
class Cart():
Expand Down
55 changes: 6 additions & 49 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,57 +37,14 @@ def send_data():
output_checker = Output_Check(javascript_output, javascript_output_type)
print(javascript_output, javascript_output_type)
# Depending on type, have to change what's returned
if javascript_output_type == 'Sandwich Agreement':
statement_returns = ['Sandwich Agreement', 'Sandwich Selection', 'Sandwich Cart Addition', 'Drink Agreement', 'Drink Size Selection', 'Drink Cart Addition', 'Fries Agreement', 'Fries Size Selection', 'Fries Upgrade', 'Fries Cart Addition', 'Ketchup Agreement', 'Ketchup Cart Addition', 'Done!']
cart_returns = ['Sandwich Amount', 'Drink Amount', 'Fries Amount', 'Ketchup Amount']
if javascript_output_type in statement_returns:
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Sandwich Selection':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Sandwich Amount':
flask_output, flask_output_type, cart_sizes, cart_types, cart_names, cart_prices, cart_total, item_quantity, coupon_eligibility = output_checker.check() # Assignment of variables from functions.py
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type, 'flask_cart_sizes': cart_sizes, 'flask_cart_types': cart_types, 'flask_cart_names': cart_names, 'flask_cart_prices': cart_prices, 'flask_cart_total': cart_total, 'flask_item_quantity': item_quantity, 'flask_coupon_eligibility': coupon_eligibility})
elif javascript_output_type == 'Sandwich Cart Addition':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Drink Agreement':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Drink Size Selection':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Drink Amount':
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type in cart_returns:
flask_output, flask_output_type, cart_sizes, cart_types, cart_names, cart_prices, cart_total, item_quantity, coupon_eligibility = output_checker.check() # Assignment of variables from functions.py
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type, 'flask_cart_sizes': cart_sizes, 'flask_cart_types': cart_types, 'flask_cart_names': cart_names, 'flask_cart_prices': cart_prices, 'flask_cart_total': cart_total, 'flask_item_quantity': item_quantity, 'flask_coupon_eligibility': coupon_eligibility})
elif javascript_output_type == 'Drink Cart Addition':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Fries Agreement':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Fries Size Selection':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Fries Upgrade':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Fries Amount':
flask_output, flask_output_type, cart_sizes, cart_types, cart_names, cart_prices, cart_total, item_quantity, coupon_eligibility = output_checker.check() # Assignment of variables from functions.py
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type, 'flask_cart_sizes': cart_sizes, 'flask_cart_types': cart_types, 'flask_cart_names': cart_names, 'flask_cart_prices': cart_prices, 'flask_cart_total': cart_total, 'flask_item_quantity': item_quantity, 'flask_coupon_eligibility': coupon_eligibility})
elif javascript_output_type == 'Fries Cart Addition':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Ketchup Agreement':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Ketchup Amount':
flask_output, flask_output_type, cart_sizes, cart_types, cart_names, cart_prices, cart_total, item_quantity, coupon_eligibility = output_checker.check() # Assignment of variables from functions.py
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type, 'flask_cart_sizes': cart_sizes, 'flask_cart_types': cart_types, 'flask_cart_names': cart_names, 'flask_cart_prices': cart_prices, 'flask_cart_total': cart_total, 'flask_item_quantity': item_quantity, 'flask_coupon_eligibility': coupon_eligibility})
elif javascript_output_type == 'Ketchup Cart Addition':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
elif javascript_output_type == 'Done!':
flask_output, flask_output_type = output_checker.check()
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type})
return jsonify({'flask_output': flask_output, 'flask_output_type': flask_output_type, 'flask_cart_sizes': cart_sizes, 'flask_cart_types': cart_types, 'flask_cart_names': cart_names, 'flask_cart_prices': cart_prices, 'flask_cart_total': cart_total, 'flask_item_quantity': item_quantity, 'flask_coupon_eligibility': coupon_eligibility})

if __name__ == '__main__':
app.run(debug=True)
9 changes: 7 additions & 2 deletions static/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

@import url('https://fonts.googleapis.com/css2?family=Cousine&display=swap');

/* Toggle to color to see debug of FLASK */
.flask-output {
color: white;
display: none;
}

* {
Expand Down Expand Up @@ -255,12 +256,16 @@ html, body {

.cart-discount, .cart-total {
text-align: center;
color: white;
color: lime;
font-family: 'Cousine', monospace;
font-size: 1.5rem;
font-weight: bolder;
}

.cart-discount {
color: darkorange;
}

/* */
/* Form */
/* */
Expand Down
66 changes: 40 additions & 26 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const typing_speed = 25; // Milliseconds

// Immutables
const sandwich_typecases = ['chicken', 'beef', 'tofu', 'c', 'b', 't'];
const sandwich_prices = [5.25, 6.25, 5.75] // Chicken, Beef, and Tofu
const drink_prices = [1.00, 1.75, 2.25] // Small, Medium, and Large
const fries_prices = [1.00, 1.50, 2.00] // Small, Medium, and Large
const ketchup_price = 0.25
const sandwich_prices = [5.25, 6.25, 5.75]; // Chicken, Beef, and Tofu
const drink_prices = [1.00, 1.75, 2.25]; // Small, Medium, and Large
const fries_prices = [1.00, 1.50, 2.00]; // Small, Medium, and Large
const ketchup_price = 0.25;
const size_typecases = ['small', 'medium', 'large', 's', 'm', 'l'];
const agreement_typecases = ['y', 'yes', 'n', 'no'];
const restart_typecases = ['r', 'restart'];
var bot_typing_status = false;
var chat_count = 1;
var current_bot_message = ''
Expand Down Expand Up @@ -40,7 +41,7 @@ user_chat.remove();

var cart = document.querySelector('.shopping-cart');
const cart_item_wrapper = document.querySelector('.cart-item-container');
var total = 0
var total = 0;
cart_item_wrapper.remove();
var cart_discount = document.querySelector('.cart-discount');
cart.remove();
Expand Down Expand Up @@ -121,8 +122,8 @@ function sendVariables() {
}

function handshake() {
sendVariables()
fetchVariables()
sendVariables();
fetchVariables();
}

//
Expand All @@ -136,7 +137,6 @@ function send_bot_message(msg) {
let clone = bot_chat.cloneNode(true);
clone_msg = clone.querySelector('.bot-msg[active]');
clone_msg.textContent = '';
clone.id = ''
clone_msg.appendChild(cursor);
chat_wrapper.appendChild(clone);
type(".bot-msg[active]", msg, typing_speed, 0);
Expand All @@ -145,13 +145,16 @@ function send_bot_message(msg) {
// Typecase reminder
if (flask_output_type == 'Sandwich Agreement' || flask_output_type == 'Drink Agreement' || flask_output_type == 'Fries Agreement' || flask_output_type == 'Fries Upgrade' || flask_output_type == 'Ketchup Agreement') {
form_parameters.textContent = '(Y)es (N)o';
}
if (flask_output_type == 'Sandwich Selection') {
form_parameters.textContent = '(C)hicken, (B)eef, or (T)ofu'
} else if (flask_output_type == 'Sandwich Selection') {
form_parameters.textContent = '(C)hicken, (B)eef, or (T)ofu';
} else if (flask_output_type == 'Sandwich Amount' || flask_output_type == 'Drink Amount' || flask_output_type == 'Fries Amount' || flask_output_type == 'Ketchup Amount') {
form_parameters.textContent = '>=1 AND !==0'
form_parameters.textContent = '>=1 AND !==0';
} else if (flask_output_type == 'Drink Size Selection' || flask_output_type == 'Fries Size Selection') {
form_parameters.textContent = '(S)mall, (M)edium, or (L)arge'
form_parameters.textContent = '(S)mall, (M)edium, or (L)arge';
} else if (flask_output_type == 'Sandwich Cart Addition' || flask_output_type == 'Drink Cart Addition' || flask_output_type == 'Fries Cart Addition' || flask_output_type == 'Ketchup Cart Addition') {
form_parameters.textContent = '';
} else if (flask_output_type == 'Done!') {
form_parameters.textContent = '(R)estart';
}
}

Expand All @@ -160,10 +163,9 @@ function send_user_message(msg) {
chat_count++;
let clone = user_chat.cloneNode(true);
clone.children[0].innerHTML = msg;
clone.id = '';
chat_wrapper.appendChild(clone);
chat.scrollBy(0, 2000);
cursor.remove()
cursor.remove();
current_user_message = msg;
if (msg == 'n' || msg == 'no') {
if (flask_output_type == 'Sandwich Agreement') {
Expand All @@ -176,7 +178,7 @@ function send_user_message(msg) {
flask_output_type = 'Done!';
}
}
handshake()
handshake();
}

// Typing affect
Expand All @@ -200,7 +202,13 @@ function type(targetElement, textToType, speed, index) {
} else if (flask_output_type == 'Sandwich Cart Addition' || flask_output_type == 'Drink Cart Addition' || flask_output_type == 'Fries Cart Addition' || flask_output_type == 'Ketchup Cart Addition') {
show_cart();
} else if (flask_output_type == 'Done!') {
document.querySelector('.bot-msg-wrapper[active]').append(cart);
let cart_cloned = cart.cloneNode(true);
if (cart_cloned.querySelector('#cart-items-wrapper').innerHTML.trim() === "") {
cart_cloned.querySelector('.cart-discount').remove();
cart_cloned.querySelector('.cart-total').textContent = 'Total: $0.00';
}
document.querySelector('.bot-msg-wrapper[active]').append(cart_cloned);
document.querySelector('.bot-chat[active]').style.alignItems = 'flex-start';
}
element.removeAttribute('active');
document.querySelector('.bot-msg-wrapper[active]').removeAttribute('active');
Expand All @@ -215,19 +223,18 @@ function type(targetElement, textToType, speed, index) {
// Adds items to the options list on a bot chat, letting the user know what can be ordered
function add_item_option(cloneTo, item_id, item_title, item_size, item_type, item_name) {
let new_item = food_options.cloneNode(true);
new_item.id = '';
new_item.querySelector('.item-option-title').textContent = item_title;
if (item_type == 'Sandwich') {
new_item.querySelector('.item-price').textContent = `$${sandwich_prices[item_id]}`;
} else if (item_type == 'Drink') {
new_item.querySelector('.item-price').textContent = `$${drink_prices[item_id].toFixed(2)}`;
new_item.querySelector('.item-option-image').src = '/static/images/icons/drink.svg'
new_item.querySelector('.item-option-image').src = '/static/images/icons/drink.svg';
} else if (item_type == 'Side') {
new_item.querySelector('.item-price').textContent = `$${fries_prices[item_id].toFixed(2)}`;
new_item.querySelector('.item-option-image').src = '/static/images/icons/fries.svg'
new_item.querySelector('.item-option-image').src = '/static/images/icons/fries.svg';
} else if (item_type == 'Extra') {
new_item.querySelector('.item-price').textContent = `$${ketchup_price.toFixed(2)}`;
new_item.querySelector('.item-option-image').src = '/static/images/icons/ketchup.svg'
new_item.querySelector('.item-option-image').src = '/static/images/icons/ketchup.svg';
}
new_item.querySelector('.item-size').textContent = item_size;
new_item.querySelector('.item-type').textContent = item_type;
Expand Down Expand Up @@ -318,8 +325,8 @@ function show_cart() {
document.querySelector('.bot-msg-wrapper[active]').append(new_cart);
document.querySelector('.bot-chat[active]').style.alignItems = 'flex-start';
cart = new_cart;
if (flask_output_type == 'Sandwich Cart Addition' || flask_output_type == 'Drink Cart Addition' || flask_output_type == 'Fries Cart Addition' || flask_output_type == 'Fries Upgrade' || flask_output_type == 'Ketchup Cart Addition') {
handshake()
if (flask_output_type == 'Sandwich Cart Addition' || flask_output_type == 'Drink Cart Addition' || flask_output_type == 'Fries Cart Addition' || flask_output_type == 'Ketchup Cart Addition') {
handshake();
}
}

Expand Down Expand Up @@ -376,7 +383,14 @@ form.addEventListener('submit', function(event) {
send_user_message(form_input.value);
form_input.value = '';
}
}
} else if (flask_output_type == 'Done!') {
if (!restart_typecases.includes(form_input.value.toLowerCase().trim())) {
input_border(true);
} else {
input_border(false);
location.reload();
}
}
}
});

Expand All @@ -385,7 +399,7 @@ form_input.addEventListener('input', function() {
if (sandwich_typecases.includes(form_input.value.toLowerCase().trim())) {
input_border(false);
} else if (size_typecases.includes(form_input.value.toLowerCase().trim())) {
input_border(false)
input_border(false);
}
if (form_input.value == '') {
input_border(false);
Expand All @@ -396,4 +410,4 @@ form_input.addEventListener('input', function() {
// Start
//

send_bot_message(flask_output)
send_bot_message(flask_output);
Binary file added tech_stack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f984007

Please sign in to comment.