-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
175 lines (160 loc) · 5.82 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="cache-control" content="max-age=86400, must-revalidate"><!-- cache for a day -->
<title>Gemischtwarenladen</title>
<style>
body {
font-family: 'Comic Sans MS', serif;
}
.bag-item, .item {
margin-bottom: 5px;
cursor: pointer;
}
#shop, #bag, .bag {
padding: 10px;
}
.pointer {
cursor: pointer;
}
.clean, .total-price {
margin: 5px 0;
}
.container {
display: flex;
flex-wrap: wrap;
}
body {
background: black;
color: white;
}
.box {
flex: 1;
background-color: #eee;
margin: 10px;
background-color: black;
margin: 0;padding: 0;
}
.bag-item, .item {
background-color: #eee;
color: black;
padding: 5px;
}
@media screen and (max-width: 768px) {
.box {
flex-basis: calc(50% - 20px); /* Subtracting margin */
}
}
h1, h2 { margin: 0; margin-bottom: 5px; padding: 0;}
h1 { font-size: 1.5rem; padding: 0 10px; }
h2 { font-size: 1.2rem; }
</style>
</head>
<body>
<h1>Das Silberne Emporium</h1>
<main class="container">
<div id="shop" class="box">
<h2>Gemischtwarenladen</h2>
</div>
<div id="bag" class="box">
<h2>Einkaufstasche</h2>
<div class="total-price">Gesamtwert:</div>
<div class="clean pointer">Alles löschen [x]</div>
</div>
</main>
<script>
const shopItems = [
{name: "torch", price: 50, displayname: "🕯️ Fackel", needsSlots: 1},
{name: "futter", price: 50, displayname: "🥘 Ration / Ration / Ration", maxItemsPerSlot: 3},
{name: "rope", price: 100, displayname: "🪢 Seil 60'"},
{name: "arrows", price: 100, displayname: "🏹 Pfeile (20)", maxItemsPerSlot: 20},
{name: "backpack", price: 200, displayname: "🎒 Rucksack"},
{name: "caltrops", price: 50, displayname: "Krähenfuß / Wurfeisen (ein Sack)"},
{name: "crossbowbolts", price: 100, displayname: "Armbrustbolzen (20)", maxItemsPerSlot: 20},
{name: "crowbar", price: 50, displayname: "Brecheisen"},
{name: "flask-bottle", price: 30, displayname: "🍼 Flasche"},
{name: "grapplinghook", price: 100, displayname: "🪝 Enterhaken"},
{name: "flint-n-steel", price: 50, displayname: "🔥 Feuerstein & Stahl"},
{name: "ironspikes", price: 100, displayname: "Eisenstacheln (10)", maxItemsPerSlot: 10},
{name: "lantern", price: 500, displayname: "Laterne"},
{name: "mirror", price: 1000, displayname: "🪞 Spiegel"},
{name: "oilflask", price: 50, displayname: "Ölfläschchen"},
{name: "pole", price: 50, displayname: "Stange"},
// {name: "test", price: 0, displayname: "Test", needsSlots: 5},
]
const shopDiv = document.getElementById("shop");
shopItems
.map(item => ({...item, needsSlots: item.needsSlots||1}))
.forEach((item, index) => {
const itemDiv = document.createElement('div');
itemDiv.classList.add('item');
itemDiv.dataset.name = item.name;
itemDiv.dataset.price = item.price;
itemDiv.dataset.needsSlots = item.needsSlots;
const preis= priceToString(convertPrice(item.price))
itemDiv.textContent = `${item.displayname ? item.displayname : item.name} - ${preis} - ${item.needsSlots}`;
itemDiv.addEventListener('click', () => addItemToBag(itemDiv));
shopDiv.appendChild(itemDiv);
});
const bag = document.getElementById('bag');
const cleaner = document.querySelector('.clean')
cleaner.addEventListener('click', () => {
bag.querySelectorAll('.bag-item').forEach(item => item.remove())
updateBagPrice();
})
function addItemToBag(item) {
if (bag.querySelectorAll('.bag-item').length >= 20) {
console.warn('too many items in bag. max 20')
return
}
const newItem = item.cloneNode(true);
newItem.id = Math.floor(Math.random()*Math.pow(10,17)); // Generate unique ID
newItem.classList.remove('item');
newItem.classList.add('bag-item');
newItem.classList.add('pointer');
newItem.addEventListener('click', () => removeItemFromBag(newItem));
bag.appendChild(newItem);
updateBagPrice();
}
function removeItemFromBag(item) {
item.remove();
updateBagPrice();
}
function convertPrice(priceInCopper) {
const gold = Math.floor(priceInCopper / 100);
const silver = Math.floor((priceInCopper % 100) / 10);
const copper = priceInCopper % 10;
return {gold, silver, copper};
}
function priceToString(convertedPrice, showAll=false) {
const totxt = (value, type) =>
(value != 0 || showAll)
? `${value}${type}`
:''
return [totxt(convertedPrice.gold, 'g'), totxt(convertedPrice.silver, 's'), totxt(convertedPrice.copper, 'c')].join(' ')
}
function updateBagPrice() {
const bagItems = bag.querySelectorAll('.bag-item');
let totalPrice = 0;
let slotNum = 0
bagItems.forEach(item => {
console.log(slotNum, item)
totalPrice += parseInt(item.dataset.price);
slotNum += parseInt(item.dataset.needsSlots);
});
const totalPriceElement = bag.querySelector('.total-price');
const txt = 'Gesamtwert: ' + priceToString(convertPrice(totalPrice), true) + `, Slots: ${slotNum}`
if (totalPriceElement) {
totalPriceElement.textContent = txt
} else {
const totalPriceDiv = document.createElement('div');
totalPriceDiv.classList.add('total-price');
totalPriceDiv.textContent = txt
bag.appendChild(totalPriceDiv);
}
}
</script>
</body>
</html>