mirror of
https://github.com/yawaflua/SPWorldsWrapper.git
synced 2025-12-13 17:16:21 +02:00
Compare commits
1 Commits
v1.0.6
...
yawaflua-p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9a51cd600 |
6
.github/workflows/nuget.yml
vendored
6
.github/workflows/nuget.yml
vendored
@@ -12,17 +12,17 @@ jobs:
|
||||
- name: Setup .NET Core
|
||||
uses: actions/setup-dotnet@v1
|
||||
with:
|
||||
dotnet-version: '7.x.x'
|
||||
dotnet-version: '8.x.x'
|
||||
- name: Set output
|
||||
id: vars
|
||||
run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
|
||||
- name: Publish to NuGet
|
||||
uses: kelson-dev/publish-nuget-fixed@2.5.6
|
||||
uses: brandedoutcast/publish-nuget@v2
|
||||
with:
|
||||
PROJECT_FILE_PATH: SPWorldsWrapper/SPWorldsWrapper.csproj
|
||||
VERSION_STATIC: ${{ steps.vars.outputs.tags }}
|
||||
TAG_COMMIT: true
|
||||
TAG_FORMAT: "*"
|
||||
TAG_FORMAT: '*'
|
||||
NUGET_KEY: ${{secrets.NUGET_API_KEY}}
|
||||
NUGET_SOURCE: https://api.nuget.org
|
||||
INCLUDE_SYMBOLS: false
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<PackageId>SPWorldsWrapper</PackageId>
|
||||
<Description>Библиотека для работы с сайтом spworlds.ru</Description>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<Version>1.0.6</Version>
|
||||
<Version>1.0.2</Version>
|
||||
<Authors>yawaflua</Authors>
|
||||
<Company>yawaflua</Company>
|
||||
<RepositoryUrl>https://github.com/yawaflua/SPWorldsWrapper</RepositoryUrl>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Newtonsoft.Json;
|
||||
using SPWorldsWrapper.Types;
|
||||
using SPWorldsWrapper.Types.Enums;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
@@ -14,23 +13,17 @@ namespace SPWorldsWrapper
|
||||
public AuthentificationError() : base() { }
|
||||
public AuthentificationError(string? message) : base(message) { }
|
||||
}
|
||||
public class ServerError : Exception
|
||||
{
|
||||
public ServerError(string? message, Exception? innerException) : base(message, innerException) { }
|
||||
public ServerError() : base() { }
|
||||
public ServerError(string? message) : base(message) { }
|
||||
}
|
||||
public class SPWrapper
|
||||
{
|
||||
public readonly HttpClient client;
|
||||
private readonly Servers server;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронный wrapper для работы напрямую с сайтом, а не с API SPWorlds.ru
|
||||
/// </summary>
|
||||
/// <param name="token">Токен от сайта(смотреть README.md)</param>
|
||||
public SPWrapper(string token, Servers server)
|
||||
public SPWrapper(string token)
|
||||
{
|
||||
var cookieContainer = new CookieContainer();
|
||||
var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
|
||||
@@ -39,10 +32,9 @@ namespace SPWorldsWrapper
|
||||
cookieContainer.Add(client.BaseAddress, new Cookie("jeff", token));
|
||||
var spwLoginMethod = spwLogin();
|
||||
if (!spwLoginMethod.Result)
|
||||
{
|
||||
throw new AuthentificationError("Ошибка парсинга данных от сайта. Проверьте токен, IP сервера и статус сайта SPWORLDS.RU");
|
||||
if (server == Servers.pl)
|
||||
throw new ServerError("Ошибка! PL закрыт, из-за этого wrapper работать не будет");
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
@@ -91,7 +83,7 @@ namespace SPWorldsWrapper
|
||||
/// <returns><see cref="SPUser" /> пользователь от сайта, или null.</returns>
|
||||
public async Task<SPUser?> getUserData(string userName)
|
||||
{
|
||||
var request = await client.GetAsync($"{server}/accounts/{userName}");
|
||||
var request = await client.GetAsync($"pl/accounts/{userName}");
|
||||
SPUser? response = JsonConvert.DeserializeObject<SPUser>(request.Content.ReadAsStringAsync().Result.ToString());
|
||||
return response;
|
||||
}
|
||||
@@ -102,7 +94,7 @@ namespace SPWorldsWrapper
|
||||
public async Task<IEnumerable<SPCity>> getAllSities()
|
||||
{
|
||||
var citiesArray = new List<SPCity>();
|
||||
var request = await client.GetAsync($"{server}/cities");
|
||||
var request = await client.GetAsync("https://spworlds.ru/api/pl/cities");
|
||||
JsonNode jsonBody = await request.Content.ReadFromJsonAsync<JsonNode>();
|
||||
foreach (JsonNode node in jsonBody.AsArray())
|
||||
{
|
||||
@@ -120,7 +112,7 @@ namespace SPWorldsWrapper
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = await client.PostAsync($"{server}/cities", JsonContent.Create(city));
|
||||
var request = await client.PostAsync("https://spworlds.ru/api/pl/cities", JsonContent.Create(city));
|
||||
if (request.StatusCode.HasFlag(HttpStatusCode.OK))
|
||||
{
|
||||
return city;
|
||||
@@ -144,7 +136,7 @@ namespace SPWorldsWrapper
|
||||
/// <returns><see cref="SPCity"/>: удаленный город или <see cref="null"/> если нет роли игрок</returns>
|
||||
public async Task<SPCity?> deleteSityFromMap(SPCity city)
|
||||
{
|
||||
var request = await client.DeleteAsync($"{server}/cities/{city.id}");
|
||||
var request = await client.DeleteAsync($"https://spworlds.ru/api/pl/cities/{city.id}");
|
||||
if (request.StatusCode.Equals(200))
|
||||
{
|
||||
return city;
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SPWorldsWrapper.Types.Enums
|
||||
{
|
||||
public enum Servers
|
||||
{
|
||||
spm,
|
||||
sp,
|
||||
pl,
|
||||
spb
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Microsoft.VisualBasic;
|
||||
using SPWorldsWrapper.Types.UserTypes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -6,45 +8,59 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SPWorldsWrapper.Types
|
||||
{
|
||||
public class CardsOwned
|
||||
{
|
||||
public string name { get; set; }
|
||||
public int color { get; set; }
|
||||
public string number { get; set; }
|
||||
public string id { get; set; }
|
||||
}
|
||||
|
||||
public class City
|
||||
{
|
||||
public Mayor mayor { get; set; }
|
||||
public string name { get; set; }
|
||||
public int x { get; set; }
|
||||
public int z { get; set; }
|
||||
}
|
||||
|
||||
public class Mayor
|
||||
{
|
||||
public string id { get; set; }
|
||||
}
|
||||
|
||||
public class SPUser
|
||||
{
|
||||
public string id { get; set; }
|
||||
public bool isBanned { get; set; }
|
||||
public User user { get; set; }
|
||||
public List<string> roles { get; set; }
|
||||
public string[] roles { get; set; }
|
||||
public City? city { get; set; }
|
||||
public string status { get; set; }
|
||||
public DateTime createdAt { get; set; }
|
||||
public List<CardsOwned> cardsOwned { get; set; }
|
||||
public bool isFollowed { get; set; }
|
||||
public bool isFollowingYou { get; set; }
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public bool isAdmin { get; set; }
|
||||
public string minecraftUUID { get; set; }
|
||||
public string username { get; set; }
|
||||
public Dictionary<string, object> toKeyValuePairs()
|
||||
{
|
||||
string cards = "[\n";
|
||||
foreach (var card in cardsOwned)
|
||||
{
|
||||
cards += " {\n";
|
||||
foreach (var kvp in card.toKeyValuePairs())
|
||||
{
|
||||
cards += $" {kvp.Key}: {kvp.Value},\n";
|
||||
}
|
||||
cards += " },\n";
|
||||
}
|
||||
cards += "]";
|
||||
return new ()
|
||||
{
|
||||
{ "id", id },
|
||||
{ "isBanned", isBanned },
|
||||
{ "status", status },
|
||||
{ "created_at", createdAt },
|
||||
{ "isFollowed", isFollowed },
|
||||
{ "isFollowingYou", isFollowingYou },
|
||||
{ "user", user.ToString() },
|
||||
{ "roles", $"[{string.Join(", ", roles)}]" },
|
||||
{ "city", city?.ToString() ?? "Null" },
|
||||
{ "cardsOwner", cards },
|
||||
};
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string stringToReturn = "{\n";
|
||||
foreach (var kvp in toKeyValuePairs())
|
||||
{
|
||||
stringToReturn += $"\n {kvp.Key}: {kvp.Value},";
|
||||
}
|
||||
stringToReturn += "\n}";
|
||||
return stringToReturn;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user