Illustrate.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. Illustrate = {}
  2. SYS_ILLUSTRATEINFO = "T$illustrate_info"
  3. SYS_ILLUSTRATEHISTORY = "G$illustrate_history"
  4. SYS_ILLUSTRATEHISTORYSELF = "T$illustrate_history_self"
  5. SYS_ILLUSTRATEATTRS = "T$illustrate_attrs"
  6. local HistoryLen = 50
  7. -- function Illustrate.onLoginEnd(actor)
  8. -- end
  9. -- 1:获取info信息. 2:获得全服召唤记录
  10. -- 10:召唤
  11. function Illustrate.onHandlereQuest(actor, msgid, sMsg)
  12. local type = sMsg.type
  13. if type == 1 then
  14. Illustrate.getMainInfo(actor, msgid)
  15. elseif type == 10 then
  16. Illustrate.onSummon(actor, msgid)
  17. elseif type == 20 then
  18. Illustrate.onRefresh(actor, msgid)
  19. elseif type == 30 then
  20. Illustrate.onHistorySelf(actor, msgid)
  21. elseif type == 31 then
  22. Illustrate.onHistory(actor, msgid)
  23. elseif type == 40 then
  24. Illustrate.onAcviveItem(actor, msgid, sMsg)
  25. elseif type == 50 then
  26. Illustrate.onAcviveGroup(actor, msgid, sMsg)
  27. end
  28. end
  29. function Illustrate.getInfo(actor)
  30. local info = APIGetTable(actor, SYS_ILLUSTRATEINFO) or {}
  31. local isInited = true
  32. -- 召唤次数
  33. if info.summonNum == nil then
  34. info.summonNum = 0
  35. isInited = false
  36. end
  37. -- 刷新次数
  38. if info.refreshNum == nil then
  39. info.refreshNum = 0
  40. isInited = false
  41. end
  42. -- 召唤的物品
  43. if info.items == nil or table.count(info.items) <= 0 then
  44. info.items = Illustrate.getItems()
  45. isInited = false
  46. end
  47. if info.activeitems == nil then
  48. info.activeitems = {}
  49. end
  50. if info.activegroups == nil then
  51. -- {[main][sub]=endtime}
  52. info.activegroups = {}
  53. end
  54. if isInited ~= true then
  55. Illustrate.setInfo(actor, info)
  56. end
  57. return info
  58. end
  59. function Illustrate.setInfo(actor, info)
  60. APISetTable(actor, SYS_ILLUSTRATEINFO, info)
  61. end
  62. function Illustrate.getHistorySelf(actor)
  63. local info = APIGetTable(actor, SYS_ILLUSTRATEHISTORYSELF) or {}
  64. return info
  65. end
  66. function Illustrate.setHistorySelf(actor, history)
  67. APISetTable(actor, SYS_ILLUSTRATEHISTORYSELF, history)
  68. end
  69. function Illustrate.getHistory()
  70. local info = getsysvar(SYS_ILLUSTRATEHISTORY) or {}
  71. return info
  72. end
  73. function Illustrate.setHistory(history)
  74. setsysvar(SYS_ILLUSTRATEHISTORY, history)
  75. end
  76. function Illustrate.getItems()
  77. local items = {}
  78. local group1 = {}
  79. local group2 = {}
  80. local groupTotal1 = 0
  81. local groupTotal2 = 0
  82. local cfgs = ConfigDataManager.getList("cfg_illustrate_summon")
  83. for k, v in pairs(cfgs) do
  84. if v.group == "1" then
  85. groupTotal1 = groupTotal1 + tonumber(v.weightshow)
  86. table.insert(
  87. group1,
  88. {
  89. id = v.id,
  90. weighted = tonumber(v.weightshow)
  91. }
  92. )
  93. elseif v.group == "2" then
  94. groupTotal2 = groupTotal2 + tonumber(v.weightshow)
  95. table.insert(
  96. group2,
  97. {
  98. id = v.id,
  99. weighted = tonumber(v.weightshow)
  100. }
  101. )
  102. end
  103. end
  104. -- 配置出错
  105. if table.count(group1) < 1 or table.count(group2) < 1 then
  106. return {}
  107. end
  108. local item1 = Illustrate.RandomWeightedOne(group1, groupTotal1)
  109. if item1 ~= nil then
  110. table.insert(items, item1.id)
  111. end
  112. for i = 1, 12 do
  113. local item2 = Illustrate.RandomWeightedOne(group2, groupTotal2)
  114. -- 配置出错
  115. if item2 == nil then
  116. return {}
  117. end
  118. item2.isSelected = true
  119. groupTotal2 = groupTotal2 - item2.weighted
  120. table.insert(items, item2.id)
  121. end
  122. return items
  123. end
  124. function Illustrate.getMainInfo(actor, msgid)
  125. local info = Illustrate.getInfo(actor)
  126. sendluamsg(
  127. actor,
  128. msgid,
  129. {
  130. type = 1,
  131. info = info
  132. }
  133. )
  134. end
  135. function Illustrate.onSummon(actor, msgid)
  136. local info = Illustrate.getInfo(actor)
  137. local weightgetTotal = 0
  138. local items = {}
  139. for k, v in pairs(info.items) do
  140. local cfg = ConfigDataManager.getById("cfg_illustrate_summon", tonumber(v))
  141. -- 可能策划改配置了
  142. if cfg ~= nil then
  143. table.insert(
  144. items,
  145. {
  146. id = v,
  147. weighted = tonumber(cfg.weightget)
  148. }
  149. )
  150. weightgetTotal = weightgetTotal + tonumber(cfg.weightget)
  151. end
  152. end
  153. local reward = Illustrate.RandomWeightedOne(items, weightgetTotal)
  154. if reward == nil then
  155. tipinfo(actor, "1001#召唤失败!")
  156. return
  157. end
  158. -- 扣除消耗
  159. local summonNum = info.summonNum
  160. local cfgCost = nil
  161. local cfgs = ConfigDataManager.getList("cfg_illustrate_cost")
  162. for i, v in ipairs(cfgs) do
  163. local group = tonumber(v.group)
  164. local num = tonumber(v.num) - 1
  165. if group == 1 and cfgCost == nil then
  166. cfgCost = v
  167. elseif group == 1 and summonNum >= num then
  168. cfgCost = v
  169. end
  170. end
  171. if cfgCost == nil then
  172. tipinfo(actor, "1002#召唤扣除失败!")
  173. return
  174. end
  175. local cost_map = {}
  176. if not string.isNullOrEmpty(cfgCost.costs) then
  177. string.putIntIntMap(cost_map, cfgCost.costs, "#", "|")
  178. end
  179. if table.count(cost_map) <= 0 then
  180. tipinfo(actor, "1002#召唤扣除失败!")
  181. return
  182. end
  183. if not API.CheckItemMap(actor, cost_map) then
  184. tipinfo(actor, "材料不足")
  185. return
  186. end
  187. API.TakeItemMap(actor, cost_map)
  188. info.summonNum = info.summonNum + 1
  189. Illustrate.setInfo(actor, info)
  190. local cfg = ConfigDataManager.getById("cfg_illustrate_summon", reward.id)
  191. local tmpcosts = {}
  192. string.putIntIntMap(tmpcosts, cfg.items, "#", "|")
  193. if table.count(tmpcosts) > 0 then
  194. Bag.addItemMapToBag(actor, tmpcosts, 0, 9999, "图鉴召唤")
  195. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, tmpcosts)
  196. end
  197. if cfg.mark == "1" then
  198. local playerName = getbaseinfo(actor, "rolename")
  199. local historySelf = Illustrate.getHistorySelf(actor)
  200. local history = Illustrate.getHistory()
  201. for k, v in pairs(tmpcosts) do
  202. local data = {
  203. cfgId = k,
  204. name = playerName
  205. }
  206. API.InsertAndKeepLen(historySelf, data, HistoryLen)
  207. API.InsertAndKeepLen(history, data, HistoryLen)
  208. end
  209. Illustrate.setHistory(history)
  210. Illustrate.setHistorySelf(actor, historySelf)
  211. end
  212. -- 发放奖励
  213. sendluamsg(
  214. actor,
  215. msgid,
  216. {
  217. type = 10,
  218. cfgid = reward.id
  219. }
  220. )
  221. Illustrate.getMainInfo(actor, msgid)
  222. Illustrate.onHistorySelf(actor, msgid)
  223. Illustrate.onHistory(actor, msgid)
  224. end
  225. function Illustrate.RandomWeightedOne(datasWeighteds, weightedTotal)
  226. local random_weight = math.random(1, weightedTotal)
  227. for i, v in ipairs(datasWeighteds) do
  228. if v.isSelected == true then
  229. else
  230. if random_weight <= v.weighted then
  231. return v
  232. end
  233. random_weight = random_weight - v.weighted
  234. end
  235. end
  236. return datasWeighteds[1]
  237. end
  238. function Illustrate.onRefresh(actor, msgid)
  239. -- 判断消耗
  240. local info = Illustrate.getInfo(actor)
  241. -- 扣除消耗
  242. local refreshNum = info.refreshNum
  243. local cfgCost = nil
  244. local cfgs = ConfigDataManager.getList("cfg_illustrate_cost")
  245. for i, v in ipairs(cfgs) do
  246. local group = tonumber(v.group)
  247. local num = tonumber(v.num) - 1
  248. if group == 2 and cfgCost == nil then
  249. cfgCost = v
  250. elseif group == 2 and refreshNum >= num then
  251. cfgCost = v
  252. end
  253. end
  254. if cfgCost == nil then
  255. tipinfo(actor, "1002#刷新扣除失败!")
  256. return
  257. end
  258. local cost_map = {}
  259. if not string.isNullOrEmpty(cfgCost.costs) then
  260. string.putIntIntMap(cost_map, cfgCost.costs, "#", "|")
  261. end
  262. if table.count(cost_map) <= 0 then
  263. tipinfo(actor, "1002#刷新扣除失败!")
  264. return
  265. end
  266. if not API.CheckItemMap(actor, cost_map) then
  267. tipinfo(actor, "材料不足")
  268. return
  269. end
  270. API.TakeItemMap(actor, cost_map)
  271. info.refreshNum = info.refreshNum + 1
  272. info.items = Illustrate.getItems()
  273. Illustrate.setInfo(actor, info)
  274. sendluamsg(
  275. actor,
  276. msgid,
  277. {
  278. type = 1,
  279. info = info
  280. }
  281. )
  282. end
  283. -- 历史记录.保存玩家抽奖信息
  284. -- index
  285. -- name
  286. -- cfgId
  287. function Illustrate.onHistorySelf(actor, msgid)
  288. local historys = Illustrate.getHistorySelf(actor)
  289. sendluamsg(
  290. actor,
  291. msgid,
  292. {
  293. type = 31,
  294. historys = historys
  295. }
  296. )
  297. -- API.InsertAndKeepLen(arr, value, len)
  298. end
  299. function Illustrate.onHistory(actor, msgid)
  300. local historys = Illustrate.getHistory()
  301. sendluamsg(
  302. actor,
  303. msgid,
  304. {
  305. type = 30,
  306. historys = historys
  307. }
  308. )
  309. end
  310. function Illustrate.onAcviveItem(actor, msgid, sMsg)
  311. local cfgid = sMsg.cfgid
  312. local info = Illustrate.getInfo(actor)
  313. if info.activeitems[cfgid] ~= nil and info.activeitems[cfgid].active == 1 then
  314. tipinfo(actor, "不可重复激活")
  315. return
  316. end
  317. local cfg = ConfigDataManager.getById("cfg_illustrate", cfgid)
  318. if cfg == nil then
  319. tipinfo(actor, "配置错误")
  320. return
  321. end
  322. -- 判断消耗
  323. local cost_map = {}
  324. if not string.isNullOrEmpty(cfg.items) then
  325. string.putIntIntMap(cost_map, cfg.items, "#", "|")
  326. end
  327. if not API.CheckItemMap(actor, cost_map) then
  328. tipinfo(actor, "材料不足")
  329. return
  330. end
  331. API.TakeItemMap(actor, cost_map)
  332. if info.activeitems[cfgid] ~= nil then
  333. info.activeitems[cfgid].active = 1
  334. else
  335. info.activeitems[cfgid] = {active = 1}
  336. end
  337. -- table.insert(info.activeitems, {
  338. -- active = 1,
  339. -- })
  340. Illustrate.setInfo(actor, info)
  341. sendluamsg(
  342. actor,
  343. msgid,
  344. {
  345. type = 1,
  346. info = info
  347. }
  348. )
  349. end
  350. function Illustrate.onAcviveGroup(actor, msgid, sMsg)
  351. local main = sMsg.main
  352. local sub = sMsg.sub
  353. local nowsec = getbaseinfo(actor, "nowsec")
  354. local info = Illustrate.getInfo(actor)
  355. local activegroupsec =
  356. info.activegroups[main] ~= nil and info.activegroups[main][sub] ~= nil and info.activegroups[main][sub] or nil
  357. if activegroupsec ~= nil and activegroupsec > nowsec then
  358. tipinfo(actor, "无法重复激活图鉴")
  359. return
  360. end
  361. local cfgs = ConfigDataManager.getTable("cfg_illustrate", "group", main, "subgroup", sub)
  362. if table.count(cfgs) <= 0 then
  363. tipinfo(actor, "配置出错")
  364. return
  365. end
  366. local cfgMain = nil
  367. local isActiveAll = true
  368. for i, v in ipairs(cfgs) do
  369. if not string.isNullOrEmpty(v.groupattrs) then
  370. cfgMain = v
  371. end
  372. if info.activeitems[tonumber(v.id)] == nil or info.activeitems[tonumber(v.id)].active ~= 1 then
  373. isActiveAll = false
  374. end
  375. end
  376. if cfgMain == nil then
  377. tipinfo(actor, "配置出错")
  378. return
  379. end
  380. if isActiveAll ~= true then
  381. tipinfo(actor, "图鉴未收集完成")
  382. return
  383. end
  384. -- 判断消耗
  385. local cost_map = {}
  386. if not string.isNullOrEmpty(cfgMain.groupitems) then
  387. string.putIntIntMap(cost_map, cfgMain.groupitems, "#", "|")
  388. end
  389. if not API.CheckItemMap(actor, cost_map) then
  390. tipinfo(actor, "材料不足")
  391. return
  392. end
  393. API.TakeItemMap(actor, cost_map)
  394. if info.activegroups[main] == nil then
  395. info.activegroups[main] = {}
  396. end
  397. info.activegroups[main][sub] = (nowsec + tonumber(cfgMain.grouptime))
  398. Illustrate.setInfo(actor, info)
  399. Illustrate.updateattrs(actor)
  400. sendluamsg(
  401. actor,
  402. msgid,
  403. {
  404. type = 1,
  405. info = info
  406. }
  407. )
  408. -- if info.activeitems == nil then
  409. -- info.activeitems = {}
  410. -- end
  411. -- if info.activegroups == nil then
  412. -- -- {[main][sub]=endtime}
  413. -- info.activegroups = {}
  414. -- end
  415. end
  416. function Illustrate.updateattrs(actor)
  417. local info = APIGetTable(actor, SYS_ILLUSTRATEINFO)
  418. if info == nil or info.activegroups == nil then
  419. return
  420. end
  421. local nowsec = getbaseinfo(actor, "nowsec")
  422. local attrs = APIGetTable(actor, SYS_ILLUSTRATEATTRS) or {}
  423. local isbreak = false
  424. local ischange = false
  425. for mk, mv in pairs(info.activegroups) do
  426. if attrs[mk] == nil then
  427. ischange = true
  428. break
  429. end
  430. for sk, sv in pairs(mv) do
  431. if attrs[mk][sk] == nil then
  432. ischange = true
  433. break
  434. end
  435. if sv < nowsec then
  436. ischange = true
  437. break
  438. end
  439. end
  440. if isbreak == true then
  441. break
  442. end
  443. end
  444. if ischange ~= true then
  445. return
  446. end
  447. APISetTable(actor, SYS_ILLUSTRATEATTRS, attrs)
  448. attrs = {}
  449. local addAttrs = {}
  450. -- 删除过期的属性
  451. for mk, mv in pairs(info.activegroups) do
  452. for sk, sv in pairs(mv) do
  453. if sv < nowsec then
  454. info.activegroups[mk][sk] = nil
  455. end
  456. end
  457. end
  458. local cfgs = ConfigDataManager.getTable("cfg_illustrate")
  459. for mk, mv in pairs(info.activegroups) do
  460. if attrs[mk] == nil then
  461. attrs[mk] = {}
  462. end
  463. for sk, sv in pairs(mv) do
  464. local findIndex, cfg =
  465. table.findByCondi(
  466. cfgs,
  467. function(a)
  468. return tonumber(a.group) == mk and tonumber(a.subgroup) == sk and
  469. not string.isNullOrEmpty(a.groupattrs)
  470. end
  471. )
  472. if cfg ~= nil then
  473. attrs[mk][sk] = 1
  474. local groupattrs = string.split(cfg.groupattrs, "|")
  475. for i, v in ipairs(groupattrs) do
  476. local av = string.split(v, "#")
  477. local attrId = av[1]
  478. local attrValue = av[2]
  479. if addAttrs[attrId] == nil then
  480. addAttrs[attrId] = attrValue
  481. else
  482. addAttrs[attrId] = tostring(tonumber(addAttrs[attrId]) + tonumber(attrValue))
  483. end
  484. end
  485. end
  486. end
  487. end
  488. addrolekmlattributes(actor, "illustrategroupattrs", addAttrs)
  489. end