From 53631ad42df86be557746064a1e3161d32e3228d Mon Sep 17 00:00:00 2001
From: Michael <Michael@some.where>
Date: Wed, 3 May 2017 09:44:29 -0400
Subject: [PATCH] Fix highscore list

---
 .../Applications/Pong.Designer.cs             |  7 +--
 ShiftOS.WinForms/Applications/Pong.cs         | 53 +++++++++++++------
 ShiftOS_TheReturn/UniteClient.cs              | 24 +++++++++
 3 files changed, 63 insertions(+), 21 deletions(-)

diff --git a/ShiftOS.WinForms/Applications/Pong.Designer.cs b/ShiftOS.WinForms/Applications/Pong.Designer.cs
index f28868a..aef8911 100644
--- a/ShiftOS.WinForms/Applications/Pong.Designer.cs
+++ b/ShiftOS.WinForms/Applications/Pong.Designer.cs
@@ -80,7 +80,7 @@ namespace ShiftOS.WinForms.Applications
             this.tmrstoryline = new System.Windows.Forms.Timer(this.components);
             this.pgcontents = new ShiftOS.WinForms.Controls.Canvas();
             this.pnlhighscore = new System.Windows.Forms.Panel();
-            this.lbhighscore = new System.Windows.Forms.ListBox();
+            this.lbhighscore = new System.Windows.Forms.ListView();
             this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
             this.button2 = new System.Windows.Forms.Button();
             this.label10 = new System.Windows.Forms.Label();
@@ -193,11 +193,8 @@ namespace ShiftOS.WinForms.Applications
             // lbhighscore
             // 
             this.lbhighscore.Dock = System.Windows.Forms.DockStyle.Fill;
-            this.lbhighscore.FormattingEnabled = true;
             this.lbhighscore.Location = new System.Drawing.Point(0, 36);
-            this.lbhighscore.MultiColumn = true;
             this.lbhighscore.Name = "lbhighscore";
-            this.lbhighscore.SelectionMode = System.Windows.Forms.SelectionMode.None;
             this.lbhighscore.Size = new System.Drawing.Size(539, 246);
             this.lbhighscore.TabIndex = 1;
             // 
@@ -745,7 +742,7 @@ namespace ShiftOS.WinForms.Applications
         internal System.Windows.Forms.Label Label8;
         internal System.Windows.Forms.Timer tmrstoryline;
         private System.Windows.Forms.Panel pnlhighscore;
-        private System.Windows.Forms.ListBox lbhighscore;
+        private System.Windows.Forms.ListView lbhighscore;
         private System.Windows.Forms.Label label10;
         internal Canvas pgcontents;
         internal Canvas ball;
diff --git a/ShiftOS.WinForms/Applications/Pong.cs b/ShiftOS.WinForms/Applications/Pong.cs
index 6198cad..e0fe793 100644
--- a/ShiftOS.WinForms/Applications/Pong.cs
+++ b/ShiftOS.WinForms/Applications/Pong.cs
@@ -29,6 +29,7 @@ using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using Newtonsoft.Json;
@@ -310,15 +311,9 @@ namespace ShiftOS.WinForms.Applications
             SetupStats();
         }
 
+        [Obsolete("This method does nothing. Use UniteClient for highscore queries.")]
         public void SendHighscores()
         {
-            var highscore = new PongHighscore
-            {
-                UserName = $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}",
-                HighestLevel = level,
-                HighestCodepoints = totalreward
-            };
-            ServerManager.SendMessage("pong_sethighscore", JsonConvert.SerializeObject(highscore));
         }
 
         // ERROR: Handles clauses are not supported in C#
@@ -614,24 +609,50 @@ namespace ShiftOS.WinForms.Applications
         public void SetupHighScores()
         {
             lbhighscore.Items.Clear();
-            ServerManager.MessageReceived += (msg) =>
+            lbhighscore.View = View.Details;
+            lbhighscore.FullRowSelect = true;
+            lbhighscore.Columns.Clear();
+            var n = new ColumnHeader();
+            n.Text = "Player";
+            n.Width = lbhighscore.Width / 3;
+            var l = new ColumnHeader();
+            l.Text = "Level";
+            l.Width = n.Width;
+            var c = new ColumnHeader();
+            c.Text = "Codepoints";
+            c.Width = n.Width;
+            lbhighscore.Columns.Add(n);
+            lbhighscore.Columns.Add(l);
+            lbhighscore.Columns.Add(c);
+
+            var t = new Thread(() =>
             {
-                if(msg.Name == "pong_highscores")
+                try
                 {
-                    var hs = JsonConvert.DeserializeObject<List<PongHighscore>>(msg.Contents);
 
-                    var orderedhs = hs.OrderByDescending(i => i.HighestLevel);
-
-                    foreach(var score in orderedhs)
+                    var unite = new ShiftOS.Unite.UniteClient("http://getshiftos.ml", SaveSystem.CurrentSave.UniteAuthToken);
+                    var hs = unite.GetPongHighscores();
+                    foreach (var score in hs.Highscores)
                     {
+                        string username = unite.GetDisplayNameId(score.UserId);
                         this.Invoke(new Action(() =>
                         {
-                            lbhighscore.Items.Add($"{score.UserName}\t\t\t{score.HighestLevel}\t\t{score.HighestCodepoints} CP");
+                            var name_item = new ListViewItem();
+                            name_item.Text = username;
+                            lbhighscore.Items.Add(name_item);
+                            name_item.SubItems.Add(score.Level.ToString());
+                            name_item.SubItems.Add(score.CodepointsCashout.ToString());
                         }));
                     }
                 }
-            };
-            ServerManager.SendMessage("pong_gethighscores", null);
+                catch
+                {
+                    Infobox.Show("Service unavailable.", "The Pong Highscore service is unavailable at this time.");
+                    this.Invoke(new Action(pnlgamestats.BringToFront));
+                    return;
+                }
+            });
+            t.Start();
             pnlhighscore.Show();
         }
 
diff --git a/ShiftOS_TheReturn/UniteClient.cs b/ShiftOS_TheReturn/UniteClient.cs
index cb08382..1136b5c 100644
--- a/ShiftOS_TheReturn/UniteClient.cs
+++ b/ShiftOS_TheReturn/UniteClient.cs
@@ -4,6 +4,7 @@ using System.Linq;
 using System.Net;
 using System.Text;
 using System.Threading.Tasks;
+using Newtonsoft.Json;
 
 namespace ShiftOS.Unite
 {
@@ -12,6 +13,16 @@ namespace ShiftOS.Unite
         public string Token { get; private set; }
         public string BaseURL { get; private set; }
 
+        public string GetDisplayNameId(string id)
+        {
+            return MakeCall("/API/GetDisplayName/" + id);
+        }
+
+        public PongHighscoreModel GetPongHighscores()
+        {
+            return JsonConvert.DeserializeObject<PongHighscoreModel>(MakeCall("/API/GetPongHighscores"));
+        }
+
         public UniteClient(string baseurl, string usertoken)
         {
             BaseURL = baseurl;
@@ -100,4 +111,17 @@ namespace ShiftOS.Unite
             MakeCall("/API/SetCodepoints/" + value.ToString());
         }
     }
+
+    public class PongHighscoreModel
+    {
+        public int Pages { get; set; }
+        public PongHighscore[] Highscores { get; set; }
+    }
+
+    public class PongHighscore
+    {
+        public string UserId { get; set; }
+        public int Level { get; set; }
+        public long CodepointsCashout { get; set; }
+    }
 }