From 39eb0ef50ebe7132616bc3f808fcd675ddea3e26 Mon Sep 17 00:00:00 2001 From: GuoWQ222 Date: Tue, 7 May 2024 17:05:59 +0800 Subject: [PATCH] fix:Httpsender --- logic/Server/GameServer.cs | 14 ++++++++------ logic/Server/HttpSender.cs | 10 +++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/logic/Server/GameServer.cs b/logic/Server/GameServer.cs index 8eafafaf..65efb626 100755 --- a/logic/Server/GameServer.cs +++ b/logic/Server/GameServer.cs @@ -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) @@ -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) @@ -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(); } diff --git a/logic/Server/HttpSender.cs b/logic/Server/HttpSender.cs index 3db5658a..a7ad5980 100755 --- a/logic/Server/HttpSender.cs +++ b/logic/Server/HttpSender.cs @@ -25,7 +25,7 @@ public string Token // { // this.SendHttpRequest(new()).Wait(); // } - public async Task SendHttpRequest(int[] scores, int mode) + public async Task SendHttpRequest(int[] scores, string state) { try { @@ -33,12 +33,8 @@ public async Task SendHttpRequest(int[] scores, int mode) 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()}");