-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.py
More file actions
147 lines (135 loc) · 3.75 KB
/
Copy pathapp.py
File metadata and controls
147 lines (135 loc) · 3.75 KB
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
from flask import Flask, session, jsonify, request, render_template, url_for
app = Flask(__name__)
# app.secret_key = "replace-with-your-own-secret"
app.secret_key = "r43irh4i3h54hg4iughnigu4bn3g"
# move to .env later
# Enhanced weapon catalog with prices and better rarities - now using local images
WEAPONS = [
{
"id": 1,
"name": "Assault Rifle",
"rarity": "Rare",
"damage": 35,
"fire_rate": 5.5,
"price": 800,
"image": "assault_rifle.png"
},
{
"id": 2,
"name": "Pump Shotgun",
"rarity": "Epic",
"damage": 80,
"fire_rate": 1.0,
"price": 1200,
"image": "pump_shotgun.png"
},
{
"id": 3,
"name": "SCAR",
"rarity": "Legendary",
"damage": 42,
"fire_rate": 5.5,
"price": 1500,
"image": "scar.png"
},
{
"id": 4,
"name": "SMG",
"rarity": "Uncommon",
"damage": 25,
"fire_rate": 10,
"price": 500,
"image": "smg.png"
},
{
"id": 5,
"name": "Burst Assault Rifle",
"rarity": "Epic",
"damage": 37,
"fire_rate": 4.0,
"price": 1200,
"image": "burst_rifle.png"
},
{
"id": 6,
"name": "Heavy Sniper Rifle",
"rarity": "Legendary",
"damage": 150,
"fire_rate": 0.7,
"price": 2000,
"image": "heavy_sniper.png"
},
{
"id": 7,
"name": "Tactical Shotgun",
"rarity": "Uncommon",
"damage": 72,
"fire_rate": 1.5,
"price": 600,
"image": "tactical_shotgun.png"
},
{
"id": 8,
"name": "Rocket Launcher",
"rarity": "Epic",
"damage": 120,
"fire_rate": 0.75,
"price": 1800,
"image": "rocket_launcher.png"
}
]
# this will be in database ideally
# initialize a cart for a session
@app.before_request
def ensure_cart():
session.setdefault("cart", [])
# home page
@app.route("/")
def index():
return render_template("index.html")
# list all weapons
@app.route("/api/weapons")
def list_weapons():
# Add the full URL for images
weapons_with_urls = []
for weapon in WEAPONS:
weapon_copy = weapon.copy()
weapon_copy["image"] = url_for('static', filename=f'images/{weapon["image"]}')
weapons_with_urls.append(weapon_copy)
return jsonify(weapons_with_urls)
# manage cart
@app.route("/api/cart", methods=["GET", "POST"])
def manage_cart():
if request.method == "GET":
# return full weapon objects in cart
cart_ids = session["cart"]
items = []
for w in WEAPONS:
if w["id"] in cart_ids:
weapon_copy = w.copy()
weapon_copy["image"] = url_for('static', filename=f'images/{w["image"]}')
items.append(weapon_copy)
return jsonify(items)
else:
data = request.get_json() or {}
wid = data.get("id")
if not any(w["id"] == wid for w in WEAPONS):
return jsonify({"error": "Weapon not found"}), 404
session["cart"].append(wid)
session.modified = True
return jsonify({"message": "Added to cart"}), 201
# clear cart
@app.route("/api/cart/clear", methods=["POST"])
def clear_cart():
session["cart"] = []
session.modified = True
return jsonify({"message": "Cart cleared"}), 200
@app.route("/api/cart/remove/<int:item_id>", methods=["POST"])
def remove_from_cart(item_id):
if item_id in session["cart"]:
session["cart"].remove(item_id)
session.modified = True
return jsonify({"message": "Item removed from cart"}), 200
return jsonify({"error": "Item not in cart"}), 404
if __name__ == "__main__":
app.run(debug=True)