cyzhao 3 سال پیش
والد
کامیت
560e6ec91d

+ 1 - 0
CSserver/DataTransfer/DataTransfer.csproj

@@ -12,6 +12,7 @@
 
   <ItemGroup>
     <PackageReference Include="MySql.Data" Version="8.0.25" />
+    <PackageReference Include="NPOI" Version="2.5.4" />
     <PackageReference Include="StackExchange.Redis" Version="2.2.50" />
   </ItemGroup>
 

+ 22 - 3
CSserver/DataTransfer/Program.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using System.Diagnostics;
+using DataTransfer.tongji;
 
 namespace DataTransfer
 {
@@ -22,6 +23,8 @@ namespace DataTransfer
             
         }
         public static int mask = 0;
+        public static int mask_lose = 0;
+        public static int mask_usernum = 0;
 
         public static void transfer()
         {
@@ -31,7 +34,7 @@ namespace DataTransfer
                 try
                 {                               
                     int hour = DateTime.Now.Hour;            
-                    if (hour == 10 && mask == 0)//正式的是晚上2点进行数据传输
+                    if (hour == 2 && mask == 0)//正式的是晚上2点进行数据传输
                     {
                         Console.WriteLine("数据传输的时间到了开始执行程序.......");
                         //进行数据传输
@@ -39,10 +42,26 @@ namespace DataTransfer
                         dataTransfer();                      
                     }
 
-                    if (hour != 10 && mask != 0)
+                    if (hour != 2 && mask != 0)
                     {
                         mask = 0;
                     }
+
+                    #region------流失玩家指标分析
+
+                    if (hour == 17 && mask_lose == 0)//正式的是晚上12点进行数据传输,否则程序里的day就不是今天了
+                    {
+                        Console.WriteLine("统计信息程序开始执行............");
+                        //进行数据传输
+                        mask_lose = 1;
+                        UserStatistics.DataDeal();                     
+                    }
+
+                    if (hour != 17 && mask_lose != 0)
+                    {
+                        mask_lose = 0;
+                    }
+                    #endregion                   
                 }
                 catch (Exception ex)
                 {                  
@@ -241,6 +260,6 @@ namespace DataTransfer
             {
                 Console.WriteLine("玩家任务进度redis-key不存在所以不在数据传输处理");
             }          
-        }
+        }  
     }
 }

+ 236 - 0
CSserver/DataTransfer/tongji/UserStatistics.cs

