Fix highscore list

This commit is contained in:
Michael 2017-05-03 09:44:29 -04:00
parent 3488ac86fb
commit 53631ad42d
3 changed files with 63 additions and 21 deletions

View file

@ -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;

View file

@ -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();
}

View file

@ -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; }
}
}