Skip to content

Commit

Permalink
fix:Httpsender
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoWQ222 committed May 7, 2024
1 parent aba79ae commit 39eb0ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 8 additions & 6 deletions logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void SaveGameResult(string path)

}

protected void SendGameResult(int[] scores, int mode) // 天梯的 Server 给网站发消息记录比赛结果
protected void SendGameResult(int[] scores, bool crashed) // 天梯的 Server 给网站发消息记录比赛结果
{
string? url2 = Environment.GetEnvironmentVariable("FINISH_URL");
if (url2 == null)
Expand All @@ -122,7 +122,8 @@ protected void SendGameResult(int[] scores, int mode) // 天梯的 Server 给
httpSender.Url = url2;
httpSender.Token = options.Token;
}
httpSender?.SendHttpRequest(scores, mode).Wait();
string state = crashed ? "Crashed" : "Finished";
httpSender?.SendHttpRequest(scores, state).Wait();
}

protected double[] PullScore(double[] scores)
Expand Down Expand Up @@ -214,15 +215,16 @@ private void OnGameEnd()
double[] doubleArray = scores.Select(x => (double)x).ToArray();
if (options.Mode == 2)
{
bool crash = false;
doubleArray = PullScore(doubleArray);
scores = doubleArray.Select(x => (int)x).ToArray();
if (scores.Length == 0)
if (doubleArray.Length == 0)
{
crash = true;
Console.WriteLine("Error: No data returned from the web!");

}
else
SendGameResult(scores, options.Mode);
scores = doubleArray.Select(x => (int)x).ToArray();
SendGameResult(scores, crash);
}
endGameSem.Release();
}
Expand Down
10 changes: 3 additions & 7 deletions logic/Server/HttpSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ public string Token
// {
// this.SendHttpRequest(new()).Wait();
// }
public async Task SendHttpRequest(int[] scores, int mode)
public async Task SendHttpRequest(int[] scores, string state)
{
try
{
var request = new HttpClient();
request.DefaultRequestHeaders.Authorization = new("Bearer", token);
using var response = await request.PutAsync(url, JsonContent.Create(new
{
result = new TeamScore[]
{
new() { TeamID = 0, Score = scores[0], },
new() { TeamID = 1, Score = scores[1], },
},
mode
status = state,
scores = new int[] { scores[0], scores[1] },
}));
GameServerLogging.logger.ConsoleLog("Send to web successfully!");
GameServerLogging.logger.ConsoleLog($"Web response: {await response.Content.ReadAsStringAsync()}");
Expand Down

0 comments on commit 39eb0ef

Please sign in to comment.