123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- local skynet = require "skynet"
- require "skynet.manager"
- local queue = require "skynet.queue"
- local util = require "util"
- local logger = require "logger"
- local stringify = require "stringify"
- local character = require "model.character"
- local log = require "model.log"
- local reward = require "model.reward"
- local payment = require "model.payment"
- local mailbox
- local shengtian
- local pbSproto = {}
- local skynet_retpack = skynet.retpack
- local skynet_now = skynet.now
- local skynet_sleep = skynet.sleep
- local string_format = string.format
- local traceback = debug.traceback
- local max = util.max
- local trace = logger.trace
- local loginserver = skynet.localname(".loginserver")
- local s_public = skynet.localname(".public")
- local module = {
- require "model.rechargemod",
- require "model.currency",
- require "model.role",
- require "model.adventure",
- require "model.mailbox",
- require "model.hero",
- require "model.embattle",
- require "model.equip",
- require "model.talent",
- require "model.time_box",
-
- require "model.red_point",
- require "model.presence",
-
- require "model.mq",
- require "model.gm",
- }
- local REQUEST = setmetatable({}, {
- __newindex = function (t, k, v)
- local f = pbSproto.register_msg
- if f then
- f(k)
- end
- rawset(t, k, v)
- end}
- )
- local CMD = {}
- local ipaddr
- local kick
- local send
- local throw_exception
- local lasttime
- local synchronized = queue()
- local namecenter
- local usercenter
- local player = {}
- local function keepalive()
- local args = { time=0 }
- local lastping = 0
- lasttime = skynet_now()
- while true do
- local now = skynet.now()
- if lasttime+36000 > now then
- lastping = max(lastping, lasttime)
- if lastping+1000 <= now then
- args.time = now
- lastping = now
- send('ping', args)
- end
- skynet_sleep(100)
- else
- kick("heartbeat timeout")
- break
- end
- end
- end
- local function watchtime()
- local os_time = os.time
- local dispatch_event = character.dispatch
- local daily_refresh = util.today(character.daily_refresh)
- local weekly_refresh = util.today(character.weekly_refresh)
- local today = util.today()
- local four = today + 4*3600
- local minute = os_time() + 60
- while true do
- local now = os_time()
- if now >= four then
- dispatch_event("timer.four", four)
- four = four + 86400
- end
-
- if now >= minute then
- dispatch_event("timer.minute", now)
- minute = minute+60
- end
- if now >= daily_refresh then
- dispatch_event("daily_refresh", now)
- daily_refresh = util.today(now)+24*3600
- character.set_daily_refresh(daily_refresh+12*3600)
- end
- if now >= weekly_refresh then
- dispatch_event("weekly_refresh", now)
- local add_day = 1 - tonumber(os.date("%w"))
- if add_day <= 0 then
- add_day = add_day + 7
- end
- weekly_refresh = util.today(now)+add_day*24*3600
- character.set_weekly_refresh(weekly_refresh+12*3600)
- end
- dispatch_event("timer.sec", now)
- skynet.sleep(100)
- end
- end
- local isready = false
- local function ready(bfirst)
-
- for _, elem in ipairs(module) do
- if elem.monitor then
- local ok, msg = xpcall(elem.monitor, traceback, character)
- if not ok then
- logger.error(msg)
- end
- end
- end
-
- for _, elem in ipairs(module) do
- if elem.launch then
- local ok, msg = xpcall(elem.launch, traceback, character)
- if not ok then
- logger.error(msg)
- end
- end
- end
- local currency = require "model.currency"
- send('user', {
- uid = character.uid,
- rename_time = character.rename_time,
- nickname = character.nickname,
- level = character.level,
- exp = character.exp,
- avatar = character.avater,
- currency = currency.get_client_data(character),
- svrtime = os.time(),
- createtime = character.createtime,
- sid = character.sid,
- bfirst = bfirst,
- })
-
- for _, elem in ipairs(module) do
- if elem.ready then
- local ok, msg = xpcall(elem.ready, traceback, character)
- if not ok then
- logger.error(msg)
- end
- end
- end
-
-
- isready = true
- character.set_lastlogin(os.time())
-
- skynet.fork(function()
- watchtime()
- end)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- character.dispatch("ready")
- return {errno = 0}
- end
- local IOS_MI_SIGN = 2
- local function migration_ios(args)
- local ret =skynet.call(s_public, "lua","migration_ios", "migration", args,IOS_MI_SIGN)
- if IOS_MI_SIGN ==1 then
- return ret
- elseif IOS_MI_SIGN ==2 then
- if ret then
- args.account = ret
- end
- else
- logger.trace("##### migration_ios 功能暂未开启")
- end
- end
- local function enter(args)
- local sid = args.sid
- local account = args.account
- local channel = args.channel
-
-
-
-
-
- if sid == nil then return { errno = 255 } end
- if account == nil then return { errno = 255 } end
-
- if channel == nil then return { errno = 255 } end
-
-
-
-
-
-
-
-
-
-
-
- local errno, rets = skynet.call(usercenter, "lua", "login", args, skynet.self())
- if errno == 1 then return { errno=STD_ERR.PLYAER_ERR_SERVERID or 8 } end
- if errno == 2 then return { errno=STD_ERR.PLYAER_OTHER_LOGIN or 4 } end
- if errno == 3 then return { errno=STD_ERR.PLYAER_MAINTAIN or 9 } end
- assert(errno == 0)
-
- character.loadfrom(rets)
- character.login_args = args
- local now = os.time()
- if character.forbidden > now then
- return { errno=STD_ERR.PLYAER_FORBID or 3 }
- end
-
- for _, elem in ipairs(module) do
- if elem.parse then
- local ok, msg = xpcall(elem.parse, traceback, character)
- if not ok then
- logger.error(msg)
- end
- end
- end
-
-
- character.send = send
- character.kick = kick
- character.throw_exception = throw_exception
- local first = false
- if character.nickname then
- if character.channel == "shengtian" then
- shengtian = shengtian or require "service.loginserver.shengtian"
- local content = {
- account = character.account,
- uid = character.uid,
- name = character.nickname,
- server = character.sid,
- level = character.level,
- power = character.power,
- }
- shengtian.upload(5, content)
- end
- else
- first = true
- local ok, name = skynet.call(namecenter, "lua", "register_nickname", "", character.uid)
- if not ok then
- return { errno=STD_ERR.PLYAER_DUPLICATION_NAME}
- end
- character.set_nickname(name)
- if character.channel == "shengtian" then
- shengtian = shengtian or require "service.loginserver.shengtian"
- local content = {
- account = character.account,
- uid = character.uid,
- name = character.nickname,
- server = character.sid,
- level = character.level,
- power = character.power,
- }
- shengtian.upload(1, content)
- end
- end
- logger.label(string_format("<Agent %s@%s.%s>", character.channel, character.uid, character.nickname))
- return ready(first)
- end
- function player.start(ctx)
- local args = assert(ctx.args)
- ipaddr = assert(ctx.ipaddr)
- kick = assert(ctx.kick)
- send = assert(ctx.send)
- throw_exception = assert(ctx.throw_exception)
- namecenter = skynet.localname(".namecenter")
- usercenter = skynet.localname(".usercenter")
- pbSproto = ctx.pbSproto
- for key in pairs(REQUEST) do
- pbSproto.register_msg(key)
- end
- for _, elem in ipairs(module) do
- local list_request_interests = elem.list_request_interests
- if list_request_interests then
- local rets = list_request_interests() or {}
- for k, f in pairs(rets) do
- assert(not rawget(REQUEST, k),k)
- REQUEST[k] = f
- end
- end
- local list_command_interests = elem.list_command_interests
- if list_command_interests then
- local rets = list_command_interests() or {}
- for k, f in pairs(rets) do
- assert(not rawget(CMD, k))
- CMD[k] = f
- end
- end
- end
-
-
- return enter(args)
- end
- local func_switch
- local function dispatch_request(pname, args)
- local f = REQUEST[pname]
- assert(f, string_format("Not found request '%s'", pname))
- return f(character, args)
- end
- function player.request(pname, args)
-
- lasttime = skynet_now()
- return synchronized(dispatch_request, pname, args)
- end
- function player.response(pname, args, callback)
- trace("dispath response '%s'", pname)
- lasttime = skynet_now()
- return synchronized(callback, character, args)
- end
- function player.command(session, cmd, ...)
- local f = CMD[cmd]
- assert(f, string_format("Not found command '%s'", cmd))
- if session == 0 then
- f(character, ...)
- else
- skynet_retpack(f(character, ...))
- end
- end
- function player.disconnect()
-
- if not isready then
- skynet.call(usercenter, "lua", "logout", character.uid, skynet.self())
- return
- end
-
- repeat
- local ok, msg = xpcall(character.dispatch, traceback, "disconnect")
- if not ok then
- logger.error(msg)
- end
- until 0
-
- for i=#module, 1, -1 do
- local elem = module[i]
- if elem.saybye then
- local ok, msg = xpcall(elem.saybye, traceback, character)
- if not ok then
- logger.error(msg)
- end
- end
- end
- local ok, msg = xpcall(function()
-
-
-
-
- end, traceback)
- if not ok then
- logger.error(msg)
- end
-
- character.set_lastlogout(os.time())
- character.flushall()
-
- skynet.call(usercenter, "lua", "logout", character.uid, skynet.self())
- end
- function REQUEST.create(_, args)
- local nickname = args.nickname or ""
- local deviceid = args.deviceid or "NULL-device"
-
- local len = string.len(nickname)
- if len < 3 or len > 32 then return { errno=STD_ERR.PLYAER_LLLEGAL_NAME } end
- if character.nickname then return { errno=STD_ERR.PLYAER_LLLEGAL_OPERATION } end
-
- logger.label(string_format("<Agent %s@%s.%s>", character.channel, character.uid, nickname))
- character.set_nickname(nickname)
- return ready()
- end
- local facebook = nil
- function CMD.broadcast(character, pname, args)
- if isready then
- return send(pname, args)
- end
- end
- local mailbox
- local parse
- function REQUEST.ping(character)
- return {time = os.time()}
- end
- return player
|