@@ -0,0 +1,236 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using System.Threading;
+using System.Data;
+using StackExchange.Redis;
+using CSharpUtil;
+
+using Newtonsoft.Json.Linq;
+using System.Diagnostics;
+
+namespace DataTransfer.tongji
+{
+    class UserStatistics
+    {
+        public static void DataDeal()
+        {
+            LoseUserTargetCollect();
+        }
+
+        /// <summary>
+        ///  流失玩家的指标的统计
+        /// </summary>
+        public static void LoseUserTargetCollect()
+        {
+            Console.WriteLine("进入流失玩家的指标的统计方法..............");
+
+            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
+            float day = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
+            Console.WriteLine("当前时间day==.............."+ day);
+            var mem = Redis.Ins.GetDatabase(0);
+
+            List<int> retainedlist = new List<int>();
+            retainedlist.Add(1);
+            retainedlist.Add(2);
+            retainedlist.Add(3);
+            retainedlist.Add(4);
+            retainedlist.Add(5);
+            retainedlist.Add(6);
+            retainedlist.Add(7);
+            retainedlist.Add(14);
+            //retainedlist.Add(30);
+
+            HashEntry[] zoneidList = mem.HashGetAll(MemKey_Statistics.Zonelist());
+            foreach (var item in zoneidList)
+            {  
+                string[] sList = item.ToString().Split(':');
+                int zoneid = int.Parse(sList[0]);//区
+                Console.WriteLine("zoneid............"+zoneid);
+                foreach (int type in retainedlist)
+                {
+                    Console.WriteLine("type............" + type);
+                    if (mem.KeyExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day)))
+                    {                    
+                        //loseUserDataTransfer.excelText(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), type, zoneid,day);
+
+                        continue;
+                    }
+
+                    List<string> loseUIdList = loseUserListCollect(type,zoneid,day);
+                    if (loseUIdList.Count <= 0)
+                    {
+                        continue;
+                    }     
+                    TargetCollect(loseUIdList,type,zoneid, day);                   
+                }
+
+                userNumRecord(zoneid);
+            }
+        }
+       
+        /// <summary>
+        /// 统计流失玩家uid
+        /// </summary>
+        /// <param name="type"></param>
+        /// <param name="zoneid"></param>
+        /// <param name="day"></param>
+        public static List<string> loseUserListCollect(int type,int zoneid, float day)
+        {
+            //次日留存               
+            string dayKey = MemKey_Statistics.GamerunLoginUser(zoneid, day);
+            string lastDayKey = MemKey_Statistics.GamerunLoginUser(zoneid, day - type);
+
+            List<string> loseUIdList = new List<string>();
+
+            var mem = Redis.Ins.GetDatabase(0);
+            if (!mem.KeyExists(dayKey) || !mem.KeyExists(lastDayKey))
+            {
+                return loseUIdList;
+            }
+            
+            string[] list = mem.HashKeys(dayKey).ToStringArray();
+            string[] nextDaylist = mem.HashKeys(lastDayKey).ToStringArray();
+            
+            foreach (string uid in nextDaylist)
+            {
+                if (!list.Contains(uid))
+                {
+                    Console.WriteLine("uid............" + uid);
+                    loseUIdList.Add(uid);
+                }
+            }
+            return loseUIdList;
+        }
+
+        /// <summary>
+        /// 查看次日流失玩家的指标信息
+        /// </summary>
+        /// <param name="loseUIdList"></param>
+        /// <param name="zoneid"></param>
+        /// <param name="day"></param>
+        public static void TargetCollect(List<string> loseUIdList,int type,int zoneid,float day)
+        {
+            var mem = Redis.Ins.GetDatabase(0);
+
+            foreach (string uid in loseUIdList)
+            {              
+                if (!mem.KeyExists(MemKey_User.gameInfo(uid, zoneid)))
+                {                  
+                    continue;
+                }
+              
+                #region----------玩家等级
+                //玩家等级
+                string baseInfo = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "baseInfo");
+                JObject baseInfoDic = JObject.Parse(baseInfo);
+                int level = int.Parse(baseInfoDic["level"].ToString());
+                string k = "level-" + level;
+                if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), k))
+                {
+                    mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), k, 1);
+                }
+                else
+                {
+                    List<HashEntry> list = new List<HashEntry>();
+                    HashEntry d = new HashEntry(k, 1);
+                    list.Add(d);
+
+                    mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), list.ToArray());
+                }
+                #endregion
+
+                #region--------------主线任务
+                
+                //主线任务
+                string privateState = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "privateState");
+                JObject privateStateDic = JObject.Parse(privateState); //
+                JObject jtaskCardTracing = JObject.Parse(privateStateDic["taskCardTracing"].ToString());
+                string taskId = "0";
+                if (jtaskCardTracing.ContainsKey("1"))//主线
+                {
+                    taskId = jtaskCardTracing["1"].ToString();                  
+                }
+
+                string store = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "store");
+                JObject storeDic = JObject.Parse(store);
+                JObject taskcardsDic = JObject.Parse(storeDic["taskcards"].ToString());
+
+                if (taskId != "0" )
+                {
+                    JObject dict = JObject.Parse(taskcardsDic[taskId].ToString());
+                    string typeId = dict["typeId"].ToString();
+                    string taskKey = "task-" + typeId;
+                    if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), taskKey))
+                    {
+                        mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), taskKey, 1);
+                    }
+                    else
+                    {
+                        List<HashEntry> list = new List<HashEntry>();
+                        HashEntry d = new HashEntry(taskKey, 1);
+                        list.Add(d);
+
+                        mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), list.ToArray());
+                    }
+                }
+                #endregion
+
+                #region--------------关卡
+                
+                string gates = mem.HashGet(MemKey_User.gameInfo(uid, zoneid), "gates");
+                JObject gatesDic = JObject.Parse(gates);
+                JObject normalsDic = JObject.Parse(gatesDic["normal"].ToString());
+                JObject lastgatesDic = JObject.Parse(normalsDic["gates"].ToString());
+                int gateId = 0;
+                foreach (KeyValuePair<string,JToken>kv in lastgatesDic)
+                {
+                    gateId = int.Parse(kv.Key);
+                }
+
+                string gateKey = "gateId-" + gateId;
+                if (mem.HashExists(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), gateKey))
+                {
+                    mem.HashIncrement(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), gateKey, 1);
+                }
+                else
+                {
+                    List<HashEntry> list = new List<HashEntry>();
+                    HashEntry d = new HashEntry(gateKey, 1);
+                    list.Add(d);
+
+                    mem.HashSet(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day), list.ToArray());
+                }
+                #endregion
+            }
+
+            loseUserDataTransfer.excelText(MemKey_Statistics.LoseUserKeyCount(type, zoneid, day),type,zoneid,day);
+
+        }
+
+        /// <summary>
+        /// 每天登陆人数纪录----以防刚哥记得的uid的rediskey删除
+        /// </summary>
+        /// <param name="zoneid"></param>
+        public static void userNumRecord(int zoneid)
+        {
+            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
+            float day = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
+
+
+            var mem = Redis.Ins.GetDatabase(0);
+            HashEntry[] ctxList = mem.HashGetAll(MemKey_Statistics.GamerunLoginUser(zoneid, day - 1));
+            int length = ctxList.Length;
+
+
+            List<HashEntry> list = new List<HashEntry>();
+            HashEntry d = new HashEntry(day, length);
+            list.Add(d);
+
+            mem.HashSet(MemKey_Statistics.userNumRecord(zoneid), list.ToArray());
+        }
+    }
+}

