This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordRPCManager.cs
108 lines (84 loc) · 3.43 KB
/
DiscordRPCManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Diagnostics.CodeAnalysis;
using Avalonia.Threading;
using Discord;
namespace Minesweeper;
public class DiscordRPCManager : IDisposable {
public const long ClientId = 1114858545142321245;
Discord.Discord Discord { get; } = new(ClientId, (ulong)CreateFlags.Default);
MainWindow Window { get; }
public DiscordRPCManager(MainWindow window) {
Window = window;
DispatcherTimer.Run(Callback, TimeSpan.FromMilliseconds(100));
}
public void SetActivity(GameMode gameMode, string? state = null, DateTimeOffset? startTime = null, DateTimeOffset? stopTime = null) {
string details = $"{gameMode}";
Assets? smallImage = gameMode switch {
GameMode.Beginner => Assets.one_star,
GameMode.Amateur => Assets.two_stars,
GameMode.Professional => Assets.three_stars,
GameMode.Custom => Assets.custom,
_ => null
};
SetActivity(state, details, startTime, stopTime, smallImage);
}
public void SetActivity(string? state = null, string? details = null, DateTimeOffset? startTime = null, DateTimeOffset? stopTime = null, Assets? smallImage = null) {
ActivityAssets assets = new() { LargeImage = $"{Assets.game}", LargeText = "Minesweeper by C6OI#6060" };
if (smallImage.HasValue) {
assets.SmallImage = $"{smallImage.Value}";
switch (smallImage.Value) {
case Assets.waiting:
assets.SmallText = "Getting ready";
break;
case Assets.lose_game:
assets.SmallText = $"Lost the game in {(stopTime - startTime)?.ToString(@"mm\:ss")}";
break;
case Assets.win_game:
assets.SmallText = $"Win the game in {(stopTime - startTime)?.ToString(@"mm\:ss")}";
break;
case Assets.one_star:
assets.SmallText = "Beginner mode";
break;
case Assets.two_stars:
assets.SmallText = "Amateur mode";
break;
case Assets.three_stars:
assets.SmallText = "Professional mode";
break;
case Assets.custom:
assets.SmallText = "Custom mode";
break;
case Assets.game:
default:
throw new ArgumentOutOfRangeException();
}
}
int mines = Window.MinesCount;
int width = Window.FieldWidth;
int height = Window.FieldHeight;
Activity activity = new() {
Assets = assets,
State = state ?? "",
Details = $"{details} ({width}x{height}, {mines} mines)"
};
if (startTime.HasValue && !stopTime.HasValue) activity.Timestamps = new ActivityTimestamps { Start = startTime.Value.ToUnixTimeSeconds() };
Discord.GetActivityManager().UpdateActivity(activity, _ => { });
}
public void Dispose() => Discord.Dispose();
bool Callback() {
Discord.RunCallbacks();
return true;
}
}
[SuppressMessage("ReSharper", "InconsistentNaming"),
SuppressMessage("ReSharper", "IdentifierTypo")]
public enum Assets {
game,
waiting,
lose_game,
win_game,
one_star,
two_stars,
three_stars,
custom
}