Commit 312b9db56e705df16efa6f205cec67c802103358
1 parent
9962b061
่ๅ
Showing
6 changed files
with
205 additions
and
9 deletions
Show diff stats
src/ProtocolCode.lua
... | ... | @@ -21,6 +21,8 @@ actionCodes = { |
21 | 21 | Role_updateItems = 107, |
22 | 22 | Role_changeUpdate = 108, |
23 | 23 | Role_pipelining = 109, |
24 | + Role_saleItemRpc = 110, | |
25 | + Role_openItemRpc = 111, | |
24 | 26 | |
25 | 27 | Adv_startAdvRpc = 151, |
26 | 28 | Adv_roleFormatRpc = 152, |
... | ... | @@ -43,6 +45,7 @@ actionCodes = { |
43 | 45 | Hero_loveItemRpc = 212, |
44 | 46 | Hero_loveTaskRpc = 213, |
45 | 47 | Hero_changeSkinRpc = 214, |
48 | + Hero_createHeroRpc = 215, | |
46 | 49 | |
47 | 50 | Hang_startRpc = 251, |
48 | 51 | Hang_checkRpc = 252, | ... | ... |
src/actions/HeroAction.lua
... | ... | @@ -404,4 +404,25 @@ function _M.changeSkinRpc(agent, data) |
404 | 404 | return true |
405 | 405 | end |
406 | 406 | |
407 | +function _M.createHeroRpc(agent, data) | |
408 | + local role = agent.role | |
409 | + local msg = MsgPack.unpack(data) | |
410 | + local heroType = msg.heroType | |
411 | + local unitData = csvdb["unitCsv"][heroType] | |
412 | + if not unitData then return end | |
413 | + local cost = globalCsv.unit_fragment_cost[unitData["rare"]] | |
414 | + if not cost then return end | |
415 | + if role:getItemCount(heroType) < cost then return end | |
416 | + | |
417 | + for _, hero in pairs(role.heros) do | |
418 | + if hero:getProperty("type") == heroType then return end | |
419 | + end | |
420 | + | |
421 | + role:costItems({[heroType] = cost}, {}) | |
422 | + role:award({[heroType + ItemStartId.Hero] = 1}, {}) | |
423 | + | |
424 | + SendPacket(actionCodes.Hero_createHeroRpc, "") | |
425 | + return true | |
426 | +end | |
427 | + | |
407 | 428 | return _M |
408 | 429 | \ No newline at end of file | ... | ... |
src/actions/RoleAction.lua
... | ... | @@ -246,4 +246,58 @@ function _M.syncTimeRpc(agent, data) |
246 | 246 | return true |
247 | 247 | end |
248 | 248 | |
249 | +function _M.saleItemRpc(agent, data) | |
250 | + local role = agent.role | |
251 | + local msg = MsgPack.unpack(data) | |
252 | + local itemId = msg.itemId | |
253 | + local count = msg.count | |
254 | + if math.illegalNum(count, 1, role:getItemCount(itemId)) then return end | |
255 | + local itemData = csvdb["itemCsv"][itemId] | |
256 | + if itemData.sell_effect == "" then return end | |
257 | + local sellEffect = itemData.sell_effect:toArray(true, "=") | |
258 | + | |
259 | + role:costItems({[itemId] = count}, {}) | |
260 | + local reward = role:award({[sellEffect[1]] = sellEffect[2] * count}, {}) | |
261 | + | |
262 | + SendPacket(actionCodes.Role_saleItemRpc, MsgPack.pack({reward = reward})) | |
263 | + return true | |
264 | +end | |
265 | + | |
266 | +function _M.openItemRpc(agent, data) | |
267 | + local role = agent.role | |
268 | + local msg = MsgPack.unpack(data) | |
269 | + local itemId = msg.itemId | |
270 | + local count = msg.count | |
271 | + if math.illegalNum(count, 1, role:getItemCount(itemId)) then return end | |
272 | + local itemData = csvdb["itemCsv"][itemId] | |
273 | + if itemData.use_type ~= 2 then return end | |
274 | + local randomData = csvdb["item_randomCsv"][tonumber(itemData.use_effect)] | |
275 | + if not randomData then return end | |
276 | + | |
277 | + local reward = randomData.gift:toNumMap() | |
278 | + for _id, _count in pairs(reward) do | |
279 | + reward[_id] = _count * count | |
280 | + end | |
281 | + if randomData.random_num > 0 and randomData.random_gift ~= "" then | |
282 | + for i = 1, count do | |
283 | + local pool = {} | |
284 | + for _, temp in ipairs(randomData.random_gift:toArray()) do | |
285 | + table.insert(pool, temp:toArray(true, "=")) | |
286 | + end | |
287 | + local needCount = math.min(#pool, randomData.random_num) | |
288 | + for j = 1, needCount do | |
289 | + local idx = math.randWeight(pool, 3) | |
290 | + reward[pool[idx][1]] = (reward[pool[idx][1]] or 0) + pool[idx][2] | |
291 | + table.remove(pool, idx) | |
292 | + end | |
293 | + end | |
294 | + end | |
295 | + role:costItems({[itemId] = count}, {}) | |
296 | + reward = role:award(reward, {}) | |
297 | + | |
298 | + SendPacket(actionCodes.Role_openItemRpc, MsgPack.pack({reward = reward})) | |
299 | + return true | |
300 | +end | |
301 | + | |
302 | + | |
249 | 303 | return _M |
250 | 304 | \ No newline at end of file | ... | ... |
src/models/Role.lua
... | ... | @@ -28,12 +28,15 @@ Role.schema = { |
28 | 28 | ctime = {"number", skynet.timex()}, -- ๅๅปบๆถ้ด |
29 | 29 | ignoreMt = {"number", 0}, -- ๅฟฝ็ฅ็ปดๆคๆฆๆช |
30 | 30 | sversion = {"number", globalCsv.StructVersion or 0}, -- ้ๆดๆฐๆฎ็ๆฌ |
31 | - | |
31 | + diamond = {"number", 0}, | |
32 | + reDiamond = {"number", 0}, | |
32 | 33 | -- roleInfo |
33 | 34 | level = {"number", 0}, |
34 | 35 | items = {"string", ""}, |
35 | 36 | loveStatus = {"string", ""}, --็ป่ฎก่ง่ฒ็ๆ้ซ ๅฅฝๆๅบฆ็ญ็บง ็ฑปๅ็ธๅ ณ -- type=loveL type=loveL |
36 | 37 | |
38 | + bagLimit = {"table", globalCsv.store_limit_max}, | |
39 | + | |
37 | 40 | --ๅ้ฉ็ธๅ ณ |
38 | 41 | advPass = {"string", ""}, -- ้ๅ ณ่ฎฐๅฝ |
39 | 42 | advItems = {"string", ""}, -- ๅ้ฉไธดๆถ่ๅ |
... | ... | @@ -147,6 +150,8 @@ function Role:data() |
147 | 150 | level = self:getProperty("level"), |
148 | 151 | items = self:getProperty("items"):toNumMap(), |
149 | 152 | loveStatus = self:getProperty("loveStatus"):toNumMap(), |
153 | + diamond = self:getAllDiamond(), | |
154 | + bagLimit = self:getProperty("bagLimit"), | |
150 | 155 | |
151 | 156 | advPass = self:getProperty("advPass"), |
152 | 157 | advInfo = self:getProperty("advInfo"), | ... | ... |
src/models/RolePlugin.lua
... | ... | @@ -35,6 +35,29 @@ function RolePlugin.bind(Role) |
35 | 35 | |
36 | 36 | end |
37 | 37 | |
38 | + local function checkItemCount(self, itemId, count) | |
39 | + local itemData = csvdb["itemCsv"][itemId] | |
40 | + -- ็ง็ฑป ็ฑปๅๆฐ้้ๅถ | |
41 | + local page = globalCsv.store_type[itemData.type] | |
42 | + local limit = self:getProperty("bagLimit")[page] | |
43 | + if limit and self:getItemCount(itemId) == 0 then | |
44 | + local curCount = 0 | |
45 | + local items = self:getProperty("items"):toNumMap() | |
46 | + for _itemId, _count in pairs(items) do | |
47 | + local _itemData = csvdb["itemCsv"][itemId] | |
48 | + if globalCsv.store_type[_itemData.type] == page then | |
49 | + curCount = curCount + 1 | |
50 | + if curCount >= limit then | |
51 | + count = 0 | |
52 | + break | |
53 | + end | |
54 | + end | |
55 | + end | |
56 | + end | |
57 | + -- ๅ ถไป | |
58 | + | |
59 | + return count | |
60 | + end | |
38 | 61 | |
39 | 62 | local function _award(self, itemId, count, params) |
40 | 63 | local itemData = csvdb["itemCsv"][itemId] |
... | ... | @@ -49,13 +72,16 @@ function RolePlugin.bind(Role) |
49 | 72 | end |
50 | 73 | end, |
51 | 74 | } |
52 | - | |
53 | - if itemTypeAward[curType] then | |
54 | - itemTypeAward[curType]() | |
55 | - else | |
56 | - params.itemId = itemId | |
57 | - params.count = count | |
58 | - self:addItem(params) | |
75 | + -- ๅฏนๆฐ้็ญๆฅ | |
76 | + local count = checkItemCount(self, itemId, count) | |
77 | + if count ~= 0 then | |
78 | + if itemTypeAward[curType] then | |
79 | + itemTypeAward[curType]() | |
80 | + else | |
81 | + params.itemId = itemId | |
82 | + params.count = count | |
83 | + self:addItem(params) | |
84 | + end | |
59 | 85 | end |
60 | 86 | |
61 | 87 | return count, change -- count ๅทๆฐๅฎ้ ๅๆพ็ๅฅๅฑไธชๆฐ change ็ฉๅๅฎ้ ๅฅๅฑไธๅฝๅid ไธ็ฌฆ ๅฐฑๅ็่ฝฌๆข ่ไธๅฎ้ ๅๅฅ |
... | ... | @@ -83,7 +109,9 @@ function RolePlugin.bind(Role) |
83 | 109 | end |
84 | 110 | table.insert(allChange, {form = {[itemId] = count}, to = cr}) |
85 | 111 | else |
86 | - reward[itemId] = (reward[itemId] or 0) + count | |
112 | + if count > 0 then | |
113 | + reward[itemId] = (reward[itemId] or 0) + count | |
114 | + end | |
87 | 115 | end |
88 | 116 | end |
89 | 117 | |
... | ... | @@ -92,6 +120,10 @@ function RolePlugin.bind(Role) |
92 | 120 | |
93 | 121 | function Role:addItem(params) |
94 | 122 | params = params or {} |
123 | + if params.itemId == ItemId.Diamond then | |
124 | + self:gainDiamond(params) | |
125 | + return | |
126 | + end | |
95 | 127 | local items = self:getProperty("items") |
96 | 128 | local origin = items:getv(params.itemId, 0) |
97 | 129 | local nums = origin+params.count |
... | ... | @@ -119,18 +151,88 @@ function RolePlugin.bind(Role) |
119 | 151 | end |
120 | 152 | |
121 | 153 | function Role:costItems(itemCountT, params) |
154 | + if itemCountT[ItemId.Diamond] then --ไผๅ ๆฃ้ค้ป็ณ | |
155 | + local pms = clone(params or {}) | |
156 | + pms.count = itemCountT[ItemId.Diamond] | |
157 | + if not self:costDiamond(pms) then | |
158 | + return | |
159 | + end | |
160 | + itemCountT[ItemId.Diamond] = nil | |
161 | + end | |
122 | 162 | for itemId, count in pairs(itemCountT) do |
123 | 163 | local pms = clone(params or {}) |
124 | 164 | pms.itemId = itemId |
125 | 165 | pms.count = - count |
126 | 166 | self:addItem(pms) |
127 | 167 | end |
168 | + return true | |
128 | 169 | end |
129 | 170 | |
130 | 171 | function Role:getItemCount(itemId) |
172 | + if itemId == ItemId.Diamond then | |
173 | + return self:getAllDiamond() | |
174 | + end | |
131 | 175 | return self:getProperty("items"):getv(itemId, 0) |
132 | 176 | end |
133 | 177 | |
178 | + function Role:getAllDiamond() | |
179 | + return self:getProperty("diamond") + self:getProperty("reDiamond") | |
180 | + end | |
181 | + | |
182 | + function Role:gainDiamond(params) | |
183 | + if not params or type(params) ~= "table" then return false end | |
184 | + local count = tonum(params.count) | |
185 | + if isnan(count) then | |
186 | + return false | |
187 | + end | |
188 | + local origind = self:getProperty("diamond") | |
189 | + local originr = self:getProperty("reDiamond") | |
190 | + local origin = origind + originr | |
191 | + if params.isRecharge then | |
192 | + self:incrProperty("reDiamond", count) | |
193 | + else | |
194 | + self:incrProperty("diamond", count) | |
195 | + end | |
196 | + self:notifyUpdateProperty("diamond", self:getAllDiamond()) | |
197 | + return true | |
198 | + end | |
199 | + | |
200 | + function Role:costDiamond(params) | |
201 | + if not params or type(params) ~= "table" then return false end | |
202 | + local count = tonum(params.count) | |
203 | + if isnan(count) then | |
204 | + return false | |
205 | + end | |
206 | + if count <= 0 then | |
207 | + return false | |
208 | + end | |
209 | + local origind = self:getProperty("diamond") | |
210 | + local originr = self:getProperty("reDiamond") | |
211 | + local origin = origind + originr | |
212 | + | |
213 | + if origin < 0 then | |
214 | + return false | |
215 | + end | |
216 | + if origin < count then | |
217 | + return false | |
218 | + end | |
219 | + local last = count | |
220 | + local costFirst = {"diamond", "reDiamond"} | |
221 | + if params.isRecharge then | |
222 | + costFirst = {"reDiamond", "diamond"} | |
223 | + end | |
224 | + last = math.max(last - self:getProperty(costFirst[1]), 0) | |
225 | + if last < count then | |
226 | + self:incrProperty(costFirst[1], last - count) | |
227 | + end | |
228 | + if last > 0 then | |
229 | + self:incrProperty(costFirst[2], -last) | |
230 | + end | |
231 | + | |
232 | + self:notifyUpdateProperty("diamond", self:getAllDiamond()) | |
233 | + return true | |
234 | + end | |
235 | + | |
134 | 236 | function Role:addHero(params) |
135 | 237 | local roleId = self:getProperty("id") |
136 | 238 | local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) | ... | ... |
src/utils/MathUtil.lua
... | ... | @@ -39,4 +39,15 @@ function math.randWeight(dataset, field) |
39 | 39 | end |
40 | 40 | |
41 | 41 | return nil |
42 | +end | |
43 | + | |
44 | +function math.illegalNum(num, min, max) | |
45 | + local illegal = true | |
46 | + for i = min, max do | |
47 | + if num == i then | |
48 | + illegal = false | |
49 | + break | |
50 | + end | |
51 | + end | |
52 | + return illegal | |
42 | 53 | end |
43 | 54 | \ No newline at end of file | ... | ... |