void OnGUI() { // button get highscore if (GUI.Button (new Rect (10, 10, 200, 50), "Receive Highscore")) { StartCoroutine(highscore.GetEntries()); } // button insert highscore if (GUI.Button (new Rect (420, 10, 200, 50), "Publish Score")) { // store into highscore entry model object HighscoreEntryModel model = new HighscoreEntryModel(); model.LastName = lastNameToInsert; model.FirstName = firstNameToInsert; model.DisplayName = displayNameToInsert; model.Email = emailToInsert; model.Score = long.Parse(scoreToInsert); StartCoroutine(highscore.PublishEntry(model)); } // get receive message to display string message = string.Empty; if (highscore.ReceiveState == HighscoreTransmissionState.Success) { message = highscore.ToString (highscore.LastReceivedHighscoreEntries); } else message = highscore.ReceiveState.ToString(); // show scroll highscore scroll view scrollViewVector = GUI.BeginScrollView (new Rect (10, 70, 400, 400), scrollViewVector, new Rect (0, 0, 1000, 100)); GUI.TextArea (new Rect (0, 0, 400, 400), message); GUI.EndScrollView (); // show input fields GUI.Label (new Rect (420, 70, 100, 30), "First Name"); firstNameToInsert = GUI.TextField (new Rect (520, 70, 200, 30), firstNameToInsert); GUI.Label (new Rect (420, 120, 100, 30), "Last Name"); lastNameToInsert = GUI.TextField (new Rect (520, 120, 200, 30), lastNameToInsert); GUI.Label (new Rect (420, 170, 100, 30), "Display Name"); displayNameToInsert = GUI.TextField (new Rect (520, 170, 200, 30), displayNameToInsert); GUI.Label (new Rect (420, 220, 100, 30), "email"); emailToInsert = GUI.TextField (new Rect (520, 220, 200, 30), emailToInsert); GUI.Label (new Rect (420, 270, 100, 30), "score"); scoreToInsert = GUI.TextField (new Rect (520, 270, 200, 30), scoreToInsert); GUI.Label (new Rect(420, 320, 200, 30), "State: " + highscore.SendState.ToString()); GUI.Label (new Rect(420, 370, 400, 120), "Last Message: " + highscore.LastMessage); }
public IEnumerator PublishEntry(HighscoreEntryModel model) { var message = Serialize (model); // encrypt message var encryptionResult = EncryptionHelper.EncryptMessage (message, highscorePasswordBytes); var encryptedMessage = encryptionResult.EncryptedMessage; var rijndaelIv = encryptionResult.RijndaelIv; // append encrypted message and iv var bytesToSend = new List<byte> (); bytesToSend.AddRange (encryptedMessage); bytesToSend.AddRange (rijndaelIv); // send var bytesToSendBase64 = System.Convert.ToBase64String (bytesToSend.ToArray ()); WWWForm form = new WWWForm(); form.AddField("content", bytesToSendBase64); WWW www = new WWW(highscoreInsertUrl, form); SendState = HighscoreTransmissionState.Wait; yield return www; // process result if (string.IsNullOrEmpty (www.error)) { string serverResponseString = www.text; var serverResponse = TryDeserializeServerResponse(serverResponseString); if (serverResponse.State == ServerResponseState.Success) SendState = HighscoreTransmissionState.Success; else if (serverResponse.State == ServerResponseState.Warning) SendState = HighscoreTransmissionState.Warning; else SendState = HighscoreTransmissionState.Error; LastMessage = serverResponse.Message; } else { LastPublishHttpError = "error: " + www.error; SendState = HighscoreTransmissionState.Error; } }
// serialize a highscore entry model to json private string Serialize(HighscoreEntryModel model) { string json = string.Format("{{\"LastName\": \"{0}\", \"FirstName\": \"{1}\" , \"DisplayName\": \"{2}\", \"Email\": \"{3}\", \"Score\": {4}}}", model.LastName, model.FirstName, model.DisplayName, model.Email, model.Score.ToString()); return json; }
// deserialize received json string to highscore entry model (without email for security) private void DeserializeModels(string json) { LastReceivedHighscoreEntries.Clear (); List<object> objectsRaw = (List<object>)Json.Deserialize (json); if (objectsRaw != null) { foreach (Dictionary<string, object> objectRaw in objectsRaw) { // parse string lastName = (string) objectRaw ["LastName"]; string firstName = (string) objectRaw ["FirstName"]; string displayName = (string) objectRaw ["DisplayName"]; long score = -1; long.TryParse((string)objectRaw["Score"], out score); // fill HighscoreEntryModel highscoreEntry = new HighscoreEntryModel { LastName = lastName, FirstName = firstName, DisplayName = displayName, Score = score }; LastReceivedHighscoreEntries.Add (highscoreEntry); } } }