コード例 #1
0
        private static void UploadData()
        {
            var 务器     = Config["服务器"];
            var 本地数据路径 = new DirectoryInfo(Config["本地数据路径"]);
            var 间隔     = int.Parse(Config["上传间隔"]);
            var DB用户名  = Config["DB用户名"];
            var DB密码   = Config["DB密码"];
            var OSSAKI = Config["OSSAKI"];
            var OSSAKS = Config["OSSAKS"];

            OssHelper.IsDevelopment   = true;
            OssHelper.AccessKeyID     = OSSAKI;
            OssHelper.AccessKeySecret = OSSAKS;

            var dataContext = new MkhxCoreContext(DB用户名, DB密码);

            while (true)
            {
                WriteText("\r\n");
                WriteText(DateTime.Now + "\t");
                WriteInfo("更新数据..." + "\r\n");

                var files = 本地数据路径.GetFiles("*.txt");
                foreach (var fi in files)
                {
                    var FileName = fi.Name.Split('_')[0].ToLower();

                    switch (FileName)
                    {
                    case "allcards":
                    case "allrunes":
                    case "allskills":
                    case "allmapstage":
                    case "allmaphardstage":
                    case "keywords":
                    {
                        try
                        {
                            var Version = GetVersion(fi);

                            WriteText(DateTime.Now + "\t");
                            WriteText(FileName + "\t");
                            WriteText(Version + "\t");

                            var newest = dataContext.GameDataFiles.Where(m => m.Server == 务器 && m.FileName == FileName).OrderByDescending(m => m.Time).FirstOrDefault();

                            if (newest?.Version != Version)
                            {
                                var key = "data/" + Version + ".json";
                                if (!OssHelper.Exist(key))
                                {
                                    OssHelper.Upload(key, fi);
                                }

                                var GameDataFiles = new GameDataFiles
                                {
                                    Id       = Guid.NewGuid(),
                                    Version  = Version,
                                    FileName = FileName,
                                    Server   = 务器,
                                    Time     = fi.LastWriteTime,
                                    Remark   = "",
                                };
                                dataContext.GameDataFiles.Add(GameDataFiles);
                                dataContext.SaveChanges();
                                WriteSuccess("成功" + "\r\n");

                                Console.Title = $"服务器{Config["服务器"]} 更新时间:{fi.LastWriteTime}";
                            }
                            else
                            {
                                WriteWarning("不需要更新" + "\r\n");
                            }
                        }
                        catch (Exception ex)
                        {
                            WriteError("失败 - " + ex + "\r\n");
                        }
                        break;
                    }
                    }
                }

                var Status     = "GameDataLastUpdateTime";
                var UpdateTime = DateTime.Now.ToString();
                var Data       = dataContext.Enum.First(m => m.Type == "GameDataLastUpdateTime");
                Data.Desc         = Status;
                Data.Value1Format = UpdateTime;
                dataContext.SaveChanges();

                WriteText(DateTime.Now + "\t");
                WriteInfo("数据更新完毕" + "\r\n");
                WriteText("\r\n");

                Thread.Sleep(间隔);
            }
        }