CreateRole.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = 20
  28. local ROLE_NAME_LEN_MIN = 2
  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 countWordsInThaiString(thaiString)
  46. local thaiPattern = "[%z\xE0-\xEF][\x81-\xBF][\x81-\xBF]" -- UTF-8编码的泰文字符范围
  47. local thaiCount = 0
  48. for thaiChar in string.gmatch(thaiString, thaiPattern) do
  49. thaiCount = thaiCount + 1
  50. end
  51. local count = 0
  52. for i= 1,#thaiString do
  53. local c = string.byte(thaiString, i)
  54. if c < 128 then --ASCII
  55. count = count + 1
  56. end
  57. end
  58. return count + thaiCount
  59. end
  60. local function checkRoleName(roleName, default)
  61. --如果roleName是nil 游戏名不能为空
  62. if not roleName then
  63. return false, Lang.CREATE_ROLE_FAIL_NAME_NIL
  64. end
  65. local nameLen = countWordsInThaiString(roleName)
  66. --如果游戏名长度小于游戏名最小长度 角色名过短
  67. if nameLen < ROLE_NAME_LEN_MIN then
  68. return false, Lang.CREATE_ROLE_FAIL_NAME_LEN1
  69. end
  70. --如果游戏名长度大于游戏名最小长度 角色名过长
  71. if nameLen > ROLE_NAME_LEN_MAX then
  72. return false, Lang.CREATE_ROLE_FAIL_NAME_LEN2
  73. end
  74. if default == nil then
  75. --如果游戏名中包含非法字符 角色名中有非法字符
  76. if roleName ~= FilterUtil.filterName(roleName) then
  77. -- return false, Lang.CREATE_ROLE_FAIL_NAME_FILTER
  78. end
  79. end
  80. --角色名已经存在
  81. if RoleDBLogic.isNameExistInDB(roleName) then
  82. return false, Lang.CREATE_ROLE_FAIL_NAME_DUPLICATE
  83. end
  84. return true
  85. end
  86. function creatFakeDb(account)
  87. local db = RoleDBLogic.createDefaultRole(account)
  88. local identity = genIdentity()
  89. db.identity = identity
  90. db.name = account
  91. return db
  92. end
  93. function createNewRole(human)
  94. if _G.is_middle == true then
  95. return
  96. end
  97. if human.db or RoleDBLogic.isAccountExistInDB(human.account) then
  98. assert(nil)
  99. return
  100. end
  101. human.db = RoleDBLogic.createDefaultRole(human.account)
  102. -- 保存数据
  103. human.db.name = human.account
  104. local identity = genIdentity()
  105. human.db.identity = identity
  106. -- 测试 临时增加,用于防沉迷测试
  107. human.db.age = 20
  108. ----------
  109. RelationGiftLogic.relationCreate(human)
  110. ObjHuman.initHuman(human)
  111. ObjHuman.save(human)
  112. ObjHuman.onLogin(human, true)
  113. createRoleSettingUp(human)
  114. ReportManager.register(human)
  115. end
  116. -- 改名操作结果
  117. CHANGE_NAME_RET_FAIL = 0 -- 失败
  118. CHANGE_NAME_RET_OK = 1 -- 成功,首次改名
  119. CHANGE_NAME_RET_OK2 = 2 -- 成功,非首次改名
  120. CHANGE_NAME_COST = 200
  121. function changeName(human, roleName)
  122. local msgRet = Msg.gc.GC_ROLE_CHANGE_NAME
  123. msgRet.ret = CHANGE_NAME_RET_FAIL
  124. local isOk, tip = checkRoleName(roleName)
  125. if not isOk then
  126. msgRet.tip = tip
  127. Msg.send(msgRet, human.fd)
  128. return
  129. end
  130. if not human.db.changeNameCnt then
  131. human.db.changeNameCnt = 1
  132. else
  133. if human.db.zuanshi < CHANGE_NAME_COST then
  134. msgRet.tip = Lang.COMMON_NO_ZUANSHI
  135. Msg.send(msgRet, human.fd)
  136. return
  137. end
  138. human.db.changeNameCnt = human.db.changeNameCnt + 1
  139. ObjHuman.decZuanshi(human, -CHANGE_NAME_COST, "change_name")
  140. end
  141. ObjHuman.onlineHuman[human.db.name] = nil
  142. ObjHuman.onlineHuman[roleName] = human
  143. human.db.name = roleName
  144. ObjHuman.save(human)
  145. msgRet.ret = CHANGE_NAME_RET_OK
  146. if human.db.changeNameCnt > 1 then
  147. msgRet.ret = CHANGE_NAME_RET_OK2
  148. end
  149. msgRet.tip = "ok"
  150. Msg.send(msgRet, human.fd)
  151. if human.db.changeNameCnt == 1 then
  152. 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)
  153. 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)
  154. ReportManager.create(human)
  155. Log.write(Log.LOGID_OSS_LOGIN, human.db._id, human.db.account, human.db.name, human.db.lv, human.db.ip)
  156. end
  157. end
  158. -- 改名查询
  159. function changeNameQuery(human)
  160. local msgRet = Msg.gc.GC_ROLE_CHANGE_NAME_QUERY
  161. if human.db.changeNameCnt == nil then
  162. msgRet.price = 0
  163. 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)
  164. else
  165. msgRet.price = CHANGE_NAME_COST
  166. end
  167. Msg.send(msgRet,human.fd)
  168. end
  169. function randomName(human, sex)
  170. local msgRet = Msg.gc.GC_ROLE_RANDOM_NAME
  171. msgRet.sex = sex
  172. msgRet.name = getRandomName(sex)
  173. Msg.send(msgRet, human.fd)
  174. end
  175. function getRandomName(sex)
  176. if not sex then
  177. sex = math.random(1, 2)
  178. end
  179. local random1 = math.random(1, #PlayerNamesExcel.xing)
  180. if sex == 1 then
  181. local random2 = math.random(1, #PlayerNamesExcel.name)
  182. return PlayerNamesExcel.xing[random1].xing..PlayerNamesExcel.name[random2].name
  183. else
  184. local random2 = math.random(1, #PlayerNamesExcel.name2)
  185. return PlayerNamesExcel.xing[random1].xing..PlayerNamesExcel.name2[random2].name
  186. end
  187. end
  188. -- 创角时修改角色信息
  189. function createRoleChangeInfo(human,name,sex,firendCode)
  190. local msgRet = Msg.gc.GC_ROLE_CREATE_ROLE_CHANGE_MSG
  191. local isOk = checkRoleName(name)
  192. if isOk == false then
  193. msgRet.ret = 1
  194. Msg.send(msgRet,human.fd)
  195. return
  196. end
  197. -- 创角
  198. if not human.db.changeNameCnt then
  199. human.db.changeNameCnt = 0
  200. else
  201. if human.db.zuanshi < CHANGE_NAME_COST then
  202. msgRet.ret = 1
  203. Msg.send(msgRet, human.fd)
  204. return
  205. end
  206. human.db.changeNameCnt = human.db.changeNameCnt + 1
  207. ObjHuman.decZuanshi(human, -CHANGE_NAME_COST, "change_name")
  208. end
  209. ObjHuman.onlineHuman[human.db.name] = nil
  210. ObjHuman.onlineHuman[name] = human
  211. human.db.name = name
  212. human.db.sex = sex
  213. RoleHeadLogic.setHead(human, sex, RoleHeadLogic.HEAD_TYPE_1)
  214. ObjHuman.save(human)
  215. ChatPaoMaLogic.loginTips(human)
  216. -- 好友推荐 todo
  217. RelationGiftLogic.relationBind(human, firendCode)
  218. msgRet.ret = 0
  219. Msg.send(msgRet,human.fd)
  220. -- 记录流失日志
  221. if human.db.changeNameCnt == 0 then
  222. 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)
  223. 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)
  224. ReportManager.create(human)
  225. Log.write(Log.LOGID_OSS_LOGIN, human.db._id, human.db.account, human.db.name, human.db.lv, human.db.ip)
  226. end
  227. end
  228. function createRoleSettingUp(human)
  229. -- 赠送新英雄 上阵
  230. local heroId = SysParameter.getSysParameter(SysParameter.PARAMETER_8)
  231. if heroId and heroId ~= 0 then
  232. local bagIndex, uuid = HeroLogic.addHero(human, heroId,nil, 1, "create_role")
  233. if bagIndex and uuid and type(uuid) == "string" then
  234. local msg = {}
  235. msg.type = CombatDefine.COMBAT_TYPE1
  236. msg.formation = 1
  237. msg.heroList = "0,"..uuid
  238. msg.helpList = ""
  239. msg.jibanList = ""
  240. CombatPosLogic.updatePos(human, msg)
  241. end
  242. end
  243. end
  244. RANDOM_HEAD_ID_TABLE = nil
  245. function getRandomHead()
  246. if RANDOM_HEAD_ID_TABLE == nil then
  247. RANDOM_HEAD_ID_TABLE = {}
  248. for k, v in pairs(RoleExcel.head) do
  249. RANDOM_HEAD_ID_TABLE[#RANDOM_HEAD_ID_TABLE + 1] = k
  250. end
  251. end
  252. local index = math.random(1, #RANDOM_HEAD_ID_TABLE)
  253. return RANDOM_HEAD_ID_TABLE[index]
  254. end
  255. RANDOM_BODY_ID_TABLE = nil
  256. function getRandomBody()
  257. if RANDOM_BODY_ID_TABLE == nil then
  258. RANDOM_BODY_ID_TABLE = {}
  259. for k, v in pairs(RoleExcel.body) do
  260. RANDOM_BODY_ID_TABLE[#RANDOM_BODY_ID_TABLE + 1] = k
  261. end
  262. end
  263. local index = math.random(1, #RANDOM_BODY_ID_TABLE)
  264. return RANDOM_BODY_ID_TABLE[index]
  265. end