public void SaveScore() { HighscoreData data = LoadHighScores(HighscoresFilename); int scoreIndex = -1; for (int i = 0; data.count1 - 1 < -1; i--) { if (Manager.playerScore >= data.score[i]) { scoreIndex = i; } } if (scoreIndex > -1) { for (int i = data.count1 - 1; i > scoreIndex; i--) { data.score[i] = data.score[i - 1]; } data.score[scoreIndex] = Manager.playerScore; SaveHighScores(data, HighscoresFilename, device); } }
IEnumerator SetText(string uriSend) { UnityWebRequest www = UnityWebRequest.Get(uriSend); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.LogFormat("isNet: {0} ; isHttp: {1}; uri: {2}", www.isNetworkError, www.isHttpError, uri); Debug.LogError(www.error); } else { // successful Debug.Log(www.downloadHandler.text); // parse json text HighscoreData data = JsonUtility.FromJson <HighscoreData>(www.downloadHandler.text); Debug.Log(data + ": Data!"); //Debug.Log(data.entries); //for (int i = 0; i < data.entries.Count; i++) //{ // Debug.Log(data.entries[i].user + ": " + data.entries[i].score); //} if (OnHighscoreDataReceived != null) { OnHighscoreDataReceived(data); } } }
public static void AddHighscoreToList(HighscoreData highscore) { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream; HighscoresList highscoresList; if (File.Exists(Path)) { stream = new FileStream(Path, FileMode.Open); highscoresList = formatter.Deserialize(stream) as HighscoresList; if (highscoresList == null) { Debug.LogError("Highscore System: Couldn't deserialize highscores list from save file."); } stream.Close(); } else { highscoresList = new HighscoresList(); } stream = new FileStream(Path, FileMode.Create); highscoresList.AddHighscore(highscore); formatter.Serialize(stream, highscoresList); stream.Close(); }
//Called by submit button public void changePanels() { HighscoreData newHighScore = new HighscoreData() { name = nameInputField.text, score = currentScore }; highscoresList.Add(newHighScore); trimHighscoresList(); updateHighscoresUI(); //Change color of my score to red for (int i = 0; i < maxHighscorers; i++) { if (nameHighScore[i].text == nameInputField.text) { nameHighScore[i].color = new Color(1.0f, 0.0f, 0.0f); scoreHighScore[i].color = new Color(1.0f, 0.0f, 0.0f); } } newHighscorePanel.GetComponent <Animator>().SetBool("move", true); topHighscorePanel.GetComponent <Animator>().SetBool("move", true); GameObject.Find("AirHorn").GetComponent <AudioSource>().Play(); }
public void UpdateUI() { int levelIndex = SceneManager.GetActiveScene().buildIndex; foreach (HighscoreData _levelData in data.highscoreDatas) { if (_levelData.levelIndex == levelIndex) { levelData = _levelData; break; } } if (!levelData) { return; } music.Play(); string stringTimer = FormatedTime(levelData.timeInSeconds); UIController.BestTimerEvent?.Invoke(stringTimer); UIController.DeathTextEvent?.Invoke(levelData.totalDeaths.ToString()); }
void SaveHighscore() { if (_highscoreResource.AddHighscore(HighscoreData.Create(_scoreReactiveProperty.Value, 10))) { // notify to highscore observers when the new score is added _highscoreDataStream.OnNext(GetHighscoresFromTheResource()); } }
public void ShouldSortHighscoresByScore() { var highscores = new Highscores(); var newScoreOne = HighscoreData.Create(5000, 20); Assert.IsTrue(highscores.AddHighscore(newScoreOne)); Assert.AreEqual(newScoreOne, highscores[0]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); var newScoreTwo = HighscoreData.Create(2500, 10); Assert.IsTrue(highscores.AddHighscore(newScoreTwo)); Assert.AreEqual(newScoreOne, highscores[0]); Assert.AreEqual(newScoreTwo, highscores[1]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); var newScoreThree = HighscoreData.Create(10000, 10); Assert.IsTrue(highscores.AddHighscore(newScoreThree)); Assert.AreEqual(newScoreThree, highscores[0]); Assert.AreEqual(newScoreOne, highscores[1]); Assert.AreEqual(newScoreTwo, highscores[2]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); var newScoreFour = HighscoreData.Create(7500, 30); Assert.IsTrue(highscores.AddHighscore(newScoreFour)); Assert.AreEqual(newScoreThree, highscores[0]); Assert.AreEqual(newScoreFour, highscores[1]); Assert.AreEqual(newScoreOne, highscores[2]); Assert.AreEqual(newScoreTwo, highscores[3]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); var newScoreFive = HighscoreData.Create(2000, 30); Assert.IsTrue(highscores.AddHighscore(newScoreFive)); Assert.AreEqual(newScoreThree, highscores[0]); Assert.AreEqual(newScoreFour, highscores[1]); Assert.AreEqual(newScoreOne, highscores[2]); Assert.AreEqual(newScoreTwo, highscores[3]); Assert.AreEqual(newScoreFive, highscores[4]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); var newScoreSix = HighscoreData.Create(1000, 30); // return false when the score wasnt be added Assert.IsFalse(highscores.AddHighscore(newScoreFive)); Assert.AreEqual(newScoreThree, highscores[0]); Assert.AreEqual(newScoreFour, highscores[1]); Assert.AreEqual(newScoreOne, highscores[2]); Assert.AreEqual(newScoreTwo, highscores[3]); Assert.AreEqual(newScoreFive, highscores[4]); Assert.AreEqual(highscores.Count, Highscores.maxHighscoreTables); }
private IEnumerator SendAndGetScores() { yield return(GameStateManager.SendHighscore()); WWW req = new WWW("https://hitchh1k3rsguide.com/api/ld40_highscore.php"); yield return(req); highscoreData = JsonUtility.FromJson <HighscoreData>(req.text); }
public static void SaveHighscore(int highscore) { BinaryFormatter formatter = new BinaryFormatter(); string path = Path.Combine(Application.persistentDataPath, fileName); FileStream stream = new FileStream(path, FileMode.Create); HighscoreData data = new HighscoreData(highscore); formatter.Serialize(stream, data); stream.Close(); }
// Obtains the ONLY Reference to the Object public static HighscoreData getInstance(int score = 0) { // The only ONE! if (theOne != null) { return(theOne); } // Create a new Instance and return it theOne = new HighscoreData(score); return(theOne); }
public void Load() { if (File.Exists(Application.persistentDataPath + "/highscore.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.Open(Application.persistentDataPath + "/highscore.dat", FileMode.Open); HighscoreData data = (HighscoreData)bf.Deserialize(fs); fs.Close(); highscores = data.highscores; } }
public static void SaveHighScore() { // Construct Streams & Formatter to Start Saving BinaryFormatter formatter = new BinaryFormatter(); FileStream outStream = new FileStream(highScorePath, FileMode.Create); // Get the ONLY Data & Save it HighscoreData data = HighscoreData.getInstance(); formatter.Serialize(outStream, data); outStream.Close(); }
public void Save() { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.Create(Application.persistentDataPath + "/highscore.dat"); HighscoreData data = new HighscoreData(); data.highscores = highscores; bf.Serialize(fs, data); fs.Close(); }
public void UpdateValues() { highscoresNumber.text = HighscoreData.ToString(); levelCount.text = LevelCountData.ToString(); foreach (GameObject item in stars) { item.SetActive(false); } for (int i = 0; i < StarsData; i++) { stars[i].SetActive(true); } }
public void ShouldCreateHighscoreFromItsFactory() { var highscoreData = HighscoreData.Create(); // Should be 0 by default Assert.AreEqual(0, highscoreData.score); Assert.AreEqual(0, highscoreData.combo); highscoreData = HighscoreData.Create(5000, 99); Assert.AreEqual(5000, highscoreData.score); Assert.AreEqual(99, highscoreData.combo); }
// Start is called before the first frame update void Awake() { if (Instance != null) { Destroy(this); return; } Instance = this; DontDestroyOnLoad(this); data = new HighscoreData(10); LoadHighscores(); Debug.Log(data.ToString()); }
public override void OnApplyTemplate() { // Set title switch (Layout) { case Board.Layouts.FourByFour: title.Content = "4 x 4"; break; case Board.Layouts.FiveByFive: title.Content = "5 x 5"; break; case Board.Layouts.SixByFour: title.Content = "6 x 4"; break; case Board.Layouts.SixBySix: title.Content = "6 x 6"; break; } // Get all scores... List<HighscoreData.Data> highscoreDatas = HighscoreData.Load() .Where(score => score.layout == Layout).ToList() // ...Of this layout type... .OrderByDescending(score => score.score).ToList(); // ...And order it from highest to lowest // Set all label's content to the data or hide it for (int i = 0; i < labels.Count; i++) { Label label = labels[i]; if (i < highscoreDatas.Count) { HighscoreData.Data highscoreData = highscoreDatas[i]; label.Content = string.Format(labelFormat, highscoreData.name, highscoreData.score); label.Visibility = Visibility.Visible; } else label.Visibility = Visibility.Hidden; } // If there's no scores for this layout. Show a message instead if (highscoreDatas.Count == 0) { labels[0].Visibility = Visibility.Visible; labels[0].Content = noScoresMessage; } base.OnApplyTemplate(); }
public bool IsHighscore(HighscoreData newScore) { if (highscores.Count < listLength) { return(true); } for (int i = 0; i < highscores.Count; i++) { if (highscores[i] == null || highscores[i].score < newScore.score) { return(true); } } return(false); }
//Returns saved game info //Initialises it to default values if no info found //Should only be called after trying to load saved game public HighscoreData getInfo() { //If no info has been loaded if (data == null) { //If saved file exists then load save file if (File.Exists(Application.persistentDataPath + "/save.xml")) { XmlSerializer serializer = new XmlSerializer(typeof(HighscoreData)); FileStream file = File.Open(Application.persistentDataPath + "/save.xml", FileMode.Open); data = (HighscoreData)serializer.Deserialize(file); file.Dispose(); }//Otherwise create new highscore file with empty scores else { data = new HighscoreData(); int[] tempIntArray = new int[5]; string[] tempStringArray = new string[5]; initialiseArrays(tempIntArray, tempStringArray); data.level1HighScores = tempIntArray; data.level1Names = tempStringArray; tempIntArray = new int[5]; tempStringArray = new string[5]; initialiseArrays(tempIntArray, tempStringArray); data.level2HighScores = tempIntArray; data.level2Names = tempStringArray; tempIntArray = new int[5]; tempStringArray = new string[5]; initialiseArrays(tempIntArray, tempStringArray); data.level3HighScores = tempIntArray; data.level3Names = tempStringArray; tempIntArray = new int[5]; tempStringArray = new string[5]; initialiseArrays(tempIntArray, tempStringArray); data.level4HighScores = tempIntArray; data.level4Names = tempStringArray; tempIntArray = new int[5]; tempStringArray = new string[5]; initialiseArrays(tempIntArray, tempStringArray); data.level5HighScores = tempIntArray; data.level5Names = tempStringArray; this.save(); } } return(data); }
public static void SaveHighScores(HighscoreData data, string filename , StorageDevice device) { string fullpath = "highscores.dat"; FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate); try { XmlSerializer serialize = new XmlSerializer(typeof(HighscoreData)); serialize.Serialize(stream, data); } finally { stream.Close(); } }
// Shows Game Over Canvas public void ShowGameOver(int score) { // Load in Highscore Data SaveSystem.LoadHighScore(); // Get HighScore Data & Save HighscoreData hs = HighscoreData.getInstance(); bool newHS = hs.setNewScore(score); SaveSystem.SaveHighScore(); // Set the Score Texts CS_text.text = $"Score: {score}"; HS_text.text = (newHS ? "NEW " : "") + $"Highscore: {hs.score}"; // Start the Animation deathTransition.SetTrigger("Start"); }
public static int LoadHighscore() { string path = Path.Combine(Application.persistentDataPath, fileName); if (File.Exists(path)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Open); HighscoreData data = formatter.Deserialize(stream) as HighscoreData; stream.Close(); return(data.highscore); } else { Debug.LogError("Save file not found in " + path); return(0); } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { IsMouseVisible = true; string fullpath = @"highscores.dat"; if (!File.Exists(fullpath)) { data = new HighscoreData(5); data.score[0] = 500; data.score[1] = 500; data.score[2] = 500; data.score[3] = 500; data.score[4] = 500; SaveHighScores(data, HighscoresFilename, device); } base.Initialize(); }
public void CanCompareHighscores() { var highscoreDataOne = HighscoreData.Create(5000, 99); var highscoreDataTwo = HighscoreData.Create(2500, 20); var highscoreDataThree = HighscoreData.Create(1000, 120); var highscoreDataFour = HighscoreData.Create(5000, 50); // parameter should be minus when left hand side is bigger than right hand side // so the Highscore list will be sorted by decendant Assert.AreEqual(-1, highscoreDataOne.CompareTo(highscoreDataTwo)); Assert.AreEqual(-1, highscoreDataOne.CompareTo(highscoreDataThree)); Assert.AreEqual(1, highscoreDataTwo.CompareTo(highscoreDataOne)); Assert.AreEqual(-1, highscoreDataTwo.CompareTo(highscoreDataThree)); Assert.AreEqual(1, highscoreDataThree.CompareTo(highscoreDataOne)); Assert.AreEqual(1, highscoreDataThree.CompareTo(highscoreDataTwo)); // should be equal when scores are same Assert.AreEqual(0, highscoreDataOne.CompareTo(highscoreDataFour)); }
// Use this for initialization void Start() { data = HighscoreManager.getHighscoreManager().getInfo(); int[] scoreArray; string[] nameArray; scoreArray = data.level1HighScores; nameArray = data.level1Names; PrintLevelScoreInfo(scoreArray, nameArray, level1Highscores); scoreArray = data.level2HighScores; nameArray = data.level2Names; PrintLevelScoreInfo(scoreArray, nameArray, level2Highscores); scoreArray = data.level3HighScores; nameArray = data.level3Names; PrintLevelScoreInfo(scoreArray, nameArray, level3Highscores); scoreArray = data.level4HighScores; nameArray = data.level4Names; PrintLevelScoreInfo(scoreArray, nameArray, level4Highscores); scoreArray = data.level5HighScores; nameArray = data.level5Names; PrintLevelScoreInfo(scoreArray, nameArray, level5Highscores); }
public static HighscoreData LoadHighScores(string filename) { HighscoreData data; string fullpath = "highscores.dat"; FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read); try { XmlSerializer serializer = new XmlSerializer(typeof(HighscoreData)); data = (HighscoreData)serializer.Deserialize(stream); } finally { stream.Close(); } return(data); }
public void AddHighscore(HighscoreData newHighscore) { if (highscores.Count == 0) { highscores.Add(newHighscore); return; } highscores.Sort((HighscoreData x, HighscoreData y) => { if (x.score > y.score) { return(-1); } else { return(1); } }); bool added = false; for (int i = 0; i < highscores.Count; i++) { if (highscores[i].score < newHighscore.score) { highscores.Insert(i, newHighscore); added = true; break; } } if (highscores.Count < listLength && !added) { highscores.Add(newHighscore); } if (highscores.Count > listLength) { highscores.RemoveAt(listLength); } }
// Loads Data into Singleton Class // Returns True/False to indicate Loaded or not public static bool LoadHighScore() { // Verify File Exists if (File.Exists(highScorePath)) { BinaryFormatter formatter = new BinaryFormatter(); FileStream inStream = new FileStream(highScorePath, FileMode.Open); // Load in data HighscoreData data = formatter.Deserialize(inStream) as HighscoreData; // Save the Score HighscoreData.getInstance(data.score); inStream.Close(); Debug.Log("Saved File to " + highScorePath); return(true); } else { Debug.LogError("Save file not found in " + highScorePath); return(false); } }
private void InitialiseHighscores() { highscoreData = Transform.FindObjectOfType<HighscoreData>(); if (highscoreData == null) { GameObject go = Instantiate(Resources.Load("Prefabs/HighscoreData")) as GameObject; highscoreData = go.GetComponent<HighscoreData>(); } LoadData(); }
public void SetHighscoreData(HighscoreData data) { PlayerName = data.PlayerName; Score = data.Score; ID = data.ID; }
//Updates the saved game information public void updateInfo(HighscoreData newInfo) { data = newInfo; save(); }