Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoWQ222 committed May 6, 2024
1 parent 6b2a3c8 commit 65a0c2d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 27 deletions.
66 changes: 42 additions & 24 deletions logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,42 +111,44 @@ private void SaveGameResult(string path)

protected void SendGameResult(int[] scores, int mode) // 天梯的 Server 给网站发消息记录比赛结果
{
string? url2 = Environment.GetEnvironmentVariable("FINISH_URL");
if (url2 == null)
{
GameServerLogging.logger.ConsoleLog("Null FINISH_URL!");
return;
}
else
{
httpSender.Url = url2;
httpSender.Token = options.Token;
}
httpSender?.SendHttpRequest(scores, mode).Wait();

}

protected async Task<double[]> GetLadderScore(double[] scores)
protected double[] PullScore(double[] scores)
{

string? url2 = Environment.GetEnvironmentVariable("SCORE_URL");
if (url2 != null)
{
try
httpSender.Url = url2;
httpSender.Token = options.Token;
double[] org = httpSender.GetLadderScore(scores).Result;
if (org.Length == 0)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new("Bearer", options.Token);
var response = await httpClient.PostAsync(url2, JsonContent.Create(new { HttpHeaders = options.Token }));

// 读取响应内容为字符串
var jsonString = await response.Content.ReadAsStringAsync();

// 解析 JSON 字符串
var result = JsonConvert.DeserializeObject<List<ContestResult>>(jsonString);
double[] org = (from r in result select (double)(r.score)).ToArray();
double[] final = LadderCalculate(org, scores);
return final;
GameServerLogging.logger.ConsoleLog("Error: No data returned from the web!");
return new double[0];
}
catch (Exception e)
else
{
GameServerLogging.logger.ConsoleLog("No response from ladder URL!");
GameServerLogging.logger.ConsoleLog(e.ToString());
return [];
double[] final = Cal(org, scores);

Check failure on line 144 in logic/Server/GameServer.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The name 'Cal' does not exist in the current context

Check failure on line 144 in logic/Server/GameServer.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

The name 'Cal' does not exist in the current context
return final;
}
}
else
{
GameServerLogging.logger.ConsoleLog("Null URL!");
return [];
GameServerLogging.logger.ConsoleLog("Null SCORE_URL Environment!");
return new double[0];
}
}

Expand Down Expand Up @@ -212,9 +214,15 @@ private void OnGameEnd()
double[] doubleArray = scores.Select(x => (double)x).ToArray();
if (options.Mode == 2)
{
doubleArray = GetLadderScore(doubleArray).Result;
doubleArray = PullScore(doubleArray);
scores = doubleArray.Select(x => (int)x).ToArray();
SendGameResult(scores, options.Mode);
if (scores.Length == 0)
{
Console.WriteLine("Error: No data returned from the web!");

}
else
SendGameResult(scores, options.Mode);
}
endGameSem.Release();
}
Expand Down Expand Up @@ -453,11 +461,21 @@ public GameServer(ArgumentOptions options)
}
}


string? token2 = Environment.GetEnvironmentVariable("TOKEN");
if (token2 == null)
{
GameServerLogging.logger.ConsoleLog("Null TOKEN Environment!");
}
else
options.Token = token2;
if (options.Url != DefaultArgumentOptions.Url && options.Token != DefaultArgumentOptions.Token)
{
httpSender = new(options.Url, options.Token);
}
else
{
httpSender = new(DefaultArgumentOptions.Url, DefaultArgumentOptions.Token);
}
}
}
}
46 changes: 43 additions & 3 deletions logic/Server/HttpSender.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using System.Net.Http.Json;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json;
using System.Net.Http.Json;

namespace Server
{
class HttpSender(string url, string token)
{
private readonly string url = url;
private readonly string token = token;
private string url = url;
private string token = token;

public string Url
{
get { return url; }
set { url = value; }
}

public string Token
{
get { return token; }
set { token = value; }
}

// void Test()
// {
Expand Down Expand Up @@ -35,6 +49,32 @@ public async Task SendHttpRequest(int[] scores, int mode)
GameServerLogging.logger.ConsoleLog(e.ToString());
}
}

public async Task<double[]> GetLadderScore(double[] scores)
{

try
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new("Bearer", token);
var response = await httpClient.PostAsync(url, JsonContent.Create(new { HttpHeaders = token }));

// 读取响应内容为字符串
var jsonString = await response.Content.ReadAsStringAsync();

// 解析 JSON 字符串
var result = JsonConvert.DeserializeObject<List<ContestResult>>(jsonString);
double[] org = (from r in result select (double)(r.score)).ToArray();
return org;
}
catch (Exception e)
{
GameServerLogging.logger.ConsoleLog("Error when pulling ladder score!");
GameServerLogging.logger.ConsoleLog(e.ToString());
return new double[0];
}

}
}

internal class TeamScore
Expand Down

0 comments on commit 65a0c2d

Please sign in to comment.