Blame view

src/actions/EmailAction.lua 6.44 KB
dc9d814f   zhouhaihai   邮件
1
2
3
4
5
  local _M = {}
  local tarr2tab = table.array2Table
  
  local function loadEmails(roleId)
  	local emails = {}
2ca93972   liuzujun   添加邮件表
6
7
8
9
  	local res = mysqlproxy:query(string.format("SELECT * FROM `Email` WHERE `roleId` = %s", roleId))
  	for _, data in ipairs(res) do
  		local email = require("models.Email").new({key = string.format("%d",data.id), id=data.id})
  		if email:load(data) then
dc9d814f   zhouhaihai   邮件
10
11
12
13
14
15
  			table.insert(emails, email)
  		end
  	end
  	return emails
  end
  
2ca93972   liuzujun   添加邮件表
16
  local function delExpireEmails(roleId)
e9bfa7a2   liuzujun   其他玩家展示界面数据支持, 更新s...
17
18
19
20
21
22
23
24
25
26
27
28
  	--local sql = [[
  	--	DELETE FROM Email
  	--	WHERE id IN (
  	--		SELECT id FROM (
  	--		  SELECT id
  	--		  FROM Email WHERE roleId=%s
  	--		  ORDER BY createtime DESC, id desc
  	--		  LIMIT %s, 100000
  	--		) a
  	--	);
  	--]]
      local sql = "SELECT id FROM Email WHERE roleId=%s ORDER BY createtime DESC, id desc LIMIT %s, 100000"
2ca93972   liuzujun   添加邮件表
29
  	sql = string.format(sql, roleId, EMAIL_LIMIT)
e9bfa7a2   liuzujun   其他玩家展示界面数据支持, 更新s...
30
31
32
33
34
  	local res = mysqlproxy:query(sql)
      local tmp = {}
      for _, v in ipairs(res) do
          table.insert(tmp, v.id)
      end
b2ff0064   liuzujun   优化删除过期邮件mysql
35
36
37
      if next(tmp) then
          mysqlproxy:query(string.format("DELETE FROM Email WHERE id IN (%s)", table.concat(tmp, ",")))
      end
2ca93972   liuzujun   添加邮件表
38
39
  end
  
dc9d814f   zhouhaihai   邮件
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  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)
62ed4275   liuzujun   添加新的延時類型郵件
61
62
63
64
65
66
67
68
69
  			-- 0 需要判斷創角時間小於郵件創建時間 1 只需要在時間段內登陸即可領取
  			local delayType = tonum(email.delayType)
  			local flag = false
  			if delayType == 1 then
  				flag = skynet.timex() > tonum(email.createtime)
  			else 
  				flag = tonum(email.createtime) > role:getProperty("ctime")
  			end
  			if flag and ( not email.mid or tonum(email.mid) == mid ) 
dc9d814f   zhouhaihai   邮件
70
  				and ( not email.endtime or tonum(email.endtime) > now )then
1fc9c12a   zhouhaihai   or
71
  				local time = math.max(tonum(email.timestamp, 0) , tonum(email.createtime))
2ca93972   liuzujun   添加邮件表
72
  				mysqlproxy:insertEmail({
dc9d814f   zhouhaihai   邮件
73
74
75
76
  					roleId = roleId, 
  					emailId = 0,
  					createtime = time,
  					title = email.title, 
eb4b0152   zhouhaihai   邮件增加 stitle
77
  					stitle = email.stitle, 
dc9d814f   zhouhaihai   邮件
78
79
80
  					content = email.content, 
  					attachments = email.attachments
  				})
f22a33af   zhouhaihai   自己的日志
81
  				role:mylog("mail_action", {desc = "get_global", key1 = email.title, key2 = email.attachments})
dc9d814f   zhouhaihai   邮件
82
83
84
  			end
  		end
  	end
2ca93972   liuzujun   添加邮件表
85
  	delExpireEmails(roleId)
dc9d814f   zhouhaihai   邮件
86
87
88
89
90
91
  
  	local emails = loadEmails(roleId)
  	for _, email in ipairs(emails) do
  		table.insert(result, email:data())
  	end
  
94edf97b   zhouhaihai   邮件
92
  	SendPacket(actionCodes.Email_listRpc, MsgPack.pack({list = result}))
dc9d814f   zhouhaihai   邮件
93
94
95
96
97
98
99
100
101
102
103
104
  	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
ebbed4db   zhangqijia   fix: 获得邮件奖励的bug
105
106
107
108
109
  			if next(rewardPms) then
  				attachments = emailData.attachment:format(table.unpack(rewardPms))
  			else
  				attachments = emailData.attachment
  			end
dc9d814f   zhouhaihai   邮件
110
111
112
113
114
115
116
117
118
  		end
  	end
  	return attachments
  end
  
  function _M.drawAllAttachRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
  
7bb30dca   zhouhaihai   修改发奖
119
  	local reward, change = {}
dc9d814f   zhouhaihai   邮件
120
121
122
  	local ids = {}
  
  	local emails = loadEmails(roleId)
39bcd7ca   zhouhaihai   LOG
123
124
125
  	for _, email in ipairs(emails) do
  		local attachments = getEmailAttachments(email)
  		if attachments ~= "" then
6136eaca   liuzujun   添加好友表
126
  			email:setProperty("status", 2, true)
