示例#1
0
 public async Task <IActionResult> GetAutoInfo([FromBody] DSinfo info)
 {
     _logger.LogInformation("Getting info from {id}", info.Name);
     if (info.Name.Length != 64)
     {
         return(BadRequest("Wrong id."));
     }
     if (r.IsMatch(info.Name))
     {
         return(BadRequest("Wrong id."));
     }
     return(Ok(_dataRepository.AutoInfo(info)));
 }
示例#2
0
        public string AutoInfo(DSinfo info)
        {
            DSRestPlayer player = null;

            lock (dblock)
            {
                player = _context.DSRestPlayers.FirstOrDefault(f => f.Name == info.Name);
            }

            string myreturn = "";

            if (player != null)
            {
                //DEBUG
                //player.LastRep = new DateTime(2020, 4, 21);

                DateTime LastRep = DateTime.MinValue;
                if (info.LastRep.Length == 14)
                {
                    int year  = int.Parse(info.LastRep.Substring(0, 4));
                    int month = int.Parse(info.LastRep.Substring(4, 2));
                    int day   = int.Parse(info.LastRep.Substring(6, 2));
                    int hour  = int.Parse(info.LastRep.Substring(8, 2));
                    int min   = int.Parse(info.LastRep.Substring(10, 2));
                    int sec   = int.Parse(info.LastRep.Substring(12, 2));
                    LastRep = new DateTime(year, month, day, hour, min, sec);
                }

                Version dbversion = new Version(player.Version);
                Version clversion = new Version(info.Version);
                Version bpversion = new Version("2.0.0");
                _logger.LogInformation($"db: {dbversion}; client: {clversion}");
                if (clversion >= bpversion && dbversion < bpversion)
                {
                    myreturn = "0";
                }
                else
                if (player.LastRep == LastRep)
                {
                    myreturn = "UpToDate";
                }
                else
                {
                    myreturn = player.LastRep.ToString("yyyyMMddHHmmss");
                }
            }
            else
            {
                player         = new DSRestPlayer();
                player.Name    = info.Name;
                player.Json    = info.Json;
                player.Total   = info.Total;
                player.Version = info.Version;
                myreturn       = "0";
                _context.DSRestPlayers.Add(player);
            }
            lock (dblock)
            {
                _context.SaveChanges();
            }

            Infos[info.Name] = info;

            return(myreturn);
        }
