-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.js
270 lines (253 loc) · 7 KB
/
shop.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
import Chess from "./chess.js";
import lib from "./lib.js";
import utils from "./utils.js";
class ChessStore {
constructor(level = 1) {
this.level = level; // 商店等級
this.stock = []; // 商品庫存
this.oldStock = [];
this.generateStock();
this.User = null;
this.display = true;
this.storeScope = null;
this.firstLoad = false;
}
// 升級商店
upgrade() {
this.level++;
}
resetLevel() {
this.level = 1;
}
hidden() {
const shopWrap = document.querySelector(".shop-wrap");
shopWrap.style.display = "none";
this.display = false;
}
show() {
const shopWrap = document.querySelector(".shop-wrap");
shopWrap.style.display = "flex";
this.display = true;
}
getDisplay() {
return this.display;
}
// 隨機生成商品庫存
generateStock() {
const racesList = [
["cavalry", "shield", "spearman"],
["archer", "handCannoneer", "horseArcher"],
["ninja", "drummer", "medic"],
];
const shopLength = this.level > 1 ? 10 : 5;
const races = [];
for (let i = 0; i < this.level; i++) {
races.push(...racesList[i]);
}
this.stock = [];
for (let i = 0; i < shopLength; i++) {
// 每次生成五種商品
const race = races[Math.floor(Math.random() * races.length)];
const piece = new Chess(race);
// 旗子金額
this.stock.push(piece);
}
}
// 展示商品庫存
displayStock() {
return this.stock;
}
// 購買商品
purchase(index) {
if (index < 0) {
console.log("Invalid index.");
return;
}
// 判斷玩家金額
const _user = this.User;
const _userMoney = _user.getMoney();
const _userMaxPieces = _user.maxPieces;
const _userPieceLength = _user.displayPiecesLength();
const _chess = this.stock[index];
const _chessMoney = _chess.price;
if (!_chess) return;
if (_userMoney < _chessMoney) {
utils
.popUps({
type: "fail",
title: window.$i18n.t("popup.notMoney.title"),
content: window.$i18n.t("popup.notMoney.content"),
mask: true,
})
.then((v) => {
const fightWrap = document.querySelector(".fight-wrap");
fightWrap.classList.add("hidden");
});
return;
}
// 合成
if (_user.checkPieceThree(this.stock[index])) {
const piece = this.stock.splice(index, 1, "")[0]; // 從庫存中移除已購買的商品
_user.buyPiece(piece);
_user.makeupPieceLevel(piece);
this.renderShop();
return;
}
if (_userMaxPieces <= _userPieceLength) {
utils
.popUps({
type: "fail",
title: window.$i18n.t("popup.chessFull.title"),
content: window.$i18n.t("popup.chessFull.content"),
mask: true,
})
.then((v) => {
const fightWrap = document.querySelector(".fight-wrap");
fightWrap.classList.add("hidden");
});
return;
}
const piece = this.stock.splice(index, 1, "")[0]; // 從庫存中移除已購買的商品
_user.buyPiece(piece);
this.renderShop();
return piece;
}
refresh() {
const refreshMoney = 10;
const _user = this.User;
const _userMoney = _user.getMoney();
if (_userMoney < refreshMoney) {
utils
.popUps({
type: "fail",
title: window.$i18n.t("popup.notMoney.title"),
content: window.$i18n.t("popup.notMoney.content"),
mask: true,
})
.then((v) => {
const fightWrap = document.querySelector(".fight-wrap");
fightWrap.classList.add("hidden");
});
return;
}
_user.reduceMoney(refreshMoney);
this.generateStock();
this.renderShop();
}
render() {
this.renderShop();
}
getRenderIndex() {
if (!this.stock.length) {
this.stock = [null, null, null, null, null, null];
}
const _stock = this.stock;
const _oldStock = this.oldStock;
const renderIndexList = _stock.reduce((arr, item, index) => {
if (item !== _oldStock[index]) {
arr.push(index);
}
return arr;
}, []);
this.oldStock = this.stock.slice();
return renderIndexList;
}
renderShop() {
const _this = this;
const reRenderIndexList = _this.getRenderIndex();
const shopWrap = document.querySelector(".shop-wrap");
const storeList = this.displayStock();
const parent = document.querySelector(".shop-piece");
const childList = parent.childNodes;
if ([...childList].length > this.stock.length) {
[...childList].forEach((element, index) => {
if (index + 1 > this.stock.length) {
element.remove();
}
});
}
reRenderIndexList.forEach((reRenderIndex) => {
const item = storeList[reRenderIndex];
const index = reRenderIndex;
const childItem = childList[index];
const chessWrap = lib.createDOM("div", "", {
className: "chess-item-wrap",
});
const elementDiv = lib.createDOM("div", "", {
className: "chess-item",
});
let elementImg;
if (item) {
elementImg = lib.createDOM("img", "", {
src: "./static/user/level-1.png",
});
elementImg.style.backgroundImage = `url(./static/user/${item?.name}.png)`;
elementImg.style.backgroundSize = "cover";
} else {
elementImg = lib.createDOM("img", "", {
src: "./static/space.png",
});
}
elementDiv.addEventListener("click", () => {
_this.purchase(index);
});
elementDiv.appendChild(elementImg);
chessWrap.appendChild(elementDiv);
if (!childItem) {
parent.appendChild(chessWrap);
} else {
parent.replaceChild(chessWrap, childItem);
}
});
if (!this.firstLoad) {
setTimeout(() => {
const { left, right, top, bottom } = shopWrap.getBoundingClientRect();
this.storeScope = {
left: left - 15,
right: right + 15,
top: top - 15,
bottom: bottom + 15,
};
this.shopAddEvent();
});
this.firstLoad = true;
}
}
shopAddEvent() {
const _this = this;
const shopCloseBtn = document.querySelector(".shop-close");
const shopShowBtn = document.querySelector(".shop-show-btn");
const shopRefreshBtn = document.querySelector(".shop-refresh-btn");
shopCloseBtn.onclick = null;
shopShowBtn.onclick = null;
shopRefreshBtn.onclick = null;
shopCloseBtn.onclick = () => {
_this.hidden();
};
shopShowBtn.onclick = () => {
_this.show();
};
shopRefreshBtn.onclick = () => {
_this.refresh();
};
}
getScope() {
return this.storeScope;
}
setUserObject(User) {
this.User = User;
}
}
export default ChessStore;
// 創建一個商店
// const store = new ChessStore(1);
// 升級商店
// store.upgrade();
// 生成商品庫存
// store.generateStock();
// 展示商品庫存
// store.displayStock();
// 購買商品
// const purchasedPiece = store.purchase(2);
// 再次展示商品庫存
// store.displayStock();