Commit 6136eacad7d37a699e3994647f94751fa227421d
1 parent
6af09a92
添加好友表
Showing
16 changed files
with
351 additions
and
163 deletions
Show diff stats
| ... | ... | @@ -10,10 +10,10 @@ fi |
| 10 | 10 | pid=`cat skynet.pid` |
| 11 | 11 | run=`ps aux | grep skynet | grep -v grep | grep -c $pid` |
| 12 | 12 | |
| 13 | -if [ $run = 1 ]; then | |
| 13 | +if [[ $run = 1 ]]; then | |
| 14 | 14 | echo "服务端正在运行" |
| 15 | 15 | exit 0 |
| 16 | 16 | fi |
| 17 | 17 | |
| 18 | 18 | skynet/skynet config/${config}.lua |
| 19 | -echo "服务端启动完毕" | |
| 20 | 19 | \ No newline at end of file |
| 20 | +echo "服务端启动完毕" | ... | ... |
src/actions/ActivityAction.lua
| ... | ... | @@ -1006,16 +1006,6 @@ function _M.friendHelpRpc(agent, data) |
| 1006 | 1006 | |
| 1007 | 1007 | local actData = role.activity:getActData("FriendEnergy") or {} |
| 1008 | 1008 | |
| 1009 | - local function getIds() | |
| 1010 | - local ids = {} | |
| 1011 | - local friends = redisproxy:hgetall(FRIEND_KEY:format(roleId)) | |
| 1012 | - for i = 1, #friends , 2 do | |
| 1013 | - local objId = tonumber(friends[i]) | |
| 1014 | - ids[objId] = 1 | |
| 1015 | - end | |
| 1016 | - return ids | |
| 1017 | - end | |
| 1018 | - | |
| 1019 | 1009 | if oper == 1 then -- 赠送好友能量 |
| 1020 | 1010 | local giveAE = actData.giveAE or {} |
| 1021 | 1011 | local objId = msg.roleId |
| ... | ... | @@ -1025,7 +1015,7 @@ function _M.friendHelpRpc(agent, data) |
| 1025 | 1015 | if not objId then |
| 1026 | 1016 | return 3 |
| 1027 | 1017 | end |
| 1028 | - if not redisproxy:hexists(FRIEND_KEY:format(roleId), objId) then | |
| 1018 | + if not role.friends[objId] then | |
| 1029 | 1019 | result = 2 |
| 1030 | 1020 | end |
| 1031 | 1021 | if giveAE[objId] then |
| ... | ... | @@ -1071,7 +1061,6 @@ function _M.friendHelpRpc(agent, data) |
| 1071 | 1061 | local gift2 = gifts[2] |
| 1072 | 1062 | local limit = actData.limit or 0 |
| 1073 | 1063 | local cmd1, cmd2 = 0, 0 |
| 1074 | - local ids = getIds() | |
| 1075 | 1064 | |
| 1076 | 1065 | local members = {} |
| 1077 | 1066 | local temp = redisproxy:smembers(FRIEND_ENERGY:format(roleId)) |
| ... | ... | @@ -1080,7 +1069,7 @@ function _M.friendHelpRpc(agent, data) |
| 1080 | 1069 | end |
| 1081 | 1070 | |
| 1082 | 1071 | redisproxy:pipelining(function(red) |
| 1083 | - for friendId, _ in pairs(ids) do | |
| 1072 | + for friendId, _ in pairs(role.friends) do | |
| 1084 | 1073 | if not giveAE[friendId] then |
| 1085 | 1074 | giveAE[friendId] = 1 |
| 1086 | 1075 | award[gift1[1]] = (award[gift1[1]] or 0) + gift1[2] |
| ... | ... | @@ -1146,13 +1135,12 @@ function _M.friendHelpRpc(agent, data) |
| 1146 | 1135 | |
| 1147 | 1136 | local ids = {} |
| 1148 | 1137 | local members = {} |
| 1149 | - local friendIds = getIds() | |
| 1150 | 1138 | local temp = redisproxy:smembers(FRIEND_ENERGY:format(roleId)) |
| 1151 | 1139 | for _, id in pairs(temp) do |
| 1152 | 1140 | members[tonumber(id)] = 1 |
| 1153 | 1141 | end |
| 1154 | 1142 | |
| 1155 | - for id, _ in pairs(friendIds) do | |
| 1143 | + for id, _ in pairs(role.friends) do | |
| 1156 | 1144 | if members[id] then |
| 1157 | 1145 | ids[id] = 1 |
| 1158 | 1146 | end | ... | ... |
src/actions/EmailAction.lua
| ... | ... | @@ -111,7 +111,7 @@ function _M.drawAllAttachRpc(agent, data) |
| 111 | 111 | for _, email in ipairs(emails) do |
| 112 | 112 | local attachments = getEmailAttachments(email) |
| 113 | 113 | if attachments ~= "" then |
| 114 | - email:setProperty("status", 2) | |
| 114 | + email:setProperty("status", 2, true) | |
| 115 | 115 | email:log(role, 2) |
| 116 | 116 | ids[email:getProperty("id")] = 1 |
| 117 | 117 | for key, v in pairs(attachments:toNumMap()) do |
| ... | ... | @@ -139,7 +139,7 @@ function _M.drawAttachRpc(agent, data) |
| 139 | 139 | if attachments == "" then return end |
| 140 | 140 | |
| 141 | 141 | local reward, change = role:award(attachments, {log = {desc = "draw_attach", int1 = id}}) |
| 142 | - email:setProperty("status", 2) | |
| 142 | + email:setProperty("status", 2, true) | |
| 143 | 143 | email:log(role, 2) |
| 144 | 144 | SendPacket(actionCodes.Email_drawAttachRpc, MsgPack.pack({reward = reward, change = change})) |
| 145 | 145 | role:mylog("mail_action", {desc = "draw_attach", int1 = id, key1 = email:getProperty("title"), key2 = attachments}) |
| ... | ... | @@ -156,7 +156,7 @@ function _M.checkRpc(agent, data) |
| 156 | 156 | local email = require("models.Email").new({key = string.format("%d", id), id = id}) |
| 157 | 157 | if not email:load() then return end |
| 158 | 158 | |
| 159 | - email:setProperty("status", 1) | |
| 159 | + email:setProperty("status", 1, true) | |
| 160 | 160 | email:log(role, 1) |
| 161 | 161 | role:mylog("mail_action", {desc = "check_mail", int1 = id}) |
| 162 | 162 | ... | ... |
src/actions/FriendAction.lua
| ... | ... | @@ -11,6 +11,7 @@ local table_unpack = table.unpack |
| 11 | 11 | local table_find = table.find |
| 12 | 12 | local table_nums = table.nums |
| 13 | 13 | local math_random = math.randomInt |
| 14 | +require "utils.MysqlUtil" | |
| 14 | 15 | |
| 15 | 16 | local _M = {} |
| 16 | 17 | |
| ... | ... | @@ -23,12 +24,6 @@ local function formatArray(tb) |
| 23 | 24 | return t |
| 24 | 25 | end |
| 25 | 26 | |
| 26 | -local function checkFriendLimit(roleId) | |
| 27 | - roleId = tonumber(roleId) | |
| 28 | - local count = redisproxy:hlen(FRIEND_KEY:format(roleId)) | |
| 29 | - return count < globalCsv.friendListLimit | |
| 30 | -end | |
| 31 | - | |
| 32 | 27 | |
| 33 | 28 | local function addAndCheckApplyLimit(roleId, objId) |
| 34 | 29 | roleId = tonumber(roleId) |
| ... | ... | @@ -78,7 +73,7 @@ function _M.searchRpc(agent, data) |
| 78 | 73 | local objIds = {} |
| 79 | 74 | local tempId = tonumber(key) |
| 80 | 75 | if tempId then |
| 81 | - if redisproxy:exists(string_format("role:%d", tempId)) then | |
| 76 | + if roleExists(tempId) then | |
| 82 | 77 | objIds[tempId] = 1 |
| 83 | 78 | end |
| 84 | 79 | end |
| ... | ... | @@ -92,13 +87,13 @@ function _M.searchRpc(agent, data) |
| 92 | 87 | for objId, _ in pairs(objIds) do |
| 93 | 88 | local online, info = getRoleInfo(objId) |
| 94 | 89 | local redret = redisproxy:pipelining(function (red) |
| 95 | - red:hexists(FRIEND_KEY:format(roleId), objId) | |
| 96 | 90 | red:zscore(FRIEND_APPLY_KEY:format(objId), roleId) |
| 97 | 91 | red:sismember(FRIEND_BLACK_KEY:format(roleId), objId) |
| 98 | 92 | end) |
| 99 | - local isFriend = redret[1] == 1 and 1 or nil | |
| 100 | - local hadApply = redret[2] and 1 or nil | |
| 101 | - local inBlack = redret[3] == 1 and 1 or nil | |
| 93 | + | |
| 94 | + local isFriend = role.friends[objId] and 1 or nil | |
| 95 | + local hadApply = redret[1] and 1 or nil | |
| 96 | + local inBlack = redret[2] == 1 and 1 or nil | |
| 102 | 97 | |
| 103 | 98 | table.insert(searchList, table_merge({ |
| 104 | 99 | roleId = objId, |
| ... | ... | @@ -123,6 +118,7 @@ function _M.applyRpc(agent, data) |
| 123 | 118 | |
| 124 | 119 | local msg = MsgPack.unpack(data) |
| 125 | 120 | local objectId = msg.roleId |
| 121 | + dump(msg) | |
| 126 | 122 | |
| 127 | 123 | if objectId == roleId then |
| 128 | 124 | return |
| ... | ... | @@ -130,44 +126,41 @@ function _M.applyRpc(agent, data) |
| 130 | 126 | |
| 131 | 127 | local result = nil |
| 132 | 128 | local redret = redisproxy:pipelining(function (red) |
| 133 | - red:exists(string_format("role:%d", objectId)) | |
| 134 | - red:hexists(FRIEND_KEY:format(roleId), objectId) | |
| 135 | 129 | red:zscore(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 136 | 130 | red:sismember(FRIEND_BLACK_KEY:format(objectId), roleId) |
| 137 | 131 | red:sismember(FRIEND_BLACK_KEY:format(roleId), objectId) |
| 138 | - red:hlen(FRIEND_KEY:format(roleId)) | |
| 139 | - red:hlen(FRIEND_KEY:format(objectId)) | |
| 140 | 132 | end) |
| 133 | + dump(redret) | |
| 141 | 134 | |
| 142 | 135 | -- 玩家id不存在 |
| 143 | - if not result and redret[1] ~= 1 then | |
| 136 | + if not result and not roleExists(objectId) then | |
| 144 | 137 | result = 1 |
| 145 | 138 | end |
| 146 | 139 | |
| 147 | 140 | -- 已经有这个好友 |
| 148 | - if not result and redret[2] == 1 then | |
| 141 | + if not result and role.friends[objectId] then | |
| 149 | 142 | result = 2 |
| 150 | 143 | end |
| 151 | 144 | -- 已经申请 |
| 152 | - if not result and redret[3] then | |
| 145 | + if not result and redret[1] then | |
| 153 | 146 | result = 3 |
| 154 | 147 | end |
| 155 | 148 | -- 对方把你拉黑 |
| 156 | - if not result and redret[4] == 1 then | |
| 149 | + if not result and redret[2] == 1 then | |
| 157 | 150 | result = 4 |
| 158 | 151 | end |
| 159 | 152 | |
| 160 | 153 | -- 你把对方拉黑了 |
| 161 | - if not result and redret[5] == 1 then | |
| 154 | + if not result and redret[3] == 1 then | |
| 162 | 155 | result = 5 |
| 163 | 156 | end |
| 164 | 157 | |
| 165 | 158 | -- 自己好友已经满 |
| 166 | - if not result and redret[6] >= globalCsv.friendListLimit then | |
| 159 | + if not result and table.numbers(role.friends) >= globalCsv.friendListLimit then | |
| 167 | 160 | result = 6 |
| 168 | 161 | end |
| 169 | 162 | -- 对方的好友已满 |
| 170 | - if not result and redret[7] >= globalCsv.friendListLimit then | |
| 163 | + if not result and getFriendCount(objectId) >= globalCsv.friendListLimit then | |
| 171 | 164 | result = 7 |
| 172 | 165 | end |
| 173 | 166 | |
| ... | ... | @@ -209,29 +202,28 @@ function _M.applyListRpc(agent, data) |
| 209 | 202 | return true |
| 210 | 203 | end |
| 211 | 204 | |
| 212 | -local function checkHandleApply(roleId, objectId, needAddNew) | |
| 205 | +local function checkHandleApply(role, objectId, needAddNew) | |
| 213 | 206 | needAddNew = needAddNew or 0 |
| 207 | + local roleId = role:getProperty("id") | |
| 214 | 208 | local redret = redisproxy:pipelining(function (red) |
| 215 | - red:hlen(FRIEND_KEY:format(roleId)) | |
| 216 | - red:hlen(FRIEND_KEY:format(objectId)) | |
| 217 | 209 | red:sismember(FRIEND_BLACK_KEY:format(objectId), roleId) |
| 218 | 210 | red:sismember(FRIEND_BLACK_KEY:format(roleId), objectId) |
| 219 | 211 | end) |
| 220 | 212 | |
| 221 | 213 | --自己好友满了 |
| 222 | - if (redret[1] + needAddNew) >= globalCsv.friendListLimit then | |
| 214 | + if (table.numbers(role.friends) + needAddNew) >= globalCsv.friendListLimit then | |
| 223 | 215 | return 1 |
| 224 | 216 | end |
| 225 | 217 | -- 对方好友满了 |
| 226 | - if redret[2] >= globalCsv.friendListLimit then | |
| 218 | + if getFriendCount(objectId) >= globalCsv.friendListLimit then | |
| 227 | 219 | return 2 |
| 228 | 220 | end |
| 229 | 221 | -- 对方把你拉黑 |
| 230 | - if redret[3] == 1 then | |
| 222 | + if redret[1] == 1 then | |
| 231 | 223 | return 3 |
| 232 | 224 | end |
| 233 | 225 | -- 你把对方拉黑了 |
| 234 | - if redret[4] == 1 then | |
| 226 | + if redret[2] == 1 then | |
| 235 | 227 | return 4 |
| 236 | 228 | end |
| 237 | 229 | |
| ... | ... | @@ -255,14 +247,13 @@ function _M.handleApplyRpc(agent, data) |
| 255 | 247 | return |
| 256 | 248 | end |
| 257 | 249 | local curCount |
| 258 | - result, curCount = checkHandleApply(roleId, objectId) | |
| 250 | + result, curCount = checkHandleApply(role, objectId) | |
| 259 | 251 | |
| 260 | 252 | if not result then |
| 261 | 253 | redisproxy:pipelining(function (red) |
| 262 | 254 | red:ZREM(FRIEND_APPLY_KEY:format(roleId), objectId) |
| 263 | 255 | red:ZREM(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 264 | - red:hsetnx(FRIEND_KEY:format(roleId), objectId, newTag) | |
| 265 | - red:hsetnx(FRIEND_KEY:format(objectId), roleId, newTag)--告知对放有新好友 | |
| 256 | + addFriend(roleId, objectId) | |
| 266 | 257 | end) |
| 267 | 258 | local myInfo = role:friendSInfo() |
| 268 | 259 | myInfo.online = true |
| ... | ... | @@ -310,7 +301,7 @@ function _M.handleApplyRpc(agent, data) |
| 310 | 301 | local needAddInfo = {} |
| 311 | 302 | for _, objId in ipairs(allIds) do |
| 312 | 303 | objId = tonumber(objId) |
| 313 | - local cr, curCount = checkHandleApply(roleId, objId, #needAdd) | |
| 304 | + local cr, curCount = checkHandleApply(role, objId, #needAdd) | |
| 314 | 305 | if not cr then |
| 315 | 306 | table.insert(needAdd, objId) |
| 316 | 307 | table.insert(needAddMy, objId) |
| ... | ... | @@ -335,12 +326,9 @@ function _M.handleApplyRpc(agent, data) |
| 335 | 326 | red:ZREM(FRIEND_APPLY_KEY:format(roleId), table_unpack(needAdd)) |
| 336 | 327 | for _, objectId in pairs(needAdd) do |
| 337 | 328 | red:ZREM(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 338 | - red:hsetnx(FRIEND_KEY:format(objectId), roleId, newTag)--告知对放有新好友 | |
| 329 | + addFriend(roleId, objectId) | |
| 339 | 330 | end |
| 340 | 331 | end |
| 341 | - if next(needAddMy) then | |
| 342 | - red:HMSET(FRIEND_KEY:format(roleId), table_unpack(needAddMy)) | |
| 343 | - end | |
| 344 | 332 | end) |
| 345 | 333 | local myInfo = role:friendSInfo() |
| 346 | 334 | myInfo.roleId = roleId |
| ... | ... | @@ -373,27 +361,21 @@ function _M.listRpc(agent, data) |
| 373 | 361 | |
| 374 | 362 | local friendList = {} |
| 375 | 363 | local redret = redisproxy:pipelining(function (red) |
| 376 | - red:hgetall(FRIEND_KEY:format(roleId)) | |
| 377 | 364 | red:SMEMBERS(FRIEND_POINT:format(roleId)) |
| 378 | 365 | end) |
| 379 | 366 | |
| 380 | - local friends = redret[1] | |
| 381 | - local fpoint = formatArray(redret[2]) | |
| 367 | + local fpoint = formatArray(redret[1]) | |
| 382 | 368 | local hadGet = role.dailyData:getProperty("getFP") |
| 383 | 369 | local hadGive = role.dailyData:getProperty("giveFP") |
| 384 | 370 | |
| 385 | - local clearRed = {} | |
| 386 | - for i = 1, #friends, 2 do | |
| 387 | - local id = friends[i] | |
| 388 | - local data = friends[i + 1] | |
| 389 | - local friendInfo = MsgPack.unpack(data) | |
| 390 | - id = tonumber(id) | |
| 371 | + local addNew = false | |
| 372 | + for id, friend in pairs(role.friends) do | |
| 391 | 373 | local online, info = getRoleInfo(id) |
| 392 | 374 | local roleInfo = { |
| 393 | 375 | roleId = id, |
| 394 | 376 | online = online, |
| 395 | - addTime = friendInfo[1], | |
| 396 | - isNew = friendInfo[2], | |
| 377 | + addTime = friend:getProperty("addTime"), | |
| 378 | + isNew = friend:getProperty("isNew"), | |
| 397 | 379 | pGive = hadGive[id], |
| 398 | 380 | pGet = hadGet[id] and -1 or (fpoint[id] and 1 or nil) |
| 399 | 381 | } |
| ... | ... | @@ -401,14 +383,12 @@ function _M.listRpc(agent, data) |
| 401 | 383 | |
| 402 | 384 | friendList[#friendList + 1] = roleInfo |
| 403 | 385 | |
| 404 | - if friendInfo[2] then | |
| 405 | - friendInfo[2] = nil --清除新好友标记 | |
| 406 | - clearRed[#clearRed + 1] = id | |
| 407 | - clearRed[#clearRed + 1] = MsgPack.pack(friendInfo) | |
| 386 | + if roleInfo.isNew then | |
| 387 | + friend:setProperty("isNew", 0) | |
| 388 | + addNew = true | |
| 408 | 389 | end |
| 409 | 390 | end |
| 410 | - if next(clearRed) then | |
| 411 | - redisproxy:hmset(FRIEND_KEY:format(roleId), table_unpack(clearRed)) --清除新好友标记 | |
| 391 | + if addNew then | |
| 412 | 392 | role:checkTaskEnter("AddFriend", {count = #friendList}) |
| 413 | 393 | end |
| 414 | 394 | |
| ... | ... | @@ -421,14 +401,14 @@ function _M.deleteRpc(agent, data) |
| 421 | 401 | local roleId = role:getProperty("id") |
| 422 | 402 | local msg = MsgPack.unpack(data) |
| 423 | 403 | local objectId = msg.roleId |
| 424 | - if not redisproxy:exists(string_format("role:%d", objectId)) then | |
| 404 | + if not roleExists(objectId) then | |
| 425 | 405 | return |
| 426 | 406 | end |
| 427 | 407 | -- 是否在好友列表中 |
| 428 | - if redisproxy:hexists(FRIEND_KEY:format(roleId), objectId) then | |
| 408 | + if role.friends[objectId] then | |
| 409 | + role.friends[objectId] = nil | |
| 429 | 410 | redisproxy:pipelining(function (red) |
| 430 | - red:hdel(FRIEND_KEY:format(roleId), objectId) | |
| 431 | - red:hdel(FRIEND_KEY:format(objectId), roleId) | |
| 411 | + delFriend(roleId, objectId) | |
| 432 | 412 | red:ZREM(FRIEND_APPLY_KEY:format(roleId), objectId) |
| 433 | 413 | red:ZREM(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 434 | 414 | end) |
| ... | ... | @@ -450,7 +430,7 @@ function _M.blockRpc(agent, data) |
| 450 | 430 | local msg = MsgPack.unpack(data) |
| 451 | 431 | local cmd = msg.cmd |
| 452 | 432 | local objectId = msg.roleId |
| 453 | - if not redisproxy:exists(string_format("role:%d", objectId)) then | |
| 433 | + if not roleExists(objectId) then | |
| 454 | 434 | return |
| 455 | 435 | end |
| 456 | 436 | |
| ... | ... | @@ -463,8 +443,7 @@ function _M.blockRpc(agent, data) |
| 463 | 443 | -- 删除好友 |
| 464 | 444 | if not result then |
| 465 | 445 | redisproxy:pipelining(function (red) |
| 466 | - red:hdel(FRIEND_KEY:format(roleId), objectId) | |
| 467 | - red:hdel(FRIEND_KEY:format(objectId), roleId) | |
| 446 | + delFriend(roleId, objectId) | |
| 468 | 447 | red:ZREM(FRIEND_APPLY_KEY:format(roleId), objectId) |
| 469 | 448 | red:ZREM(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 470 | 449 | red:sadd(FRIEND_BLACK_KEY:format(roleId), objectId) |
| ... | ... | @@ -511,19 +490,19 @@ function _M.infoRpc(agent, data) |
| 511 | 490 | local roleId = role:getProperty("id") |
| 512 | 491 | local msg = MsgPack.unpack(data) |
| 513 | 492 | local objectId = msg.roleId |
| 514 | - if not redisproxy:exists(string_format("role:%d", objectId)) then | |
| 493 | + | |
| 494 | + if not roleExists(objectId) then | |
| 515 | 495 | return |
| 516 | 496 | end |
| 517 | 497 | |
| 518 | 498 | local online, info = getRoleAllInfo(objectId) |
| 519 | 499 | local redret = redisproxy:pipelining(function (red) |
| 520 | - red:hexists(FRIEND_KEY:format(roleId), objectId) | |
| 521 | 500 | red:zscore(FRIEND_APPLY_KEY:format(objectId), roleId) |
| 522 | 501 | red:sismember(FRIEND_BLACK_KEY:format(roleId), objectId) |
| 523 | 502 | end) |
| 524 | - local isFriend = redret[1] == 1 and 1 or nil | |
| 525 | - local hadApply = redret[2] == 1 and 1 or nil | |
| 526 | - local inBlack = redret[3] == 1 and 1 or nil | |
| 503 | + local isFriend = role.friends[objectId] and 1 or nil | |
| 504 | + local hadApply = redret[1] == 1 and 1 or nil | |
| 505 | + local inBlack = redret[2] == 1 and 1 or nil | |
| 527 | 506 | |
| 528 | 507 | local objInfo = table_merge({ |
| 529 | 508 | roleId = objectId, |
| ... | ... | @@ -551,7 +530,7 @@ function _M.pointRpc(agent, data) |
| 551 | 530 | if not result and giveP[objId] then |
| 552 | 531 | result = 1 |
| 553 | 532 | end |
| 554 | - if not result and not redisproxy:hexists(FRIEND_KEY:format(roleId), objId) then | |
| 533 | + if not result and not role.friends[objId] then | |
| 555 | 534 | result = 2 |
| 556 | 535 | end |
| 557 | 536 | if not result then |
| ... | ... | @@ -586,11 +565,9 @@ function _M.pointRpc(agent, data) |
| 586 | 565 | elseif cmd == 3 then -- 一键赠送领取 |
| 587 | 566 | -- 赠送 |
| 588 | 567 | local giveP = role.dailyData:getProperty("giveFP") |
| 589 | - local friends = redisproxy:hgetall(FRIEND_KEY:format(roleId)) | |
| 590 | 568 | local change = 0 |
| 591 | 569 | redisproxy:pipelining(function(red) |
| 592 | - for i = 1, #friends , 2 do | |
| 593 | - local objId = tonumber(friends[i]) | |
| 570 | + for objId, friend in pairs(role.friends) do | |
| 594 | 571 | if not giveP[objId] then |
| 595 | 572 | giveP[objId] = 1 |
| 596 | 573 | change = change + 1 |
| ... | ... | @@ -645,22 +622,17 @@ function _M.randomRpc(agent, data) |
| 645 | 622 | local roleId = role:getProperty("id") |
| 646 | 623 | |
| 647 | 624 | local redret = redisproxy:pipelining(function (red) |
| 648 | - red:hgetall(FRIEND_KEY:format(roleId)) | |
| 649 | 625 | red:zrevrange(FRIEND_RECOMMEND, 0, globalCsv.friendRecommendLimit + globalCsv.friendListLimit + 10) --扩充10个 |
| 650 | 626 | red:SMEMBERS(FRIEND_BLACK_KEY:format(roleId)) |
| 651 | 627 | end) |
| 652 | - local friends = {} | |
| 653 | - for i = 1, #redret[1], 2 do | |
| 654 | - friends[tonumber(redret[1][i])] = redret[1][i + 1] | |
| 655 | - end | |
| 656 | 628 | |
| 657 | - local newList = redret[2] | |
| 658 | - local hadBlack = formatArray(redret[3]) | |
| 629 | + local newList = redret[1] | |
| 630 | + local hadBlack = formatArray(redret[2]) | |
| 659 | 631 | |
| 660 | 632 | local needRoleIds = {} |
| 661 | 633 | for _, newId in pairs(newList) do |
| 662 | 634 | local numNewId = tonumber(newId) |
| 663 | - if numNewId ~= roleId and not friends[numNewId] and not hadBlack[numNewId] then | |
| 635 | + if numNewId ~= roleId and not role.friends[numNewId] and not hadBlack[numNewId] then | |
| 664 | 636 | table.insert(needRoleIds, numNewId) |
| 665 | 637 | end |
| 666 | 638 | end | ... | ... |
src/actions/RoleAction.lua
| ... | ... | @@ -87,11 +87,11 @@ function _M.loginRpc( agent, data ) |
| 87 | 87 | -- 2 |
| 88 | 88 | if not role then |
| 89 | 89 | local roleKey = string_format("%d", roleId) |
| 90 | - --if not redisproxy:exists(roleKey) then | |
| 91 | - -- response.result = "DB_ERROR" | |
| 92 | - -- SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(response)) | |
| 93 | - -- return true | |
| 94 | - --end | |
| 90 | + if not roleExists(roleId) then | |
| 91 | + response.result = "DB_ERROR" | |
| 92 | + SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(response)) | |
| 93 | + return true | |
| 94 | + end | |
| 95 | 95 | -- 2a |
| 96 | 96 | role = require("models.Role").new({key = roleKey}) |
| 97 | 97 | role:load() |
| ... | ... | @@ -156,7 +156,6 @@ function _M.loginRpc( agent, data ) |
| 156 | 156 | end |
| 157 | 157 | end |
| 158 | 158 | |
| 159 | - --SERV_OPEN = redisproxy:hget("autoincrement_set", "server_start") | |
| 160 | 159 | SERV_OPEN = getDbCfgVal("server_info", "server_start", "str_value") |
| 161 | 160 | |
| 162 | 161 | role:changeStructVersion() -- 数据结构 版本更新 |
| ... | ... | @@ -1107,21 +1106,20 @@ function _M.chatRpc(agent, data) |
| 1107 | 1106 | local objectId = msg.roleId |
| 1108 | 1107 | response.objId = objectId |
| 1109 | 1108 | local redret = redisproxy:pipelining(function(red) |
| 1110 | - red:exists(string.format("role:%d", objectId)) | |
| 1111 | 1109 | red:sismember(FRIEND_BLACK_KEY:format(roleId), objectId) |
| 1112 | 1110 | red:sismember(FRIEND_BLACK_KEY:format(objectId), roleId) |
| 1113 | 1111 | end) |
| 1114 | - if redret[1] ~= 1 then | |
| 1112 | + if not roleExists(objectId) then | |
| 1115 | 1113 | result = 1 |
| 1116 | 1114 | return |
| 1117 | 1115 | end |
| 1118 | 1116 | -- 你把对方拉黑拉黑 |
| 1119 | - if redret[2] == 1 then | |
| 1117 | + if redret[1] == 1 then | |
| 1120 | 1118 | result = 2 |
| 1121 | 1119 | return |
| 1122 | 1120 | end |
| 1123 | 1121 | -- 对方把你拉黑 |
| 1124 | - local isBlock = redret[3] == 1 | |
| 1122 | + local isBlock = redret[2] == 1 | |
| 1125 | 1123 | |
| 1126 | 1124 | local bin = MsgPack.pack(response) |
| 1127 | 1125 | if not isBlock then | ... | ... |
src/models/Activity.lua
| ... | ... | @@ -938,17 +938,11 @@ activityFunc[Activity.ActivityType.FriendEnergy] = { |
| 938 | 938 | |
| 939 | 939 | function Activity:getActFriendNew() |
| 940 | 940 | local roleId = self.owner:getProperty("id") |
| 941 | - local friendIds = {} | |
| 942 | - local friends = redisproxy:hgetall(FRIEND_KEY:format(roleId)) | |
| 943 | - for i = 1, #friends , 2 do | |
| 944 | - local objId = tonumber(friends[i]) | |
| 945 | - friendIds[objId] = 1 | |
| 946 | - end | |
| 947 | 941 | |
| 948 | 942 | local ids = {} |
| 949 | 943 | local members = redisproxy:smembers(FRIEND_ENERGY:format(roleId)) |
| 950 | 944 | for _, id in pairs(members) do |
| 951 | - if friendIds[tonumber(id)] then | |
| 945 | + if self.owner.friends[tonumber(id)] then | |
| 952 | 946 | ids[tonumber(id)] = 1 |
| 953 | 947 | end |
| 954 | 948 | end | ... | ... |
src/models/Diner.lua
| ... | ... | @@ -7,10 +7,10 @@ end |
| 7 | 7 | Diner.schema = { |
| 8 | 8 | id = {"number", 0, "pri"}, -- 角色id |
| 9 | 9 | buildL = {"string", ""}, -- 家具等级 1=1 2=1 3=1 |
| 10 | - order = {"string", "[]", 1024}, -- 特殊订单 | |
| 11 | - sells = {"string", "[]", "", 1024}, -- 贩卖位置 | |
| 12 | - dishTree = {"string", "1=1 101=1 201=1"}, -- 料理天赋 | |
| 13 | - skillTree = {"string", ""}, -- 支援天赋 | |
| 10 | + order = {"string", "[]", "blob"}, -- 特殊订单 | |
| 11 | + sells = {"string", "[]", "blob"}, -- 贩卖位置 | |
| 12 | + dishTree = {"string", "1=1 101=1 201=1", "blob"}, -- 料理天赋 | |
| 13 | + skillTree = {"string", "", "blob"}, -- 支援天赋 | |
| 14 | 14 | popular = {"number",0}, -- 累计人气 |
| 15 | 15 | expedite = {"number",1}, --每日加速次数 |
| 16 | 16 | gfood = {"table", {}}, -- 愿望食材 {{id = 123, st = 1232144},} | ... | ... |
| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | +local Friend = class("Friend", require("shared.ModelBaseMysql")) | |
| 2 | + | |
| 3 | +function Friend:ctor(properties) | |
| 4 | + Friend.super.ctor(self, properties) | |
| 5 | +end | |
| 6 | + | |
| 7 | +Friend.schema = { | |
| 8 | + id = {"number", 0, "pri_auto"}, | |
| 9 | + roleid = {"number", 0, "index"}, | |
| 10 | + fid = {"number", 0, "index"}, | |
| 11 | + isNew = {"number", 1}, | |
| 12 | + addTime = {"number", skynet.timex()}, | |
| 13 | +} | |
| 14 | + | |
| 15 | +return Friend | |
| 0 | 16 | \ No newline at end of file | ... | ... |
src/models/Role.lua
src/models/RoleCross.lua
| ... | ... | @@ -24,6 +24,20 @@ RoleCross.bind = function (Role) |
| 24 | 24 | return info |
| 25 | 25 | end |
| 26 | 26 | |
| 27 | + function Role:addFriend(friendId) | |
| 28 | + local res = mysqlproxy:query(string.format("SELECT * FROM `Friend` WHERE `roleid` = %s AND `fid` = %s;", self:getProperty("id"), friendId)) | |
| 29 | + for _, data in ipairs(res) do | |
| 30 | + local friend = require("models.Friend").new({key = string.format("%d",data.id), id = data.id}) | |
| 31 | + if friend:load(data) then | |
| 32 | + self.friends[friend:getProperty("fid")] = friend | |
| 33 | + end | |
| 34 | + end | |
| 35 | + end | |
| 36 | + | |
| 37 | + function Role:delFriend(friendId) | |
| 38 | + self.friends[friendId] = nil | |
| 39 | + end | |
| 40 | + | |
| 27 | 41 | -- 好友队伍战斗信息 |
| 28 | 42 | function Role:friendBattleInfo() |
| 29 | 43 | return self:getProperty("pvpTBVC") ~= 0 and self:getProperty("pvpTBC") or self:getProperty("hangTB") |
| ... | ... | @@ -231,33 +245,30 @@ local function getRoleKey(roleId) |
| 231 | 245 | end |
| 232 | 246 | |
| 233 | 247 | function CMD.setProperty(roleId, field, value) |
| 234 | - local value = packRoleField(field, value) | |
| 235 | - if not value then return end | |
| 236 | - return redisproxy:hset(getRoleKey(roleId), field, value) | |
| 248 | + local tb = {} | |
| 249 | + tb[field] = value | |
| 250 | + return CMD.setProperties(roleId, tb) | |
| 237 | 251 | end |
| 238 | 252 | |
| 239 | 253 | |
| 240 | 254 | function CMD.setProperties(roleId, fields) |
| 241 | - local result = {} | |
| 242 | - for k,v in pairs(fields) do | |
| 243 | - local value = packRoleField(k, v) | |
| 244 | - if value then | |
| 245 | - result[#result + 1] = k | |
| 246 | - result[#result + 1] = value | |
| 247 | - end | |
| 248 | - end | |
| 249 | - return redisproxy:hmset(getRoleKey(roleId), table.unpack(result)) | |
| 255 | + local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) | |
| 256 | + return role:updateFields(fields) | |
| 250 | 257 | end |
| 251 | 258 | |
| 252 | 259 | function CMD.getProperty(roleId, field) |
| 253 | - return unpackRoleField(field ,redisproxy:hget(getRoleKey(roleId), field)) | |
| 260 | + local ret = CMD.getProperties(roleId, {field}) | |
| 261 | + return ret[field] | |
| 254 | 262 | end |
| 255 | 263 | |
| 256 | 264 | function CMD.getProperties(roleId, fields) |
| 257 | - local returnValue = redisproxy:hmget(getRoleKey(roleId), table.unpack(fields)) | |
| 265 | + local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) | |
| 258 | 266 | local ret = {} |
| 259 | - for index, key in ipairs(fields) do | |
| 260 | - ret[key] = unpackRoleField(key, returnValue[index]) | |
| 267 | + local datas = role:loadFields(fields) | |
| 268 | + if datas then | |
| 269 | + for index, key in ipairs(fields) do | |
| 270 | + ret[key] = unpackRoleField(key, datas[key]) | |
| 271 | + end | |
| 261 | 272 | end |
| 262 | 273 | return ret |
| 263 | 274 | end | ... | ... |
src/models/RolePlugin.lua
| ... | ... | @@ -13,6 +13,7 @@ function RolePlugin.bind(Role) |
| 13 | 13 | self:loadActivity() |
| 14 | 14 | self:loadStoreInfo() |
| 15 | 15 | self:loadRoleIncre() |
| 16 | + self:loadFriends() | |
| 16 | 17 | end |
| 17 | 18 | |
| 18 | 19 | function Role:reloadWhenLogin() |
| ... | ... | @@ -621,6 +622,17 @@ function RolePlugin.bind(Role) |
| 621 | 622 | end |
| 622 | 623 | end |
| 623 | 624 | |
| 625 | + function Role:loadFriends() | |
| 626 | + local roleId = self:getProperty("id") | |
| 627 | + local res = mysqlproxy:query(string.format("SELECT * FROM `Friend` WHERE `roleid` = %s", roleId)) | |
| 628 | + for _, data in ipairs(res) do | |
| 629 | + local friend = require("models.Friend").new({key = string.format("%d",data.id), id = data.id}) | |
| 630 | + if friend:load(data) then | |
| 631 | + self.friends[friend:getProperty("fid")] = friend | |
| 632 | + end | |
| 633 | + end | |
| 634 | + end | |
| 635 | + | |
| 624 | 636 | function Role:loadDaily() |
| 625 | 637 | local roleId = self:getProperty("id") |
| 626 | 638 | local dataKey = string.format("%d", roleId) |
| ... | ... | @@ -834,12 +846,6 @@ function RolePlugin.bind(Role) |
| 834 | 846 | end |
| 835 | 847 | |
| 836 | 848 | -- delete rune |
| 837 | - --redisproxy:pipelining(function (red) | |
| 838 | - -- for _, runeId in pairs(bDel) do | |
| 839 | - -- red:del(string.format(R_RUNE, roleId, runeId)) | |
| 840 | - -- red:srem(string.format(R_RUNEIDS, roleId), runeId) | |
| 841 | - -- end | |
| 842 | - --end) | |
| 843 | 849 | mysqlproxy:query(string.format("DELETE FROM `Rune` WHERE `uid` in (%s)", table.concat(bDel, ","))) |
| 844 | 850 | |
| 845 | 851 | local response = {} |
| ... | ... | @@ -1555,6 +1561,21 @@ function RolePlugin.bind(Role) |
| 1555 | 1561 | function Role:onRecoverTimer(now) |
| 1556 | 1562 | self:updateTimeReset(now, true) |
| 1557 | 1563 | self:checkNewEvent(now) |
| 1564 | + self:saveRoleData() | |
| 1565 | + end | |
| 1566 | + | |
| 1567 | + function Role:saveRoleData() | |
| 1568 | + self:update() | |
| 1569 | + local objs = {self.activity, self.dailyData, self.dinerData, self.storeData, self.roleIncre} | |
| 1570 | + for _, info in ipairs(objs) do | |
| 1571 | + info:update() | |
| 1572 | + end | |
| 1573 | + local tbObjs = {self.friends, self.heros, self.runeBag} | |
| 1574 | + for _, tbObj in ipairs(tbObjs) do | |
| 1575 | + for _, info in pairs(tbObj) do | |
| 1576 | + tbObj:update() | |
| 1577 | + end | |
| 1578 | + end | |
| 1558 | 1579 | end |
| 1559 | 1580 | |
| 1560 | 1581 | local function breath(sec, name) |
| ... | ... | @@ -1812,6 +1833,8 @@ function RolePlugin.bind(Role) |
| 1812 | 1833 | redisproxy:hdel(string.format(R_ORDERS, roleId), rechargeId) |
| 1813 | 1834 | end |
| 1814 | 1835 | |
| 1836 | + orderObject:update() | |
| 1837 | + | |
| 1815 | 1838 | if status ~= "unknow" then |
| 1816 | 1839 | self:log("setOrder", { |
| 1817 | 1840 | order_status = ({success = 100, finsh = 200, fail = 300})[status] or 1000, -- "订单状态:100 - 开始下单(玩家还未开始付费行为记录)200 - 支付完成并发货(SDK通知可以发货时记录),300 - 订单被取消,1000 - 其他" | ... | ... |
src/services/dbseed.lua
| ... | ... | @@ -34,6 +34,8 @@ local function initRedisDb( ... ) |
| 34 | 34 | initAutoIncrementUid("autoincrement_set", "email") |
| 35 | 35 | initAutoIncrementUid("autoincrement_set", "emailTimestamp") |
| 36 | 36 | initAutoIncrementUid("autoincrement_set", "delay_email") |
| 37 | + initAutoIncrementUid("autoincrement_set", "stopcreate") | |
| 38 | + initAutoIncrementUid("autoincrement_set", "maintain") | |
| 37 | 39 | |
| 38 | 40 | --redisproxy:hsetnx("adv_season", "idx", 0) |
| 39 | 41 | --redisproxy:hsetnx("adv_season", "chapter", globalCsv.adv_endless_default_chapter) |
| ... | ... | @@ -80,6 +82,8 @@ local function initAutoIncreUidTable() |
| 80 | 82 | mysqlproxy:query(string.format(tpl, "email", 0)) |
| 81 | 83 | mysqlproxy:query(string.format(tpl, "emailTimestamp", 0)) |
| 82 | 84 | mysqlproxy:query(string.format(tpl, "delay_email", 0)) |
| 85 | + mysqlproxy:query(string.format(tpl, "stopcreate", 0)) | |
| 86 | + mysqlproxy:query(string.format(tpl, "maintain", 0)) | |
| 83 | 87 | end |
| 84 | 88 | end |
| 85 | 89 | |
| ... | ... | @@ -102,7 +106,7 @@ local function initAdvSeasonTable() |
| 102 | 106 | end |
| 103 | 107 | |
| 104 | 108 | local function checkRoleTables() |
| 105 | - local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order", "Email"} | |
| 109 | + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order", "Email", "Friend"} | |
| 106 | 110 | for _, name in ipairs(list) do |
| 107 | 111 | local obj = require("models."..name).new({key = "key"}) |
| 108 | 112 | print("check table [" .. name .. "] begin.") |
| ... | ... | @@ -111,6 +115,48 @@ local function checkRoleTables() |
| 111 | 115 | end |
| 112 | 116 | end |
| 113 | 117 | |
| 118 | +local function createMysqlSp() | |
| 119 | + mysqlproxy:query "DROP PROCEDURE IF EXISTS `add_friends`" | |
| 120 | + mysqlproxy:query [[ | |
| 121 | + CREATE PROCEDURE `add_friends`(IN role_id bigint, IN friend_id bigint, IN add_time int) | |
| 122 | + BEGIN | |
| 123 | + DECLARE t_error INTEGER DEFAULT 0; | |
| 124 | + DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; | |
| 125 | + | |
| 126 | + START TRANSACTION; | |
| 127 | + INSERT INTO `Friend`(`roleid`,`fid`,`addTime`) VALUES(role_id, friend_id, add_time); | |
| 128 | + INSERT INTO `Friend`(`roleid`,`fid`,`addTime`) VALUES(friend_id, role_id, add_time); | |
| 129 | + | |
| 130 | + IF t_error = 1 THEN | |
| 131 | + ROLLBACK; | |
| 132 | + ELSE | |
| 133 | + COMMIT; | |
| 134 | + END IF; | |
| 135 | + select t_error; | |
| 136 | + END | |
| 137 | + ]] | |
| 138 | + | |
| 139 | + mysqlproxy:query "DROP PROCEDURE IF EXISTS `del_friends`" | |
| 140 | + mysqlproxy:query [[ | |
| 141 | + CREATE PROCEDURE `del_friends`(IN role_id bigint, IN friend_id bigint) | |
| 142 | + BEGIN | |
| 143 | + DECLARE t_error INTEGER DEFAULT 0; | |
| 144 | + DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; | |
| 145 | + | |
| 146 | + START TRANSACTION; | |
| 147 | + DELETE FROM `Friend` WHERE `roleid` = role_id AND `fid` = friend_id; | |
| 148 | + DELETE FROM `Friend` WHERE `roleid` = friend_id AND `fid` = role_id; | |
| 149 | + | |
| 150 | + IF t_error = 1 THEN | |
| 151 | + ROLLBACK; | |
| 152 | + ELSE | |
| 153 | + COMMIT; | |
| 154 | + END IF; | |
| 155 | + select t_error; | |
| 156 | + END | |
| 157 | + ]] | |
| 158 | +end | |
| 159 | + | |
| 114 | 160 | local steps = { |
| 115 | 161 | [1] = { |
| 116 | 162 | handler = initServerDatabase, |
| ... | ... | @@ -127,9 +173,35 @@ local steps = { |
| 127 | 173 | [4] = { |
| 128 | 174 | handler = checkRoleTables, |
| 129 | 175 | desc = "check role tables " |
| 130 | - } | |
| 176 | + }, | |
| 177 | + [5] = { | |
| 178 | + handler = createMysqlSp, | |
| 179 | + desc = "create mysql store procedure " | |
| 180 | + }, | |
| 131 | 181 | } |
| 132 | 182 | |
| 183 | +local function loadAllUserInfo() | |
| 184 | + local maxId = 0 | |
| 185 | + local sql = "SELECT `id`, `uid`, `name` FROM `Role` WHERE `id` > %d ORDER BY `id` LIMIT 1000;" | |
| 186 | + while true do | |
| 187 | + local res = mysqlproxy:query(string.format(sql, maxId)) | |
| 188 | + if not next(res) then | |
| 189 | + return | |
| 190 | + else | |
| 191 | + for _, info in ipairs(res) do | |
| 192 | + if info["id"] > maxId then | |
| 193 | + maxId = info["id"] | |
| 194 | + end | |
| 195 | + redisproxy:pipelining(function (red) | |
| 196 | + local dbName = string.upper(info["name"]) | |
| 197 | + red:set(string.format("user:%s", dbName), info["id"]) | |
| 198 | + red:set(string.format("uid:%s", info["uid"]), dbName) | |
| 199 | + end) | |
| 200 | + end | |
| 201 | + end | |
| 202 | + end | |
| 203 | +end | |
| 204 | + | |
| 133 | 205 | skynet.start(function () |
| 134 | 206 | --local new = redisproxy:hsetnx("autoincrement_set", "server_start", os.date("%Y%m%d", skynet.timex())) == 1 |
| 135 | 207 | --if not new then |
| ... | ... | @@ -146,5 +218,6 @@ skynet.start(function () |
| 146 | 218 | print(action.desc .. "finished ...") |
| 147 | 219 | end |
| 148 | 220 | initRedisDb() |
| 221 | + loadAllUserInfo() | |
| 149 | 222 | skynet.exit() |
| 150 | 223 | end) |
| 151 | 224 | \ No newline at end of file | ... | ... |
src/services/globald.lua
| ... | ... | @@ -171,6 +171,8 @@ local function save_autoincrement_timer() |
| 171 | 171 | saveUidToMysql("autoincrement_set", "email") |
| 172 | 172 | saveUidToMysql("autoincrement_set", "emailTimestamp") |
| 173 | 173 | saveUidToMysql("autoincrement_set", "delay_email") |
| 174 | + saveUidToMysql("autoincrement_set", "stopcreate") | |
| 175 | + saveUidToMysql("autoincrement_set", "maintain") | |
| 174 | 176 | |
| 175 | 177 | skynet.timeout(SAVE_AUTOINCREMENT_SET_INTERVAL, save_autoincrement_timer) |
| 176 | 178 | end | ... | ... |
src/services/mysqld.lua
| ... | ... | @@ -31,6 +31,8 @@ skynet.start(function() |
| 31 | 31 | if cmd == "open" then |
| 32 | 32 | local f = command[string.lower(cmd)] |
| 33 | 33 | skynet.ret(skynet.pack(f(...))) |
| 34 | + elseif string.lower(cmd) == "quote_sql_str" then | |
| 35 | + skynet.ret(skynet.pack(db[string.lower(cmd)](...))) | |
| 34 | 36 | else |
| 35 | 37 | skynet.ret(skynet.pack(db[string.lower(cmd)](db, ...))) |
| 36 | 38 | end | ... | ... |
src/shared/ModelBaseMysql.lua
| ... | ... | @@ -141,6 +141,10 @@ function ModelBaseMysql:save() |
| 141 | 141 | end |
| 142 | 142 | sql = string_format(sql, tbName, key_list, value_list, update_list) |
| 143 | 143 | local res = mysqlproxy:query(sql) |
| 144 | + if res["errno"] then | |
| 145 | + skynet.error(sql) | |
| 146 | + skynet.error(res["error"]) | |
| 147 | + end | |
| 144 | 148 | end |
| 145 | 149 | end |
| 146 | 150 | |
| ... | ... | @@ -228,7 +232,7 @@ function ModelBaseMysql:getProperty(property) |
| 228 | 232 | return self:getProperties({property})[property] |
| 229 | 233 | end |
| 230 | 234 | |
| 231 | -function ModelBaseMysql:setProperty(property, value) | |
| 235 | +function ModelBaseMysql:setProperty(property, value, forceSave) | |
| 232 | 236 | if not self.class.schema[property] then |
| 233 | 237 | print(string_format("%s [%s:setProperty()] Invalid property : %s", |
| 234 | 238 | tostring(self), self.class.__cname, property)) |
| ... | ... | @@ -245,13 +249,21 @@ function ModelBaseMysql:setProperty(property, value) |
| 245 | 249 | assert(type(value) == typ, |
| 246 | 250 | string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", |
| 247 | 251 | tostring(self), self.class.__cname, property, typ, type(value))) |
| 248 | - self[propname] = value | |
| 249 | 252 | |
| 250 | - self:save() | |
| 253 | + if typ == "number" or typ == "string" then | |
| 254 | + if self[propname] == value then | |
| 255 | + return | |
| 256 | + end | |
| 257 | + end | |
| 258 | + self[propname] = value | |
| 259 | + --self:save() | |
| 260 | + self.cacheFields[property] = self[propname] | |
| 261 | + if forceSave then | |
| 262 | + self:update() | |
| 263 | + end | |
| 251 | 264 | end |
| 252 | 265 | |
| 253 | -function ModelBaseMysql:setProperties(fields) | |
| 254 | - local result = {} | |
| 266 | +function ModelBaseMysql:setProperties(fields, forceSave) | |
| 255 | 267 | for property, value in pairs(fields) do |
| 256 | 268 | if not self.class.schema[property] then |
| 257 | 269 | print(string_format("%s [%s:setProperty()] Invalid property : %s", |
| ... | ... | @@ -266,20 +278,23 @@ function ModelBaseMysql:setProperties(fields) |
| 266 | 278 | assert(type(value) == typ, |
| 267 | 279 | string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", |
| 268 | 280 | tostring(self), self.class.__cname, property, typ, type(value))) |
| 269 | - self[propname] = value | |
| 270 | 281 | |
| 271 | - table_insert(result, property) | |
| 272 | - if typ == "table" then | |
| 273 | - table_insert(result, MsgPack.pack(self[propname])) | |
| 274 | - else | |
| 275 | - table_insert(result, self[propname]) | |
| 282 | + if typ == "number" or typ == "string" then | |
| 283 | + if self[propname] == value then | |
| 284 | + return | |
| 285 | + end | |
| 276 | 286 | end |
| 287 | + self[propname] = value | |
| 288 | + self.cacheFields[property] = self[propname] | |
| 277 | 289 | end |
| 278 | 290 | end |
| 279 | - self:save() | |
| 291 | + if forceSave then | |
| 292 | + self:update() | |
| 293 | + end | |
| 294 | + --self:save() | |
| 280 | 295 | end |
| 281 | 296 | |
| 282 | -function ModelBaseMysql:incrProperty(property, value) | |
| 297 | +function ModelBaseMysql:incrProperty(property, value, forceSave) | |
| 283 | 298 | if not self.class.schema[property] then |
| 284 | 299 | print(string_format("%s [%s:setProperty()] Invalid property : %s", |
| 285 | 300 | tostring(self), self.class.__cname, property)) |
| ... | ... | @@ -289,15 +304,10 @@ function ModelBaseMysql:incrProperty(property, value) |
| 289 | 304 | local typ, def = table_unpack(self.class.schema[property]) |
| 290 | 305 | local propname = property .. "_" |
| 291 | 306 | |
| 292 | - if typ == "table" then return end | |
| 307 | + if typ == "table" or typ == "string" then return end | |
| 293 | 308 | if typ == "number" then value = tonumber(value) end |
| 294 | 309 | |
| 295 | - assert(type(value) == typ, | |
| 296 | - string_format("%s [%s:setProperties()] Type mismatch, %s expected %s, actual is %s", | |
| 297 | - tostring(self), self.class.__cname, property, typ, type(value))) | |
| 298 | - self[propname] = self[propname] + value | |
| 299 | - | |
| 300 | - self:save() | |
| 310 | + self:setProperty(property, self[propname] + value, forceSave) | |
| 301 | 311 | end |
| 302 | 312 | |
| 303 | 313 | function ModelBaseMysql:onLoad() |
| ... | ... | @@ -438,4 +448,65 @@ function ModelBaseMysql:checkTableSchema() |
| 438 | 448 | |
| 439 | 449 | end |
| 440 | 450 | |
| 451 | +function ModelBaseMysql:loadFields(fields) | |
| 452 | + if not self:isValidKey() then | |
| 453 | + print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname)) | |
| 454 | + return | |
| 455 | + end | |
| 456 | + if not next(fields) then | |
| 457 | + return | |
| 458 | + end | |
| 459 | + local final = {} | |
| 460 | + for _, v in ipairs(fields) do | |
| 461 | + table.insert(final, '`'..v..'`') | |
| 462 | + end | |
| 463 | + local field_list = table.concat(final, ",") | |
| 464 | + local res = mysqlproxy:query(string_format("SELECT %s from `%s` where `%s` = %s;", field_list, self.class.__cname, self.pri_key, self:getKey())) | |
| 465 | + if res["errno"] then | |
| 466 | + return | |
| 467 | + end | |
| 468 | + | |
| 469 | + return res[1] | |
| 470 | +end | |
| 471 | + | |
| 472 | +function ModelBaseMysql:update() | |
| 473 | + if next(self.cacheFields) then | |
| 474 | + self:updateFields(self.cacheFields) | |
| 475 | + self.cacheFields = {} | |
| 476 | + end | |
| 477 | +end | |
| 478 | + | |
| 479 | +function ModelBaseMysql:updateFields(fields) | |
| 480 | + local params = {} | |
| 481 | + for field, value in pairs(fields) do | |
| 482 | + if self.class.schema[field][1] == "table" then | |
| 483 | + if next(value) then | |
| 484 | + local result = mysqlproxy:quote_sql_str(MsgPack.pack(value)) | |
| 485 | + params[field] = result | |
| 486 | + end | |
| 487 | + elseif self.class.schema[field][1] == "string" then | |
| 488 | + local result = mysqlproxy:quote_sql_str(value) | |
| 489 | + params[field] = result | |
| 490 | + else | |
| 491 | + params[field] = value | |
| 492 | + end | |
| 493 | + end | |
| 494 | + if next(params) then | |
| 495 | + local sql = "UPDATE `%s` SET %s WHERE `%s` = %s;" | |
| 496 | + local tbName = self.class.__cname | |
| 497 | + local tmp = {} | |
| 498 | + for k, v in pairs(params) do | |
| 499 | + table.insert(tmp, '`' .. k .. '` = ' .. v) | |
| 500 | + end | |
| 501 | + sql = string_format(sql, tbName, table.concat(tmp, ","), self.pri_key, self:getKey()) | |
| 502 | + local res = mysqlproxy:query(sql) | |
| 503 | + if res["errno"] then | |
| 504 | + skynet.error(sql) | |
| 505 | + skynet.error(res["error"]) | |
| 506 | + return false | |
| 507 | + end | |
| 508 | + end | |
| 509 | + return true | |
| 510 | +end | |
| 511 | + | |
| 441 | 512 | return ModelBaseMysql |
| 442 | 513 | \ No newline at end of file | ... | ... |
src/utils/MysqlUtil.lua
| ... | ... | @@ -16,4 +16,42 @@ function setDbCfgVal(tbName, keyName, fieldName, fieldVal) |
| 16 | 16 | if type(fieldVal) == "string" then fieldVal = string.format("'%s'", fieldVal) end |
| 17 | 17 | local sql = string.format("UPDATE `%s` SET `%s` = %s WHERE `key` = '%s';", tbName, fieldName, fieldVal, keyName) |
| 18 | 18 | mysqlproxy:query(sql) |
| 19 | +end | |
| 20 | + | |
| 21 | +function getFriendCount(roleId) | |
| 22 | + local res = mysqlproxy:query(string.format("SELECT `id` FROM `Friend` WHERE `roleid` = %s", roleId)) | |
| 23 | + if res["errno"] then | |
| 24 | + return globalCsv.friendListLimit | |
| 25 | + end | |
| 26 | + | |
| 27 | + return #res | |
| 28 | +end | |
| 29 | + | |
| 30 | +function addFriend(roleId, friendId) | |
| 31 | + local stmt_csp = mysqlproxy:prepare("call add_friends(?, ?, ?)") | |
| 32 | + local r = mysqlproxy:execute(stmt_csp, roleId, friendId, skynet.timex()) | |
| 33 | + if r[1][1]["t_error"] == 0 then | |
| 34 | + rpcRole(roleId, "addFriend", friendId) | |
| 35 | + rpcRole(friendId, "addFriend", roleId) | |
| 36 | + end | |
| 37 | + dump(r) | |
| 38 | +end | |
| 39 | + | |
| 40 | +function delFriend(roleId, friendId) | |
| 41 | + local stmt_csp = mysqlproxy:prepare("call del_friends(?, ?)") | |
| 42 | + local r = mysqlproxy:execute(stmt_csp, roleId, friendId) | |
| 43 | + if r[1][1]["t_error"] == 0 then | |
| 44 | + rpcRole(roleId, "delFriend", friendId) | |
| 45 | + rpcRole(friendId, "delFriend", roleId) | |
| 46 | + end | |
| 47 | + dump(r) | |
| 48 | +end | |
| 49 | + | |
| 50 | +function roleExists(roleId) | |
| 51 | + local res = mysqlproxy:query(string.format("SELECT `id` FROM `Role` WHERE `id` = %s", roleId)) | |
| 52 | + if res["errno"] or not next(res) then | |
| 53 | + return false | |
| 54 | + end | |
| 55 | + | |
| 56 | + return true | |
| 19 | 57 | end |
| 20 | 58 | \ No newline at end of file | ... | ... |