Browse Source

搭配赛图片下载部分优化

zhangyuqian 11 months ago
parent
commit
172cee1453

+ 157 - 1
GameClient/Assets/Game/HotUpdate/Data/MatchingCompetitionDataManager.cs

@@ -1,4 +1,5 @@
-using ET;
+
+using ET;
 using System;
 using System.Collections;
 using System.Collections.Generic;
@@ -7,6 +8,7 @@ using UnityEngine;
 using Live2D.Cubism.Rendering;
 using FairyGUI;
 using UI.MatchingCompetition;
+using System.IO;
 
 namespace GFGGame
 {
@@ -591,6 +593,160 @@ namespace GFGGame
         {
             ViewManager.Show<MatchingCompetitionRuleTipsView>();
         }
+
+        //**********下面接口只针对于搭配赛排行榜*************
+        //不建议频繁修改用户设备上的文件。
+        //需要在移动平台上管理文件,最好是在应用的沙盒目录内操作
+        public string LocalRankPath = Application.streamingAssetsPath + "/ImageDPSRank";
+        public string LocalBeforePath = Application.streamingAssetsPath + "/ImageDPSBe";
+        public string LoaclMy = Application.streamingAssetsPath + "/ImageMy";
+        //将获取的图片下载到本地,通过排行榜判断,如果该图片不存在于最新的排行榜内就删除
+        public void DownloadImageListToLocal(List<Byte[]> imageBytesList, List<long> ImageName,string targerPath)
+        {
+            // 检查目录是否存在
+            if (!Directory.Exists(targerPath))
+            {
+                // 目录不存在,创建目录
+                Directory.CreateDirectory(targerPath);
+            }
+            else
+            {
+                // 目录已存在
+            }
+            for (int i = 0; i < imageBytesList.Count; i++)
+            {
+                //判断手机内是否存在该图片
+                string path = Path.Combine(targerPath, string.Format("/{0}.jpg", ImageName[i].ToString()));
+                bool fileExists = File.Exists(path);
+                if (!fileExists)
+                {
+                    string outputImagePath = targerPath + string.Format("/{0}.jpg", ImageName[i].ToString());
+                    // 保存到磁盘
+                    File.WriteAllBytes(outputImagePath, imageBytesList[i]);
+                }
+            }
+        }
+        //将获取的图片下载到本地
+        public void DownloadImageToLocal(Byte[] imageBytesList, long ImageName, string targerPath)
+        {
+            // 检查目录是否存在
+            if (!Directory.Exists(targerPath))
+            {
+                // 目录不存在,创建目录
+                Directory.CreateDirectory(targerPath);
+            }
+            else
+            {
+                // 目录已存在
+            }
+            //判断手机内是否存在该图片
+            string path = Path.Combine(targerPath, string.Format("/{0}.jpg", ImageName.ToString()));
+            bool fileExists = File.Exists(path);
+            if (!fileExists)
+            {
+                string outputImagePath = targerPath + string.Format("/{0}.jpg", ImageName.ToString());
+                // 保存到磁盘
+                File.WriteAllBytes(outputImagePath, imageBytesList);
+            }
+        }
+        //获取指定目录下所有图片文件
+        public List<string> GetImageFileNames(string directoryPath)
+        {
+            // 检查目录是否存在
+            if (!Directory.Exists(directoryPath))
+            {
+                // 目录不存在,创建目录
+                Directory.CreateDirectory(directoryPath);
+            }
+            else
+            {
+                // 目录已存在
+            }
+            List<string> imageFileNames = new List<string>();
+            string[] imageExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; // 支持的图片格式
+            // 获取目录中所有文件
+            string[] files = Directory.GetFiles(directoryPath);
+            // 遍历文件,检查扩展名是否为图片格式
+            foreach (string file in files)
+            {
+                foreach (string extension in imageExtensions)
+                {
+                    if (Path.GetExtension(file).Equals(extension, StringComparison.OrdinalIgnoreCase))
+                    {
+                        imageFileNames.Add(Path.GetFileName(file));
+                        break; // 找到匹配的扩展名,跳出循环
+                    }
+                }
+            }
+            return imageFileNames;
+        }
+        //获取指定图片
+        public NTexture GetTargetImage(string ImageName,string Path)
+        {
+            string AllPath = Path +"/"+ ImageName;
+            byte[] fileData = File.ReadAllBytes(AllPath);
+            Texture2D texture = new Texture2D(1, 1);
+            texture.LoadImage(fileData);
+            return new NTexture(texture);
+        }
+        //删除指定图片
+        public void DeleteImage(string imagePath)
+        {
+            // 检查文件是否存在
+            if (File.Exists(imagePath))
+            {
+                try
+                {
+                    // 删除文件
+                    File.Delete(imagePath);
+                    //Debug.Log("图片已删除: " + imagePath);
+                }
+                catch (Exception e)
+                {
+                    // 如果出现异常,打印错误信息
+                    Debug.LogError("删除图片时出错: " + e.Message);
+                }
+            }
+            else
+            {
+                // 文件不存在
+                Debug.LogWarning("图片不存在,无法删除: " + imagePath);
+            }
+        }
+        //判断本地图片是否在新获取到的排行榜内
+        public bool ImageIdIsInRank(string ImageName)
+        {
+            foreach (var item in _currentRankList)
+            {
+                string rankImageName = item.JudgingInfo.WorksId.ToString() + ".jpg";
+                if (rankImageName == ImageName)
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+        //判断排行榜id是否存在本地图片
+        public bool RankIdisInLocal(string ImageName, List<string> LoaclList)
+        {
+            foreach (var item in LoaclList)
+            {
+                if (item == ImageName)
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+        //通过作品id从本地获取下载的图片如果没有则下载到本地保存,如果有直接加载
+        //这个接口只对搭配赛有用,因为只修改了搭配赛的数据
+        public NTexture GetNtextrueByLocal(string ImageName)
+        {
+            NTexture LocalNtextture = null;
+
+            return LocalNtextture;
+        }
+        //****************************************************
     }
 
     class MatchingOneDataManager : SingletonBase<MatchingOneDataManager>

+ 37 - 14
GameClient/Assets/Game/HotUpdate/Data/MatchingPhotoHelper.cs

@@ -37,14 +37,35 @@ namespace GFGGame
         }
 
 
-        public static IEnumerator Download(List<MatchingPhotoWorksData> list)
+        public static IEnumerator Download(List<MatchingPhotoWorksData> list,string targetPath)
         {
             for (int i = 0; i < list.Count; i++)
             {
-                MatchingPhotoWorksData data = list[i];
-                if (data == null || data.Ntexture != null) continue;
-                int count = 0;
-                yield return DownloadPicture(data, count);
+                List<string> imageList = MatchingCompetitionDataManager.Instance.GetImageFileNames(targetPath);
+                string ImageName = list[i].JudgingInfo.WorksId.ToString() + ".jpg";
+                //排行榜内图片是否存在本地,否就下载,是就赋值
+                if (!MatchingCompetitionDataManager.Instance.RankIdisInLocal(ImageName, imageList))
+                {
+                    MatchingPhotoWorksData data = list[i];
+                    if (data == null || data.Ntexture != null) continue;
+                    int count = 0;
+                    yield return DownloadPicture(data, count, targetPath);
+                }
+                else
+                {
+                    list[i].Bytes = File.ReadAllBytes((targetPath+ "/" + ImageName));
+                    list[i].Ntexture = MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName,targetPath);
+                    yield return null;
+                }
+                //本地图片是否存在排行榜,否就删除
+                foreach (string item in imageList)
+                {
+                    string getImagePath = targetPath +"/"+ list[i].JudgingInfo.WorksId.ToString()+".jpg";
+                    if (!MatchingCompetitionDataManager.Instance.ImageIdIsInRank(item))
+                    {
+                        MatchingCompetitionDataManager.Instance.DeleteImage(getImagePath);
+                    }
+                }
             }
             ET.Log.Debug("Download  finish!!!");
             EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
@@ -61,15 +82,15 @@ namespace GFGGame
             ET.Log.Debug("Download  finish!!!");
             EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
         }
-        public static IEnumerator Download(MatchingPhotoWorksData list)
+        public static IEnumerator Download(MatchingPhotoWorksData list, string targetPath)
         {
                 MatchingPhotoWorksData data = list;
-                yield return DownloadPicture(data, 1);
+                yield return DownloadPicture(data, 1,targetPath);
             ET.Log.Debug("Download  finish!!!");
             EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
         }
 
-        private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count)
+        private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count,string targetPath)
         {
             if (count >= 3)
             {
@@ -86,13 +107,14 @@ namespace GFGGame
                 {
                     ET.Log.Error("Download  failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
                     count += 1;
-                    yield return DownloadPicture(data, count);
+                    yield return DownloadPicture(data, count, targetPath);
                 }
                 else
                 {
                     Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
                     data.Ntexture = new NTexture(texture);
                     data.Bytes = texture.EncodeToJPG();
+                    MatchingCompetitionDataManager.Instance.DownloadImageToLocal(data.Bytes,data.JudgingInfo.WorksId,targetPath);
                 }
             }
         }
@@ -120,18 +142,18 @@ namespace GFGGame
                         Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
                         data.Ntexture = new NTexture(texture);
                         data.Bytes = texture.EncodeToJPG();
-                    }
+                }
                 }
             }
 
