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

Commit

Permalink
自动更新和获取积分的更改
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyl18 committed May 9, 2018
1 parent de60f9b commit 3378e7d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
1 change: 1 addition & 0 deletions CardSharp/CardSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="GameComponents\Player.cs" />
<Compile Include="MessageSenderBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReleaseGetter.cs" />
<Compile Include="Rules\Chain.cs" />
<Compile Include="Rules\IRule.cs" />
<Compile Include="Rules\Rule3.cs" />
Expand Down
46 changes: 44 additions & 2 deletions CardSharp/GameSteps/StandardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using CardSharp.GameComponents;
using Humanizer;
Expand Down Expand Up @@ -44,8 +45,15 @@ public void Parse(Desk desk, Player player, string command)
var px = DateTime.Now - pconfig.LastTime;
if (px.TotalSeconds.Seconds() > 12.Hours())
{
pconfig.AddPoint();
desk.AddMessage($"领取成功. 你当前积分为{pconfig.Point}");
if (pconfig.Point > 50000)
{
desk.AddMessage($"你超过了领取积分的上限");
}
else
{
pconfig.AddPoint();
desk.AddMessage($"领取成功. 你当前积分为{pconfig.Point}");
}
}
else
{
Expand Down Expand Up @@ -130,6 +138,10 @@ 带有[R]的命令 是正式功能,'一般'不会做更改
case "最近更新":
Commits(desk);
break;

case "最后更新":
LatestUpdate(desk);
break;
}

if (pconfig.IsAdmin)
Expand All @@ -148,6 +160,10 @@ 带有[R]的命令 是正式功能,'一般'不会做更改
player.ForceSend = true;
player.AddMessage(string.Join(Environment.NewLine, desk.Players.Select(p => $"{p.PlayerId} {p.Cards.ToFormatString()}")));
break;

case "更新CardSharp":
Update(desk);
break;
}

if (command.StartsWith("设置积分"))
Expand All @@ -173,6 +189,32 @@ 带有[R]的命令 是正式功能,'一般'不会做更改
}
}

private void LatestUpdate(Desk desk)
{
var data = ReleaseGetter.Get();
desk.AddMessage($"更新时间为: {data.assets.First().updated_at}");
}

private void Update(Desk desk)
{
const string path = "Origind.Card.Game\\CardSharp.dll";
const string filename = "CardSharp.dll";
try
{
File.Delete(path);
}
catch (Exception e)
{
desk.AddMessage($"删除文件时发生错误 {e}");
return;
}
var data = ReleaseGetter.Get();
var wc = new WebClient();
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
wc.DownloadFile(data.assets.First(asset => asset.name == filename).browser_download_url, path);
desk.AddMessage($"文件下载完成.");
}

private static void Commits(Desk desk)
{
var updates = CommitsGetter.Get()
Expand Down
100 changes: 100 additions & 0 deletions CardSharp/ReleaseGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CardSharp
{
public static class ReleaseGetter
{
public static ReleaseData Get()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var wc = new WebClient { Encoding = Encoding.UTF8 };
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
return wc.DownloadString("https://api.github.com/repos/Cyl18/CardSharp/releases/latest").JsonDeserialize<ReleaseData>();
}
}

public class ReleaseData
{
public string url { get; set; }
public string assets_url { get; set; }
public string upload_url { get; set; }
public string html_url { get; set; }
public int id { get; set; }
public string tag_name { get; set; }
public string target_commitish { get; set; }
public object name { get; set; }
public bool draft { get; set; }
public Author2 author { get; set; }
public bool prerelease { get; set; }
public DateTime created_at { get; set; }
public DateTime published_at { get; set; }
public Asset[] assets { get; set; }
public string tarball_url { get; set; }
public string zipball_url { get; set; }
public object body { get; set; }
}

public class Author2
{
public string login { get; set; }
public int id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}

public class Asset
{
public string url { get; set; }
public int id { get; set; }
public string name { get; set; }
public string label { get; set; }
public Uploader uploader { get; set; }
public string content_type { get; set; }
public string state { get; set; }
public int size { get; set; }
public int download_count { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public string browser_download_url { get; set; }
}

public class Uploader
{
public string login { get; set; }
public int id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
}

0 comments on commit 3378e7d

Please sign in to comment.