+ 172 - 0
CSserver/DataTransfer/tongji/loseUserDataTransfer.cs

@@ -0,0 +1,172 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NPOI.HSSF.UserModel;
+using System.IO;
+using StackExchange.Redis;
+
+
+//using System.Threading;
+//using System.Data;
+
+using CSharpUtil;
+//using Newtonsoft.Json;
+//using Newtonsoft.Json.Linq;
+//using System.Diagnostics;
+
+namespace DataTransfer.tongji
+{
+    class loseUserDataTransfer
+    {
+        public static void excelText111()
+        {
+            //导出:将数据库中的数据,存储到一个excel中
+
+            //1、查询数据库数据  
+
+            User user1 = new User() { Id = 1, Name = "小明", pwd = "123123", type = "学生" };
+            User user2 = new User() { Id = 2, Name = "小红", pwd = "123123", type = "学生" };
+            User user3 = new User() { Id = 3, Name = "小绿", pwd = "123123", type = "学生" };
+            User user4 = new User() { Id = 4, Name = "小白", pwd = "123123", type = "老师" };
+            User user5 = new User() { Id = 5, Name = "小黑", pwd = "123123", type = "老师" };
+            User user6 = new User() { Id = 6, Name = "小蓝", pwd = "123123", type = "老师" };
+            List<User> list = new List<User>();
+            list.Add(user1);
+            list.Add(user2);
+            list.Add(user3);
+            list.Add(user4);
+            list.Add(user5);
+            list.Add(user6);
+
+            //2、  生成excel
+            //2_1、生成workbook
+            //2_2、生成sheet
+            //2_3、遍历集合,生成行
+            //2_4、根据对象生成单元格
+            HSSFWorkbook workbook = new HSSFWorkbook();
+            //创建工作表
+            var sheet = workbook.CreateSheet("信息表");
+            //创建标题行(重点) 从0行开始写入
+            var row = sheet.CreateRow(0);
+            //创建单元格
+            var cellid = row.CreateCell(0);
+            cellid.SetCellValue("编号");
+            var cellname = row.CreateCell(1);
+            cellname.SetCellValue("用户名");
+            var cellpwd = row.CreateCell(2);
+            cellpwd.SetCellValue("密码");
+            var celltype = row.CreateCell(3);
+            celltype.SetCellValue("类型");
+
+            //遍历集合,生成行
+            int index = 1; //从1行开始写入
+            for (int i = 0; i < list.Count; i++)
+            {
+                int x = index + i;
+                var rowi = sheet.CreateRow(x);
+                var id = rowi.CreateCell(0);
+                id.SetCellValue(list[i].Id);
+                var name = rowi.CreateCell(1);
+                name.SetCellValue(list[i].Name);
+                var pwd = rowi.CreateCell(2);
+                pwd.SetCellValue(list[i].pwd);
+                var type = rowi.CreateCell(3);
+                type.SetCellValue(list[i].type);
+            }
+
+            FileStream file = new FileStream(@"F:\excel\信息表.xls", FileMode.CreateNew, FileAccess.Write);
+            workbook.Write(file);
+            file.Dispose();
+        }
+
+        public static void excelText(string memKey,int retain,int zoneid,float day)
+        {
+            //获取redis种的数据
+            var mem = Redis.Ins.GetDatabase(0);
+
+            HashEntry[] ctxList = mem.HashGetAll(memKey);
+            int length = ctxList.Length;      
+            if (length <= 0)
+            {
+                return;
+            }
+
+            HSSFWorkbook workbook = new HSSFWorkbook();
+            //创建工作表            
+            var sheet = workbook.CreateSheet("信息表");
+            var row = sheet.CreateRow(0);
+            //创建单元格
+            var cellid = row.CreateCell(0);
+            cellid.SetCellValue("指标类型");
+            var cellname = row.CreateCell(1);
+            cellname.SetCellValue("指标id");
+            var cellpwd = row.CreateCell(2);
+            cellpwd.SetCellValue("指标累计值");
+
+            int x = 1;
+            foreach (var item in ctxList)
+            {
+                
+                string[] sList = item.ToString().Split(':');
+                string type = sList[0];
+                int num = int.Parse(sList[1]);
+
+                string[] ctx = type.Split('-');
+                string name = ctx[0];
+                int id = int.Parse(ctx[1]);
+            
+                var rowi = sheet.CreateRow(x);
+                var cname = rowi.CreateCell(0);
+                cname.SetCellValue(name);
+
+                var cid = rowi.CreateCell(1);
+                cid.SetCellValue(id);
+
+                var cnum = rowi.CreateCell(2);
+                cnum.SetCellValue(num);
+                
+                x += 1;
+            }
+
+            string txtName = "流失玩家指标累计信息表_" +"zone"+zoneid+"_"+retain+"retaine_" + day;       
+
+            FileStream file = new FileStream(@"F:\excel\"+txtName+".xls", FileMode.CreateNew, FileAccess.Write);
+            workbook.Write(file);
+            file.Dispose();
+        }
+
+    }
+
+    public class User
+    {
+        /// <summary>
+        /// 索引
+        /// </summary>
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+
+        public string pwd { get; set; }
+
+        public string type { get; set; }
+    }
+
+    public class Model
+    {
+        /// <summary>
+        /// 索引
+        /// </summary>
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+
+        public int pwd { get; set; }
+
+        public string type { get; set; }
+    }
+
+
+
+}

+ 21 - 0
CSserver/Lib1/MemKey_User.cs

@@ -108,6 +108,11 @@ public class MemKey_User
     /// <param name="zoneid"></param>
     /// <returns></returns>
     public static string Guild(string uid, int zoneid) => $"{Key_(zoneid, uid)}-guild";
+   
+    public static string gameInfo(string uid, int zoneid)
+    {
+        return bUseShort() ? Key_(zoneid, uid) + "-g" : Key_(zoneid, uid) + "-gameinfo";
+    }
     #endregion
 }
 
@@ -162,4 +167,20 @@ public class MemKey_Statistics
     {
         return "TaskDataCollect-"+ zoneid;
     }
+
+    /// <summary>
+    /// 流失玩家的指标的统计
+    /// </summary>
+    /// <param name="zoneid"></param>
+    /// <returns></returns>
+    public static string LoseUserKeyCount(int type,int zoneid,float day)
+    {
+        string key = "LoseUserKeyCount-" + type + "Retaine-" +"zone"+zoneid + "-" + day;       
+        return key;
+    }
+
+    public static string userNumRecord(int zoneid)
+    {
+        return "loginUserNum-zone" + zoneid;
+    }
 }