leadercraft/Leadercraft/Scoring/UploadJob.cs

83 lines
2.2 KiB
C#

using System;
using System.Threading;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using GamecraftModdingAPI.Utility;
using Leadercraft.Server;
namespace Leadercraft.Scoring
{
public struct UploadJob
{
private int points;
private int time;
private float[] location;
private ulong player;
private string playerName;
private ulong game;
public UploadJob(int score, double timespan, float3 position, ulong playerId, string playerName, ulong gameId)
{
this.points = score;
this.time = (int)Math.Ceiling(timespan);
this.location = new float[3] { position.x, position.y, position.z};
this.player = playerId;
this.playerName = playerName;
this.game = 2;//gameId;
LeadercraftPlugin.BuildApi();
}
public void RunInNewThread()
{
Thread job = new Thread(new ThreadStart(Execute));
job.Start();
}
private void Execute()
{
if (player < 1000 || game == 0) return; // offline game
LeadercraftResult<KeyStruct> tokenResult = LeadercraftPlugin.Api.RequestPOSTToken(playerName);
if (tokenResult.IsError)
{
Logging.LogWarning($"Leadercraft API token web request failed with status {tokenResult.StatusCode}");
try
{
Logging.LogWarning($"API Error Response: {tokenResult.ParseError().Items[0]}");
}
catch (Exception) { }
return;
}
string token = tokenResult.ParseResult().Items[0].Token;
CriteriaStruct criteria = new CriteriaStruct
{
Location = new float[][] { location, location },
Time = time,
GameID = game,
PlayerID = player,
Complete = true,
Points = points
};
LeadercraftResult<string> criteriaResult = LeadercraftPlugin.Api.RequestPOSTCriteria(criteria, token);
if (criteriaResult.IsError)
{
Logging.LogWarning($"Leadercraft API criteria web request failed with status {criteriaResult.StatusCode}");
try
{
Logging.LogWarning($"API Error Response: {criteriaResult.ParseError().Items[0]}");
}
catch (Exception) { }
return;
}
Logging.MetaLog($"Criteria request succeeded with status {criteriaResult.StatusCode}");
}
}
}