You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.8 KiB
Lua

local skynet = require "skynet"
local cjson = require "cjson"
local dateutils = require "dateutils"
local activitylogic = require "activitylogic"
require "functions"
local baoxiangmodel = {}
--t1, t2, t3
--[[
t1 = 购买
t2 = 领取
t3 = 分
]]
function baoxiangmodel:init(t1, t2, t3, t4, start_time, end_time)
self.buys = {}
t1 = t1 or {}
for k, v in pairs(t1) do
self.buys[k] = checkint(v)
end
self.receivebuys = {}
t2 = t2 or {}
for k, v in pairs(t2) do
self.receivebuys[k] = checkint(v)
end
self.receives = {}
t3 = t3 or {}
for k, v in pairs(t3) do
self.receives[k] = checkint(v)
end
self.score = checkint(t4)
self.start_time = start_time
self.end_time = end_time
end
function baoxiangmodel:incrscore(val)
local rounds = activitylogic.getbaoxiangroundconf()
local round = 1
local score = 0
local myscore = 0
for k,v in pairs(rounds) do
score = score + v
if self.score < score then
myscore = self.score - (score - v)
break
end
round = round + 1
end
local cfg = activitylogic.getbaoxiangconfbyround(round)
local last_score = myscore
local cur_score = myscore + val
for i,v in ipairs(cfg) do
if last_score < v.points and cur_score >= v.points then
local ok = pcall(skynet.call, ".maild", "lua", "baoxiangactivity", UID, v.rewards)
end
end
self.score = self.score + val
local ok = skynet.call(".activityd", "lua", "incrbaoxiangscore", UID, val)
end
function baoxiangmodel:checkbuy(id)
id = checkint(id)
local cfg = activitylogic.getbaoxiangshangchengconfbyid(id)
local buy_num = self.buys[tostring(id)] or 0
local receive_num = self.receivebuys[tostring(id)] or 0
if cfg.price <= 0 then
buy_num = cfg.limit
end
if receive_num < buy_num then
return true
end
return false
end
function baoxiangmodel:checkreceive(id)
id = checkint(id)
local cfg = activitylogic.getbaoxiangconfbyid(id)
if not self.receives[tostring(id)] and cfg.points >= self.socre then
return true
end
return false
end
function baoxiangmodel:receive(id)
id = checkint(id)
self.receives[tostring(id)] = os.time()
local ok = skynet.call(".activityd", "lua", "receivebaoxiang", UID, id)
end
function baoxiangmodel:buy(id)
id = checkint(id)
self.buys[tostring(id)] = self.buys[tostring(id)] or 0
self.buys[tostring(id)] = self.buys[tostring(id)] + 1
local ok = skynet.call(".activityd", "lua", "buybaoxiang", UID, id, 1)
end
function baoxiangmodel:checkreceivebuy(id)
id = checkint(id)
local cfg = activitylogic.getbaoxiangshangchengconfbyid(id)
local buy_num = self.buys[tostring(id)] or 0
if cfg.price <= 0 then
buy_num = cfg.limit
end
local receive_num = self.receivebuys[tostring(id)] or 0
if buy_num > receive_num then
return true
end
return false
end
function baoxiangmodel:receivebuy(id)
id = checkint(id)
self.receivebuys[tostring(id)] = self.receivebuys[tostring(id)] or 0
self.receivebuys[tostring(id)] = self.receivebuys[tostring(id)] + 1
local ok = skynet.call(".activityd", "lua", "receivebuybaoxiang", UID, id, 1)
end
function baoxiangmodel:serialize()
local resp = {}
local infos = {}
local cfg = activitylogic.getbaoxiangshangchengconf()
for k,v in pairs(cfg) do
local buy_num = self.buys[k] or 0
if v.price <= 0 then
buy_num = v.limit
end
infos[k] = {buy_num=buy_num, receive_num=self.receivebuys[k] or 0}
end
resp.tabs = {{id=201, infos = infos }, {id=202, infos = self.receives}}
resp.score = self.score
resp.start_time = self.start_time
resp.end_time = self.end_time
return resp
end
return baoxiangmodel