Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Commit

Permalink
add scoreboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyl18 committed May 8, 2018
1 parent 39b8d94 commit a7b8e2a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions CardSharp/GameComponents/PlayerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static PlayerConfig()
if (!Directory.Exists(Constants.ConfigDir)) Directory.CreateDirectory(Constants.ConfigDir);
}

public PlayerConfig(string playerid, long point = default, DateTime lastTime = default, bool isAdmin = default )
public PlayerConfig(string playerid, long point = default, DateTime lastTime = default, bool isAdmin = default)
{
PlayerID = playerid ?? throw new ArgumentNullException(nameof(playerid));
Point = point;
Expand All @@ -23,6 +23,7 @@ static PlayerConfig()
public DateTime LastTime { get; set; }

private bool _isAdmin = false;

public bool IsAdmin
{
get => PlayerID == "775942303" || _isAdmin;
Expand All @@ -33,13 +34,21 @@ public static PlayerConfig GetConfig(Player player)
{
var path = GetConfigPath(player.PlayerId);
return File.Exists(path)
? File.ReadAllText(path).JsonDeserialize<PlayerConfig>()
? FromJson(File.ReadAllText(path))
: new PlayerConfig(player.PlayerId);
}

public void Save()
{
File.WriteAllText(GetConfigPath(PlayerID), this.ToJsonString());
if (uint.TryParse(PlayerID, out var _)) // e.g. 机器人233
{
File.WriteAllText(GetConfigPath(PlayerID), this.ToJsonString());
}
}

public string ToAtCode()
{
return $"[CQ:at,qq={PlayerID}]";
}

public void AddPoint()
Expand All @@ -49,6 +58,8 @@ public void AddPoint()
Save();
}

public static PlayerConfig FromJson(string json) => json.JsonDeserialize<PlayerConfig>();

private static string GetConfigPath(string playerid)
{
return Path.Combine(Constants.ConfigDir, $"{playerid}.json");
Expand Down
18 changes: 18 additions & 0 deletions CardSharp/GameSteps/StandardParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using CardSharp.GameComponents;
using Humanizer;
using Humanizer.Localisation;
Expand Down Expand Up @@ -119,6 +122,21 @@ 带有[R]的命令 是正式功能,'一般'不会做更改
player.AutoPass = false;
desk.AddMessage("Done.");
break;

case "排行榜":
var configs = Directory.GetFiles(Constants.ConfigDir)
.Select(File.ReadAllText)
.Select(PlayerConfig.FromJson)
.OrderByDescending(conf => conf.Point)
.Take(10);
var sb = new StringBuilder();
sb.AppendLine("积分排行榜: ");
foreach (var config in configs)
sb.AppendLine($"{(config.IsAdmin ? "**" : "")}{config.PlayerID}-{config.ToAtCode()}: {config.Point}");

desk.AddMessage(sb.ToString());

break;
}

if (pconfig.IsAdmin)
Expand Down

0 comments on commit a7b8e2a

Please sign in to comment.