|
- -- 游戏逻辑相关的全局函数
-
- local DownloaderHeadManager=require("luaScript.Tools.DownloaderHeadManager")
- -- 滚动提示
- -- items:滚动的Ui节点
- -- speedTime:滚动速度
- -- text: 文本显示内容
- -- parentNode: 裁剪的父节点
- function startScrollTips(items, speedTime, text, parentNode)
- local html = items;
- local parent = parentNode;
- html:setText(text);
- html:setPositionType(0);
-
- local winSize = parent:getSize();
- local size = html:getSize();
- local speed = speedTime;
- html:runAction(cc.RepeatForever:create(createActionArray(function()html:setPosition(cc.p(winSize.width , html:getPositionY()));end
- , cc.MoveTo:create((size.width + winSize.width) / speed , cc.p(-size.width , html:getPositionY()))
- )));
- end
-
- -- 数字增长动画
- --@curValue 当前值
- --@desValue 终值
- --@time 增长动画时长
- function numIncAnime(node, curValue, desValue, time)
- node:setText(curValue);
- if curValue == desValue then
- return
- end
- time = time or 2
- local function setNum(sum)
- if sum > time then
- sum = time
- end
- local showNum = math.ceil(curValue +(desValue - curValue) * sum / time)
- node:setText(tostring(showNum));
- end
- scheduleUpdateScriptFunc(node, time, setNum)
- end
-
- -------------------------- dingding game --------------------------
- -- 是否正在显示总结算界面
- g_isShowCountAll = false
- function setShowCountAll(bVisible)
- g_isShowCountAll = bVisible
- end
- function isShowCountAll()
- return g_isShowCountAll;
- end
- --是否在俱乐部和房间里面
- g_bInClubRoom = false
- function setInClubRoom(bVisible)
- g_bInClubRoom = bVisible
- end
- function getInClubRoom()
- return g_bInClubRoom;
- end
-
- -- 亲友圈是否退出房间继续进入房间游戏
- g_isContinueRoomGamebol = false
- function setisContinueRoomGamebol(bVisible)
- g_isContinueRoomGamebol = bVisible
- end
- function isContinueRoomGamebol()
- return g_isContinueRoomGamebol;
- end
-
- -- 亲友圈重新进入其他房间数据
- g_isContinueRoomGamedata = nil
- function setisContinueRoomGamedata(data)
- g_isContinueRoomGamedata = data
- end
- function isContinueRoomGamedata()
- return g_isContinueRoomGamedata;
- end
-
- -- 单个茶馆数据
- g_isRoomGamedata = nil
- function setIsRoomGamedata(data)
- g_isRoomGamedata = data
- end
- function getIsRoomGamedata()
- return g_isRoomGamedata;
- end
-
- --自己是否处于游戏中
- g_mygamestatus = false
- function setMyGameStatus(bol)
- g_mygamestatus = bol
- end
- function getMyGameStatus()
- return g_mygamestatus;
- end
-
- --------------再来一局start-----------------
- ---
- -- 设置是否再来一局
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param bol 是否再来一局
- -- @param wanfadata 子游戏玩法
- -- @param uidList 待邀请玩家列表
- -- @return
- --
- function setIsAgainGamebol(bol,wanfadata,uidList)
- dd.IPlayGameAgain.setIsAgainGamebol(bol, wanfadata, uidList)
- end
- ---
- -- 是否再来一局
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getIsAgainGamebol()
- return dd.IPlayGameAgain.getIsAgainGamebol()
- end
- ---
- -- 再来一局包间id
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param idx
- -- @return
- --
- function setAgainGameBaoJianId(idx)
- dd.IPlayGameAgain.setAgainGameBaoJianId(idx)
- end
- ---
- -- 获取再来一局包间id
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getAgainGameBaoJianId()
- return dd.IPlayGameAgain.getAgainGameBaoJianId()
- end
- ---
- -- 保存再来一局接受邀请数据
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param data
- -- @return
- --
- function setAgainGameInvitedata(data)
- dd.IPlayGameAgain.setAgainGameInvitedata(data)
- end
- ---
- -- 获取再来一局接受邀请数据
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getAgainGameInvitedata()
- return dd.IPlayGameAgain.getAgainGameInvitedata()
- end
- ---
- -- 保存再来一局玩法状态
- -- 检测点再来一局的时候,玩法是否被修改。如果是修改那么提示创建失败。
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param bol
- -- @return
- --
- function setRuleChangeStatus(bol)
- dd.IPlayGameAgain.setRuleChangeStatus(bol)
- end
- ---
- -- 获取再来一局玩法状态
- -- 检测点再来一局的时候,玩法是否被修改。如果是修改那么提示创建失败。
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getRuleChangeStatus()
- return dd.IPlayGameAgain.getRuleChangeStatus()
- end
- ---
- -- 保存房间id
- -- 邀请的时候需要用到数据
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param idx
- -- @return
- --
- function setGameRoomId(idx)
- dd.IPlayGameAgain.setGameRoomId(idx)
- end
- ---
- -- 获取房间id
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getGameRoomId()
- return dd.IPlayGameAgain.getGameRoomId()
- end
- ---
- -- 保存玩家是否创建过房间
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @param bol
- -- @return
- --
- function setMyGameCreateStatus(bol)
- dd.IPlayGameAgain.setMyGameCreateStatus(bol)
- end
- ---
- -- 获取玩家是否创建过房间
- --(再来一局加新接口请移步luaScript.Interface.IPlayGameAgain)
- -- @return
- --
- function getMyGameCreateStatus()
- return dd.IPlayGameAgain.getMyGameCreateStatus()
- end
- --------------再来一局end-----------------
-
- ----------------------------即将弃用的接口-----------------------------------------
- ----------------------------即将弃用的接口-----------------------------------------
- ---
- -- !!!
- -- 亲友圈当前邀请游戏规则数据
- -- 子游戏发布后,这个接口setisAgainGamebol可以删掉
- -- @param data
- -- @return
- --
- g_RoomGameRuledata = nil
- function setRoomGameRuledata(data)
- g_RoomGameRuledata = data
- end
- function getRoomGameRuledata()
- return g_RoomGameRuledata;
- end
- ---
- -- !!!
- -- 子游戏发布后,这个接口setisAgainGamebol可以删掉
- -- @param bol
- -- @param wanfadata
- -- @return
- --
- function setisAgainGamebol(bol,wanfadata)
- dd.IPlayGameAgain.isAgainGameBol = bol
- if wanfadata ~= nil then
- dd.IPlayGameAgain.wanfa = wanfadata
- end
- end
- ---
- -- !!!
- -- 子游戏发布后,这个接口setisAgainGamebol可以删掉
- -- @return
- --
- function isAgainGameide()
- return dd.IPlayGameAgain.getAgainGameBaoJianId()
- end
- ---
- -- !!!
- -- 子游戏发布后,这个接口setisAgainGamebol可以删掉
- -- @param idx
- -- @return
- --
- function setisAgainGameide(idx)
- dd.IPlayGameAgain.setAgainGameBaoJianId(idx)
- end
- -------------------------------------------------------------------
- -------------------------------------------------------------------
- local _ImageTimes;
- local _ImageTimesFile = "ImageTimes.data"
- local function loadImageTimesFromFile()
- if not _ImageTimes then
- local fileString = loadStringFromFile(_ImageTimesFile)
- if fileString then
- _ImageTimes = json.decode(fileString) or {}
- else
- _ImageTimes = {}
- end
- end
- end
- local function saveImageTimesToFile()
- _ImageTimes = _ImageTimes or {}
- local fileString = json.encode(_ImageTimes)
- saveStringToFile(fileString, _ImageTimesFile)
- end
- local function loadImageTime(imageName)
- loadImageTimesFromFile()
- return _ImageTimes[imageName]
- end
- local function saveImageTime(imageName, timeNew)
- loadImageTimesFromFile()
- _ImageTimes[imageName] = timeNew
- saveImageTimesToFile();
- end
- -------------------------------------------------------------------
- -------------------------------------------------------------------
-
- -- 加载单个图片到缓存
- function loadSpriteFile(fileName)
- cc.TextureCache:getInstance():addImage(fileName);
- end
-
- -- 加载纹理合集
- function loadSpriteFrameFile(fileName)
- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile(fileName);
- end
-
- function dowloadImageFile(imageUrl,fileName,callback)
-
- -- 地址是否有效
- if not imageUrl or string.len(imageUrl) < 10 then
- if callback then
- callback()
- end
- return
- end
-
- local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName
-
- if isWin32Platform() then -- or isAndroidPlatform()
- getImageFromUrlWithTime(imageUrl, fileName, nil, callback)
- else
- local function onFinish(result)
- local resultJson = json.decode(result)
- dump(resultJson,"dowloadFile onFinish")
- local code = tonumber(resultJson.code)
- local msg = resultJson.message
- if callback then
- callback()
- end
- local isExist = cc.FileSystem:fileExists(fullPath)
- if isExist then
- saveImageTime(fileName, os.time())
- end
- end
-
- app.plugin:createTask(imageUrl,fullPath,onFinish,-1)
- end
- end
-
- -- 从网络上下载一张图片到本地
- -- imageUrl : 图片链接
- -- filename :保存到本地的文件名
- -- time :图片时间戳
- -- callback :完成后回调,如果本地有这个文件,则直接回调
-
- -- 时间戳的作用:
- -- 如果有值,则和本地记录的时间进行比较,不一致就需要重新下载
- -- 如果无值,则使用上次更新的时间很当前时间进行比较,大于一定的时间才重新下载
- function getImageFromUrlWithTime(imageUrl, fileName, time, onEnd)
- logD("getImageFromUrlWithTime()", imageUrl, fileName, time)
- local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName;
- local timeNow = os.time()
- local timeOld = loadImageTime(fileName) or 0
-
- -- 执行回调
- local function doCallback(ret)
- if onEnd then
- onEnd(ret);
- end
- end
-
- -- 从网络下载这个图片
- local function downloadImageFromUrl()
- local function onUpdate(status , info)
- -- status = progress,successed,failed
- logD("getImageFromUrlWithTime() downloadImageFromUrl()", tostring(status))
- if status ~= "successed" then
- doCallback(false)
- -- showTooltip(string.format("下载update.json文件失败 (%d) ", self.gameId))
- return
- end
-
- local targetFile = io.open(fullPath, "wb")
- if targetFile == nil then
- doCallback(false)
- print("写入文件:" ..fullPath .."时磁盘创建文件失败")
- return
- end
- targetFile:write(info)
- targetFile:close()
-
- -- logD("getImageFromUrlWithTime() downloadImageFromUrl()", tostring(status))
- local timeNew = 0;
- if time and time > 0 then
- timeNew = time
- else
- timeNew = timeNow;
- end
- --确保使用时间戳的时候,我才去保存图片,不然过了7天又重新下载。
- -- if time then
- saveImageTime(fileName, timeNew)
- -- end
- doCallback(true)
-
- -- if status == "successed" then
- -- logD("getImageFromUrlWithTime() downloadImageFromUrl()", tostring(status))
- -- local timeNew = 0;
- -- if time and time > 0 then
- -- timeNew = time
- -- else
- -- timeNew = timeNow;
- -- end
- -- saveImageTime(fileName, timeNew)
- -- -- fullPath
- -- doCallback(true)
- -- elseif status == "failed" then
- -- logD("getImageFromUrlWithTime() downloadImageFromUrl()", tostring(status))
- -- doCallback(false)
- -- else
-
- -- end
- end
-
- logD("getImageFromUrlWithTime() 请求的图片路径", imageUrl)
- downloadFileByUrls(imageUrl, onUpdate)
- end
-
- -- 地址是否有效
- if not imageUrl or string.len(imageUrl) < 10 then
- doCallback(false)
- return
- end
-
- -- 检查本地是否有这个图片
- local isExist = cc.FileSystem:fileExists(fullPath)
- if not isExist then
- -- 本地没有这个图片,直接下载
- logD("getImageFromUrl() file not exist")
- downloadImageFromUrl()
- return
- end
- logD("getImageFromUrl() file exist")
- -- 本地已有这个文件,则需要检测时间戳
- if time and time > 0 then
- -- 已使用时间戳
- if (time) == (timeOld) then
- -- 执行回调
- doCallback(true)
- else
- -- 重新下载
- os.remove(fullPath)
- downloadImageFromUrl()
- end
- else
- -- 未使用时间戳
- if timeNow - timeOld <= 7 * 24 * 3600 then
- logD("图片小于7天")
- -- 距离上次更新少于7天,不下载,执行回调
- doCallback(true)
- else
- -- 重新下载
- os.remove(fullPath)
- downloadImageFromUrl()
- end
- end
- end
-
- -- 从网络加载一张图片纹理
- -- image : ImageView节点
- -- url : 下载地址
- function setImageFromUrl(image,url)
- local iconUrl, iconTime = convertIconUrl(url)
- local pngName = getImageNameFromUrl(iconUrl)
- getImageFromUrlWithTime(iconUrl, pngName, tonumber(iconTime), function(ret)
- if ret then
- local texture = loadTextureFromFile(pngName)
- if not texture then
- return
- end
- if not image or tolua.isnull(image) then
- return
- end
- image:setTexture2(texture)
- end
- end)
- end
-
-
-
- -- 从本地加载一张图片纹理
- -- fileName : 文件名字,不带路径
- -- return : texture
- function loadTextureFromFile(fileName,isCircle)
- print("loadTextureFromFile() ", tostring(fileName))
- if not fileName then
- return
- end
-
- local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName;
-
- if cc.FileSystem:fileExists(fullPath) then
- log("loadTextureFromFile() file exist")
-
- local imageData = cc.FileSystem:readData(fullPath);
-
- if not imageData then
- log("loadTextureFromFile() imageData is null ")
- return nil
- end
-
- local image = cc.Image:createWithDataLua(imageData, string.len(imageData));
- if tolua.isnull(image) then
- log("loadTextureFromFile() image is null ")
- return nil
- end
-
- if isCircle then
- image = image:convertToCircleImage()
- if tolua.isnull(image) then
- log("getUserHeadImageWithUrl() image is null ")
- return
- end
- end
-
- local texture = cc.Texture:createWithImage(image);
- if tolua.isnull(texture) then
- log("loadTextureFromFile() texture is null ")
- return nil
- end
-
- return texture
- else
- return nil
- end
- end
-
- -- 给一个 ImageView 节点设置玩家头像
- -- nUserId :用户ID
- -- imageUrl :头像地址
- -- nodeImage:头像节点
- function setPlayerHeadImage(nUserId, imageUrl, nodeImage,isCircle)
- --A玩家下载成功出去,B玩家进来坐了A的位置,假设B的头像也失败了,B就用了A的头像。A再进来,就会重复头像
- setDefaultHeadImg(nil,nUserId,nodeImage,nodeImage:getContentSize().width,isCircle)
-
- local fileName = "HeadImage_"..tostring(nUserId)..".png";
- local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName;
- --uid的引进是为了防止:假设A进来,头像没下载完,不是失败,A出去了。B进来,B下载失败,A这个时候又成功。
- nodeImage.uid = nUserId
-
- -- 更新头像
- local function updateHeadImage()
- -- 从本地文件读取纹理
- local texture = loadTextureFromFile(fileName,isCircle)
- if not texture then
- return
- end
- -- 判断头像节点是否还有效
- if tolua.isnull(nodeImage) then
- return
- end
- --
- if nodeImage.uid == nUserId then
- -- 设置头像
- nodeImage:setTexture2(texture)
- texture = nil
- end
- end
-
- local timeNow = os.time()
- local timeOld = loadImageTime(fileName) or 0 --tonumber(loadUserInfo(fileName)) or 0
- local timeDelta = 14 * 24 * 60 * 60
- local isExist = cc.FileSystem:fileExists(fullPath)
- dump(isExist,"setPlayerHeadImage timeNow:"..timeNow.." timeOld:"..timeOld)
- -- 文件不存在,或者离上次更新的时间很长了,才需要下载
- if not isExist or timeNow >= timeOld + timeDelta then
- logD("头像不存在")
- if cc.Application:getInstance():getTargetPlatform() == 3 then
- logD("------android shi yong curl xiazai------")
- getImageFromUrlWithTime(imageUrl, fileName, nil, updateHeadImage)
- else
- logD("------zheng chang xia zai--------")
- DownloaderHeadManager.addTask(nodeImage,imageUrl, fileName, updateHeadImage)
- end
- else
- logD("头像存在,使用已经缓存的头像!")
- updateHeadImage()
- end
- end
-
- -- 删除玩家头像
- function updatePlayerHeadImage(nUserId, url)
- local fileName = "HeadImage_"..tostring(nUserId)..".png";
- local fullPath = cc.FileUtils:getInstance():getWritablePath()..fileName;
- os.remove(fullPath)
- getImageFromUrlWithTime(url, fileName)
- end
-
-
- ------------------------------------------------------------------------------
-
- --[[
- %a 一星期中天数的简写 (Wed)
- %A 一星期中天数的全称 (Wednesday)
- %b 月份的简写 (Sep)
- %B 月份的全称 (September)
- %c 日期和时间 (09/16/98 23:48:10)
- %d 一个月中的第几天 (16)[0 ~ 31]
- %H 24小时制中的小时数 (23)[00 ~ 23]
- %I 12小时制中的小时数 (11)[01 ~ 12]
- %j 一年中的第几天 (259)[01 ~ 366]
- %M 分钟数 (48)[00 ~ 59]
- %m 月份数 (09)[01 ~ 12]
- %P "上午(am)" 或 "下午(pm)" (pm)
- %S 秒数 (10)[00 ~ 59]
- %w 一星期中的第几天 (3)[0 ~ 6 = 星期天 ~ 星期六]
-
- %W 一年中的第几个星期 0 ~ 52
- %x 日期 (09/16/98)
- %X 时间 (23:48:10)
- %y 两位数的年份 (90)[00 ~ 99]
- %Y 完整的年份 (2009)
- %% 字符串'%'
- ]]
-
- -- 月-日 时:分:秒
- function getTimeString()
- return os.date("%Y-%m-%d %X")
- end
-
- function getTimeString2()
- return os.date("%X")
- end
-
- --- 月-日 时:分:秒
- function getTimeString3()
- return os.date("%m-%d %X")
- end
-
- --深度拷贝
- function DeepCopy(object)
- local SearchTable = {}
-
- local function Func(object)
- if type(object) ~= "table" then
- return object
- end
- local NewTable = {}
- SearchTable[object] = NewTable
- for k, v in pairs(object) do
- NewTable[Func(k)] = Func(v)
- end
-
- return setmetatable(NewTable, getmetatable(object))
- end
-
- return Func(object)
- end
-
- --获取两点间距离
- function getDistance(x1,y1,x2,y2)
- -- local x = x1 - x2
- -- local y = y1 - y2
- -- local dis = math.sqrt( x * x + y * y)
-
- -- print("getDistance() x1 = "..x1)
- -- print("getDistance() y1 = "..y1)
- -- print("getDistance() x2 = "..x2)
- -- print("getDistance() y2 = "..y2)
- -- print("getDistance() x = "..x)
- -- print("getDistance() y = "..y)
- -- print("getDistance() dis = "..dis)
- -- return dis
-
- if type(x1) == "number" and type(y1) == "number" and type(x2) == "number" and type(y2) == "number" then
- local pk = 180 / 3.14169;
- local a1 = x1 / pk;
- local a2 = y1 / pk;
- local b1 = x2 / pk;
- local b2 = y2 / pk;
- local t1 = math.cos(a1) * math.cos(a2) * math.cos(b1) * math.cos(b2);
- local t2 = math.cos(a1) * math.sin(a2) * math.cos(b1) * math.sin(b2);
- local t3 = math.sin(a1) * math.sin(b1);
- local tt = math.acos(t1 + t2 + t3);
- return 6371000 * tt;
- end
- return -1;
- end
-
- -- 获得距离的描述,
- -- 传入参数单位米
- function getDistanceDesc(num)
- if num < 1000 then
- return string.format("%d米", math.floor(num))
- else
- return string.format("%0.1f公里", num / 1000)
- end
- end
-
- -- 获取默认头像
- function getDefaultAvatar(sex, uid,isCircle)
- if isCircle then
- return "res/ui/zy_dating/dating/icon_default_head_circle.png"
- else
- return "res/ui/zy_dating/dating/icon_default_head.png"
- end
-
- end
-
- --设置默认头像
- function setDefaultHeadImg(sex, uid, nodeHead, curHeadWidth,isCircle)
- -- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_dating/dating/dating.plist")
- -- 先设置成默认头像
- local imageFile = getDefaultAvatar(sex, uid,isCircle)
- nodeHead:loadTexture(imageFile)
- nodeHead:setScale(1.0);
- local head = {}
- head.width = nodeHead:getContentSize().width
- nodeHead:setScale(curHeadWidth / head.width)
- end
-
- --获取裁剪后的昵称
- function getSubStringNickname(nickname)
- if cc.Application:getInstance():getTargetPlatform() == 0 then
- return nickname
- end
-
- if not nickname then
- return "未知昵称"
- end
- -- 裁剪4个字(4指名称前4个字,并非长度)
- nickname = string.getShortName(nickname, 4)
-
- return nickname
- end
-
-
- function getShortName(name)
- return getSubStringNickname(name)
- end
-
- --第二种方式截取名字(过滤掉所有特殊字符)
- function getShortName2(name)
- if not name then
- return "未知昵称"
- end
- -- 裁剪4个字(4指名称前4个字,并非长度)
- nickname = string.getShortName2(name, 4)
-
- return nickname
- end
-
- --裁剪
- function getShortNameByLength(name,lens)
- if not name then
- return "未知昵称"
- end
- -- 裁剪lens个字(lens指名称前lens个字,并非长度)
- nickname = string.getShortName(name, lens)
-
- return nickname
- end
-
- function downloadSubGame(gameId, endCallback)
- local view = import("luaScript.SubGame.SubGameLoadView"):new(gameId, endCallback)
- -- view:setAnchorPoint(cc.p(0.5, 0.5))
- app:showSytemDialog(view)
- -- app:showWaitDialog(view,0)
- end
-
- function requestDownloadSubGame(gameId, endCallback)
- logD("GameGlobalFunctions::requestDownloadSubGame() gameId = ", gameId)
- local function onOk()
- downloadSubGame(gameId, endCallback)
- end
- local function onCancel()
-
- end
- local subGameName = getSubGameName(gameId)
- if gameId == 2 then
- subGameName = "游戏资源"
- end
- local versionInfo = app.serverConfigs:getSubGameVersionInfo(gameId)
- logD("GameGlobalFunctions::requestDownloadSubGame()", table.tostring(versionInfo))
- if versionInfo then
- local zipSize = 3
- if versionInfo.size and tonumber(versionInfo.size) > 0 then
- zipSize = math.ceil(tonumber(versionInfo.size) / 1024 / 1024)
- end
- local strContent = string.format("是否下载[%s], 文件大小约%sM", subGameName or "", zipSize)
- showConfirmDialog(strContent, onOk, onCancel)
- else
- if endCallback then
- endCallback();
- end
- end
- end
-
- --game --
-
- --[[
- CARD_VALUE =
- {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d, --//方块 A - K
- 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d, --//梅花 A - K
- 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d, --//红桃 A - K
- 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d, --//黑桃 A - K
- }
- ]]
- function getCardImage(nValue)
- local nColor = math.ceil(nValue / 16)
- local nValue = math.floor(nValue % 16)
-
- local strValue = "" -- 牌值
- local strImage = "" -- 图案
-
- local strColor = "red"
- if nColor == 2 or nColor == 4 then
- strColor = "black"
- end
- strValue = string.format("num_%s_%02d.png", strColor, nValue)
-
- if nValue > 10 then
- strImage = string.format("di_%02d_%02d.png",nValue, nColor)
- else
- strImage = string.format("color_%02d.png",nColor)
- end
-
- log(string.format("getCardImage() nColor = %d, nValue = %d, strValue = %s, strImage = %s", nColor, nValue, strValue, strImage))
- return nColor, nValue, strValue, strImage
- end
-
- --[[是否是特殊牌型]]
- function getIsSpecialCard(cardType)
- return CONST_GAME_CARDTYPE.ETYPE_NIU_NIU < cardType
- end
-
- function getVisibleSize()
- return cc.Director:getInstance():getVisibleSize()
- end
-
- function getWinSize()
- return cc.Director:getInstance():getWinSize()
- end
-
- function autoAdapt(node)
-
- local glview = cc.Director:getInstance():getOpenGLView();
- local finalResourceSize = glview:getDesignResolutionSize();
- local resourceSize = {width = 1280 ; height = 720};
-
- -- 宽高比例单独计算
- local g_radio_x = finalResourceSize.width / resourceSize.width
- local g_radio_y = finalResourceSize.height / resourceSize.height
-
- local uiSize = node:getSize()
- local realSize = {width = uiSize.width * g_radio_x, height = uiSize.height * g_radio_y}
- node:setSize(realSize)
- end
-
- function autoAdaptWidth(node)
- local glview = cc.Director:getInstance():getOpenGLView();
- local finalResourceSize = glview:getDesignResolutionSize();
- local resourceSize = {width = 1280 ; height = 720};
-
- -- 宽高比例单独计算
- local g_radio_x = finalResourceSize.width / resourceSize.width
- local g_radio_y = finalResourceSize.height / resourceSize.height
-
- -- 适配
- local uiSize = node:getSize()
- local realSize = {width = uiSize.width * g_radio_x, height = uiSize.height}
- node:setSize(realSize)
- end
-
- function autoAdaptHeight(node)
- local glview = cc.Director:getInstance():getOpenGLView();
- local finalResourceSize = glview:getDesignResolutionSize();
- local resourceSize = {width = 1280 ; height = 720};
-
- -- 宽高比例单独计算
- local g_radio_x = finalResourceSize.width / resourceSize.width
- local g_radio_y = finalResourceSize.height / resourceSize.height
-
- -- 适配
- local uiSize = node:getSize()
- local realSize = {width = uiSize.width, height = uiSize.height * g_radio_y}
- node:setSize(realSize)
- end
-
- function showPHPFailedResult(content)
- if not content then
- return
- end
-
- local timeStr = getTimeString()
- local uid = app.user.loginInfo.uid or "0"
- local message = string.format("[%s]%s,%s", timeStr, uid, content)
- --showConfirmDialog(message, function() end,nil)
- showTooltip(message)
-
- --上传日志,便于查找错误问题
- uploadLogs(GAME_ERROR_TYPE.HTTP)
- end
-
- function showPHPSuccessResult(content)
- if not content then
- return
- end
-
- showConfirmDialog(content,nil,nil);
- end
-
- function showPHPError(content)
- if not content then
- return
- end
- local uid = app.user.loginInfo.uid or "null"
- local userID = " 用户ID:"..uid
- local timeStr = " 时间:"..getTimeString()
- --local str = "PHP Error :"..content..userID
- local str = "接口异常,请重新登陆再试。"..userID
- showConfirmDialog(str,function() end,nil)
-
- --上传日志,便于查找错误问题
- --uploadLogs(GAME_ERROR_TYPE.AUTO)
- end
-
- -- 检查PHP返回结果是否有效
- -- 会检查status、response、response.code、response.result的有效性
- -- 失败会返回false及对应的描述
- -- 成功会返回true及response.result(jsonData)
- function checkPhpResponse(status, response)
- if status ~= "successed" then
- return false, "status = "..tostring(status)
- end
- local ttResposne = json.decode(response)
- if not ttResposne then
- return false, "ttResposne is nil"
- end
- if ttResposne.code ~= 200 then
- return false, "code is "..ttResposne.code
- end
- if not ttResposne.result then
- return false, "result is nil"
- end
- return true, ttResposne.result
- end
-
- --[[
- 将带有时间戳的图片地址,转换为图片地址和时间戳
- input : "http://www.dingdingqipai.com/doudizhu.png?v=100086"
- output : "http://www.dingdingqipai.com/doudizhu.png", "100086"
- -- ]]
- function convertIconUrl(url)
- local iconUrl;
- local iconTime;
- local arr = toStringArray("?")(url)
- if table.nums(arr) > 0 then
- iconUrl = arr[1]
- end
- if table.nums(arr) > 1 then
- local arr2 = toStringArray("=")(arr[2])
- if table.nums(arr2) > 1 then
- iconTime = arr2[2]
- end
- end
- return iconUrl,iconTime;
- end
-
- --[[
- 获取一个网络图片的图片文件名
- input : "http://www.dingdingqipai.com/doudizhu.png"
- output : "doudizhu.png"
- --]]
- function getImageNameFromUrl(url)
- local arr = toStringArray("/")(url)
- local num = table.nums(arr)
- if (num) > 0 then
- return arr[num]
- end
- return nil
- end
-
- --[[
- 播放金币动画
- nodeBegin : 起始点
- nodeEnd : 结束点
- goldNum : 金币数量
- goldIcon : 金币图标
- endCallback : 动画完成后的回调
- sizeRange : 范围大小,如果金币是点对点,则不传此参数
-
- 测试用例: MainView:testCoinAnimation()
- --]]
- function playCoinAnimation(nodeBegin, nodeEnd, goldNum, goldIcon, endCallback, sizeRange)
- local coinPlayer = import("luaScript.Tools.CoinAniPlayer"):new()
- if coinPlayer then
- coinPlayer:playAnimation(nodeBegin, nodeEnd, goldNum, goldIcon, endCallback, sizeRange);
- end
- end
-
-
- --[[
- 播放道具动画
- nodeBegin : 起始点
- nodeEnd : 结束点
- propId : 道具ID
-
- 测试用例: MainView:testPropAnimation()
- --]]
- function playPropAnimation(nodeBegin, nodeEnd, propId)
- local propPlayer = import("luaScript.Tools.PropAniPlayer"):new()
- if propPlayer then
- propPlayer:playAnimation(nodeBegin, nodeEnd, propId);
- end
- end
-
- --[[
- 播放plist帧动画
- plist : plist 文件路径
- imgName : 对应的plist图片名字 例如animation_%d.png
- startIndex : startIndex 开始帧
- endIndex : endIndex 结束帧
-
- 测试用例: MainView:testPropAnimation()
- ]]--
- function createAnimationByPlist(plist,imgName,startIndex,endIndex,delayTime)
- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile(plist)
- local sprite = cc.Sprite:createWithSpriteFrameName(string.format(imgName,startIndex))
- local animation = cc.SpriteAnimation:create()
- for i = startIndex,endIndex do
- local frame = cc.SpriteFrameCache:getInstance():getSpriteFrameByName(string.format(imgName,i))
- animation:addSpriteFrame(frame)
- end
- animation:setDelayPerUnit(1/20)
- if delayTime then
- if delayTime == -1 then
- sprite:runAction(
- cc.Sequence:create(
- cc.SpriteAnimate:create(animation),
- cc.CallFunc:create(function()
- sprite:removeFromParent()
- end)
- )
- )
- else
- sprite:runAction(
- cc.RepeatForever:create(
- cc.Sequence:create(
- cc.SpriteAnimate:create(animation),
- cc.DelayTime:create(delayTime)
- )
- )
- )
- end
- else
- sprite:runAction(
- cc.RepeatForever:create(
- cc.SpriteAnimate:create(animation)
- )
- )
- end
- return sprite
- end
-
-
- -- 从一段文本中检测出房间号,只针对全文只有一组六位数数据
- function getRoomIdFromString(str)
- logD("getRoomIdFromString() str = ", str);
- local roomId;
- local nums = {}
- for w in string.gmatch(str, "%d+") do
- local ww = tostring(w)
- logD("getRoomIdFromString()", ww);
- local sw = string.len(ww)
- if sw == 6 then
- roomId = ww
- end
- end
- return roomId;
- end
-
- -- 从复制文本中检测出房间号
- function getRoomIdFromCopyString(str)
- logD("getRoomIdFromCopyString() str = ", str);
- local roomId;
- local nums = {}
- --只读取此格式中的数字"[123456]"
- for w in string.gmatch(str, "%[%d+%]") do
- local ww = string.sub(w, 2, -2)
- logD("getRoomIdFromCopyString()", ww);
- local sw = string.len(ww)
- if sw == 6 then
- roomId = ww
- break;
- end
- end
- return roomId;
- end
-
- function checkIsNumberString(str)
- local n = #str
- local isNumber = true
- for i = 1,n do
- local c = string.byte(str, i)
- if c < 48 or c > 57 then
- isNumber = false
- break
- end
- end
- return isNumber
- end
-
- --密码只能是大小写字母加数字
- function checkIsPasswordString(str)
- local n = #str
- local isValid = false
- for i = 1,n do
- local c = string.byte(str, i)
- if c >= 48 and c <= 122 and not (c >= 58 and c <= 64) and not (c >= 91 and c <= 96) then
- isValid = true
- break
- end
- end
- return isValid
- end
-
- --过滤特殊字符,保留中文、英文和数字
- function filter_spec_chars(s)
- local ss = {}
- local k = 1
- while true do
- if k > #s then
- break
- end
-
- --获取字符ASCII码
- local c = string.byte(s,k)
- if not c then
- break
- end
-
- if c<192 then
- if (c>=48 and c<=57) or (c>= 65 and c<=90) or (c>=97 and c<=122) then
- --转化字节流为字符串
- local st = string.char(c)
- table.insert(ss, st)
- end
- k = k + 1
- elseif c<224 then
- k = k + 2
- elseif c<240 then
- if c>=228 and c<=233 then
- local c1 = string.byte(s,k+1)
- local c2 = string.byte(s,k+2)
- if c1 and c2 then
- local a1,a2,a3,a4 = 128,191,128,191
-
- if c == 228 then
- a1 = 184
- elseif c == 233 then
- a2,a4 = 190,c1 ~= 190 and 191 or 165
- end
-
- if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
- table.insert(ss, string.char(c,c1,c2))
- end
- end
- end
- k = k + 3
- elseif c<248 then
- k = k + 4
- elseif c<252 then
- k = k + 5
- elseif c<254 then
- k = k + 6
- end
- end
- return table.concat(ss)
- end
-
- --解码URL
- function decodeURI(s)
- s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
- return s
- end
-
-
- function openClub()
- local function open()
- app.waitDialogManager:closeWaitNetworkDialog()
- if app.club_php.clubList[app.club_php.clubID] then
- local clubView = import("luaScript.Views.Club.ClubTable"):new(app.club_php.clubID)
- clubView:setAnchorPoint(cc.p(0.5, 0.5))
- app:showWaitDialog(clubView)
- else
- showTooltip((app.club_php:getCestIsOpen(app.club_php.clubID) and PLN.CLUB_CEST_NAME or PLN.CLUB_NAME).."不存在或已经解散!")
- end
- end
- if table.nums(app.club_php.clubList)>0 then
- open()
- else
- app.waitDialogManager:showWaitNetworkDialog("请稍等...")
- app.club_php:requestClubList(open)
- end
- end
-
- function showFuLiConfirm(info)
- local view = import("luaScript.Views.Main.JiangLi"):new(info);
- view:setAnchorPoint(cc.p(0.5, 0.5))
- app:showWaitDialog(view)
- end
-
- function showFuLiConfirmForCoin(info)
- local view = import("luaScript.Views.Coin.RoomJiangLi"):new(info);
- view:setAnchorPoint(cc.p(0.5, 0.5))
- app:showWaitDialog(view)
- end
-
- --[[比较两个时间,返回相差多少时间]]
- function timediff(long_time,short_time)
- local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{}
- local colMax = {60,60,24,os.date('*t',os.time{year=n_short_time.year,month=n_short_time.month+1,day=0}).day,12,0}
- n_long_time.hour = n_long_time.hour - (n_long_time.isdst and 1 or 0) + (n_short_time.isdst and 1 or 0) -- handle dst
- for i,v in ipairs({'sec','min','hour','day','month','year'}) do
- diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0)
- carry = diff[v] < 0
- if carry then
- diff[v] = diff[v] + colMax[i]
- end
- end
- return diff
- end
-
- function addUIEventListener(data)
- if not data or not data.node then
- return
- end
- --注册触摸事件
- local listener = cc.EventListenerTouchOneByOne:create()
- --设置穿透属性
- if data.swallow ~= false or data.swallow ~= true then
- data.swallow = true
- end
- listener:setSwallowTouches(data.swallow)
- --注册
- function onTouchBegan(touch, event)
- if data.node:isVisible() then
- if data.beGanCallback then
- data.beGanCallback(touch, event)
- end
- return true
- else
- return false
- end
- end
- function onTouchMoved(touch, event)
- if data.moveCallback then
- data.moveCallback(touch, event)
- end
- end
- function onTouchEnded(touch, event)
- if data.endCallback then
- data.endCallback(touch, event)
- end
- end
- --添加事件和回调
- listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
- listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
- listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
- --得到事件派发器
- local eventDispatcher = data.node:getEventDispatcher()
- --将监听器注册到派发器中
- eventDispatcher:addEventListenerWithSceneGraphPriority(listener, data.node)
- end
-
- -- gameId : 需要检测的子游戏ID
- -- endCallback : 完成后的回调
- function checkSubGameFiles(gameId, endCallback)
- local function doCallback()
- if endCallback then
- endCallback()
- end
- end
- local result, subGameId, missingFiles = app.subGameManager:checkGameFiles(gameId)
- if not result then
- logD("checkSubGameFiles() 发现子游戏文件缺失,gameId = "..subGameId)
- for k,v in pairs(missingFiles) do
- logD(v);
- end
- local function onOk()
- downloadSubGame(subGameId, doCallback);
- end
- app.subGameManager:clearGame(subGameId);
- showConfirmDialog("发现文件缺失,点击确定开始修复", onOk);
- else
- doCallback();
- end
- end
-
- function getCurrentPlatform()
- local RomSetting = loadRomSettingScript()
- if RomSetting.Platform then
- return RomSetting.Platform
- end
- end
-
-
-
- function getGpsOpen()
- local isOpen = false
- local gpsInfo = app.user.gpsInfo
- if gpsInfo then
- --测试发现Gps关了,返回的gpsStatus是2(已开启),数据xy为0
- if gpsInfo.gpsStatus == GpsStatus.enable and gpsInfo.x ~= 0 and gpsInfo.y ~= 0 then
- isOpen = true
- end
- end
- return isOpen
- end
-
- function isEnableEnterRoom()
- if not app.club_php.clubID or app.club_php.clubID == 0 or not app.club_php.clubList[app.club_php.clubID] then
- logD("isEnableEnterRoom club php is not exist!")
- return true
- end
-
- local clubInfo = app.club_php.clubList[app.club_php.clubID]
- --is_gps :1开启强制定位 0 未开启
- if clubInfo and clubInfo.groupext and not (clubInfo.groupext.is_gps == 1) then
- return true
- end
-
- local bEnter = getGpsOpen()
-
- if not bEnter then
- showTooltip("GPS未开启,无法加入房间,请开启后再加入!!")
- setIsAgainGamebol(false,"")
- end
-
- return bEnter
- end
-
- function getGpsDistance()
- if not app.club_php.clubID or app.club_php.clubID == 0 or not app.club_php.clubList[app.club_php.clubID] then
- logD("getGpsDistance club php is not exist!")
- return 500
- end
-
- local clubInfo = app.club_php.clubList[tonumber(app.club_php.clubID)]
- local distance = tonumber(clubInfo.groupext.gps_limit) or -1
- return distance
- end
-
- -- 顺序执行大厅里面的任务
- -- 主要用于有多个弹框需要逐个打开的时候。先加入队列的活动优先执行
- -- 注意:在这里打开的界面,在关闭的时候一定要执行回调,不然后面的界面会打不开
-
- -- 例如:我要先打开A绑定界面,
- -- 关闭A界面之后,再打开活动弹框界面
- -- 关闭活动弹框之后,再打开红包卡界面
-
- local g_MainViewExecList = {}
- local g_MainViewExecRunning = false
-
- function execInMainViewQueue(callback)
- table.insert(g_MainViewExecList, callback)
-
- local function runFunc()
-
- if #g_MainViewExecList <= 0 then
- g_MainViewExecRunning = false
- return
- end
-
- local func = g_MainViewExecList[1]
- table.remove(g_MainViewExecList,1)
-
- g_MainViewExecRunning = true
- func(function()
- g_MainViewExecRunning = false
- runFunc()
- end)
- end
-
- log("g_MainViewExecRunning:",g_MainViewExecRunning)
-
- if not g_MainViewExecRunning then
- runFunc()
- end
- end
|