StoreAction.lua 2.79 KB
local _M = {}

function _M.rechargeRpc(agent , data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local id = msg.id
	local dataSet = csvdb["shop_rechargeCsv"][id]
	if not dataSet then return end
	local diamondCount
	if dataSet.type == 0 then -- 钻石
		local rechargeF = role:getProperty("rechargeF")
		diamondCount = dataSet.diamond + dataSet.diamondExtra
		if not rechargeF[id] then
			diamondCount = diamondCount + dataSet.diamondFirst
			rechargeF[id] = 1
			role:updateProperty({field = "rechargeF", value = rechargeF})
		end
		role:gainDiamond({count = diamondCount, isRecharge = true})
	elseif dataSet.type == 1 then  --月卡
		return
	elseif dataSet.type == 2 then  -- 赛季通行证
		return
	else
		return
	end

	-- 累充
	local rmb = dataSet.rmb
	role:updateProperty({field = "rmbC", delta = rmb})
	
	SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({diamond = diamondCount}))
	return true
end


function _M.dailyBuyRpc(agent , data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local id = msg.id
	local count = msg.count or 1

	local dataSet = csvdb["shop_diamondCsv"][id]
	if not dataSet then return 1 end

	local dailySDC = role.dailyData:getProperty("dailySDC")

	if math.illegalNum(count, 1, (dataSet.limit == 0 and math.huge or dataSet.limit - (dailySDC[id] or 0))) then return 1 end

	local cost = dataSet.cost
	if dataSet.type == 0 then
		local dailySDD = role.dailyData:getProperty("dailySDD")
		if dailySDD[id] then -- 折扣
			cost = math.ceil(cost * (1 - dataSet.disount / 100))
		end
	elseif dataSet.type == 1 then
	else
		return 3 
	end

	if not role:costDiamond({count = cost * count}) then
		return 4 
	end

	if dataSet.limit ~= 0 then
		dailySDC[id] = (dailySDC[id] or 0) + count
		role.dailyData:updateProperty({field = "dailySDC", value = dailySDC})
	end
	local reward = role:award(dataSet.gift)

	SendPacket(actionCodes.Store_dailyBuyRpc, MsgPack.pack({reward = reward}))
	return true
end


function _M.dinerBuyRpc(agent , data)
	local role = agent.role
	local msg = MsgPack.unpack(data)
	local id = msg.id
	local count = msg.count or 1

	local dataSet = csvdb["shop_dinerCsv"][id]
	if not dataSet then return end

	local dinerS = role:getProperty("dinerS")
	if math.illegalNum(count, 1, (dataSet.limit == 0 and math.huge or dataSet.limit - (dinerS[id] or 0))) then return 1 end

	local cost = {[ItemId.DinerCoin] = dataSet.cost}
	if not role:checkItemEnough(cost) then return end

	if dataSet.limit ~= 0 then
		dinerS[id] = (dinerS[id] or 0) + count
		role:updateProperty({field = "dinerS", value = dinerS})
	end

	role:costItems(cost)

	local gift = {}
	for _id, _count in pairs(dataSet.gift:toNumMap()) do
		gift[_id] = _count * count
	end
	local reward = role:award(gift)

	SendPacket(actionCodes.Store_dinerBuyRpc, MsgPack.pack({reward = reward}))
	return true
end

return _M