CreateRole.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ----------------------
  2. -- 创角相关逻辑
  3. ----------------------
  4. local Config = require("Config")
  5. local PlayerNamesExcel = require("excel.playerNames")
  6. local CommonDB = require("common.CommonDB")
  7. local Lang = require("common.Lang")
  8. local FilterUtil = require("common.FilterUtil")
  9. local Log = require("common.Log")
  10. local LogDefine = require("common.LogDefine")
  11. local CommonDefine = require("common.CommonDefine")
  12. local Msg = require("core.Msg")
  13. local ObjHuman = require("core.ObjHuman")
  14. local RoleDBLogic = require("role.RoleDBLogic")
  15. local RoleHeadLogic = require("role.RoleHeadLogic")
  16. local Broadcast = require("broadcast.Broadcast")
  17. local RoleExcel = require("excel.role")
  18. local ReportManager = require("platform.ReportManager")
  19. local HeroLogic = require("hero.HeroLogic")
  20. local RelationGiftLogic = require("present.RelationGiftLogic")
  21. local CombatDefine = require("combat.CombatDefine")
  22. local CombatPosLogic = require("combat.CombatPosLogic")
  23. local SysParameter = require("common.SysParameter")
  24. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  25. local MailExcel = require("excel.mail")
  26. local MailManager = require("mail.MailManager")
  27. local ROLE_NAME_LEN_MAX = 21
  28. local ROLE_NAME_LEN_MIN = 5
  29. local OP_RET_SUCC = 1
  30. local OP_RET_FAIL = 0
  31. -- 生成一个11位的全局不重复的数字组成的id
  32. -- 规则 前6位服务器index 后五位自增id
  33. function genIdentity()
  34. local firstStr= Config.SVR_INDEX
  35. local nowMax = CommonDB.getIdentityMax()
  36. nowMax = nowMax + 1
  37. CommonDB.setIdentityMax(nowMax)
  38. return firstStr .. "" .. nowMax
  39. end
  40. function getFakeIdentityMax()
  41. local firstStr= Config.SVR_INDEX
  42. local id = CommonDB.getFakeIdentityMax()
  43. return firstStr .. string.format("%05d", id)
  44. end
  45. local function checkRoleName(roleName, default)
  46. --如果roleName是nil 游戏名不能为空
  47. if not roleName then
  48. return false, Lang.CREATE_ROLE_FAIL_NAME_NIL
  49. end
  50. local nameLen = string.len(roleName)
  51. --如果游戏名长度小于游戏名最小长度 角色名过短
  52. if nameLen < ROLE_NAME_LEN_MIN then
  53. return false, Lang.CREATE_ROLE_FAIL_NAME_LEN1
  54. end
  55. --如果游戏名长度大于游戏名最小长度 角色名过长
  56. if nameLen > ROLE_NAME_LEN_MAX then
  57. return false, Lang.CREATE_ROLE_FAIL_NAME_LEN2
  58. end
  59. if default == nil then
  60. --如果游戏名中包含非法字符 角色名中有非法字符
  61. if roleName ~= FilterUtil.filterName(roleName) then
  62. return false, Lang.CREATE_ROLE_FAIL_NAME_FILTER
  63. end
  64. end
  65. --角色名已经存在
  66. if RoleDBLogic.isNameExistInDB(roleName) then
  67. return false, Lang.CREATE_ROLE_FAIL_NAME_DUPLICATE
  68. end
  69. return true
  70. end
  71. function creatFakeDb(account)
  72. local db = RoleDBLogic.createDefaultRole(account)
  73. local identity = genIdentity()
  74. db.identity = identity
  75. db.name = account
  76. return db
  77. end
  78. function createNewRole(human)
  79. if _G.is_middle == true then
  80. return
  81. end
  82. if human.db or RoleDBLogic.isAccountExistInDB(human.account) then
  83. assert(nil)
  84. return
  85. end
  86. human.db = RoleDBLogic.createDefaultRole(human.account)
  87. -- 保存数据
  88. human.db.name = human.account
  89. local identity = genIdentity()
  90. human.db.identity = identity
  91. -- 测试 临时增加,用于防沉迷测试
  92. human.db.age = 20
  93. ----------
  94. RelationGiftLogic.relationCreate(human)
  95. ObjHuman.initHuman(human)
  96. ObjHuman.save(human)
  97. ObjHuman.onLogin(human, true)
  98. createRoleSettingUp(human)
  99. ReportManager.register(human)
  100. end
  101. -- 改名操作结果
  102. CHANGE_NAME_RET_FAIL = 0 -- 失败
  103. CHANGE_NAME_RET_OK = 1 -- 成功,首次改名
  104. CHANGE_NAME_RET_OK2 = 2 -- 成功,非首次改名
  105. CHANGE_NAME_COST = 200
  106. function changeName(human, roleName)
  107. local msgRet = Msg.gc.GC_ROLE_CHANGE_NAME
  108. msgRet.ret = CHANGE_NAME_RET_FAIL
  109. local isOk, tip = checkRoleName(roleName)
  110. if not isOk then
  111. msgRet.tip = tip
  112. Msg.send(msgRet, human.fd)
  113. return
  114. end
  115. if not human.db.changeNameCnt then
  116. human.db.changeNameCnt = 1
  117. else
  118. if human.db.zuanshi < CHANGE_NAME_COST then
  119. msgRet.tip = Lang.COMMON_NO_ZUANSHI
  120. Msg.send(msgRet, human.fd)
  121. return
  122. end
  123. human.db.changeNameCnt = human.db.changeNameCnt + 1
  124. ObjHuman.decZuanshi(human, -CHANGE_NAME_COST, "change_name")
  125. end
  126. ObjHuman.onlineHuman[human.db.name] = nil
  127. ObjHuman.onlineHuman[roleName] = human
  128. human.db.name = roleName
  129. ObjHuman.save(human)
  130. msgRet.ret = CHANGE_NAME_RET_OK
  131. if human.db.changeNameCnt > 1 then
  132. msgRet.ret = CHANGE_NAME_RET_OK2
  133. end
  134. msgRet.tip = "ok"
  135. Msg.send(msgRet, human.fd)
  136. if human.db.changeNameCnt == 1 then
  137. Log.write(Log.LOGID_OSS_REGISTER, human.db._id, human.db.account, human.db.name, human.ip,human.pf or "",human.appid, human.db.changeNameCnt)
  138. Log.write(Log.LOGID_OSS_CREATELOSS,human.db.account, human.db.name, LogDefine.HUMAN_LOST.CREATE_NAME_FINISH, human.db.ip,human.pf or "",human.appid, human.phpChanelID)
  139. ReportManager.create(human)
  140. Log.write(Log.LOGID_OSS_LOGIN, human.db._id, human.db.account, human.db.name, human.db.lv, human.db.ip)
  141. end
  142. end
  143. -- 改名查询
  144. function changeNameQuery(human)
  145. local msgRet = Msg.gc.GC_ROLE_CHANGE_NAME_QUERY
  146. if human.db.changeNameCnt == nil then
  147. msgRet.price = 0
  148. Log.write(Log.LOGID_OSS_CREATELOSS,human.db.account, human.db.name, LogDefine.HUMAN_LOST.SHOW_NAME_PANEL, human.db.ip,human.pf or "",human.appid, human.phpChanelID)
  149. else
  150. msgRet.price = CHANGE_NAME_COST
  151. end
  152. Msg.send(msgRet,human.fd)
  153. end
  154. function randomName(human, sex)
  155. local msgRet = Msg.gc.GC_ROLE_RANDOM_NAME
  156. msgRet.sex = sex
  157. msgRet.name = getRandomName(sex)
  158. Msg.send(msgRet, human.fd)
  159. end
  160. function getRandomName(sex)
  161. if not sex then
  162. sex = math.random(1, 2)
  163. end
  164. local random1 = math.random(1, #PlayerNamesExcel.xing)
  165. if sex == 1 then
  166. local random2 = math.random(1, #PlayerNamesExcel.name)
  167. return PlayerNamesExcel.xing[random1].xing..PlayerNamesExcel.name[random2].name
  168. else
  169. local random2 = math.random(1, #PlayerNamesExcel.name2)
  170. return PlayerNamesExcel.xing[random1].xing..PlayerNamesExcel.name2[random2].name
  171. end
  172. end
  173. -- 创角时修改角色信息
  174. function createRoleChangeInfo(human,name,sex,firendCode)
  175. local msgRet = Msg.gc.GC_ROLE_CREATE_ROLE_CHANGE_MSG
  176. local isOk = checkRoleName(name)
  177. if isOk == false then
  178. msgRet.ret = 1
  179. Msg.send(msgRet,human.fd)
  180. return
  181. end
  182. -- 创角
  183. if not human.db.changeNameCnt then
  184. human.db.changeNameCnt = 0
  185. else
  186. if human.db.zuanshi < CHANGE_NAME_COST then
  187. msgRet.ret = 1
  188. Msg.send(msgRet, human.fd)
  189. return
  190. end
  191. human.db.changeNameCnt = human.db.changeNameCnt + 1
  192. ObjHuman.decZuanshi(human, -CHANGE_NAME_COST, "change_name")
  193. end
  194. ObjHuman.onlineHuman[human.db.name] = nil
  195. ObjHuman.onlineHuman[name] = human
  196. human.db.name = name
  197. human.db.sex = sex
  198. RoleHeadLogic.setHead(human, sex, RoleHeadLogic.HEAD_TYPE_1)
  199. ObjHuman.save(human)
  200. ChatPaoMaLogic.loginTips(human)
  201. -- 好友推荐 todo
  202. RelationGiftLogic.relationBind(human, firendCode)
  203. msgRet.ret = 0
  204. Msg.send(msgRet,human.fd)
  205. -- 记录流失日志
  206. if human.db.changeNameCnt == 0 then
  207. Log.write(Log.LOGID_OSS_REGISTER, human.db._id, human.db.account, human.db.name, human.ip,human.pf or "",human.appid, human.db.changeNameCnt)
  208. Log.write(Log.LOGID_OSS_CREATELOSS,human.db.account, human.db.name, LogDefine.HUMAN_LOST.CREATE_NAME_FINISH, human.db.ip,human.pf or "",human.appid, human.phpChanelID)
  209. ReportManager.create(human)
  210. Log.write(Log.LOGID_OSS_LOGIN, human.db._id, human.db.account, human.db.name, human.db.lv, human.db.ip)
  211. end
  212. end
  213. function createRoleSettingUp(human)
  214. -- 赠送新英雄 上阵
  215. local heroId = SysParameter.getSysParameter(SysParameter.PARAMETER_8)
  216. if heroId and heroId ~= 0 then
  217. local bagIndex, uuid = HeroLogic.addHero(human, heroId,nil, 1, "create_role")
  218. if bagIndex and uuid and type(uuid) == "string" then
  219. local msg = {}
  220. msg.type = CombatDefine.COMBAT_TYPE1
  221. msg.formation = 1
  222. msg.heroList = "0,"..uuid
  223. msg.helpList = ""
  224. msg.jibanList = ""
  225. CombatPosLogic.updatePos(human, msg)
  226. end
  227. end
  228. end
  229. RANDOM_HEAD_ID_TABLE = nil
  230. function getRandomHead()
  231. if RANDOM_HEAD_ID_TABLE == nil then
  232. RANDOM_HEAD_ID_TABLE = {}
  233. for k, v in pairs(RoleExcel.head) do
  234. RANDOM_HEAD_ID_TABLE[#RANDOM_HEAD_ID_TABLE + 1] = k
  235. end
  236. end
  237. local index = math.random(1, #RANDOM_HEAD_ID_TABLE)
  238. return RANDOM_HEAD_ID_TABLE[index]
  239. end
  240. RANDOM_BODY_ID_TABLE = nil
  241. function getRandomBody()
  242. if RANDOM_BODY_ID_TABLE == nil then
  243. RANDOM_BODY_ID_TABLE = {}
  244. for k, v in pairs(RoleExcel.body) do
  245. RANDOM_BODY_ID_TABLE[#RANDOM_BODY_ID_TABLE + 1] = k
  246. end
  247. end
  248. local index = math.random(1, #RANDOM_BODY_ID_TABLE)
  249. return RANDOM_BODY_ID_TABLE[index]
  250. end