xy-server/game/activity/Currency.ts

416 lines
14 KiB
TypeScript
Raw Normal View History

2025-04-23 09:34:08 +08:00
import SKDataUtil from "../gear/SKDataUtil";
import {MsgCode, Operate} from "../role/EEnum";
import * as schedule from "node-schedule";
import DB from "../utils/DB";
import ChargeSum from "../core/ChargeSum";
import GameUtil from "../core/GameUtil";
import PlayerMgr from "../object/PlayerMgr";
import GTimer from "../common/GTimer";
import SKCxfUtil from "../gear/SKCxfUtil";
/**
*
*/
export default class Currency {
static shared = new Currency();
// 当前额度
quota: number;
// 每天最大奖励总金额
maxQuota: number;
// 活动状态1 开启 0 关闭)
state: number;
// 角色剩余点券
mapCurrencyRole: Map<number, number>;
// 角色兑换订单
mapCurrencyRoleOrder: Map<number, any>;
// 奖励数据
award: any [];
// 区组及爆率
oddsMap: Map<number, number>;
constructor() {
this.state = 0;
this.quota = 0;
this.maxQuota = 500;
this.mapCurrencyRole = new Map<number, number>();
this.mapCurrencyRoleOrder = new Map<number, any[]>();
this.award = [];
this.oddsMap = new Map<number, number>();
}
init() {
this.award = [{"name": "1", "prop": 1}, {"name": "0.8", "prop": 2}, {"name": "0.5", "prop": 3}, {
"name": "0.3",
"prop": 5
}, {"name": "0.2", "prop": 6}, {"name": "0.1", "prop": 83}];
// 读取数据
DB.selectConfiguration("currency_" + GameUtil.serverId, (code: any, info: any) => {
if (info && MsgCode.SUCCESS == code) {
let data = SKDataUtil.jsonBy(info.value_data);
this.quota = data.quota;
this.state = data.state;
this.maxQuota = data.maxQuota;
}
});
// 玩家剩余余额
DB.selectCurrencyRole((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
for (let i = 0; i < info.length; i++) {
this.mapCurrencyRole.set(info[i].roleid, info[i].money);
}
}
});
// 玩家提现记录
DB.selectCurrencyWithdraw((code: any, info: any) => {
if (MsgCode.SUCCESS == code) {
for (let i = 0; i < info.length; i++) {
let data: any = info[i];
let list: any[] = this.mapCurrencyRoleOrder.get(data.roleid);
if (list == undefined || list == null) {
list = [];
}
list.push(data);
this.mapCurrencyRoleOrder.set(data.roleid, list);
}
}
});
// 每天晚上12点更新 当前额度
schedule.scheduleJob("0 0 0 * * ?", () => {
this.quota = 100;
this.save();
});
// 一区几率100
this.oddsMap.set(1000, 100);
// // 二区几率
// this.oddsMap.set(1001,100);
// // 三区几率
// this.oddsMap.set(1002,100);
// let list: any = [];
// let list2: any = [];
// for (let i = 0; i < 100; i++) {
// let a = Number((100 / 1000 + 3000 / 1000 / 100).toFixed(2))
// let flag = this.getRandom(a);
// if (flag){
// list.push(1);
// }else {
// list2.push(2);
// }
// }
// console.log("list",list.length)
// console.log("list2",list2.length)
}
/**
*
*/
mondrop(player: any) {
let chargeTotal: any = ChargeSum.shared.getPlayerChargeSum(player.roleid);
let zone: any = 0;
let charge: number = 0;
zone = this.oddsMap.get(GameUtil.serverId);
if (zone != null && zone != undefined) {
charge = Number((zone / 100 + chargeTotal / 1000).toFixed(2));
}
player.send("s2c_mondrop", {
mondrop: charge.toString(),
zone: zone.toFixed(2)
});
}
/**
*
*/
obtain_currency(player: any, task_name: string) {
if (this.state != 0) {
if (this.quota < this.maxQuota) {
// 计算几率
if (this.obtain(player)) {
// 本次获得金额
let money = this.probability();
// 获取余额
let balance = this.mapCurrencyRole.get(player.roleid);
if (balance != undefined && balance != null) {
balance += money;
} else {
balance = money;
}
// 重新存储
this.mapCurrencyRole.set(player.roleid, balance);
// 发送公告
let strRichText = `恭喜玩家在完成<color=#04eb5c >${task_name}</c > <color=#ffffff >获得 </c >【<color=#f09600 >${money}</color >元】提现金奖励,真实可喜可贺呀!</c >`;
// let strRichText = `恭喜玩家<color=#04eb5c >${player.name}</c >在完成<color=#04eb5c >${task_name}</c > <color=#ffffff >获得 </c >【<color=#f09600 >${money}</color >元】提现金奖励,真实可喜可贺呀!</c >`;
PlayerMgr.shared.broadcast('s2c_screen_msg', {
strRichText: strRichText,
bInsertFront: 0
});
// PlayerMgr.shared.broadcast('s2c_game_chat', {
// msg: strRichText,
// scale: 3
// });
this.quota += money;
// 同步数据库
this.update_currency_role(player.roleid, balance);
SKCxfUtil.getCxfCurrencyOperate({
roleId: player.roleid,
roleName: player.name,
operateType: Operate.PLAYER,
operateDepict: "获得点券奖励",
operateResSerial: player.roleid,
operateResName: `本次获得:${money}`,
operateContent: `本次获得:${money} 余额:${balance},总量:${this.quota} - ${this.maxQuota}`
});
this.save();
return;
}
}
return;
} else {
// player.send_notice("当前活动以关闭!")
return;
}
}
/**
*
* @param player
*/
obtain(player: any): boolean {
let chargeTotal: any = ChargeSum.shared.getPlayerChargeSum(player.roleid);
let obtain: any = this.oddsMap.get(GameUtil.serverId);
return this.getRandom(Number((obtain / 1000 + chargeTotal / 1000 / 100).toFixed(2)));
}
/**
*
* @param player
*/
information(player: any) {
// 获取余额
let balance = this.mapCurrencyRole.get(player.roleid);
if (balance == undefined && balance == null) {
balance = 0;
}
// 提现数据
let info: any = this.mapCurrencyRoleOrder.get(player.roleid);
if (info == null || info == undefined) {
info = [];
}
// 通知前端
player.send("s2c_information", {
balance: balance,
withdraw: SKDataUtil.toJson(info, "[]")
})
// 同步数据库
this.update_currency_role(player.roleid, balance);
}
/**
*
*/
withdraw(player: any, data: any) {
if (this.state != 0) {
// 获取当前剩余金额
let money = this.mapCurrencyRole.get(player.roleid);
if (data.money <= money) {
// 提现后剩余金额
money -= data.money;
this.mapCurrencyRole.set(player.roleid, money);
let serial: any = SKDataUtil.rolePrimaryKey(10);
// 同步记录数据
let info: any = {
serial: serial,
roleid: player.roleid,
alipay: data.alipay,
name: data.name,
money: data.money,
req_time: GTimer.getTimeFormat(),
state: 0
};
this.withdraw_res(player, info);
// 同步数据库
DB.insertCurrencyWithdraw(info, money);
player.send_notice("您的提现以提交我们将在次日晚上23:00之前打到您的账户");
return;
} else {
player.send_notice("提现金额不能大于剩余金额(" + money + "元)");
return;
}
}
player.send_notice("当前活动以关闭!")
return;
}
/**
*
* @param player
* @param data
*/
withdraw_res(player: any, data: any) {
let info: any = this.mapCurrencyRoleOrder.get(data.roleid);
if (info == null || info == undefined) {
info = [];
}
info.push(data);
this.mapCurrencyRoleOrder.set(data.roleid, info);
// 通知前端
this.information(player);
return;
}
/**
*
* @param player
* @param data
*/
update_withdraw_state(roleid: any, data: any): boolean {
let info: any[] = this.mapCurrencyRoleOrder.get(Number(roleid));
if (info == null || info == undefined) {
return false;
}
let money = 0;
for (let i = 0; i < info.length; i++) {
let order: any = info[i];
if (order.serial == Number(data.serial)) {
info.splice(i, 1);
order.state = Number(data.state);
money = Number(order.money);
info.push(order);
break;
}
}
this.mapCurrencyRoleOrder.set(roleid, info);
let player: any = PlayerMgr.shared.getPlayerByRoleId(roleid);
if (player) {
// 通知前端
this.information(player);
// 发送公告
if (money > 0) {
let strRichText = `恭喜玩家:<color=#04eb5c >${player.name}</c ><color=#ffffff >成功提现 </c >【<color=#f09600 >${money}</color >元】,真实可喜可贺呀!</c >`;
PlayerMgr.shared.broadcast('s2c_screen_msg', {
strRichText: strRichText,
bInsertFront: 0
});
PlayerMgr.shared.broadcast('s2c_game_chat', {
msg: strRichText,
scale: 3
});
}
}
return true;
}
/**
*
*/
save() {
DB.updateConfiguration("currency_" + GameUtil.serverId, SKDataUtil.toJson({
quota: this.quota,
maxQuota: this.maxQuota,
state: this.state
}, "{}"));
}
/**
* 线
*/
close() {
this.state = 0;
}
/**
*
*/
probability(): number {
let list: any = [];
for (let i = 0; i < this.award.length; i++) {
list.push(this.award[i]['prop'])
}
return Number(this.award[this.getResult(list)]['name']);
}
/**
* GM获得点券奖励
*/
gm_obtain_currency(player: any, task_name: string) {
// 本次获得金额
let money = this.probability();
// 获取余额
let balance = this.mapCurrencyRole.get(player.roleid);
if (balance != undefined && balance != null) {
balance += money;
} else {
balance = money;
}
// 重新存储
this.mapCurrencyRole.set(player.roleid, balance);
// 发送公告
let strRichText = `恭喜玩家在完成<color=#04EB5C >${task_name}</c > <color=#ffffff >获得 </c >【<color=#F09600 >${money}</color >】提现金奖励,真实可喜可贺呀!</c >`;
PlayerMgr.shared.broadcast('s2c_screen_msg', {
strRichText: strRichText,
bInsertFront: 0
});
this.quota += money;
// 同步数据库
this.update_currency_role(player.roleid, balance);
this.save();
SKCxfUtil.getCxfCurrencyOperate({
roleId: player.roleid,
roleName: player.name,
operateType: Operate.PLAYER,
operateDepict: "GM获得点券奖励",
operateResSerial: player.roleid,
operateResName: `本次获得:${money}`,
operateContent: `本次获得:${money} 余额:${balance},总量:${this.quota} - ${this.maxQuota}`
});
return;
}
// 更新金额
update_currency_role(roleid: any, money: any) {
DB.updateCurrencyRole(roleid, money);
}
// 打金爆率
getRandom(probability: number): boolean {
probability = probability * 100 || 1;
let odds = Math.floor(Math.random() * 100);
if (probability === 1) {
return true
}
;
if (odds < probability) {
return true;
} else {
return false;
}
};
getResult(arr: any): any {
let extent: number = 0;
for (let i = 0; i < arr.length; i++) {
extent += arr[i] //获取总数
}
for (let i = 0; i < arr.length; i++) {
let random: any = parseInt((Math.random() * extent).toString()); //获取 0-总数 之间的一个随随机整数
if (random < arr[i]) {
return i //如果在当前的概率范围内,得到的就是当前概率
} else {
extent -= arr[i] //否则减去当前的概率范围,进入下一轮循环
}
}
}
}