Blame view

src/actions/EmailAction.lua 5.15 KB
dc9d814f   zhouhaihai   邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  local _M = {}
  local tarr2tab = table.array2Table
  
  local function loadEmails(roleId)
  	local emails = {}
  	local rds = string.format(R_EMAIL, roleId)
  	local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1)
  	local redret = redisproxy:pipelining(function (red)
  		for _, id in ipairs(ids) do
  			red:hgetall(string.format("email:%d:%s", roleId, id))
  		end
  	end)
  	for index, id in ipairs(ids) do
  		local email = require("models.Email").new({key = string.format("email:%d:%s", roleId, id)})
  		if email:load(tarr2tab(redret[index])) then
  			table.insert(emails, email)
  		end
  	end
  	return emails
  end
  
  function _M.listRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  	local now = skynet.timex()
  	local result = {}
  	local mid = role:getProperty("sid")
  
  	local globalEmail = redisproxy:hget("autoincrement_set", "email")
  	globalEmail = tonum(globalEmail)
  	local emailSync = role:getProperty("emailSync")
  	if globalEmail > emailSync then
  		role:setProperty("emailSync", globalEmail)
  		emailSync = math.max(emailSync + 1, globalEmail - EMAIL_LIMIT + 1)
  		local result = redisproxy:pipelining(function (red)
  			for id = emailSync, globalEmail do
  				red:hgetall(string.format("globalEmail:%s", id))		
  			end
  		end)
  		local count = 1
  		for _, data in ipairs(result) do
  			local email = tarr2tab(data)
  			if tonum(email.createtime) > role:getProperty("ctime") 
  				and ( not email.mid or tonum(email.mid) == mid ) 
  				and ( not email.endtime or tonum(email.endtime) > now )then
a289c34c   zhouhaihai   创建时间戳
46
  				local time = math.max(tonum(email.timestamp, 0) or email.createtime)
dc9d814f   zhouhaihai   邮件
47
48
49
50
51
  				redisproxy:insertEmail({
  					roleId = roleId, 
  					emailId = 0,
  					createtime = time,
  					title = email.title, 
eb4b0152   zhouhaihai   邮件增加 stitle
52
  					stitle = email.stitle, 
dc9d814f   zhouhaihai   邮件
53
54
55
  					content = email.content, 
  					attachments = email.attachments
  				})
dc9d814f   zhouhaihai   邮件
56
57
58
59
60
61
62
63
64
  			end
  		end
  	end
  
  	local emails = loadEmails(roleId)
  	for _, email in ipairs(emails) do
  		table.insert(result, email:data())
  	end
  
94edf97b   zhouhaihai   邮件
65
  	SendPacket(actionCodes.Email_listRpc, MsgPack.pack({list = result}))
dc9d814f   zhouhaihai   邮件
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  	return true
  end
  
  local function getEmailAttachments( email )
  	if email:getProperty("status") == 2 then
  		return ""
  	end
  	local attachments = email:getProperty("attachments")
  	if attachments:len() == 0 then
  		local rewardPms = email:getProperty("rewardPms")
  		local emailData = csvdb["emailCsv"][email:getProperty("emailId")]
  		if emailData then
  			attachments = emailData.attachment:format(table.unpack(rewardPms))
  		end
  	end
  	return attachments
  end
  
  function _M.drawAllAttachRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  
7bb30dca   zhouhaihai   修改发奖
88
  	local reward, change = {}
dc9d814f   zhouhaihai   邮件
89
90
91
  	local ids = {}
  
  	local emails = loadEmails(roleId)
39bcd7ca   zhouhaihai   LOG
92
93
94
95
96
  	for _, email in ipairs(emails) do
  		local attachments = getEmailAttachments(email)
  		if attachments ~= "" then
  			email:setProperty("status", 2)
  			email:log(role, 2)
a3e69a31   zhouhaihai   缺了 id 来源
97
  			ids[email:getProperty("id")] = 1
39bcd7ca   zhouhaihai   LOG
98
99
  			for key, v in pairs(attachments:toNumMap()) do
  				reward[key] = (reward[key] or 0) + v
dc9d814f   zhouhaihai   邮件
100
101
  			end
  		end
39bcd7ca   zhouhaihai   LOG
102
  	end
7bb30dca   zhouhaihai   修改发奖
103
104
  	reward, change = role:award(reward, {log = {desc = "draw_attach"}})
  	SendPacket(actionCodes.Email_drawAllAttachRpc, MsgPack.pack({ids = ids, reward = reward, change = change}))
dc9d814f   zhouhaihai   邮件
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  	return true
  end
  
  function _M.drawAttachRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  	local msg = MsgPack.unpack(data)
  	local id = msg.id
  
  	local rds = string.format(R_EMAIL_ITEM, roleId, id)
  
  	local email = require("models.Email").new({key = rds})
  	if not email:load() then return end
  
  	local attachments = getEmailAttachments(email)
  	if attachments == "" then return end
  
44baeb7a   zhouhaihai   漏掉了 bug
122
  	local reward, change = role:award(attachments, {log = {desc = "draw_attach", int1 = emailId}})
39bcd7ca   zhouhaihai   LOG
123
124
  	email:setProperty("status", 2)
  	email:log(role, 2)
7bb30dca   zhouhaihai   修改发奖
125
  	SendPacket(actionCodes.Email_drawAttachRpc, MsgPack.pack({reward = reward, change = change}))
dc9d814f   zhouhaihai   邮件
126
  
dc9d814f   zhouhaihai   邮件
127
128
129
130
131
132
133
134
135
136
  	return true
  end
  
  function _M.checkRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  
  	local msg = MsgPack.unpack(data)
  	local id = msg.id
  
44baeb7a   zhouhaihai   漏掉了 bug
137
138
  	local emailRds = string.format("email:%d:%s", roleId, id)
  	local email = require("models.Email").new({key = emailRds})
39bcd7ca   zhouhaihai   LOG
139
  	if not email:load() then return end
dc9d814f   zhouhaihai   邮件
140
  
44baeb7a   zhouhaihai   漏掉了 bug
141
  	email:setProperty("status", 1)
39bcd7ca   zhouhaihai   LOG
142
  	email:log(role, 1)
dc9d814f   zhouhaihai   邮件
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  
  	SendPacket(actionCodes.Email_checkRpc, '')
  	return true
  end
  
  function _M.delRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  
  	local rds = string.format(R_EMAIL, roleId)
  	local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1)
  	local now = skynet.timex()
  	local result = {}
  	local emailSet = csvdb["emailCsv"]
  
  	redisproxy:pipelining(function (red)
  		for _, id in ipairs(ids) do
  			local emailRds = string.format("email:%d:%s", roleId, id)
  			local email = require("models.Email").new({key = emailRds})
  			if email:load() then
  				local status = email:getProperty("status")
  				local attachments = email:getProperty("attachments")
  				local emailData = emailSet[email:getProperty("emailId")]
  				if emailData and attachments:len() == 0 then
  					attachments = emailData.attachment
  				end
  				if status == 2 or (status == 1 and attachments:len() == 0) then
39bcd7ca   zhouhaihai   LOG
170
  					email:log(role, 3)
dc9d814f   zhouhaihai   邮件
171
172
173
174
175
176
177
  					red:lrem(rds, 0, id)
  					red:del(emailRds)
  					result[tonum(id)] = 1
  				end
  			end
  		end
  	end)
dc9d814f   zhouhaihai   邮件
178
179
180
181
182
  	SendPacket(actionCodes.Email_delRpc, MsgPack.pack({result = result}))
  	return true
  end
  
  return _M