-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitchPredictionsBGPlugin.cs
executable file
·137 lines (106 loc) · 4.64 KB
/
TwitchPredictionsBGPlugin.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using AutoUpdaterDotNET;
using Hearthstone_Deck_Tracker.API;
using Hearthstone_Deck_Tracker.Plugins;
using Hearthstone_Deck_Tracker.Utility.Logging;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading;
using System.Windows.Controls;
using TwitchLib.Api.Core.Enums;
namespace TwitchPredictionsBG
{
public class Plugin : IPlugin
{
private Config _config;
private TwitchApiAdapter apiAdapter;
public void OnLoad()
{
AutoUpdate();
TwitchPredictionsBG.isActivated = true;
// Triggered upon startup and when the user ticks the plugin on
CreateFileEnviroment();
GameEvents.OnGameStart.Add(TwitchPredictionsBG.GameStart);
GameEvents.OnInMenu.Add(TwitchPredictionsBG.InMenu);
GameEvents.OnTurnStart.Add(TwitchPredictionsBG.TurnStart);
GameEvents.OnGameEnd.Add(TwitchPredictionsBG.GameEnd);
// GameEvents.OnInMenu.Add(TwitchPredictionsBG.Update);
apiAdapter = new TwitchApiAdapter(_config);
var t = new Thread(LoadTokenOnLoad);
t.Name = "Twitch Authorization Thread";
t.Priority = ThreadPriority.Normal;
t.Start();
TwitchPredictionsBG.OnLoad(_config, apiAdapter, Version);
}
public void OnUnload()
{
TwitchPredictionsBG.isActivated = false;
// Triggered when the user unticks the plugin, however, HDT does not completely unload the plugin.
// see https://git.io/vxEcH
}
private void LoadTokenOnLoad()
{
var token = apiAdapter.FetchToken(TwitchApiAdapter.CLIENT_ID, TwitchApiAdapter.CLIENT_SECRET).Result;
if (token == null)
{
return;
}
TwitchPredictionsBG.isTokenLoaded = true;
string live = apiAdapter.IsCurrentUserStreamLive().Result ? "live" : "not live";
Log.Info($"User is {live}");
}
private void HandleButtonPress()
{
// Triggered when the user clicks your button in the plugin list
var token = apiAdapter.FetchOrCreateToken("http://localhost:8110/", "http://localhost:8110/", new[] { AuthScopes.Helix_Channel_Manage_Predictions, AuthScopes.Helix_Channel_Read_Predictions }, TwitchApiAdapter.CLIENT_ID, TwitchApiAdapter.CLIENT_SECRET).Result;
TwitchPredictionsBG.isTokenLoaded = true;
}
public void OnButtonPress()
{
var t = new Thread(HandleButtonPress);
t.Name = "Twitch Authorization Thread";
t.Priority = ThreadPriority.Normal;
t.Start();
}
public void OnUpdate()
{
TwitchPredictionsBG.Update();
}
private void CreateFileEnviroment()
{
var pluginDateFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\TwitchPredictionsBG\data\";
if (!Directory.Exists(pluginDateFolderPath))
{
Directory.CreateDirectory(pluginDateFolderPath);
}
if (File.Exists(Config._configLocation))
{
// load config from file, if available
_config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(Config._configLocation));
}
else
{ // create config file
_config = new Config();
_config.save();
}
}
private void AutoUpdate()
{
AutoUpdater.InstalledVersion = Version;
AutoUpdater.AppTitle = "Twitch Predictions BG";
AutoUpdater.Start("https://drive.google.com/uc?export=download&id=1-37tfKg0fhFmMin944Pwvo0tTPLP5Lf-");
AutoUpdater.DownloadPath = Hearthstone_Deck_Tracker.Config.AppDataPath + @"\Plugins\";
var currentDirectory = new DirectoryInfo(Hearthstone_Deck_Tracker.Config.AppDataPath + @"\Plugins\TwitchPredictionsBG\");
if (currentDirectory.Parent != null)
{
AutoUpdater.InstallationPath = currentDirectory.Parent.FullName;
}
}
public string Name => "Battlegrounds Twitch Predictions";
public string Description => "Integrates Twitch Predictions with Battlegrounds";
public string ButtonText => "Authorize Twitch";
public string Author => "getjump";
public Version Version => new Version(0, 0, 1, 2);
public MenuItem MenuItem => null;
}
}