-        public static IEnumerator DownloadMyself(string nTextture)
+        public static IEnumerator DownloadMyself(string nTextture,string targetPath)
         {
-            yield return DownloadMyPicture(nTextture, 1);
+            yield return DownloadMyPicture(nTextture, 1,targetPath);
             ET.Log.Debug("Download  finish!!!");
             EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
         }
 
-        private static IEnumerator DownloadMyPicture(string nTextture, int count)
+        private static IEnumerator DownloadMyPicture(string nTextture, int count,string targetPath)
         {
             if (count >= 3)
             {
@@ -145,13 +167,14 @@ namespace GFGGame
                 if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
                 {
                     count += 1;
-                    yield return DownloadMyPicture(nTextture, count);
+                    yield return DownloadMyPicture(nTextture, count,targetPath);
                 }
                 else
                 {
                     Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
                     MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture);
                     MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG();
+                    //MatchingCompetitionDataManager.Instance.DownloadImageToLocal(MatchingCompetitionDataManager.Instance.MyBytes, MatchingCompetitionDataManager.Instance.WorksID,targetPath);
                 }
             }
         }

+ 6 - 6
GameClient/Assets/Game/HotUpdate/ServerProxy/MatchingCompetitionSproxy.cs

@@ -45,7 +45,7 @@ namespace GFGGame
             }
             if(response.MyWorks != null && response.MyWorks.PictureTempUrl != "")
             {
-                Timers.inst.StartCoroutine(MatchingPhotoHelper.DownloadMyself(response.MyWorks.PictureTempUrl));
+                Timers.inst.StartCoroutine(MatchingPhotoHelper.DownloadMyself(response.MyWorks.PictureTempUrl,MatchingCompetitionDataManager.Instance.LoaclMy));
             }
             MatchingCompetitionDataManager.Instance.AnalysisInfoToList();
             return true;