示例#3
0
        public async Task <bool> GetAutoFile(string id, string myfile)
        {
            DSRestPlayer player = _context.DSRestPlayers.Include(p => p.Uploads).FirstOrDefault(f => f.Name == id);

            if (player == null)
            {
                return(false);
            }

            player.LastUpload = DateTime.UtcNow;

            string mypath = SharedDir + "/" + id;
            string mysum  = SharedDir + "/sum/" + id + ".json";

            if (!Directory.Exists(mypath))
            {
                try
                {
                    Directory.CreateDirectory(mypath);
                }
                catch (Exception e)
                {
                    _logger.LogError("Could not create directory " + mypath + " " + e.Message);
                    return(false);
                }
            }
            if (File.Exists(myfile))
            {
                if (!File.Exists(mysum))
                {
                    File.Create(mysum).Dispose();
                }
                string myjson = "";
                return(await Task.Run(() => myjson = Decompress(new FileInfo(myfile), mypath, id, _logger))
                       .ContinueWith(task =>
                {
                    if (myjson == "")
                    {
                        return false;
                    }
                    if (File.Exists(myjson) && new FileInfo(myjson).Length > 0)
                    {
                        using (StreamWriter sw = File.AppendText(mysum))
                        {
                            foreach (string line in File.ReadLines(myjson))
                            {
                                if (!line.StartsWith(@"{"))
                                {
                                    return false;
                                }
                                sw.WriteLine(line);
                                dsreplay replay = null;
                                try
                                {
                                    replay = JsonSerializer.Deserialize <dsreplay>(line);
                                    if (replay != null)
                                    {
                                        DSReplay dsreplay = Map.Rep(replay);
                                        DSPlayer dsplayer = dsreplay.DSPlayer.FirstOrDefault(f => f.NAME == "player");
                                        if (dsplayer != null)
                                        {
                                            dsplayer.NAME = id;
                                        }
                                        dsreplay.Upload = player.LastUpload;
                                        DSReplays.Add(dsreplay);
                                        player.Data++;
                                    }
                                } catch (Exception e)
                                {
                                    _logger.LogError("Could not Deserialize and map replay " + e.Message);
                                }
                            }
                        }

                        DSRestUpload upload = new DSRestUpload();
                        upload.Upload = player.LastUpload;
                        upload.DSRestPlayer = player;
                        _context.DSRestUploads.Add(upload);

                        DSinfo info = Infos.FirstOrDefault(f => f.Key == id).Value;
                        if (info != null)
                        {
                            DateTime LastRep = DateTime.MinValue;
                            if (info.LastRep.Length == 14)
                            {
                                int year = int.Parse(info.LastRep.Substring(0, 4));
                                int month = int.Parse(info.LastRep.Substring(4, 2));
                                int day = int.Parse(info.LastRep.Substring(6, 2));
                                int hour = int.Parse(info.LastRep.Substring(8, 2));
                                int min = int.Parse(info.LastRep.Substring(10, 2));
                                int sec = int.Parse(info.LastRep.Substring(12, 2));
                                LastRep = new DateTime(year, month, day, hour, min, sec);
                            }
                            player.LastRep = LastRep;
                            player.Json = info.Json;
                            player.Total = info.Total;
                            player.Version = info.Version;
                        }

                        lock (dblock)
                        {
                            _context.SaveChanges();
                        }
                        InsertDSReplays();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }));
            }
            else
            {
                return(false);
            }
        }
示例#4
0
        public bool AutoUpload()
        {
            string hash  = "UndEsWarSommer";
            string hash2 = "UndEsWarWinter";

            using (SHA256 sha256Hash = SHA256.Create())
            {
                string names = String.Join(";", DSdata.Config.Players);
                hash  = GetHash(sha256Hash, names);
                hash2 = GetHash(sha256Hash, Program.myJson_file);
            }
            var client = new RestClient("https://www.pax77.org:9126");

            // DEBUG
            //var client = new RestClient("https://192.168.178.28:9001");
            //var client = new RestClient("http://192.168.178.28:9000");
            //var client = new RestClient("https://localhost:44315");
            //var client = new RestClient("http://localhost:5000");

            List <DSReplay> UploadReplays      = new List <DSReplay>();
            List <dsreplay> UploadReplaysMaped = new List <dsreplay>();
            string          lastrep            = "";
            string          exp_csv            = "";
            RestRequest     restRequest        = null;
            IRestResponse   response           = null;

            if (DSdata.Status.Count > 0)
            {
                DSReplay lrep = null;
                lrep = _db.GetLatestReplay();
                if (lrep != null)
                {
                    lastrep = lrep.GAMETIME.ToString("yyyyMMddHHmmss");
                }
            }

            DSinfo info = new DSinfo();

            info.Name       = hash;
            info.Json       = hash2;
            info.LastRep    = lastrep;
            info.LastUpload = DSdata.Config.LastUpload;
            info.Total      = DSdata.Status.Count;
            info.Version    = DSdata.DesktopVersion.ToString();

            _logger.LogInformation("Upload: AutoInfo");
            restRequest = new RestRequest("/secure/data/autoinfo", Method.POST);
            restRequest.RequestFormat = DataFormat.Json;
            restRequest.AddHeader("Authorization", "DSupload77");
            restRequest.AddJsonBody(info);
            response = client.Execute(restRequest);

            _logger.LogInformation($"Upload: autoinfo response: {response.Content}");
            if (response != null && response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                if (response.Content.Contains("UpToDate"))
                {
                    return(true);
                }
                else
                {
                    lastrep = response.Content;
                }
            }
            else
            {
                return(false);
            }

            lastrep = new String(lastrep.Where(Char.IsDigit).Take(14).ToArray());

            if (!lastrep.Any())
            {
                return(false);
            }

            string   gametime = lastrep;
            DateTime gtime    = DateTime.MinValue;

            if (DSdata.Config.FullSend == false && gametime.Length == 14)
            {
                int year  = int.Parse(gametime.Substring(0, 4));
                int month = int.Parse(gametime.Substring(4, 2));
                int day   = int.Parse(gametime.Substring(6, 2));
                int hour  = int.Parse(gametime.Substring(8, 2));
                int min   = int.Parse(gametime.Substring(10, 2));
                int sec   = int.Parse(gametime.Substring(12, 2));
                gtime = new DateTime(year, month, day, hour, min, sec);
            }

            UploadReplays = _db.GetUploadReplay(gtime);

            if (!UploadReplays.Any())
            {
                return(true);
            }

            List <string> anonymous = new List <string>();

            foreach (DSReplay rep in UploadReplays)
            {
                rep.REPLAYPATH = "";
                foreach (DSPlayer pl in rep.DSPlayer)
                {
                    string plname = pl.NAME;
                    if (DSdata.Config.Players.Contains(pl.NAME))
                    {
                        pl.NAME = "player";
                    }
                    else
                    {
                        pl.NAME = "player" + pl.REALPOS.ToString();
                    }
                }
                string json = "";
                //json = Newtonsoft.Json.JsonConvert.SerializeObject(rep);
                try
                {
                    json = System.Text.Json.JsonSerializer.Serialize(rep);
                }
                catch (Exception e)
                {
                    _logger.LogError($"Upload: {e.Message}");
                }
                anonymous.Add(json);
            }
            exp_csv = Program.workdir + "\\export.json";

            File.WriteAllLines(exp_csv, anonymous);

            string exp_csv_gz = exp_csv + ".gz";

            using (FileStream fileToBeZippedAsStream = new FileInfo(exp_csv).OpenRead())
            {
                using (FileStream gzipTargetAsStream = new FileInfo(exp_csv_gz).Create())
                {
                    using (GZipStream gzipStream = new GZipStream(gzipTargetAsStream, CompressionMode.Compress))
                    {
                        try
                        {
                            fileToBeZippedAsStream.CopyTo(gzipStream);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"Upload: {ex.Message}");
                        }
                    }
                }
            }
            if (DSdata.Config.FullSend == true)
            {
                restRequest = new RestRequest("/secure/data/dbfullsend/" + hash);
            }
            else
            {
                restRequest = new RestRequest("/secure/data/dbupload/" + hash);
            }
            restRequest.RequestFormat = DataFormat.Json;
            restRequest.Method        = Method.POST;
            restRequest.AddHeader("Authorization", "DSupload77");
            restRequest.AddFile("content", exp_csv_gz);

            response = client.Execute(restRequest);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                if (DSdata.Config.FullSend == true)
                {
                    DSdata.Config.FullSend = false;
                    Status.SaveConfig();
                }
                DSdata.Config.LastUpload = DateTime.UtcNow;
                Status.SaveConfig();
                return(true);
            }
            else
            {
                return(false);
            }
        }