local PKDef = PKFramework.PKImport("pk_base.luaScript.PKDef") local PKFunctions = {} --预加载图片 function PKFunctions.loadSpriteFromFile(fileName) cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile(fileName); end --获取版本号 function PKFunctions.getPKCurVersion() local versionInfo = PKFramework.PKImport("PKVersion") return versionInfo.ResourceVersion end --创建一个imageNode function PKFunctions.createImageNode(fileName) local img = cc.ImageView:createNode() img:loadTextureFromPlist(fileName) return img end --获取创建房间时选择的人数 function PKFunctions.getCreateRoomPlayerNum() local roomInfo = app.room.roomInfo; local jsonInfo = json.decode(roomInfo.strGameInfo) if jsonInfo then if jsonInfo.playerNum == 5 then return 4 else return jsonInfo.playerNum end end return nil end function PKFunctions.getCardValue(card) if not card then logE("card:",card) logE("getCardValue error:") return end local value = card - math.floor(card / 16) * 16; return value; end function PKFunctions.getCardColor(card) if not card then logE("card:",card) logE("getCardColor error:") return end local color = math.floor(card / 16); return color; end --获取字牌资源名字 function PKFunctions.getPKCardImgByValue(card) if card == 0xff then return "pk_cards_back.png"; else local color = PKFunctions.getCardColor(card); local value = PKFunctions.getCardValue(card); local file = "pk_cards_"..color.."_"..value..".png"; return file; end end function PKFunctions.logCard(card) local cardColor = PKFunctions.getCardColor(card); local cardValue = PKFunctions.getCardValue(card); local str = "找不到这个牌"; local valueStr = ""; if cardValue < 9 then cardValue = cardValue + 2; valueStr = cardValue..""; elseif cardValue == 9 then valueStr = "J"; elseif cardValue == 10 then valueStr = "Q"; elseif cardValue == 11 then valueStr = "K"; elseif cardValue == 12 then valueStr = "A"; elseif cardValue == 13 then valueStr = "2"; end if cardColor == 0 then str = "方块" .. valueStr; elseif cardColor == 1 then str = "梅花".. valueStr; elseif cardColor == 2 then str = "红桃" .. valueStr; elseif cardColor == 3 then str = "黑桃" .. valueStr; elseif cardColor == 4 then if cardValue == 14 then str = "小王"; else str = "大王"; end end str = str .. "(原数据:" .. card .. ")"; logD(str) end function PKFunctions.getIsCanOutCard(list, cardInfo, lastData) local tpCardsList = {} if 0 == table.nums(lastData.cardList) then if table.nums(cardInfo) > 0 then for k,v in pairs(cardInfo) do table.insert(tpCardsList,v.cards) end end return tpCardsList end local tpCardList = {} for i = 1,table.nums(lastData.cardList) do table.insert(tpCardList,lastData.cardList[i]) end local tpLastInfo = PKFunctions.getCardType(tpCardList) for _k2,v in pairs(cardInfo) do if v.cardType == tpLastInfo[1].cardType then if v.cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUANGSHUN or v.cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_SANSHUN or v.cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_SISHUN then if table.nums(list) > table.nums(lastData.cardList) then table.insert(tpCardsList,v.cards) else if table.nums(list) == table.nums(lastData.cardList) and v.minCard > tpLastInfo[1].minCard then table.insert(tpCardsList,v.cards) end end else if v.minCard > tpLastInfo[1].minCard and table.nums(list) == table.nums(lastData.cardList) then table.insert(tpCardsList,v.cards) end end else if v.cardType > tpLastInfo[1].cardType and v.cardType > PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUNZI then table.insert(tpCardsList,v.cards) end end end return tpCardsList end function PKFunctions.getRogueCount(list) local tpCount = 0 for i = 1,table.nums(list) do if 14 == PKFunctions.getCardValue(list[i]) then tpCount = tpCount + 1 end end return tpCount end function PKFunctions.getCardType(list) logD("getCardType list:",table.tostring(list)) --存储非癞子值 local tt = {} --存储非癞子的原始数据(包括花色和值) local ttt = {} local tpCardInfo = {} local laiCount = PKFunctions.getRogueCount(list) for i = 1,table.nums(list) do local value = PKFunctions.getCardValue(list[i]); if 0x0e ~= value then table.insert(tt,value) table.insert(ttt,list[i]) end end local max = table.nums(tt) if max <= 0 then return tpCardInfo end local function sortFunc(a,b) local numA = tonumber(a) local numB = tonumber(b) if numA ~= nil and numB ~= nil then return numA < numB else return a < b end end table.sort(tt,sortFunc) table.sort(ttt,sortFunc) if max + laiCount < 3 then if tt[1] == tt[max] then if max + laiCount == 1 then--单张 table.insert(tpCardInfo,{ minCard = tt[1], cardType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SINGLE, cards = list }) elseif (max + laiCount == 2) then --对子 local tpList = {} for i = 1, table.nums(ttt) do table.insert(tpList,ttt[i]) end for i = 1,laiCount do table.insert(tpList,0x50 + PKFunctions.getCardValue(ttt[1])) end table.insert(tpCardInfo,{ minCard = tt[1], cardType = PKDef.CARD_TYPE.BANZIPAO_TYPE_DUIZI, cards = tpList }) end end elseif tt[1] == tt[max] then if max + laiCount == 3 then local tpList = {} for i = 1,table.nums(ttt) do table.insert(tpList,ttt[i]) end for i = 1,laiCount do table.insert(tpList,0x50 + PKFunctions.getCardValue(ttt[1])) end table.insert(tpCardInfo,{ minCard = tt[1], cardType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SANZHANG, cards = tpList }) elseif max + laiCount == 4 then local tpList = {} for i = 1,table.nums(ttt)do table.insert(tpList,ttt[i]) end for i = 1,laiCount do table.insert(tpList,0x50 + PKFunctions.getCardValue(ttt[1])) end table.insert(tpCardInfo,{ minCard = tt[1], cardType = PKDef.CARD_TYPE.BANZIPAO_TYPE_BOMB, cards = tpList }) end else --定义个新数组 --其中a[1]的值为list中重复一次(单张牌)的牌, -- a[2]的值为list中重复二次(对牌)的牌, -- a[3]的值为list中重复三次(三张)的牌, -- a[4]的值为list中重复四次(炸弹)的牌。 local a = {} for i = 1,4 do a[i] = {} end local valueList = {} --(key是值,value是数量) for i = 1, table.nums(tt) do local value = PKFunctions.getCardValue(tt[i]) if not valueList[value] then valueList[value] = 0 end valueList[value] = valueList[value] + 1; end for card,count in pairs(valueList) do card = tonumber(card) if count == 1 then table.insert(a[1],card) elseif count == 2 then table.insert(a[2],card) elseif count == 3 then table.insert(a[3],card) elseif count == 4 then table.insert(a[4],card) end end local isHaveKindOr2 = false; for i = 1,max do local value = tt[i] if 0x0d == value then isHaveKindOr2 = true end end --_idx :某个牌的个数 --_k :组数 local function tpMove(_idx, _k) local tpCards = {} local laiCount2 = PKFunctions.getRogueCount(list) local valueList = {} --(key是值,value是数量) 除去癞子 for i = 1, table.nums(list) do local value = PKFunctions.getCardValue(list[i]) if 0x0e ~= value then if not valueList[value] then valueList[value] = 0 end valueList[value] = valueList[value] + 1 end end --33 4 55 ? for i = 1,12 do --3-A local tpIdx = i local tpCount = 0 local laiziList = {} local totalLaiZi = laiCount2 while tpCount <= table.nums(list) and tpIdx <= 12 do if not valueList[tpIdx] then valueList[tpIdx] = 0 end if not (valueList[tpIdx] + totalLaiZi >= _k) then break end local needLaiZiNum = _k - valueList[tpIdx] needLaiZiNum = needLaiZiNum >= 0 and needLaiZiNum or 0 for l = 1,needLaiZiNum do table.insert(laiziList,0x50 + tpIdx) end totalLaiZi = totalLaiZi - needLaiZiNum tpCount = tpCount + _k tpIdx = tpIdx + 1 end if table.nums(list) == tpCount then table.insert(tpCards,{ cards = laiziList, min = i }) end end local tpType = 0 if _idx == 1 then tpType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUNZI; elseif _idx == 2 then tpType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUANGSHUN; elseif _idx == 3 then tpType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SANSHUN; elseif _idx == 4 then tpType = PKDef.CARD_TYPE.BANZIPAO_TYPE_SISHUN; end if table.nums(tpCards) > 0 and tpType then for k1 ,v1 in pairs(tpCards) do local _tpList3 = {} for _i8 = 1, table.nums(ttt) do table.insert(_tpList3,ttt[_i8]) end for k2,v2 in pairs(v1.cards) do table.insert(_tpList3,v2) end local function sortFunc2(a,b) return tonumber(PKFunctions.getCardValue(a)) < tonumber(PKFunctions.getCardValue(b)) end table.sort(_tpList3,sortFunc2) table.insert(tpCardInfo,{ minCard = tpCards[k1].min, cardType = tpType, cards = _tpList3 }) end end end local tpTotalCount = max + laiCount if not isHaveKindOr2 then local tpDif = math.abs(tt[1] - tt[max]) for k1 = table.nums(a),1,-1 do local tpk = k1 local tpDis = tpTotalCount / tpk - 1 if tpTotalCount >= 3 * tpk and tpTotalCount % tpk == 0 and tpDif <= tpDis then if table.nums(a[k1]) * tpk == max then tpMove(k1, tpk) else local tpLen = table.nums(a[k1]) * tpk for k2 = 1, k1 - 1 do tpLen = tpLen + table.nums(a[k2]) * k2 end if tpLen == max then tpMove(k1, tpk) end end end end end --天牌(初略判断:十三张牌全不一样(赖子可重复)则为天牌) if table.nums(list) == 13 and table.nums(a[2]) == 0 and table.nums(a[3]) == 0 and table.nums(a[4]) == 0 then local _tpList3 = {} for i = 1,table.nums(ttt) do table.insert(_tpList3,ttt[i]) end for i = 1,13 do if table.nums(_tpList3) < 13 then if not valueList[i] then table.insert(_tpList3,0x50 + i) end else break end end table.insert(tpCardInfo,{ minCard = ttt[1], cardType = PKDef.CARD_TYPE.BANZIPAO_TYPE_TIANPAI, cards = _tpList3 }) end end table.sort(tpCardInfo,function (a,b) return b.cardType < a.cardType end) logD("getCardType tpCardInfo:",table.tostring(tpCardInfo)) return tpCardInfo end function PKFunctions.getTip(list, lastData) local laiCard = {} local weaveList = {} local a = {} for i = 1,4 do a[i] = {} end local valueList = {} local valueCard = {} for i = 1, table.nums(list) do local value = PKFunctions.getCardValue(list[i]) if 14 == value then table.insert(laiCard,list[i]) else if not valueList[value] then valueList[value] = 0 end if not valueCard[value] then valueCard[value] = {} end valueList[value] = valueList[value] + 1; table.insert(valueCard[value],list[i]) end end for card,v in pairs(valueList) do card = tonumber(card) local count = valueList[card] if 1 == count then table.insert(a[1],card) elseif count == 2 then table.insert(a[2],card) elseif count == 3 then table.insert(a[3],card) elseif count == 4 then table.insert(a[4],card) end end function sortNumber(a, b) return a < b; end if not lastData or not lastData.cardList or table.nums(lastData.cardList) <= 0 then local tpCardInfo = PKFunctions.getCardType(list) if tpCardInfo and table.nums(tpCardInfo) > 0 then table.insert(weaveList,list) end function sortNumber(a, b) return PKFunctions.getCardValue(a[1]) < PKFunctions.getCardValue(b[1]); end for k = 1,4 do for i = 1, table.nums(a[k]) do local value = a[k][i] for j = 1, 1 do local curCard = valueCard[value][j] table.insert(weaveList,{curCard}) end if k == 1 then table.sort(weaveList,sortNumber) end end end return weaveList end local tpCardList = {} for k,v in ipairs(lastData.cardList) do table.insert(tpCardList,v) end local cardInfo = PKFunctions.getCardType(tpCardList) local laiCount = PKFunctions.getRogueCount(list) local cardList = lastData.cardList; local cardValueList = {} for k,v in ipairs(lastData.cardList) do table.insert(cardValueList,PKFunctions.getCardValue(v)) end table.sort(cardValueList,sortNumber) local lastValue = cardValueList[1] or 0 if cardInfo[1].cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_SINGLE then for k = 1,4 do for i = 1,table.nums(a[k]) do local value = a[k][i]; for j = 1,1 do if value > lastValue then local curCard = valueCard[value][j]; table.insert(weaveList,{curCard}) end end end end elseif cardInfo[1].cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_DUIZI then for i = cardInfo[1].minCard + 1,13 do --yihuo local _tpReplace = {} valueList[i] = valueList[i] or 0; if valueList[i] ~= 0 and valueList[i] + laiCount >= 2 then local tpNum = 2 - valueList[i]; if tpNum < 0 then tpNum = 0 end for l = 1,tpNum do table.insert(_tpReplace,laiCard[l]) end for m = 1,valueList[i] do if m <= 2 then table.insert(_tpReplace,valueCard[i][m]) end end table.insert(weaveList,_tpReplace) end end elseif cardInfo[1].cardType == PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUNZI then for i = cardInfo[1].minCard + 1, 13 - table.nums(cardList) do --yihuo local tpIdx = i; local tpCount = 0; local tpReplace2 = {}; local tpNotAllLai = false; local totalLaiZi = laiCount; while tpCount < table.nums(list) and tpCount < table.nums(cardList) and tpIdx <= 12 do valueList[tpIdx] = valueList[tpIdx] or 0; if not (valueList[tpIdx] + totalLaiZi >= 1) then break end local tpNum = 1 - valueList[tpIdx]; if tpNum < 0 then tpNum = 0 end --[[ for p = 1, tpNum do table.insert(tpReplace2,laiCard[p]) end--]] local i = laiCount - totalLaiZi for p = 1, tpNum do i = i + 1 table.insert(tpReplace2,laiCard[i]) end for m = 1, valueList[tpIdx] do if m <= 1 then table.insert(tpReplace2,valueCard[tpIdx][m]) tpNotAllLai = true; end end totalLaiZi = totalLaiZi - tpNum; tpCount = tpCount + 1 tpIdx = tpIdx + 1; end if tpNotAllLai and tpCount == table.nums(cardList) then table.insert(weaveList,tpReplace2) end end --去除重复的组合,比如456+癞可以组合 3456,4567 local tt = {} local removeIdx= {} for k,list in ipairs(weaveList) do local curSum = 0 for j,card in ipairs(list) do local value = PKFunctions.getCardValue(card) curSum = curSum + value end if tt[curSum] then table.insert(removeIdx,k) end tt[curSum] = k end for k,v in ipairs(removeIdx) do if weaveList[k] then table.remove(weaveList,k) end end end if cardInfo[1].cardType <= PKDef.CARD_TYPE.BANZIPAO_TYPE_SANZHANG then if cardInfo[1].cardType < PKDef.CARD_TYPE.BANZIPAO_TYPE_SANZHANG then tpIndex = 1 else tpIndex = cardInfo[1].minCard + 1 end for _i12 = tpIndex ,13 do -- yihuo local _tpReplace3 = {} valueList[_i12] = valueList[_i12] or 0; if valueList[_i12] > 0 and valueList[_i12] + laiCount >= 3 then local _tpNum2 = 3 - valueList[_i12] if _tpNum2 < 0 then _tpNum2 = 0 end for _l2 = 1, _tpNum2 do table.insert(_tpReplace3,laiCard[_l2]) end for _m2 = 1,valueList[_i12] do if _m2 <= 3 then table.insert(_tpReplace3,valueCard[_i12][_m2]) end end table.insert(weaveList,_tpReplace3) end end end if cardInfo[1].cardType <= PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUANGSHUN then for _i13 = 1, 13 - (table.nums(cardList) / 2) do --yihuo local tpIdx = _i13 local tpCount = 0 local tpReplace = {} local haveLai = laiCount; while tpCount < table.nums(list) and tpIdx <= 12 do if not valueList[tpIdx] then valueList[tpIdx] = 0 end if not (valueList[tpIdx] + haveLai >= 2) then break; end local _tpNum3 = 2 - valueList[tpIdx]; if _tpNum3 >= 0 then _tpNum3 = _tpNum3 else _tpNum3 = 0 end local _l3 = laiCount - haveLai for _p = 1, _tpNum3 do _l3 = _l3 + 1 table.insert(tpReplace,laiCard[_l3]) end for _m3 = 1,valueList[tpIdx] do if _m3 <= 2 then table.insert(tpReplace,valueCard[tpIdx][_m3]); end end haveLai = haveLai - _tpNum3; tpCount = tpCount + 2; if tpCount >= 6 and (cardInfo[1].cardType < PKDef.CARD_TYPE.BANZIPAO_TYPE_SHUANGSHUN or ((_i13 > cardInfo[1].minCard and tpCount >= table.nums(cardList)) or (_i13 <= cardInfo[1].minCard and tpCount > table.nums(cardList)))) then local tpReplace2 = {}; for k1,v in pairs(tpReplace) do table.insert(tpReplace2,tpReplace[k1]); end table.insert(weaveList,tpReplace2) end tpIdx = tpIdx + 1; end end end if cardInfo[1].cardType <= PKDef.CARD_TYPE.BANZIPAO_TYPE_BOMB then if cardInfo[1].cardType < PKDef.CARD_TYPE.BANZIPAO_TYPE_BOMB then _tpIndex = 1 else _tpIndex = cardInfo[1].minCard + 1; end for _i14 = _tpIndex, 13 do --yihuo local _tpReplace4 = {}; if not valueList[_i14] then valueList[_i14] = 0 end if valueList[_i14] > 0 and valueList[_i14] + laiCount >= 4 then local _tpNum4 = 4 - valueList[_i14]; if _tpNum4 < 0 then _tpNum4 = 0 end for _l4 = 1, _tpNum4 do table.insert(_tpReplace4,laiCard[_l4]) end for _m4 = 1, valueList[_i14] do if _m4 <= 4 then table.insert(_tpReplace4,valueCard[_i14][_m4]) end end table.insert(weaveList,_tpReplace4) end end end if cardInfo[1].cardType <= PKDef.CARD_TYPE.BANZIPAO_TYPE_SANSHUN then for _i15 = 1,13 - (table.nums(cardList) / 3) do --yihuo local _tpIdx2 = _i15; local _tpCount2 = 0; local _tpReplace5 = {}; local _haveLai2 = laiCount; while _tpCount2 < table.nums(list)and _tpIdx2 <= 12 do if not valueList[_tpIdx2] then valueList[_tpIdx2] = 0 end if not (valueList[_tpIdx2] + _haveLai2 >= 3) then break end local _tpNum5 = 3 - valueList[_tpIdx2]; if _tpNum5 < 0 then _tpNum5 = 0 end local _l5 = laiCount - _haveLai2 for _p2 = 1,_tpNum5 do table.insert(_tpReplace5,laiCard[_l5]) _l5 = _l5 + 1 end for _m5 = 1, valueList[_tpIdx2] do if _m5 <= 3 then table.insert(_tpReplace5,valueCard[_tpIdx2][_m5]) end end _haveLai2 = _haveLai2 - _tpNum5 _tpCount2 = _tpCount2 + 3; if _tpCount2 >= 9 and (cardInfo[1].cardType < PKDef.CARD_TYPE.BANZIPAO_TYPE_SANSHUN or ((_i15 > cardInfo[1].minCard and _tpCount2 >= table.nums(cardList)) or (_i15 <= cardInfo[1].minCard and _tpCount2 > table.nums(cardList)))) then local tpReplace2 = {} for k1,v in pairs(_tpReplace5) do table.insert(tpReplace2,_tpReplace5[k1]); end table.insert(weaveList,tpReplace2) end _tpIdx2 = _tpIdx2 + 1 end end end if cardInfo[1].cardType <= PKDef.CARD_TYPE.BANZIPAO_TYPE_SISHUN then for _i16 = 1, 13 - (table.nums(cardList) / 4) do local _tpIdx3 = _i16; local _tpCount3 = 0; local _tpReplace6 = {} local _haveLai3 = laiCount; while _tpCount3 < table.nums(list) and _tpIdx3 <= 12 do if not valueList[_tpIdx3] then valueList[_tpIdx3] = 0 end if not (valueList[_tpIdx3] + _haveLai3 >= 4) then break end local _tpNum6 = 4 - valueList[_tpIdx3]; if _tpNum6 < 0 then _tpNum6 = 0 end local _l6 = laiCount - _haveLai3 for _p3 = 1, _tpNum6 do table.insert(_tpReplace6,laiCard[_l6]); _l6 = _l6 + 1 end for _m6 = 1,valueList[_tpIdx3] do if _m6 <= 4 then table.insert(_tpReplace6,valueCard[_tpIdx3][_m6]); end end _haveLai3 = _haveLai3 - _tpNum6; _tpCount3 = _tpCount3 + 4; if _tpCount3 >= 12 and (cardInfo[1].cardType < PKDef.CARD_TYPE.BANZIPAO_TYPE_SISHUN or ((_i16 > cardInfo[1].minCard and _tpCount3 >= table.nums(cardList)) or (_i16 <= cardInfo[1].minCard and _tpCount3 > table.nums(cardList)))) then local tpReplace2 = {} for k1,v in pairs(_tpReplace6) do table.insert(tpReplace2,_tpReplace6[k1]) end table.insert(weaveList,tpReplace2) end _tpIdx3 = _tpIdx3 + 1 end end end logD("weaveList:",table.tostring(weaveList)) return weaveList; end function PKFunctions.convertToCards(str) str = string.trim(str) local tab = string.split(str, " ") if str == "" then tab = {} end return tab end function PKFunctions.playAniOnce(parent,max,everyFrame,path,pos,callback,scale) local firstImg = string.format(path,1) -- 创建图片 local nodeImage = cc.ImageView:createNode() nodeImage:loadTextureFromPlist(firstImg); parent:addChild(nodeImage) local size = parent:getContentSize(); nodeImage:setPosition(pos); --判断纹理是否存在 local cache = cc.SpriteFrameCache:getInstance() local spriteFrame = cache:getSpriteFrameByName(firstImg); if tolua.isnull(spriteFrame) then print("spriteFrame is not in cache") nodeImage:removeFromParent() return end if scale then nodeImage:setScale(scale) end nodeImage:runAction(cc.Sequence:create(cc.CallFunc:create(function () local indexFace = 0; --每隔多少秒切换一张图片 --local everyFrame = 0.1; local seq = cc.Sequence:create(cc.DelayTime:create(everyFrame),cc.CallFunc:create(function () indexFace = indexFace + 1 if 0 < indexFace and indexFace <= max then local name = string.format(path,indexFace) if not tolua.isnull(nodeImage) then nodeImage:loadTexture(name, cc.TextureResType.plistType) end else indexFace = 0 end end)) local seq2 = cc.Sequence:create(cc.Repeat:create(seq,max),cc.CallFunc:create(function () nodeImage:removeFromParent() if callback then callback() end end)); nodeImage:runAction(seq2) end))) end return PKFunctions