将棋牌录像与战绩进行本地化存储

lua class.cpp

下定义

int WriteDataToFile(lua_State* pArgs)
{
string sPath = Lua_Tool::GetString(pArgs, 1);
void* pUserData = Lua_Tool::GetUserData(pArgs, 2);
int nSize = Lua_Tool::GetInteger(pArgs, 3);
Data data;
data.copy((unsigned char*)pUserData, nSize);
FileUtils::getInstance()->removeFile(sPath);
FileUtils::getInstance()->writeDataToFile(data, sPath);
return 0;
}


int ReadDataFromFile(lua_State* pArgs)
{
string sPath = Lua_Tool::GetString(pArgs, 1);
Data data = FileUtils::getInstance()->getDataFromFile(sPath);
int nSize = data.getSize();
unsigned char* pUserData[10240] = { 0 };
memcpy(pUserData, data.getBytes(), nSize);
Lua_Tool::PushUserData(pArgs, pUserData);
Lua_Tool::PushInteger(pArgs, nSize);
return 2;
}

用来存储和读取录像的二进制存储数据

战绩读取成为Table后转化为json文件进行文档存储

local NewtbRecondData={};
     if fileUtils:isFileExist(recondDirPath..tostring(uUserId)..".json") == false then
        print(" buffer:ReadInt();error", recondDirPath..tostring(uUserId)..".json")
        NewtbRecondData = {};
        table.insert(NewtbRecondData,tbRecondData)
        local json=json.encode(NewtbRecondData)
        SaveRecond(json,size)
     else  
        local file = fileUtils:getStringFromFile(recondDirPath..tostring(uUserId)..".json");  
        NewtbRecondData = json.decode(file)
        local i=#NewtbRecondData
        --判断是否为同一大局
        if(tbRecondData.bigTime==NewtbRecondData[i].bigTime) then
            PrintTable(NewtbRecondData[i].tbUserPoint["0"])
            table.insert(NewtbRecondData[i].tbReplayTime,table.nums(NewtbRecondData[i].tbReplayTime),tbRecondData.tbReplayTime[0])
            NewtbRecondData[i].nGroupNumber_Now=tbRecondData.nGroupNumber_Now
            for j = 0, tbRecondData.nPersonNumber-1 do
            table.insert(NewtbRecondData[i].tbUserPoint[tostring(j)].tbPoint,table.nums(NewtbRecondData[i].tbUserPoint[tostring(j)].tbPoint),tbRecondData.tbUserPoint[j].tbPoint[0])
            NewtbRecondData[i].tbUserPoint[tostring(j)].sumPoint=tbRecondData.tbUserPoint[j].sumPoint
            local json=json.encode(NewtbRecondData)
            print("RecondData=",json);
            SaveRecond(json,size)
            end
            --如果战绩过多则删除
            if(#NewtbRecondData>50) then
            print(#NewtbRecondData)
            table.remove(NewtbRecondData,"1")
            end
        else
            table.insert(NewtbRecondData,tbRecondData)
            local json=json.encode(NewtbRecondData)
            print("RecondData=",json);
            SaveRecond(json,size)
        end

注意战绩经过json.decode转化为Table后数组下标需字符串化(tostring)

function SaveRecond(buf,size)
    local fileUtils = cc.FileUtils:getInstance();
    local strWritablePath = fileUtils:getWritablePath();
    local recondDirPath = strWritablePath.."recond/";
    if fileUtils:isDirectoryExist(strWritablePath.."recond/") == false then
        --不存在文件夹
        if fileUtils:createDirectory(recondDirPath) == false then
            --创建文件夹失败
            return false;
        end
    end
    local uUserId1 = tostring(g_tableUserData.nUserId1)
    local uUserId2 = tostring(g_tableUserData.nUserId2)
    local uUserId = uUserId1 .. uUserId2
   
    local file = io.open(recondDirPath..uUserId..".json" ,"w+");
    if file then
        if file:write(tostring(buf).."\n") == nil then 
            return false;
        end
        io.close(file);
       
        --WriteDataToFile(recondDirPath..tostring(nUserId) ,buf,size);
        --DeleteRecond(30);
        return true;
    else
        return false;
    end
   
 end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值