39bcd7ca   zhouhaihai   LOG
127
  			email:log(role, 2)
a3e69a31   zhouhaihai   缺了 id 来源
128
  			ids[email:getProperty("id")] = 1
39bcd7ca   zhouhaihai   LOG
129
130
  			for key, v in pairs(attachments:toNumMap()) do
  				reward[key] = (reward[key] or 0) + v
dc9d814f   zhouhaihai   邮件
131
  			end
f22a33af   zhouhaihai   自己的日志
132
  			role:mylog("mail_action", {desc = "draw_attach", int1 = email:getProperty("emailId"), key1 = email:getProperty("title"), key2 = attachments})
dc9d814f   zhouhaihai   邮件
133
  		end
39bcd7ca   zhouhaihai   LOG
134
  	end
e2e205be   zhangqijia   feat: 玩家等级奖励
135
136
  	if role:checkRuneFullyByReward(reward) then return 1 end
  
7bb30dca   zhouhaihai   修改发奖
137
138
  	reward, change = role:award(reward, {log = {desc = "draw_attach"}})
  	SendPacket(actionCodes.Email_drawAllAttachRpc, MsgPack.pack({ids = ids, reward = reward, change = change}))
dc9d814f   zhouhaihai   邮件
139
140
141
142
143
144
145
146
147
  	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
  
dc9d814f   zhouhaihai   邮件
148
  
2ca93972   liuzujun   添加邮件表
149
  	local email = require("models.Email").new({key = string.format("%d", id), id = id})
70fb62d1   liuzujun   服务器数据打点
150
  	email.owner = role
dc9d814f   zhouhaihai   邮件
151
152
153
154
155
  	if not email:load() then return end
  
  	local attachments = getEmailAttachments(email)
  	if attachments == "" then return end
  
e2e205be   zhangqijia   feat: 玩家等级奖励
156
157
158
159
160
161
162
163
  	local reward, change = {}
  	if type(attachments) == "string" then
  		for key, v in pairs(attachments:toNumMap()) do
  			reward[key] = (reward[key] or 0) + v
  		end
  	else
  		reward = attachments
  	end
76f0493d   zhangqijia   feat: 铭文仓库溢出奖励时,通...
164
  	if role:checkRuneFullyByReward(reward) then return 1 end
e2e205be   zhangqijia   feat: 玩家等级奖励
165
166
  	reward, change = role:award(reward, {log = {desc = "draw_attach", int1 = id}})
  
76f0493d   zhangqijia   feat: 铭文仓库溢出奖励时,通...
167
  
6136eaca   liuzujun   添加好友表
168
  	email:setProperty("status", 2, true)
39bcd7ca   zhouhaihai   LOG
169
  	email:log(role, 2)
7bb30dca   zhouhaihai   修改发奖
170
  	SendPacket(actionCodes.Email_drawAttachRpc, MsgPack.pack({reward = reward, change = change}))
70fb62d1   liuzujun   服务器数据打点
171
  	role:mylog("mail_action", {desc = "draw_attach", int1 = email:getProperty("emailId"), key1 = email:getProperty("title"), key2 = attachments})
dc9d814f   zhouhaihai   邮件
172
173
174
175
176
177
178
179
180
181
  	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
  
2ca93972   liuzujun   添加邮件表
182
  	local email = require("models.Email").new({key = string.format("%d", id), id = id})
39bcd7ca   zhouhaihai   LOG
183
  	if not email:load() then return end
dc9d814f   zhouhaihai   邮件
184
  
6136eaca   liuzujun   添加好友表
185
  	email:setProperty("status", 1, true)
39bcd7ca   zhouhaihai   LOG
186
  	email:log(role, 1)
f22a33af   zhouhaihai   自己的日志
187
  	role:mylog("mail_action", {desc = "check_mail", int1 = id})
dc9d814f   zhouhaihai   邮件
188
189
190
191
192
193
194
195
  
  	SendPacket(actionCodes.Email_checkRpc, '')
  	return true
  end
  
  function _M.delRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
dc9d814f   zhouhaihai   邮件
196
  	local result = {}
2ca93972   liuzujun   添加邮件表
197
198
199
200
201
202
203
204
205
  	local tmp = {}
  	local emails = loadEmails(roleId)
  	for _, email in ipairs(emails) do
  		local attachments = getEmailAttachments(email)
  
  		if email:getProperty("status") == 2 or (attachments == "" and email:getProperty("status") == 1) then
  			result[email:getProperty("id")] = 1
  			email:log(role, 3)
  			table.insert(tmp, email:getProperty("id"))
dc9d814f   zhouhaihai   邮件
206
  		end
2ca93972   liuzujun   添加邮件表
207
208
209
210
  	end
  
  	mysqlproxy:query(string.format("DELETE FROM `Email` WHERE `id` in (%s)", table.concat(tmp, ",")))
  
f22a33af   zhouhaihai   自己的日志
211
212
213
  	for delId, _ in ipairs(result) do
  		role:mylog("mail_action", {desc = "del_mail", int1 = delId})
  	end
dc9d814f   zhouhaihai   邮件
214
215
216
217
218
  	SendPacket(actionCodes.Email_delRpc, MsgPack.pack({result = result}))
  	return true
  end
  
  return _M