@@ -67,7 +67,7 @@ namespace GFGGame
             {
                 return false;
             }
-            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingOneDataManager.Instance.OneRoleInfo));
+            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingOneDataManager.Instance.OneRoleInfo,MatchingCompetitionDataManager.Instance.LoaclMy));
             MatchingOneDataManager.Instance.AnalysisInfoToList();
             return true;
         }
@@ -111,14 +111,14 @@ namespace GFGGame
             MatchingLeftDataManager.Instance.LeftRoleInfo.JudgingInfo = response.JudgingRoundRoleInfoList[0];
             if(response.JudgingRoundRoleInfoList[0] != null)
             {
-                Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingLeftDataManager.Instance.LeftRoleInfo));
+                Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingLeftDataManager.Instance.LeftRoleInfo,MatchingCompetitionDataManager.Instance.LoaclMy));
             }
             MatchingLeftDataManager.Instance.AnalysisInfoToList();
 
             MatchingRightDataManager.Instance.RightRoleInfo.JudgingInfo = response.JudgingRoundRoleInfoList[1];
             if (response.JudgingRoundRoleInfoList[1] != null)
             {
-                Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingRightDataManager.Instance.RightRoleInfo));
+                Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingRightDataManager.Instance.RightRoleInfo,MatchingCompetitionDataManager.Instance.LoaclMy));
             }
             MatchingRightDataManager.Instance.AnalysisInfoToList();
             return true;
@@ -163,7 +163,7 @@ namespace GFGGame
                 data.JudgingInfo = item;
                 MatchingCompetitionDataManager.Instance._currentRankList.Add(data);
             }
-            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingCompetitionDataManager.Instance._currentRankList));
+            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingCompetitionDataManager.Instance._currentRankList,MatchingCompetitionDataManager.Instance.LocalRankPath));
             return true;
         }
         //获取往期作品集
@@ -193,7 +193,7 @@ namespace GFGGame
                 data.JudgingInfo = item;
                 MatchingCompetitionDataManager.Instance._BeforeRankList.Add(data);
             }
-            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingCompetitionDataManager.Instance._BeforeRankList));
+            Timers.inst.StartCoroutine(MatchingPhotoHelper.Download(MatchingCompetitionDataManager.Instance._BeforeRankList,MatchingCompetitionDataManager.Instance.LocalBeforePath));
             return true;
         }
 

+ 5 - 1
GameClient/Assets/Game/HotUpdate/Views/MatchingCompetition/MatchingCompetitionUpLoadView.cs

@@ -56,6 +56,7 @@ namespace GFGGame
             _ui.m_c1.selectedIndex = MatchingCompetitionDataManager.Instance.MatchingState;
             _ui.m_titleText.text = JudgingRoundOpenCfgArray.Instance.dataArray[MatchingCompetitionDataManager.Instance.MatchingCompetitionSeason - 1].Name;
             isCountTime = false;
+            _ui.m_btnRank.touchable = true;
             UpdateView();
             UpdateHead();
             UpdateDress();
@@ -264,13 +265,16 @@ namespace GFGGame
         }
         private async void OnClickBtnRank()
         {
+            _ui.m_btnRank.touchable = false;
             bool result = await MatchingCompetitionSproxy.ReqCurrentRank();
             if(result)
             {
                 ViewManager.Show<MatchingCompetitionRankView>();
-            }   
+                _ui.m_btnRank.touchable = true;
+            }
             else
             {
+                _ui.m_btnRank.touchable = true;
                 PromptController.Instance.ShowFloatTextPrompt("暂无玩家上榜!");
             }
         }