Beispiel #1
0
        // Logging an account in
        public void AccountLogin(object password)
        {
            App.Popups.ShowInfo("ScrollsPost Login", "Logging in, this will only take a second...");

            WebClient wc = new WebClient();

            try {
                NameValueCollection form = new NameValueCollection();
                form["email"] = config.GetString("email");
                form["password"] = (String) password;
                form["id"] = App.MyProfile.ProfileInfo.id;
                form["uid"] = App.MyProfile.ProfileInfo.userUuid;
                form["name"] = App.MyProfile.ProfileInfo.name;

                byte[] bytes = wc.UploadValues(new Uri(mod.apiURL + "/v1/user"), "PUT", form);
                Dictionary<String, object> result = new JsonReader().Read<Dictionary<String, object>>(Encoding.UTF8.GetString(bytes));

                // Failed to login
                if( result.ContainsKey("errors") ) {
                    Dictionary<String, object> errors = (Dictionary<String, object>) result["errors"];
                    if( errors.ContainsKey("password") ) {
                        ShowAuthPassword(false, "<color=red>Password " + ((String[]) errors["password"])[0] + "</color>");
                    } else if( errors.ContainsKey("email") ) {
                        ShowAuthEmail("<color=red>Email " + ((String[]) errors["email"])[0] + "</color>");
                    }

                    // Save our keys so we don't store passwords
                } else {
                    App.Popups.ShowOk(this, "done", "Logged In!", "You're now logged into your ScrollsPost.com account!\n\nYour collection will automatically sync to ScrollsPost now, will let you know when the initial sync has finished in-game.", "Ok");

                    config.Add("user-id", (String) result["user_id"]);
                    config.Add("api-key", (String) result["api_key"]);
                    config.Remove("email");
                    config.Remove("last-card-sync");

                    if( result.ContainsKey("verif_key") ) {
                        config.Add("verif-key", (String) result["verif_key"]);
                    }

                    //if( config.ContainsKey("verif-key") ) {
                    //    verifier = new AccountVerifier(config);
                    //}

                    App.Communicator.sendRequest(new LibraryViewMessage());
                }

            } catch ( WebException we ) {
                App.Popups.ShowOk(this, "fail", "HTTP Error", "Unable to login due to an HTTP error.\nContact [email protected] for help.\n\n" + we.Message, "Ok");
                mod.WriteLog("Failed to register", we);
            }
        }
Beispiel #2
0
        public Dictionary<String, object> Upload(String path)
        {
            // Setup
            String boundary = String.Format("---------------------------{0}", (int)mod.TimeSinceEpoch());
            byte[] boundaryBytes = Encoding.ASCII.GetBytes(String.Format("\r\n--{0}\r\n", boundary));

            HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(mod.apiURL + "/v1/replays");
            wr.Method = "POST";
            wr.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);

            // Start the boundary off
            using( Stream stream = wr.GetRequestStream() ) {
                stream.Write(boundaryBytes, 0, boundaryBytes.Length);

                // File info
                String field = String.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", "replay", Path.GetFileName(path), "text/plain");
                byte[] bytes = Encoding.UTF8.GetBytes(field);

                stream.Write(bytes, 0, bytes.Length);

                // Write the file
                bytes = File.ReadAllBytes(path);
                stream.Write(bytes, 0, bytes.Length);

                bytes = Encoding.ASCII.GetBytes(String.Format("\r\n--{0}--\r\n", boundary));
                stream.Write(bytes, 0, bytes.Length);
            }

            try {
                using( WebResponse wres = wr.GetResponse() ) {
                    using( StreamReader rs = new StreamReader(wres.GetResponseStream()) ) {
                        String contents = rs.ReadToEnd();
                        Dictionary<String, object> response = new JsonReader().Read<Dictionary<String, object>>(contents);

                        if( response.ContainsKey("url") ) {
                            mod.SendMessage(String.Format("Finished uploading replay to ScrollsPost. Can be found at {0}, or by typing /sp and going to Replay List.", (response["url"] as String).Replace("scrollspost/", "scrollspost.com/")));
                            LogUploaded(path);
                        } else if( response["error"].Equals("game_too_short") ) {
                            mod.SendMessage("Replay rejected as it was too short, must go beyond 1 round to be uploaded.");
                        } else {
                            mod.SendMessage(String.Format("Error while uploading replay ({0}), please contact us for more info at [email protected]", response["error"]));
                            LogNotUploaded(path);
                        }

                        return response;
                    }
                }

            } catch ( WebException we ) {
                LogNotUploaded(path);

                Console.WriteLine("**** ERROR {0}", we.ToString());
                mod.SendMessage(String.Format("We had an HTTP error while uploading replay {0}, contact us at [email protected] for help.", Path.GetFileName(path)));
                mod.WriteLog("Failed to sync collection", we);

                Dictionary<String, object> response = new Dictionary<String, object>();
                response["error"] = we.ToString();

                return response;
            }
        }