-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCraftingStore.cs
More file actions
187 lines (154 loc) · 6.25 KB
/
CraftingStore.cs
File metadata and controls
187 lines (154 loc) · 6.25 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using Oxide.Core.Plugins;
namespace Oxide.Plugins
{
[Info("Crafting Store", "CraftingStore", "0.1.5")]
[Description("Checks the CraftingStore donation platform for new payments and executes the commands that have been set.")]
class CraftingStore : CovalencePlugin
{
private string baseUrl = "https://api.craftingstore.net/v4/";
private string baseUrlAlternative = "https://api-fallback.craftingstore.net/v4/";
private bool useAlternativeBaseUrl = false;
private string apiToken = "";
void OnServerInitialized()
{
// Set config
this.apiToken = Config["token"].ToString();
int fetchFrequencyMinutes = Int32.Parse(Config["frequencyMinutes"].ToString());
if (this.apiToken == "Enter your API token")
{
PrintError("Your API token is not yet set, please set the API token in the config and reload the CraftingStore plugin.");
return;
}
if (fetchFrequencyMinutes < 4)
{
// Set to 5 minutes when the frequency is to 4 or lower.
PrintError("The fetch frequency was set below the minimum (5 minutes). Please change this value in the config, CraftingStore will still work and fetch the commands every 5 minutes.");
fetchFrequencyMinutes = 5;
}
// Request commands on load
RequestCommands();
// Create timer that will execute the commands
timer.Repeat(fetchFrequencyMinutes * 60, 0, () =>
{
RequestCommands();
});
}
protected override void LoadDefaultConfig()
{
Puts("Creating CraftingStore Config");
Config["token"] = "Enter your API token";
Config["frequencyMinutes"] = 5;
}
private ApiResponse ParseResponse(string response)
{
ApiResponse commands = JsonConvert.DeserializeObject<ApiResponse>(response);
return commands;
}
private void GetRequest(string uri, string action)
{
// Set the authentication header
Dictionary<string, string> headers = new Dictionary<string, string> { { "token", this.apiToken } };
webrequest.Enqueue(getBaseUrl() + uri, null, (code, response) =>
GetCallback(code, response, action), this, Core.Libraries.RequestMethod.GET, headers);
}
private void GetCallback(int code, string response, string action)
{
if (response == null || code != 200) {
if (this.useAlternativeBaseUrl == false)
{
// Some installations cannot work with our TLS/SSL certificate, use our alternative endpoint.
this.useAlternativeBaseUrl = true;
RequestCommands();
return;
}
PrintError("Invalid response returned, please contact us if this error persists.");
PrintError(response);
return;
}
// Create model from the JSON response.
ApiResponse parsedResponse = ParseResponse(response);
// Validate that the request got a success response back, if not, return the message.
if (!parsedResponse.success)
{
PrintError("Did not receive success status: " + parsedResponse.message);
return;
}
if (action == "queue")
{
this.ProcessQueuedCommands(parsedResponse);
return;
}
}
private void PostRequest(string uri, string action, string payload)
{
// Set the authentication header
Dictionary<string, string> headers = new Dictionary<string, string> { { "token", this.apiToken } };
webrequest.Enqueue(getBaseUrl() + uri, payload, (code, response) =>
PostCallback(code, response, action), this, Core.Libraries.RequestMethod.POST, headers);
}
private void PostCallback(int code, string response, string action)
{
if (response == null || code != 200)
{
PrintError("Got error: Invalid response returned, please contact us if this error persists.");
return;
}
// Create model from the JSON response.
ApiResponse parsedResponse = ParseResponse(response);
// Validate that the request got a success response back, if not, return the message.
if (!parsedResponse.success)
{
PrintError("Did not receive success status: " + parsedResponse.message);
return;
}
}
private void ProcessQueuedCommands(ApiResponse parsedResponse)
{
QueueResponse[] donations = parsedResponse.result;
List<int> ids = new List<int>();
foreach (QueueResponse donation in donations)
{
// Add donation to executed list.
ids.Add(donation.id);
// Execute commands
Puts("Executing Command: " + donation.command);
server.Command(donation.command);
}
if (ids.Count > 0)
{
// Mark as complete if there are commands processed
string serializedIds = JsonConvert.SerializeObject(ids);
string payload = "removeIds=" + serializedIds;
PostRequest("queue/markComplete", "markComplete", payload);
}
}
private string getBaseUrl()
{
return this.useAlternativeBaseUrl ? this.baseUrlAlternative : this.baseUrl;
}
private void RequestCommands()
{
GetRequest("queue", "queue");
}
public class QueueResponse
{
public int id;
public string command;
public string packageName;
}
public class ApiResponse
{
public int id;
public bool success;
public string error;
public string message;
public QueueResponse[] result;
}
}
}