From a721e37ed3404ca9bfaaf75b61e659bd9a6883a1 Mon Sep 17 00:00:00 2001 From: RyoRyo Date: Thu, 16 Nov 2023 16:16:53 +0900 Subject: [PATCH] =?UTF-8?q?#14=20=E3=81=A8=E3=81=8B=E3=80=81=E3=81=84?= =?UTF-8?q?=E3=82=8D=E3=81=84=E3=82=8D=E3=81=AE=E3=83=90=E3=82=B0=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit やったね! --- .editorconfig | 3 + Assets/Scenes/CheckScores.unity | 12 +- Assets/Scripts/CheckScores/ScoreManager.cs | 127 ++++++++++++++++----- 3 files changed, 107 insertions(+), 35 deletions(-) diff --git a/.editorconfig b/.editorconfig index cc5c043..dd6a22b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -134,3 +134,6 @@ dotnet_style_qualification_for_field = false:suggestion dotnet_style_qualification_for_method = false:suggestion #prefer properties not to be prefaced with this. or Me. in Visual Basic dotnet_style_qualification_for_property = false:suggestion + +[*.{cs,vb}] +indent_style=tab \ No newline at end of file diff --git a/Assets/Scenes/CheckScores.unity b/Assets/Scenes/CheckScores.unity index 488a3f6..ebfbda7 100644 --- a/Assets/Scenes/CheckScores.unity +++ b/Assets/Scenes/CheckScores.unity @@ -193,15 +193,9 @@ MonoBehaviour: m_HorizontalOverflow: 0 m_VerticalOverflow: 0 m_LineSpacing: 1 - m_Text: '1.2023/11/01 99999999999999999pts. - - 2.2023/12/08 30pts. - - 3. - - 4. - - 5.' + m_Text: "1.\u30C7\u30FC\u30BF\u306A\u3057 0\u79D2\r\n2.\u30C7\u30FC\u30BF\u306A\u3057 + 0\u79D2\r\n3.\u30C7\u30FC\u30BF\u306A\u3057 0\u79D2\r\n4.\u30C7\u30FC\u30BF\u306A\u3057 + 0\u79D2\r\n5.\u30C7\u30FC\u30BF\u306A\u3057 0\u79D2\r\n" --- !u!222 &200360277 CanvasRenderer: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CheckScores/ScoreManager.cs b/Assets/Scripts/CheckScores/ScoreManager.cs index 0b7ad19..f000254 100644 --- a/Assets/Scripts/CheckScores/ScoreManager.cs +++ b/Assets/Scripts/CheckScores/ScoreManager.cs @@ -1,16 +1,15 @@ -using System.Collections; +using System; using System.Collections.Generic; using System.IO; +using System.Text; +using Newtonsoft.Json; using UnityEngine; using UnityEngine.UI; -using Newtonsoft.Json; -using System.Text; -using System; public class ScoreManager : MonoBehaviour { public TextAsset FirstJson; - private string filePath; + [SerializeField] public Text ScoreText; @@ -19,47 +18,123 @@ public class ScoreManager : MonoBehaviour void Start() { //Z[uf[^邩mF - filePath = Path.Combine(Application.persistentDataPath, "Scores.json"); - if(!File.Exists(filePath)) + + if (!File.Exists(FilePath)) { - StreamWriter sw = new StreamWriter(filePath,false, Encoding.UTF8); + StreamWriter sw = new StreamWriter(FilePath, false, Encoding.UTF8); sw.Write(FirstJson.text); sw.Flush(); sw.Close(); } - - //f[^ǂݍ - StreamReader sr = new StreamReader(filePath, Encoding.UTF8); - - string jsonStr = sr.ReadToEnd(); - - sr.Close(); - Scores = JsonConvert.DeserializeObject(jsonStr); + //f[^ǂݍ + Scores = ReadJson(); string scoreText = ""; //ʂɕ\ - foreach(Score s in Scores.scores) + foreach (Score s in Scores.scores) { - scoreText += $"{s.rank}.{s.date} {s.score/10.0f}b{Environment.NewLine}"; + scoreText += $"{s.rank}.{s.date} {s.score / 10.0f}b{Environment.NewLine}"; } ScoreText.text = scoreText; + + + Debug.Log(FilePath); + } + + public static void UpdateHighScore(int score) + { + //ǂݍ + ScoresJson scores = ReadJson(); + //XV + scores = UpdateData(scores, score); + //JSON֕ϊ + string json = JsonConvert.SerializeObject(scores, Formatting.Indented); + // + StreamWriter sw = new StreamWriter(FilePath,false, Encoding.UTF8); + sw.Write(json); + sw.Flush(); + sw.Close(); + } + + private static ScoresJson UpdateData(ScoresJson data, int input) + { + int where = WhereInsert(input, data); + if (where == -1) + { + return data; + } + data = ArrayShift(where, data); + data.scores[where].score = input; + data.scores[where].date = DateTime.Now.ToString("d"); + return data; + } + private static int WhereInsert(int input, ScoresJson data) + { + //1‚Aǂɓ邩 + for (int i = 0; i < data.scores.Count; i++) + { + if (data.scores[i].score < input) + { + return i; + } + if (data.scores[i].score == input) + { + return -1; + } + } + return -1; + } + private static ScoresJson ArrayShift(int from, ScoresJson data) + { + for (int i = data.scores.Count - 1; i > from; i--) + { + //data.scores[i] = data.scores[i-1]; + data.scores[i].date = data.scores[i - 1].date; + data.scores[i].score = data.scores[i - 1].score; + } + + return data; + } + + public static ScoresJson ReadJson() + { + StreamReader sr = new StreamReader(FilePath, Encoding.UTF8); + + string jsonStr = sr.ReadToEnd(); + + sr.Close(); + return JsonConvert.DeserializeObject(jsonStr); } - // Update is called once per frame - void Update() + public static string FilePath { - + get + { + return Path.Combine(Application.persistentDataPath, "Scores.json"); + } } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:X^C", Justification = "<ۗ>")] public class ScoresJson { - - public IList scores { get; set; } + + public IList scores + { + get; set; + } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:X^C", Justification = "<ۗ>")] public class Score { - public int rank { get; set; } - public string date { get; set; } - public long score { get; set; }//10Ŋ邱 + public int rank + { + get; set; + } + public string date + { + get; set; + } + public long score + { + get; set; + }//10Ŋ邱 } \ No newline at end of file