Skip to content

Commit 76cd1df

Browse files
authored
♻ Добавление обработчиков ошибок
Подготовка к запоминанию refreshToken для последующего написания самообновления токенов.
1 parent f57b442 commit 76cd1df

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

Config.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace StreamIntegration
1+
namespace DonationIntegration
22
{
33
class Config
44
{
@@ -8,6 +8,9 @@ I will make an ini config in future
88
DO NOT TOUCH ACCESS TOKEN AND WHAT'S BELOW IT
99
*/
1010

11+
12+
13+
// You have to register your app right there -> https://www.donationalerts.com/application/clients
1114
public static string client_id = ""; // You can change it to your own
1215
public static string client_secret = ""; // You can change it to your own
1316
public static string redirect_URL = ""; // You can change it to your own

DAPI.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/*
99
DonationAlerts API in C#
10-
Based on work with Centrifuge server
10+
Based on work with Centrifugo server
1111
1212
╔═══╗╔═╗╔═╗╔═══╗╔╗─╔╗╔═══╗╔╗─╔╗╔═══╗╔════╗
1313
╚╗╔╗║╚╗╚╝╔╝║╔═╗║║║─║║║╔═╗║║║─║║║╔═╗║║╔╗╔╗║
@@ -19,7 +19,7 @@ Based on work with Centrifuge server
1919
Written by dxAugust (aka fet1sov)
2020
*/
2121

22-
namespace StreamIntegration
22+
namespace DonationIntegration
2323
{
2424
class DAPI
2525
{
@@ -204,6 +204,19 @@ private static string getAccessToken(string authToken)
204204
accessToken = accessResponse.access_token;
205205
Config.refresh_token = accessResponse.refresh_token;
206206

207+
// Делаем запоминание refreshToken'а, для последующего обновления
208+
string configPath = Path.Combine(Directory.GetCurrentDirectory(), "ServerPlugins/DAIntegration/config.ini");
209+
string configDirectory = Path.Combine(Directory.GetCurrentDirectory(), "ServerPlugins/DAIntegration");
210+
211+
if (Directory.Exists(@configDirectory))
212+
{
213+
if (File.Exists(@configPath))
214+
{
215+
IniParser parser = new IniParser(@configPath);
216+
parser.AddSetting("DonationAlerts", "refreshtoken", accessResponse.refresh_token);
217+
}
218+
}
219+
207220
Debugger.successOutput("Successfully got the Access Token");
208221
}
209222
else
@@ -324,4 +337,4 @@ private static void OnSocketMessage(object sender, MessageEventArgs evt)
324337
}
325338
}
326339
}
327-
}
340+
}

Debugger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace StreamIntegration
3+
namespace DonationIntegration
44
{
55
class Debugger
66
{
@@ -25,7 +25,6 @@ public static void errorOutput(string message)
2525
Console.BackgroundColor = ConsoleColor.Black;
2626
Console.ForegroundColor = ConsoleColor.Red;
2727
Console.WriteLine(Config.plugin_tag + message);
28-
2928
makeColorsBack();
3029
}
3130

Plugin.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Hope it will make your streams funnier :)
1313
*/
1414

15-
namespace StreamIntegration
15+
namespace DonationIntegration
1616
{
1717
[ApiVersion(2, 1)]
1818
public class Plugin : TerrariaPlugin
@@ -37,22 +37,31 @@ public Plugin(Main game) : base(game)
3737
public override void Initialize()
3838
{
3939
/* Gets the ini config with auth code */
40-
if (File.Exists(@FilePath))
40+
if (Directory.Exists(FolderPath))
4141
{
42-
IniParser parser = new IniParser(@FilePath);
43-
Config.authCode = parser.GetSetting("authconfig", "authcode");
44-
} else {
42+
if (File.Exists(@FilePath))
43+
{
44+
IniParser parser = new IniParser(@FilePath);
45+
Config.authCode = parser.GetSetting("DonationAlerts", "authcode");
4546

46-
}
47-
/* ================================== */
48-
49-
if (Config.authCode.Length != 0)
50-
{
51-
DAPI donateAPI = new DAPI(); // Initilization of the DonationAlerts API
52-
random = new Random();
47+
Debugger.messageOutput(Config.authCode);
48+
49+
DAPI donateAPI = new DAPI(); // Initilization of the DonationAlerts API
50+
random = new Random();
51+
52+
parser.AddSetting("DonationAlerts", "refreshtoken", Config.refresh_token);
53+
}
5354
} else {
55+
Directory.CreateDirectory(FolderPath);
56+
57+
string path = @FilePath;
58+
string text = "[DonationAlerts]" + Environment.NewLine + "authcode=";
59+
60+
File.WriteAllText(@FilePath, text);
61+
5462
Debugger.errorOutput("You have to install authcode in your config file");
5563
}
64+
/* ================================== */
5665
}
5766

5867
protected override void Dispose(bool disposing)
@@ -84,7 +93,7 @@ public static void handleDonate(string name, string currencyCode, int amount)
8493

8594
int badThings = random.Next(1, 10);
8695

87-
int playerID = 0;
96+
int playerID = random.Next(1, TShock.Players.Length);
8897
int spamCount = 30;
8998
int zombieCount = 30;
9099
int batsCount = 50;
@@ -95,8 +104,6 @@ public static void handleDonate(string name, string currencyCode, int amount)
95104
break;
96105

97106
case 2:
98-
playerID = random.Next(1, TShock.Players.Length);
99-
100107
NPC eye = TShock.Utils.GetNPCById(4);
101108
TSPlayer.Server.SetTime(false, 0.0);
102109
TSPlayer.Server.SpawnNPC(eye.type, name, 1, TShock.Players[playerID].TileX, TShock.Players[playerID].TileY);
@@ -110,8 +117,6 @@ public static void handleDonate(string name, string currencyCode, int amount)
110117
break;
111118

112119
case 4:
113-
playerID = random.Next(1, TShock.Players.Length);
114-
115120
NPC prime = TShock.Utils.GetNPCById(127);
116121
TSPlayer.Server.SetTime(false, 0.0);
117122
TSPlayer.Server.SpawnNPC(prime.type, name, 1, TShock.Players[playerID].TileX, TShock.Players[playerID].TileY);
@@ -122,7 +127,6 @@ public static void handleDonate(string name, string currencyCode, int amount)
122127
break;
123128

124129
case 6:
125-
playerID = random.Next(1, TShock.Players.Length);
126130
TShock.Players[playerID].Kick("SORRY FOR DONATION :)", false, true);
127131
break;
128132

@@ -134,7 +138,6 @@ public static void handleDonate(string name, string currencyCode, int amount)
134138
break;
135139

136140
case 8:
137-
playerID = random.Next(1, TShock.Players.Length);
138141

139142
for (int i = 0; i < zombieCount; i++)
140143
{
@@ -149,8 +152,6 @@ public static void handleDonate(string name, string currencyCode, int amount)
149152
break;
150153

151154
case 10:
152-
playerID = random.Next(1, TShock.Players.Length);
153-
154155
for (int i = 0; i < batsCount; i++)
155156
{
156157
NPC zombies = TShock.Utils.GetNPCById(51);

0 commit comments

Comments
 (0)