hy-farm/home/web/game/js/main/14-gold-exchange.js
2026-05-23 23:50:53 +08:00

444 lines
18 KiB
JavaScript

var game;
!(function (game) {
const toExchangeInt = function (value, fallback = 0) {
const parsed = parseInt(value, 10);
return Number.isFinite(parsed) ? parsed : fallback;
};
const GoldExchangeUtils = class GoldExchangeUtils {
static request(route, data, callback) {
Global.netProxy.sendRequest(GameConfig.SERVER_PATH + route, data || {}, callback);
}
static commonRate(id, fallback = 1) {
const config = Global.gameProxy.getCommonConfigById(id) || {};
return Math.max(1, toExchangeInt(config.min, fallback));
}
static serverMessage(response, fallback) {
if (!response) return fallback || "操作失败";
if ("string" === typeof response.status) return response.status;
return (GameConfig.ServerCode && GameConfig.ServerCode[response.status]) || response.info || fallback || "操作失败";
}
static mergePlayerData(data) {
if (!data || !Global.playerProxy || !Global.playerProxy.playerData) return;
Object.keys(data).forEach(function (key) {
Global.playerProxy.playerData[key] = data[key];
});
game.EventManager.instance.dispatch(SysNotify.TOPLAYER_INFO_UPDATE);
}
static makeLabel(text, size, color, x, y, width, height) {
const label = new eui.Label();
label.text = text || "";
label.size = size || 22;
label.textColor = color || 0xfffbf2;
label.x = x || 0;
label.y = y || 0;
label.width = width || 100;
label.height = height || 30;
label.fontFamily = "huakanghaibao";
label.stroke = 1;
label.strokeColor = 0x724f2e;
label.verticalAlign = egret.VerticalAlign.MIDDLE;
return label;
}
static makeButton(label, width, height) {
const button = new eui.Button();
button.label = label || "";
button.width = width || 120;
button.height = height || 52;
button.skinName =
window.common && window.common.MidButtonSkin ? window.common.MidButtonSkin : "common.MidButtonSkin";
return button;
}
static formatDate(seconds) {
const time = toExchangeInt(seconds);
if (!time) return "";
return game.Utils && game.Utils.formatDate ? game.Utils.formatDate(new Date(1000 * time)) : new Date(1000 * time).toLocaleString();
}
};
((game.GoldExchangeUtils = GoldExchangeUtils),
__reflect(GoldExchangeUtils.prototype, "game.GoldExchangeUtils"));
const GoldExchangeHistoryItem = class GoldExchangeHistoryItem extends eui.ItemRenderer {
constructor() {
super();
this.width = 500;
this.height = 62;
}
createChildren() {
super.createChildren();
this.line = new egret.Shape();
this.addChild(this.line);
this.timeLabel = GoldExchangeUtils.makeLabel("", 18, 0xf4991d, 12, 8, 190, 24);
this.timeLabel.stroke = 0;
this.addChild(this.timeLabel);
this.descLabel = GoldExchangeUtils.makeLabel("", 18, 0x7d1c1c, 12, 34, 370, 24);
this.descLabel.stroke = 0;
this.addChild(this.descLabel);
this.numLabel = GoldExchangeUtils.makeLabel("", 18, 0x2f9c11, 390, 34, 92, 24);
this.numLabel.stroke = 0;
this.numLabel.textAlign = egret.HorizontalAlign.RIGHT;
this.addChild(this.numLabel);
}
dataChanged() {
const row = this.data || {};
this.timeLabel.text = GoldExchangeUtils.formatDate(row.created_at);
this.descLabel.text = row.description || "";
this.numLabel.text = row.numText || "";
const graphics = this.line.graphics;
graphics.clear();
graphics.lineStyle(1, 0xc78944, 0.65);
graphics.moveTo(8, 60);
graphics.lineTo(492, 60);
}
};
__reflect(GoldExchangeHistoryItem.prototype, "game.GoldExchangeHistoryItem");
const GoldExchangePanel = class GoldExchangePanel extends game.BasePanel {
constructor() {
super();
this.mode = "gemToGold";
this.gemAmount = 1;
this.goldAmount = 10;
this.isFullScreen = !0;
this.isVisibleAnimate = !0;
this.width = Const.WIN_W;
this.height = Const.WIN_H;
}
createChildren() {
super.createChildren();
this.goldToGemRate = GoldExchangeUtils.commonRate(17, 10);
this.gemToGoldRate = GoldExchangeUtils.commonRate(24, 10);
this.goldAmount = this.goldToGemRate;
this.buildView();
this.refreshMode();
this.loadHistory();
}
buildView() {
this.modalBlocker = new egret.Shape();
this.modalBlocker.graphics.beginFill(0x000000, 0.01);
this.modalBlocker.graphics.drawRect(0, 0, Const.WIN_W, Const.WIN_H);
this.modalBlocker.graphics.endFill();
this.modalBlocker.touchEnabled = !0;
this.addChild(this.modalBlocker);
const panelBg = new eui.Image("common_json.c_panel_png");
panelBg.x = 20;
panelBg.y = 76;
panelBg.width = 600;
panelBg.height = 820;
panelBg.scale9Grid = new egret.Rectangle(122, 174, 21, 10);
this.addChild(panelBg);
const titleBg = new eui.Image("common2_json.common2_diban_png");
titleBg.x = 168;
titleBg.y = 70;
titleBg.width = 304;
titleBg.height = 70;
this.addChild(titleBg);
const title = GoldExchangeUtils.makeLabel("兑换金币", 34, 0xfffbf2, 170, 84, 300, 48);
title.bold = !0;
title.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(title);
this.statusLabel = GoldExchangeUtils.makeLabel("", 20, 0x5b3616, 60, 142, 520, 34);
this.statusLabel.stroke = 0;
this.statusLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.statusLabel);
this.goldLabel = GoldExchangeUtils.makeLabel("", 22, 0x5b3616, 92, 184, 210, 30);
this.goldLabel.stroke = 0;
this.addChild(this.goldLabel);
this.gemLabel = GoldExchangeUtils.makeLabel("", 22, 0x5b3616, 332, 184, 210, 30);
this.gemLabel.stroke = 0;
this.addChild(this.gemLabel);
this.gemToGoldButton = GoldExchangeUtils.makeButton("钻石兑金币", 220, 54);
this.gemToGoldButton.x = 80;
this.gemToGoldButton.y = 228;
this.gemToGoldButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGemToGoldTap, this);
this.addChild(this.gemToGoldButton);
this.goldToGemButton = GoldExchangeUtils.makeButton("金币兑钻石", 220, 54);
this.goldToGemButton.x = 340;
this.goldToGemButton.y = 228;
this.goldToGemButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGoldToGemTap, this);
this.addChild(this.goldToGemButton);
this.modeTitleLabel = GoldExchangeUtils.makeLabel("", 28, 0xfffbf2, 80, 306, 480, 40);
this.modeTitleLabel.bold = !0;
this.modeTitleLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.modeTitleLabel);
this.rateLabel = GoldExchangeUtils.makeLabel("", 20, 0x7d1c1c, 80, 348, 480, 30);
this.rateLabel.stroke = 0;
this.rateLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.rateLabel);
this.minusButton = GoldExchangeUtils.makeButton("-", 54, 44);
this.minusButton.x = 112;
this.minusButton.y = 394;
this.minusButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onMinusTap, this);
this.addChild(this.minusButton);
const inputBg = new eui.Image("common2_json.common2_inputbg_png");
inputBg.x = 184;
inputBg.y = 396;
inputBg.width = 270;
inputBg.height = 42;
inputBg.scale9Grid = new egret.Rectangle(13, 4, 84, 30);
this.addChild(inputBg);
this.amountInput = new eui.EditableText();
this.amountInput.x = 196;
this.amountInput.y = 400;
this.amountInput.width = 246;
this.amountInput.height = 34;
this.amountInput.restrict = "0-9";
this.amountInput.textAlign = egret.HorizontalAlign.CENTER;
this.amountInput.verticalAlign = egret.VerticalAlign.MIDDLE;
this.amountInput.size = 24;
this.amountInput.textColor = 0x5b3616;
this.amountInput.fontFamily = "huakanghaibao";
this.amountInput.addEventListener(egret.Event.CHANGE, this.onAmountChanged, this);
this.addChild(this.amountInput);
this.plusButton = GoldExchangeUtils.makeButton("+", 54, 44);
this.plusButton.x = 474;
this.plusButton.y = 394;
this.plusButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPlusTap, this);
this.addChild(this.plusButton);
this.resultLabel = GoldExchangeUtils.makeLabel("", 20, 0x5b3616, 80, 448, 480, 30);
this.resultLabel.stroke = 0;
this.resultLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.resultLabel);
this.exchangeButton = GoldExchangeUtils.makeButton("兑换", 150, 58);
this.exchangeButton.x = 245;
this.exchangeButton.y = 486;
this.exchangeButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onExchangeTap, this);
this.addChild(this.exchangeButton);
const historyTitle = GoldExchangeUtils.makeLabel("兑换记录", 24, 0xfffbf2, 70, 566, 500, 32);
historyTitle.textAlign = egret.HorizontalAlign.CENTER;
historyTitle.bold = !0;
this.addChild(historyTitle);
const historyBg = new eui.Image("common_json.inner_page_png");
historyBg.x = 70;
historyBg.y = 602;
historyBg.width = 500;
historyBg.height = 214;
historyBg.scale9Grid = new egret.Rectangle(31, 31, 8, 9);
this.addChild(historyBg);
this.historyList = new eui.List();
this.historyList.width = 500;
this.historyList.height = 204;
this.historyList.itemRenderer = GoldExchangeHistoryItem;
this.historyScroller = new eui.Scroller();
this.historyScroller.x = 70;
this.historyScroller.y = 607;
this.historyScroller.width = 500;
this.historyScroller.height = 204;
this.historyScroller.viewport = this.historyList;
this.addChild(this.historyScroller);
this.closeButton = GoldExchangeUtils.makeButton("离开", 160, 58);
this.closeButton.x = 240;
this.closeButton.y = 820;
this.closeButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseTap, this);
this.addChild(this.closeButton);
}
onRemoved() {
this.gemToGoldButton &&
this.gemToGoldButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGemToGoldTap, this);
this.goldToGemButton &&
this.goldToGemButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGoldToGemTap, this);
this.minusButton && this.minusButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onMinusTap, this);
this.plusButton && this.plusButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onPlusTap, this);
this.amountInput && this.amountInput.removeEventListener(egret.Event.CHANGE, this.onAmountChanged, this);
this.exchangeButton &&
this.exchangeButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onExchangeTap, this);
this.closeButton && this.closeButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseTap, this);
super.onRemoved();
}
onGemToGoldTap(event) {
event.stopPropagation();
this.mode = "gemToGold";
this.refreshMode();
this.loadHistory();
}
onGoldToGemTap(event) {
event.stopPropagation();
this.mode = "goldToGem";
this.refreshMode();
this.loadHistory();
}
onMinusTap(event) {
event.stopPropagation();
this.changeAmount(-this.currentStep());
}
onPlusTap(event) {
event.stopPropagation();
this.changeAmount(this.currentStep());
}
onAmountChanged() {
const amount = this.currentInputAmount();
this.setModeAmount(amount);
this.updateResult();
}
onExchangeTap(event) {
event.stopPropagation();
const amount = this.currentInputAmount();
if (amount <= 0) {
TipsUtils.showTipsDownToUp("请输入大于0的数量");
return;
}
if ("goldToGem" === this.mode && amount % this.goldToGemRate !== 0) {
TipsUtils.showTipsDownToUp("请输入" + this.goldToGemRate + "的倍数");
return;
}
const player = Global.playerProxy.playerData,
isGemToGold = "gemToGold" === this.mode,
owned = isGemToGold ? toExchangeInt(player.gem) : toExchangeInt(player.gold);
if (amount > owned) {
TipsUtils.showTipsDownToUp(isGemToGold ? "钻石不足" : "金币不足");
return;
}
const result = this.currentResult(),
confirmText = isGemToGold
? "是否消耗" + amount + "钻石兑换" + result + "金币?"
: "是否消耗" + amount + "金币兑换" + result + "钻石?";
Global.alertMediator.addAlert(confirmText, this.sendExchangeRequest.bind(this, amount));
}
onCloseTap(event) {
event.stopPropagation();
game.AppFacade.getInstance().sendNotification(PanelNotify.CLOSE_CHANGE_GOLD);
}
sendExchangeRequest(amount) {
const route = "gemToGold" === this.mode ? "comm/score-to-gold" : "comm/gold-to-score",
data = "gemToGold" === this.mode ? { score: amount } : { gold: amount };
this.setStatus("正在兑换...");
GoldExchangeUtils.request(route, data, this.onExchangeResponse.bind(this));
}
onExchangeResponse(response) {
if (0 !== response.status) {
this.setStatus(GoldExchangeUtils.serverMessage(response, "兑换失败"));
return;
}
GoldExchangeUtils.mergePlayerData(response.data);
TipsUtils.showTipsDownToUp("兑换成功");
this.refreshMode();
this.loadHistory();
}
loadHistory() {
this.setStatus("正在读取兑换记录...");
GoldExchangeUtils.request(
"comm/score-gold-list",
{
type: "gemToGold" === this.mode ? 0 : 1
},
this.onHistoryResponse.bind(this)
);
}
onHistoryResponse(response) {
if (0 !== response.status) {
this.setStatus(GoldExchangeUtils.serverMessage(response, "兑换记录读取失败"));
this.historyList.dataProvider = new eui.ArrayCollection([]);
return;
}
const list = (response.data && response.data.list) || [],
isGemToGold = "gemToGold" === this.mode,
rows = list.map(function (item) {
const num = toExchangeInt(item.num || item.count || 0);
return {
created_at: item.created_at,
description: isGemToGold ? "兑换获得金币" : "兑换获得钻石",
numText: num > 0 ? "+" + num : ""
};
});
this.historyList.dataProvider = new eui.ArrayCollection(rows);
this.setStatus(rows.length ? "兑换记录已加载" : "暂无兑换记录");
this.refreshBalances();
}
refreshMode() {
const isGemToGold = "gemToGold" === this.mode;
this.gemToGoldButton.enabled = !isGemToGold;
this.goldToGemButton.enabled = isGemToGold;
this.modeTitleLabel.text = isGemToGold ? "钻石兑换金币" : "金币兑换钻石";
this.rateLabel.text = isGemToGold
? "兑换比例 1 钻石 = " + this.gemToGoldRate + " 金币"
: "兑换比例 " + this.goldToGemRate + " 金币 = 1 钻石";
this.amountInput.text = this.currentAmount().toString();
this.refreshBalances();
this.updateResult();
}
refreshBalances() {
const player = Global.playerProxy.playerData || {};
this.goldLabel.text = "金币:" + game.Utils.BigNumTostring(toExchangeInt(player.gold));
this.gemLabel.text = "钻石:" + game.Utils.BigNumTostring(toExchangeInt(player.gem));
}
updateResult() {
const amount = this.currentInputAmount(),
result = this.currentResult();
this.resultLabel.text =
"gemToGold" === this.mode
? "预计获得 " + result + " 金币"
: "预计获得 " + result + " 钻石";
}
changeAmount(delta) {
const next = Math.max(this.currentStep(), this.currentInputAmount() + delta);
this.setModeAmount(next);
this.amountInput.text = next.toString();
this.updateResult();
}
currentInputAmount() {
return toExchangeInt(this.amountInput && this.amountInput.text, this.currentAmount());
}
currentAmount() {
return "gemToGold" === this.mode ? this.gemAmount : this.goldAmount;
}
setModeAmount(amount) {
const normalized = Math.max(0, toExchangeInt(amount));
"gemToGold" === this.mode ? (this.gemAmount = normalized) : (this.goldAmount = normalized);
}
currentStep() {
return "gemToGold" === this.mode ? 1 : this.goldToGemRate;
}
currentResult() {
const amount = this.currentInputAmount();
return "gemToGold" === this.mode ? amount * this.gemToGoldRate : Math.floor(amount / this.goldToGemRate);
}
setStatus(text) {
this.statusLabel && (this.statusLabel.text = text || "");
}
};
((game.GoldExchangePanel = GoldExchangePanel), __reflect(GoldExchangePanel.prototype, "game.GoldExchangePanel"));
const GoldExchangeMediator = class GoldExchangeMediator extends BaseMediator {
constructor() {
super("GoldExchangeMediator");
}
listNotificationInterests() {
return [PanelNotify.OPEN_CHANGE_GOLD, PanelNotify.CLOSE_CHANGE_GOLD];
}
showViewComponent(layer = 7) {
((this.viewComponent = new game.GoldExchangePanel()), this.showUI(this.viewComponent, !0, 0, 0, layer));
}
handleNotification(notification) {
switch (notification.getName()) {
case PanelNotify.OPEN_CHANGE_GOLD:
this.showViewComponent(7);
break;
case PanelNotify.CLOSE_CHANGE_GOLD:
this.closeViewComponent(1);
}
}
static NAME = "GoldExchangeMediator";
};
((game.GoldExchangeMediator = GoldExchangeMediator),
__reflect(GoldExchangeMediator.prototype, "game.GoldExchangeMediator"));
})(game || (game = {}));