商店:基础数据及购买

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
+1 -1
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 }
+97 -23
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
}