Browse Source

CDK功能实现

249435196@qq.com 1 year ago
parent
commit
95e1f66635
2 changed files with 83 additions and 17 deletions
  1. 32 15
      script/common/CDK.lua
  2. 51 2
      script/module/present/CDK.lua

+ 32 - 15
script/common/CDK.lua

@@ -1,6 +1,5 @@
 
 local DB = require("common.DB")
-local Json = require("common.Json")
 local CommonDB = require("common.CommonDB")
 local CdkFixExcel = require("excel.cdkFix")
 local LuaMongo = _G.lua_mongo
@@ -27,23 +26,20 @@ local function checkCDKParam(finishTime,cdkID)
     if now > finishTime then 
         return
     end
-    if not CdkFixExcel[cdkID] then
-        return
-    end
     return true
 end
 
 local function genCDKBatch(cnt,useCnt,cdkID)
     local bat = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
     local db = {
-        batch = bat,
+        batch = bat + 1,
         cnt = cnt,
         useCnt = useCnt,
         info = cdkID,
         useCDKList = {},
     }
     LuaMongo.insert(DB.db_cdk, db)
-    CommonDB.setCDKBatch(bat + 1) -- 将下一次cdkBatch保存
+    CommonDB.setCDKBatch(bat + 1) -- 将最新的cdkBatch保存
     return bat
 end
 
@@ -65,8 +61,8 @@ local function genCDKCode(batch,index)
 end
 
 local function deCDKCode(cdk)
-    local num =  (CodeMap[cdk[3] ] - 1) * 100 +  (CodeMap[cdk[4] ] - 1) * 10 + (CodeMap[cdk[5] ] - 1)
-    local batch = (CodeMap[cdk[7] ] - 1) * 100 +  (CodeMap[cdk[8] ] - 1) * 10 + (CodeMap[cdk[9] ] - 1)
+    local num =  (CodeMap[string.sub(cdk,3,3) ] - 1) * 100 +  (CodeMap[string.sub(cdk,4,4)] - 1) * 10 + (CodeMap[string.sub(cdk,5,5) ] - 1)
+    local batch = (CodeMap[string.sub(cdk,6,6) ] - 1) * 100 +  (CodeMap[string.sub(cdk,7,7) ] - 1) * 10 + (CodeMap[string.sub(cdk,8,8) ] - 1)
     return num,batch
 end
 
@@ -81,25 +77,46 @@ function genCDK(cnt,useCnt,cdkId)
     useCnt = useCnt or 1
     cnt = cnt < 1000 and cnt or 999
     local bat = genCDKBatch(cnt,useCnt,cdkId)
+    local fileName = "cdk"..bat
+    local f,err = io.open(fileName,"w")
+    if not f then
+        print("open file failed ",err)
+        return
+    end
     local idx = 0
+    local data = "cdk : \n"
     while true do 
         if idx > cnt then 
             break
         end
         local cdk = genCDKCode(bat,idx)
+        data = data .. cdk .. "\n"
         idx = idx + 1
     end
+    f:write(data)
 end
 
 -- 使用cdk
-function use(cdk)
+function useCDK(cdk)
 
 end
 
-function test()
-    local cdk = genCDKCode(55,125)
-    print("cdk is ",cdk)
-    local idx,bat = deCDKCode(cdk)
-    print("batch is ",bat)
-    print("index is ",idx)
+-- 检查CDK
+function checkCDK(cdk)
+    local num,batch = deCDKCode(cdk)
+    local curBatch = assert(CommonDB.getCDKBatch(),"assert : cdk batch not found")
+    if curBatch < batch then
+        return "batch error"
+    end
+    QueryCDK = { batch = { ["$eq"] = batch} }
+    LuaMongo.find(DB.db_cdk,QueryCDK)
+    local cdkBatchData = {}
+    LuaMongo.next(cdkBatchData)
+    if cdkBatchData.cnt < num then 
+        return "invalid cdk"
+    end
+    if cdkBatchData.useCDKList[num] then 
+        return "cdk used"
+    end
+    return nil, cdkBatchData
 end

+ 51 - 2
script/module/present/CDK.lua

@@ -9,6 +9,7 @@ local MailManager = require("mail.MailManager")
 local CdkFixExcel = require("excel.cdkFix")
 local ProjectLogic = require("platform.ProjectLogic")
 local BagLogic = require("bag.BagLogic")
+local CDKLogic = require("common.CDK")
 
 --1 getCDKInfo/getCDKInfoRet 参数 code ts account
 --返回 ret(1成功)  code ts account id item endDate type limitCnt serverIndexs projectName
@@ -39,8 +40,8 @@ function CG_CDK(human,msg)
 	if cdkFixDo(human,code) then
 		return
 	end
-	
-	getCDKInfo(human, code)
+	cdkDo(human,code)
+	--getCDKInfo(human, code)
 end
 
 function getCDKInfo(human, code)
@@ -189,6 +190,54 @@ local function getCdkFixID(code)
 	end
 end
 
+local function transTime(date) 
+	date.hour = 0
+	date.min = 0
+	date.sec = 0
+	return os.time(date)
+end
+
+function cdkDo(human,code)
+	local err,cdkBatchData = CDKLogic.checkCDK(code)
+	if err then
+		print("check cdk error : ",err)
+		return
+	end
+	 local cdkInfo = human.db.cdk[cdkBatchData.batch] or 0
+    if cdkInfo >= cdkBatchData.useCnt then 
+        print("use cdk limit")
+        return
+    end
+    local cdkCfg = CdkFixExcel.cdkFix[cdkBatchData.info]
+    if not cdkCfg then 
+        print("batch cdk error")
+        return
+    end
+    local nowTime = os.time()
+    if nowTime > transTime(cdkCfg.endDate) then 
+        print("cdk over time")
+        return
+    end
+	if next(cdkCfg.serverIndexs) then
+		local canUse = false
+		for k,v in ipairs(cdkCfg.serverIndexs) do
+			if Config.SVR_INDEX >= v[1] and Config.SVR_INDEX <= v[2] then
+				canUse = true
+				break
+			end
+		end
+		if not canUse then
+			return
+		end
+	end
+	cdkInfo = cdkInfo + 1
+	human.db.cdk[cdkBatchData.batch] = cdkInfo
+	-- 存储
+	CDKLogic.useCDK(code)
+	BagLogic.addItemList(human, cdkCfg.item , "cdk")
+	Msg.send(Msg.gc.GC_CDK, human.fd)
+end
+
 function cdkFixDo(human,code)
 	local id,conf = getCdkFixID(code)
 	if not id then