forked from Switchi/SPlugin
/
SPluginApi.cs
187 lines (167 loc) · 5.78 KB
/
SPluginApi.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
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 System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.DB;
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using Rests;
namespace SPlugin
{
public class SPluginApi
{
private Rest Rest;
private Database DB;
/* Constructor
* */
public SPluginApi(Rest Rest, Database DB)
{
this.Rest = Rest;
this.DB = DB;
}
/* RegisterRestfulCommands
*
* Registers splugin http commands
*
* */
public void RegisterRestfulCommands()
{
// Checks if authentication is enabled
if (TShock.Config.EnableTokenEndpointAuthentication)
{
Rest.Register(new SecureRestCommand("/player_kills", PlayerKills));
Rest.Register(new SecureRestCommand("/all_players_kills", AllPlayersKills));
Rest.Register(new SecureRestCommand("/player_stats", PlayerStats));
Rest.Register(new SecureRestCommand("/all_players_stats", AllPlayersStats));
}
else
{
Rest.Register(new RestCommand("/player_kills", (a) => this.PlayerKills(new RestRequestArgs(a.Verbs, a.Parameters, a.Request, SecureRest.TokenData.None))));
Rest.Register(new RestCommand("/all_players_kills", (a) => this.AllPlayersKills(new RestRequestArgs(a.Verbs, a.Parameters, a.Request, SecureRest.TokenData.None))));
Rest.Register(new RestCommand("/player_stats", (a) => this.PlayerStats(new RestRequestArgs(a.Verbs, a.Parameters, a.Request, SecureRest.TokenData.None))));
Rest.Register(new RestCommand("/all_players_stats", (a) => this.AllPlayersStats(new RestRequestArgs(a.Verbs, a.Parameters, a.Request, SecureRest.TokenData.None))));
}
}
[Description("Returns kills list of specific player.")]
[Route("/player_kills")]
[Noun("name", false, "The name of player.", typeof(String))]
[Token]
private object PlayerKills(RestRequestArgs args)
{
var Name = args.Parameters["name"];
// Checks if parameters exists
if (string.IsNullOrWhiteSpace(Name))
{
return RestMissingParam("name");
}
// Gets and checks if result (kills) are not empty
var Kills = DB.GetKills(Name);
if (Kills == null)
{
return RestError("Invalid name");
}
var Result = new RestObject();
foreach (var Row in Kills)
{
Result.Add(Row.Key, Row.Value);
}
return Result;
}
[Description("Returns kills list of all players.")]
[Route("/all_players_kills")]
[Token]
private object AllPlayersKills(RestRequestArgs args)
{
// Gets and checks if result (kills) are not empty
var AllKills = DB.GetAllKills();
if (AllKills == null)
{
return RestError("Empty database");
}
var Result = new RestObject();
foreach (var Row in AllKills)
{
Result.Add(Row.Key, Row.Value);
}
return Result;
}
[Description("Returns specific player stats.")]
[Route("/player_stats")]
[Noun("name", false, "The name of player.", typeof(String))]
[Token]
private object PlayerStats(RestRequestArgs args)
{
var Name = args.Parameters["name"];
// Checks if parameters exists
if (string.IsNullOrWhiteSpace(Name))
{
return RestMissingParam("name");
}
// Gets and checks if result (stats) are not empty
var Stats = DB.GetStats(Name);
if (Stats == null)
{
return RestError("Invalid name");
}
var Result = new RestObject()
{
{"tiles_destroyed", Stats[0]},
{"tiles_placed", Stats[1]},
{"deaths", Stats[2]}
};
return Result;
}
[Description("Returns stats for each player.")]
[Route("/all_players_stats")]
[Token]
private object AllPlayersStats(RestRequestArgs args)
{
// Gets and checks if result (stats) are not empty
var AllStats = DB.GetAllStats();
if (AllStats == null)
{
return RestError("Empty database");
}
var Result = new RestObject();
foreach (var Row in AllStats)
{
Result.Add(Row.Key, new Dictionary<string, int>(){
{"tiles_destroyed", Row.Value[0]},
{"tiles_placed", Row.Value[1]},
{"deaths", Row.Value[2]}});
}
return Result;
}
/* RestError
*
* Returns RestObject with error message
*
* */
private RestObject RestError(string message, string status = "400")
{
return new RestObject(status) { Error = message };
}
/* RestMissingParam
*
* Custom version of RestError
*
* */
private RestObject RestMissingParam(string var)
{
return RestError("Missing or empty " + var + " parameter");
}
/* RestMissingParam
*
* Custom version of RestError
*
* */
private RestObject RestMissingParam(params string[] vars)
{
return RestMissingParam(string.Join(", ", vars));
}
}
}