商店:基础数据及购买

This commit is contained in:
luying
2022-07-25 17:41:04 +08:00
parent 4bf75fcda5
commit 4cf4511ee7
13 changed files with 952 additions and 565 deletions

View File

@@ -1,5 +1,5 @@
import { Application, BackendSession } from "pinus";
import { gameData } from "../../../pubUtils/data";
import { gameData, hasShopType } from "../../../pubUtils/data";
import { parseGoodStr, resResult } from "../../../pubUtils/util";
import { STATUS, GUILD_STRUCTURE, ITID, CONSUME_TYPE, HERO_QUALITY_TYPE, HERO_GROW_MAX, ITEM_CHANGE_REASON } from "../../../consts";
import { DicShopListModel } from "../../../db/DicShopList";
@@ -8,7 +8,7 @@ import { handleCost, addItems } from "../../../services/role/rewardService";
import { GuildModel } from "../../../db/Guild";
import { SHOP } from "../../../pubUtils/dicParam";
import { HeroModel } from "../../../db/Hero";
import { getShopListById } from "../../../services/shopService";
import { getShopDicById, getShopListByType, getShopPrice } from "../../../services/shopService";
import { RewardInter } from "../../../pubUtils/interface";
import { RoleModel } from "../../../db/Role";
@@ -21,20 +21,21 @@ export class ShopHandler {
}
// 获得商品列表
async getShopList(msg: { shopId: number }, session: BackendSession) {
async getShopList(msg: { shop: number, type: number }, session: BackendSession) {
let roleId = session.get('roleId');
let { shopId } = msg;
let serverId = session.get('serverId');
let { shop, type } = msg;
if(!gameData.shopList.has(shopId)) {
if(!hasShopType(shop, type)) {
return resResult(STATUS.WRONG_PARMS);
}
const shopItemList = await getShopListById(shopId, roleId);
return resResult(STATUS.SUCCESS, { shopItemList });
const result = await getShopListByType(shop, type, roleId, serverId);
return resResult(STATUS.SUCCESS, result);
}
// 购买商品
async purchase(msg: { shopItemId: number, count: number }, session: BackendSession) {
async purchase(msg: { activityId: number, shopItemId: number, count: number }, session: BackendSession) {
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let sid = session.get('sid');
@@ -42,24 +43,24 @@ export class ShopHandler {
let serverId = session.get('serverId');
let vipStartTime: number = session.get('vipStartTime');
let { shopItemId, count } = msg;
let { activityId, shopItemId, count } = msg;
if( count < 0) return resResult(STATUS.WRONG_PARMS);
let dicShopItem = gameData.shopItem.get(shopItemId);
let dicShopItem = await getShopDicById(activityId, shopItemId, roleId, serverId);
if(!dicShopItem) return resResult(STATUS.DIC_DATA_NOT_FOUND);
if(dicShopItem.guildLvLimit) {
let myGuild = await GuildModel.findByCode(guildCode, serverId);
if(!myGuild || myGuild.lv < dicShopItem.guildLvLimit) return resResult(STATUS.GUILD_LV_LIMIT);
}
if(dicShopItem.lvlimit) {
if(dicShopItem.lvLimit) {
let role = await RoleModel.findByRoleId(roleId, 'lv');
if(role.lv < dicShopItem.lvlimit) {
if(role.lv < dicShopItem.lvLimit) {
return resResult(STATUS.LV_LIMIT);
}
}
let userShop = await UserShopModel.findByRoleAndItem(roleId, dicShopItem.id);
let userShop = await UserShopModel.findByRoleAndItem(roleId, activityId, dicShopItem);
if(dicShopItem.purchaseLimit != -1) {
if(vipStartTime > 0 && dicShopItem.purchaseLimit != -1) {
if(userShop && userShop.count + count > dicShopItem.purchaseLimit + dicShopItem.vipPurchaseLimit) {
@@ -73,20 +74,17 @@ export class ShopHandler {
}
// 消耗
let cost = [{
id: dicShopItem.money,
count: Math.round(dicShopItem.price * count)
}];
let cost = getShopPrice(dicShopItem.money, count, userShop?.count||0, dicShopItem.price);
let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.SHOP_PURCHASE);
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
// 次数
userShop = await UserShopModel.purchase(roleId, roleName, dicShopItem, count);
userShop = await UserShopModel.purchase(roleId, roleName, activityId, dicShopItem, count);
if(!userShop) return resResult(STATUS.BUY_COUNT_OVER);
// 获得
let reward = [{
id: dicShopItem.goodid,
id: dicShopItem.goodId,
count
}];
let goods = await addItems(roleId, roleName, sid, reward, ITEM_CHANGE_REASON.SHOP_PURCHASE);

View File

@@ -133,7 +133,7 @@ export async function getModuleData(type: string, data: { role: RoleType, sessio
role.spines = role.spines.filter(cur => cur.status);
return pick(role, ENTERY_ROLE_PICK);
case 'shop': // 商店
return await getAllShopList(roleId);
return await getAllShopList(roleId, serverId);
case 'rank': // 排名
const rewards = await getRankFirstReward(role, serverId);
return { rewards }

View File

@@ -1,30 +1,104 @@
import { gameData } from "../pubUtils/data";
import { ShopItemListParam } from '../domain/roleField/shop';
import { UserShopModel } from "../db/UserShop";
import { gameData, getShopType } from "../pubUtils/data";
import { ShopData, ShopReturnData } from '../domain/roleField/shop';
import { UserShopModel, UserShopType } from "../db/UserShop";
import { getActivitiesByType, getActivityById } from "./activity/activityService";
import { ACTIVITY_TYPE } from "../consts";
import { getRoleCreateTime, getServerCreateTime } from "./redisService";
import { decodeArrayListStr } from "../pubUtils/util";
export async function getShopListById(shopId: number, roleId: string) {
export async function getAllShopList(roleId: string, serverId: number) {
let userShopRecs = await UserShopModel.findByRoleId(roleId);
let activities = await getShopActivityDatas(roleId, serverId);
let shopItemList = new Array<ShopItemListParam>(); // 返回
let userShopRecs = await UserShopModel.findMapByShopId(roleId, shopId);
// console.log(JSON.stringify([...userShopRecs]))
let dicShop = gameData.shop.get(shopId)||[];
for(let { id, type } of dicShop) {
let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;
let param = new ShopItemListParam(id, 1, type, buyCount, 0);
shopItemList.push(param);
let shops: ShopReturnData[] = [];
for(let [_, { shop, type } ] of gameData.shopType) {
let shopData = await getShopListByData(shop, type, userShopRecs, activities);
shops.push(shopData);
}
shopItemList = shopItemList.sort((a, b) => b.order - a.order);
return shopItemList;
return shops;
}
export async function getAllShopList(roleId: string) {
let shopLists: {shopId: number, shopItemList: ShopItemListParam[]}[] = [];
for(let [ shopId ] of gameData.shopList) {
let shopItemList = await getShopListById(shopId, roleId);
shopLists.push({ shopId, shopItemList });
export async function getShopListByType(shop: number, type: number, roleId: string, serverId: number) {
let userShopRecs = await UserShopModel.findByShopType(roleId, shop, type);
let activities = await getShopActivityDatas(roleId, serverId);
return await getShopListByData(shop, type, userShopRecs, activities);
}
async function getShopListByData(shop: number, type: number, userShopRecs: UserShopType[], activities: ShopData[]) {
let result = new ShopReturnData(shop, type);
let dicShopType = getShopType(shop, type);
if(dicShopType && dicShopType.isLimit) {
for(let activity of activities) {
result.setDicByActivity(activity);
}
}
return shopLists;
for(let userShop of userShopRecs) {
result.setUserRecords(userShop);
}
return result;
}
export async function getShopDicById(activityId: number, itemId: number, roleId: string, serverId: number) {
if(activityId == 0) {
return gameData.shopItem.get(itemId);
} else {
let activityData = await getShopActivityById(roleId, serverId, activityId);
return activityData.findByItemId(itemId);
}
}
export async function getShopActivityDatas(roleId: string, serverId: number) {
let createTime = await getRoleCreateTime(roleId);
let serverTime = await getServerCreateTime(serverId);
let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.SHOP);
return activities.map(activity => {
return new ShopData(activity, createTime, serverTime);
});
}
export async function getShopActivityById(roleId: string, serverId: number, activityId: number) {
let createTime = await getRoleCreateTime(roleId);
let serverTime = await getServerCreateTime(serverId);
let activity = await getActivityById(activityId);
return new ShopData(activity, createTime, serverTime);
}
/**
*
* @param goodId 物品
* @param count 本次购买次数
* @param buyCount 上次购买到多少次
* @param str 字典
*/
export function getShopPrice(goodId: number, count: number, buyCount: number, str: string) {
let arr = parseShopPrice(str);
let result = 0;
for(let i = 0; i < count; i++) {
let curPrice = 0;
for(let { times, price } of arr) {
curPrice = price;
if(buyCount + i <= times) break;
}
result += curPrice;
}
return [{
id: goodId,
count: result
}]
}
function parseShopPrice(str: string) {
let result = new Array<{ times: number, price: number }>();
if (!str) return result;
let decodeArr = decodeArrayListStr(str);
for (let [times, price] of decodeArr) {
if (isNaN(parseInt(times)) || isNaN(parseInt(price))) {
throw new Error('data table format wrong');
}
result.push({ times: parseInt(times), price: parseInt(price) });
}
return result
}