104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
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 getAllShopList(roleId: string, serverId: number) {
|
|
let userShopRecs = await UserShopModel.findByRoleId(roleId);
|
|
let activities = await getShopActivityDatas(roleId, serverId);
|
|
|
|
let shops: ShopReturnData[] = [];
|
|
for(let [_, { shop, type } ] of gameData.shopType) {
|
|
let shopData = await getShopListByData(shop, type, userShopRecs, activities);
|
|
shops.push(shopData);
|
|
}
|
|
return shops;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
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
|
|
} |