Exemplo n.º 1
0
        public static async Task <bool> UpdateSaveFromFTP(SavesClass SV, ProgressBar PB, Label LBL)
        {
            SavesContainer SVC = new SavesContainer();

            try
            {
                SV.IsUpdate = "1";
                string SV_obj = "";
                SV_obj = SV_obj + JsonConvert.SerializeObject(SV, Formatting.Indented);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data/" + WebUtility.UrlEncode(SV.FileName)));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Headers.Add("authorization", Constants.ApiKey);
                request.Method    = "POST";
                request.KeepAlive = true;
                byte[] byteArray = Encoding.UTF8.GetBytes(SV_obj);
                request.ContentLength = byteArray.Length;
                using (Stream DataStream = await request.GetRequestStreamAsync())
                {
                    DataStream.Write(byteArray, 0, byteArray.Length);
                    DataStream.Close();

                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream ResponseStream = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(ResponseStream))
                            {
                                string ServerResponse = await sr.ReadToEndAsync();

                                SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                                if (SVC.status.Equals("1"))
                                {
                                    //Success
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.UpdateMsg(103, PB, LBL);
                                    return(true);
                                }
                                else
                                {
                                    //Fail
                                    await Delete.DeleteSelSave(SV, PB, LBL);

                                    MessageBox.Show(SVC.message);
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                await Delete.DeleteSelSave(SV, PB, LBL);

                MessageBox.Show("The save info could no be updated due to the following error: " + e.Message + System.Environment.NewLine + "Please try again later.");
                return(false);
            }
        }
Exemplo n.º 2
0
        public static async Task <string> GetDLCountFromWeb(string FileName, string OldValue)
        {
            SavesContainer SVC = new SavesContainer();

            string[] ColNames = { "Username", "FileName", "GameName", "SaveType", "Region", "Size", "Title", "Description", "FilesIncluded", "HasExtData", "DLCount", "Date_Created", "Date_Modif" };
            try
            {
                SQLiteHelper   db      = new SQLiteHelper();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data/" + WebUtility.UrlEncode(FileName)));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                //request.Headers.Add("authorization", Constants.ApiKey);
                request.Method = "GET";
                using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                {
                    using (Stream ResponseStream = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(ResponseStream))
                        {
                            string ServerResponse = sr.ReadToEnd();
                            SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                            if (SVC.status.Equals("1"))
                            {
                                //Success
                                foreach (var save in SVC.Saves)
                                {
                                    await Task.Run(() => {
                                        Dictionary <string, string> Values = new Dictionary <string, string>();
                                        List <SQLiteParameter> prm         = new List <SQLiteParameter>();
                                        prm.Add(new SQLiteParameter("@FileName")
                                        {
                                            Value = FileName
                                        });
                                        Values.Add(ColNames[10], save.DLCount);
                                        db.Update(Constants.files_data, Values, "FileName = @FileName", prm);
                                    });

                                    return(save.DLCount);
                                }
                                return(OldValue);
                            }
                            else
                            {
                                //Fail
                                return(OldValue);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //MessageBox.Show("Could not get info from web due to the following error: " + e.Message);
                return(OldValue);
            }
        }
Exemplo n.º 3
0
        public static async Task <bool> UpdateDLCount(SavesClass SV)
        {
            SavesContainer SVC = new SavesContainer();

            try
            {
                SV.IsUpdate = "1";
                string SV_obj = "";

                SV_obj = SV_obj + JsonConvert.SerializeObject(SV, Formatting.Indented);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data"));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Method      = "POST";

                byte[] byteArray = Encoding.UTF8.GetBytes(SV_obj);
                request.ContentLength = byteArray.Length;
                using (Stream DataStream = await request.GetRequestStreamAsync())
                {
                    DataStream.Write(byteArray, 0, byteArray.Length);
                    DataStream.Close();

                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream ResponseStream = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(ResponseStream))
                            {
                                string ServerResponse = await sr.ReadToEndAsync();

                                SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                                if (SVC.status.Equals("1"))
                                {
                                    //Success
                                    return(true);
                                }
                                else
                                {
                                    //Fail
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public static async Task <bool> InsertNewSave(SavesClass SV, ProgressBar PB, Label LBL)
        {
            SavesContainer SVC = new SavesContainer();

            try
            {
                string SV_obj = "";

                SV_obj = SV_obj + JsonConvert.SerializeObject(SV, Formatting.Indented);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data"));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Headers.Add("authorization", Constants.ApiKey);
                request.Method = "POST";

                byte[] byteArray = Encoding.UTF8.GetBytes(SV_obj);
                request.ContentLength = byteArray.Length;
                using (Stream DataStream = await request.GetRequestStreamAsync())
                {
                    DataStream.Write(byteArray, 0, byteArray.Length);
                    DataStream.Close();

                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream ResponseStream = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(ResponseStream))
                            {
                                string ServerResponse = await sr.ReadToEndAsync();

                                SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                                if (SVC.status.Equals("1"))
                                {
                                    //Success
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.UploadMsg(103, PB, LBL);
                                    return(true);
                                }
                                else
                                {
                                    //Fail
                                    await Delete.DeleteSelSave(SV, PB, LBL);

                                    MessageBox.Show("An error ocurred when trying to register the upload in the DB, please try again.");
                                    UpdateMessages.UploadMsg(103, PB, LBL);
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException e)
            {
                await Delete.DeleteSelSave(SV, PB, LBL);

                if (e.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)e.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string error = reader.ReadToEnd();
                            SVC = JsonConvert.DeserializeObject <SavesContainer>(error);
                            MessageBox.Show("The save could no be uploaded due to the following error: " + SVC.message);
                        }
                    }
                }
                UpdateMessages.UploadMsg(103, PB, LBL);
                return(false);
            }
        }
Exemplo n.º 5
0
        public static async Task <bool> DeleteSelSave(SavesClass SV, ProgressBar PB, Label LBL)
        {
            UpdateMessages.DeleteMsg(10, PB, LBL);
            SavesContainer SVC = new SavesContainer();

            try
            {
                SV.IsDelete = "1";
                string SV_obj = "";

                SV_obj = SV_obj + JsonConvert.SerializeObject(SV, Formatting.Indented);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data/" + WebUtility.UrlEncode(SV.FileName)));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Headers.Add("authorization", Constants.ApiKey);
                request.Method = "POST";

                byte[] byteArray = Encoding.UTF8.GetBytes(SV_obj);
                request.ContentLength = byteArray.Length;
                using (Stream DataStream = await request.GetRequestStreamAsync())
                {
                    DataStream.Write(byteArray, 0, byteArray.Length);
                    DataStream.Close();
                    UpdateMessages.DeleteMsg(50, PB, LBL);
                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream ResponseStream = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(ResponseStream))
                            {
                                string ServerResponse = await sr.ReadToEndAsync();

                                SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                                if (SVC.status.Equals("1"))
                                {
                                    //Success
                                    using (MALogin MAL = new MALogin())
                                    {
                                        MegaApiClient client = MAL.Login();
                                        //DELETE FILE FROM MEGA
                                        var nodes    = client.GetNodes();
                                        var SelNodes = nodes.First(n => n.Name == SV.FileName && n.Type == NodeType.File);
                                        await client.DeleteAsync(SelNodes, false);

                                        client.Logout();
                                    }
                                    UpdateMessages.DeleteMsg(100, PB, LBL);
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.DeleteMsg(103, PB, LBL);
                                    return(true);
                                }
                                else
                                {
                                    //Fail
                                    UpdateMessages.DeleteMsg(101, PB, LBL);
                                    MessageBox.Show(SVC.message);
                                    UpdateMessages.DeleteMsg(103, PB, LBL);
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException e)
            {
                UpdateMessages.DeleteMsg(102, PB, LBL);
                //MessageBox.Show("Save could not be deleted due to the following error:" + e.Message);
                if (e.Response != null)
                {
                    using (var errorResponse = (HttpWebResponse)e.Response)
                    {
                        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                        {
                            string error = reader.ReadToEnd();
                            SVC = JsonConvert.DeserializeObject <SavesContainer>(error);


                            MessageBox.Show(SVC.message);

                            return(false);
                        }
                    }
                }
                UpdateMessages.DeleteMsg(103, PB, LBL);
                return(false);
            }
        }
Exemplo n.º 6
0
        public static async Task <bool> GetAllSavesFromWeb()
        {
            SavesContainer SVC = new SavesContainer();

            string[] ColNames = { "Username", "FileName", "GameName", "SaveType", "Region", "Size", "Title", "Description", "FilesIncluded", "HasExtData", "DLCount", "Date_Created", "Date_Modif" };
            try
            {
                SQLiteHelper   db      = new SQLiteHelper();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.URL + "/api.savecentral.com/v1/files_data"));
                request.Accept      = "application/json";
                request.ContentType = "application/json";
                request.Headers.Add("authorization", Constants.ApiKey);
                request.Method = "GET";
                using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                {
                    using (Stream ResponseStream = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(ResponseStream))
                        {
                            string ServerResponse = sr.ReadToEnd();
                            SVC = JsonConvert.DeserializeObject <SavesContainer>(ServerResponse);
                            if (SVC.status.Equals("1"))
                            {
                                //Success
                                db.ClearTable(Constants.files_data);
                                if (db.OpenNonQueryTransaction())
                                {
                                    foreach (var saves in SVC.Saves)
                                    {
                                        Dictionary <string, string> Values = new Dictionary <string, string>();
                                        Values.Add(ColNames[0], saves.Username);
                                        Values.Add(ColNames[1], saves.FileName);
                                        Values.Add(ColNames[2], saves.GameName);
                                        Values.Add(ColNames[3], saves.SaveType);
                                        Values.Add(ColNames[4], saves.Region);
                                        Values.Add(ColNames[5], saves.Size);
                                        Values.Add(ColNames[6], saves.Title);
                                        Values.Add(ColNames[7], saves.Description);
                                        Values.Add(ColNames[8], saves.FilesIncluded);
                                        Values.Add(ColNames[9], saves.HasExtData);
                                        Values.Add(ColNames[10], saves.DLCount);
                                        Values.Add(ColNames[11], saves.Date_Created);
                                        Values.Add(ColNames[12], saves.Date_Modif);

                                        db.AddNonQueryForOpenTransaction(Constants.files_data, Values);
                                    }
                                    db.CloseNonQueryTransaction();
                                }
                                return(true);
                            }
                            else
                            {
                                //Fail
                                return(false);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not download info from web due to the following error: " + e.Message);
                return(false);
            }
        }