商店:一般购物流程
This commit is contained in:
@@ -77,7 +77,8 @@ export class GuildHandler {
|
||||
let rank = await getMyRank(REDIS_KEY.GUILD_ACTIVE_RANK, serverId, guild.code);
|
||||
|
||||
// 加入channel
|
||||
|
||||
session.set('guildCode', guild.code);
|
||||
session.push('guildCode', () => {});
|
||||
addRoleToGuildChannel(roleId, sid, guild.code);
|
||||
|
||||
// 添加动态
|
||||
@@ -273,6 +274,8 @@ export class GuildHandler {
|
||||
// 打开公会页面,加入channel
|
||||
if(userGuild.guildCode) {
|
||||
addRoleToGuildChannel(roleId, sid, guild.code);
|
||||
session.set('guildCode', guild.code);
|
||||
session.push('guildCode', () => {});
|
||||
}
|
||||
|
||||
// 获取排行榜
|
||||
@@ -315,7 +318,7 @@ export class GuildHandler {
|
||||
|
||||
let hasGuild = false;
|
||||
if(isAuto) { // 自动加入
|
||||
const joinResult = await joinGuild(code, guild.name, lv, roleId, serverId);
|
||||
const joinResult = await joinGuild(code, guild.name, lv, roleId, serverId, session);
|
||||
if(joinResult.status == -1) {
|
||||
return joinResult.resResult;
|
||||
}
|
||||
@@ -377,7 +380,7 @@ export class GuildHandler {
|
||||
if(isReceived) { // 同意申请,加入
|
||||
|
||||
for(let { roleId } of applyList) {
|
||||
const joinResult = await joinGuild(code, guild.name, guild.lv, roleId, serverId);
|
||||
const joinResult = await joinGuild(code, guild.name, guild.lv, roleId, serverId, session);
|
||||
if(joinResult.status == -1) continue;
|
||||
let chatSid = await getGuildChannelSid(code);
|
||||
// 更新人数增加
|
||||
@@ -513,7 +516,7 @@ export class GuildHandler {
|
||||
const { guildCode } = invite;
|
||||
const guild = await GuildModel.findByCode(guildCode, serverId);
|
||||
|
||||
const joinResult = await joinGuild(guildCode, guild.name, guild.lv, roleId, serverId);
|
||||
const joinResult = await joinGuild(guildCode, guild.name, guild.lv, roleId, serverId, session);
|
||||
if(joinResult.status == -1) {
|
||||
return joinResult.resResult;
|
||||
}
|
||||
@@ -634,6 +637,9 @@ export class GuildHandler {
|
||||
let chatSid = await getGuildChannelSid(code);
|
||||
this.app.rpc.chat.guildRemote.addRec.toServer(chatSid,rec);
|
||||
this.app.rpc.chat.guildRemote.memberQuit.toServer(chatSid,code, roleId, guild, sid);
|
||||
|
||||
session.set('guildCode', '');
|
||||
session.push('guild', () => {});
|
||||
|
||||
return resResult(STATUS.SUCCESS, { hasGuild: role.hasGuild });
|
||||
}
|
||||
|
||||
148
game-server/app/servers/role/handler/shopHandler.ts
Normal file
148
game-server/app/servers/role/handler/shopHandler.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { Application, BackendSession } from "pinus";
|
||||
import { gameData } from "../../../pubUtils/data";
|
||||
import { resResult } from "../../../pubUtils/util";
|
||||
import { STATUS, GUILD_STRUCTURE } from "../../../consts";
|
||||
import { DicShopListModel } from "../../../db/DicShopList";
|
||||
import { ShopItem } from "../../../domain/dbGeneral";
|
||||
import { ShopItemListParam } from '../../../domain/roleField/shop';
|
||||
import { UserShopModel } from "../../../db/UserShop";
|
||||
import { handleCost, addItems } from "../../../services/rewardService";
|
||||
import { GuildModel } from "../../../db/Guild";
|
||||
|
||||
export default function(app: Application) {
|
||||
return new ShopHandler(app);
|
||||
}
|
||||
|
||||
export class ShopHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
// 获得商品列表
|
||||
async getShopList(msg: { shopId: number }, session: BackendSession) {
|
||||
let roleId = session.get('roleId');
|
||||
let { shopId } = msg;
|
||||
|
||||
if(!gameData.shopList.has(shopId)) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
|
||||
let shopItemList = new Array<ShopItemListParam>(); // 返回
|
||||
let dbDicShop = await DicShopListModel.findByShopId(shopId);
|
||||
let userShopRecs = await UserShopModel.findMapByShopId(roleId, shopId);
|
||||
console.log(JSON.stringify([...userShopRecs]))
|
||||
|
||||
|
||||
if(!dbDicShop || dbDicShop.useJson) { // 完全使用json中配置的商品,数据库只做排序用,数据库内没有的排到最后
|
||||
let items = dbDicShop?.items||[];
|
||||
let map = new Map<number, ShopItem>();
|
||||
for(let item of items) {
|
||||
map.set(item.id, item);
|
||||
}
|
||||
let dicShop = gameData.shop.get(shopId)||[];
|
||||
for(let { id, type } of dicShop) {
|
||||
let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;
|
||||
if(map.has(id)) {
|
||||
let item = map.get(id);
|
||||
let param = new ShopItemListParam(id, item.discount, type, buyCount, item.order);
|
||||
shopItemList.push(param);
|
||||
} else {
|
||||
let param = new ShopItemListParam(id, 1, type, buyCount, 0);
|
||||
shopItemList.push(param);
|
||||
}
|
||||
}
|
||||
|
||||
} else { // 只返回数据库内的商品
|
||||
let items = dbDicShop?.items||[];
|
||||
for(let item of items) {
|
||||
let { id, order, discount } = item;
|
||||
let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;
|
||||
let dicShop = gameData.shopItem.get(id);
|
||||
if(dicShop) {
|
||||
let param = new ShopItemListParam(id, discount, dicShop.type, buyCount, order);
|
||||
shopItemList.push(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shopItemList = shopItemList.sort((a, b) => b.order - a.order);
|
||||
|
||||
return resResult(STATUS.SUCCESS, { shopItemList });
|
||||
}
|
||||
|
||||
// 购买商品
|
||||
async purchase(msg: { shopItemId: number, count: number }, session: BackendSession) {
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let sid = session.get('sid');
|
||||
let guildCode = session.get('guildCode');
|
||||
let serverId = session.get('serverId');
|
||||
|
||||
let { shopItemId, count } = msg;
|
||||
|
||||
let dicShopItem = gameData.shopItem.get(shopItemId);
|
||||
if(!dicShopItem) return resResult(STATUS.DIC_DATA_NOT_FOUND);
|
||||
|
||||
if(dicShopItem.guildLvLimit) {
|
||||
let myGuild = await GuildModel.findByCode(guildCode, serverId);
|
||||
if(!myGuild) return resResult(STATUS.GUILD_LV_LIMIT);
|
||||
let { structure } = myGuild;
|
||||
let curStructure = structure.find(cur => cur.id == GUILD_STRUCTURE.STORE);
|
||||
if(!curStructure || curStructure.lv < dicShopItem.guildLvLimit) {
|
||||
return resResult(STATUS.GUILD_LV_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
let dbShop = await DicShopListModel.findByShopId(dicShopItem.shop);
|
||||
|
||||
let discount = 1;
|
||||
if(dbShop) {
|
||||
let item = dbShop.items.find(cur => cur.id == shopItemId);
|
||||
if(item) {
|
||||
discount = item.discount;
|
||||
}
|
||||
}
|
||||
|
||||
let userShop = await UserShopModel.findByRoleAndItem(roleId, dicShopItem.id);
|
||||
if(userShop && userShop.count + count > dicShopItem.purchaseLimit) {
|
||||
return resResult(STATUS.BUY_COUNT_OVER);
|
||||
}
|
||||
|
||||
// 消耗
|
||||
let cost = [{
|
||||
id: dicShopItem.money,
|
||||
count: Math.round(dicShopItem.price * count * discount)
|
||||
}];
|
||||
let costResult = await handleCost(roleId, sid, cost);
|
||||
if(!costResult) return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
|
||||
|
||||
// 次数
|
||||
userShop = await UserShopModel.purchase(roleId, roleName, dicShopItem, count);
|
||||
if(!userShop) return resResult(STATUS.BUY_COUNT_OVER);
|
||||
|
||||
// 获得
|
||||
let reward = [{
|
||||
id: dicShopItem.goodid,
|
||||
count
|
||||
}];
|
||||
let goods = await addItems(roleId, roleName, sid, reward);
|
||||
|
||||
return resResult(STATUS.SUCCESS, {
|
||||
shopItemId, count,
|
||||
goods
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// 将魂回收
|
||||
async recycleSoul(msg: { goodsId: number, count: number }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
// !测试接口,初始化:将json表中的内容加入数据库内
|
||||
async initShopList() {
|
||||
let index = gameData.shopItem.size;
|
||||
for(let [id, dic] of gameData.shopItem) {
|
||||
await DicShopListModel.createShop(dic.shop, true, { id, order: index--, discount: 1 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,18 +174,18 @@ export async function getRaceActivityRank(guildCode: string, serverId: number) {
|
||||
let guildRank = new Array<SimpleGuildRankWithTimeParam>();
|
||||
for(let { rank, code, name, num } of guildRankResult.ranks) {
|
||||
let _obj = await obj.getWoodenHorse(code, serverId);
|
||||
let param = new SimpleGuildRankWithTimeParam(rank, code, name, num, _obj?_obj.time:0, _obj?_obj.durability:0);
|
||||
let param = new SimpleGuildRankWithTimeParam(rank, code, name, _obj);
|
||||
guildRank.push(param);
|
||||
}
|
||||
let myGuildRank: SimpleGuildRankWithTimeParam;
|
||||
if(guildRankResult.myRank) {
|
||||
let { rank, code, name, num } = guildRankResult.myRank;
|
||||
let _obj = await obj.getWoodenHorse(code, serverId);
|
||||
myGuildRank = new SimpleGuildRankWithTimeParam(rank, code, name, num, _obj?_obj.time:0, _obj?_obj.durability:0);
|
||||
myGuildRank = new SimpleGuildRankWithTimeParam(rank, code, name, _obj);
|
||||
} else {
|
||||
let guild = await GuildModel.findByCode(guildCode, serverId, 'name');
|
||||
let _obj = await obj.getWoodenHorse(guildCode, serverId);
|
||||
myGuildRank = new SimpleGuildRankWithTimeParam(0, guildCode, guild?.name, 0, _obj?_obj.time:0, _obj?_obj.durability:0);
|
||||
myGuildRank = new SimpleGuildRankWithTimeParam(0, guildCode, guild?.name, _obj);
|
||||
}
|
||||
return { guildRank, myGuildRank }
|
||||
}
|
||||
@@ -317,6 +317,7 @@ export async function sendGuildActEndMsg(aid: number) {
|
||||
await raceActivitySettleReward(guildCode, woodenHorse);
|
||||
}
|
||||
}
|
||||
raceActivityObj = new RaceActivityObject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -588,7 +589,7 @@ export async function calWoodenHorseAndSend(serverId: number) {
|
||||
for(let i = 0; i < l; i++) {
|
||||
let { rank, code, name, num } = ranks[i]
|
||||
let _obj = await obj.getWoodenHorse(code, serverId);
|
||||
let param = new SimpleGuildRankWithTimeParam(rank, code, name, _obj?_obj.distance:0, _obj?_obj.time:0, _obj?_obj.durability:0);
|
||||
let param = new SimpleGuildRankWithTimeParam(rank, code, name, _obj);
|
||||
guildRank.push(param);
|
||||
}
|
||||
|
||||
@@ -617,7 +618,7 @@ export async function calWoodenHorseAndSend(serverId: number) {
|
||||
console.log('send', woodenHorseList.length);
|
||||
let curRank = ranks[rank];
|
||||
let wh = await obj.getWoodenHorse(curRank.code, serverId);
|
||||
let myGuildRank = new SimpleGuildRankWithTimeParam(curRank.rank, curRank.code, curRank.name, curRank.num, wh?wh.time:0, wh?wh.durability:0);
|
||||
let myGuildRank = new SimpleGuildRankWithTimeParam(curRank.rank, curRank.code, curRank.name, wh);
|
||||
|
||||
let chatSid = await getGuildChannelSid(curRank.code);
|
||||
let events = obj.getEvents(curRank.code, wh?wh.distance:0);
|
||||
|
||||
@@ -7,7 +7,7 @@ import { UserGuildModel, UserGuildType } from "../db/UserGuild";
|
||||
import { UserGuildApplyModel } from "../db/UserGuildApply";
|
||||
import { SystemConfigModel } from "../db/SystemConfig";
|
||||
import { nowSeconds } from "../pubUtils/timeUtil";
|
||||
import { pinus } from "pinus";
|
||||
import { pinus, BackendSession } from "pinus";
|
||||
import { ARMY } from "../pubUtils/dicParam";
|
||||
import { sendMail } from "./mailService";
|
||||
import { setRank, getMyRank, initSingleRank, getRoleOnlineInfo } from "./redisService";
|
||||
@@ -40,7 +40,7 @@ export async function checkAuth(func: number, roleId: string, code?: string, use
|
||||
* @param lv 公会当前等级,判断人数用
|
||||
* @param roleId 加入的玩家
|
||||
*/
|
||||
export async function joinGuild(code: string, guildName: string, lv: number, roleId: string, serverId: number) {
|
||||
export async function joinGuild(code: string, guildName: string, lv: number, roleId: string, serverId: number, session: BackendSession) {
|
||||
|
||||
// 周结算锁
|
||||
let weeklySumLock = await lockDataNoRetry(serverId, DATA_NAME.WEEKLY_GUILD_SUM, code);
|
||||
@@ -86,6 +86,9 @@ export async function joinGuild(code: string, guildName: string, lv: number, rol
|
||||
if (sid) {
|
||||
await addRoleToGuildChannel(roleId, sid, code);
|
||||
}
|
||||
|
||||
session.set('guildCode', code);
|
||||
session.push('guildCode', () => {});
|
||||
return { status: 0, guild, userGuild, roleName: role.roleName, memberCnt: guild.memberCnt, guildCe: guild.guildCe }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user