Skip to content

Commit fd6311a

Browse files
authored
Merge pull request #134 from eesast/fix-run
Fix run
2 parents 1d83019 + 0e54397 commit fd6311a

5 files changed

Lines changed: 39 additions & 49 deletions

File tree

dependency/shell/run.sh

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,13 @@ fi
4545
: "${PORT:=8888}"
4646
: "${TEAM_COUNT:=2}"
4747
: "${CHARACTER_NUM:=6}"
48-
: "${GAME_TIME:=600}"
48+
: "${GAME_TIME:=10}"
4949
: "${EXPOSED:=1}"
5050
: "${TEAM_SEQ_ID:=0}"
5151
: "${TEAM_LABEL:=TeamA}"
5252
: "${CONNECT_IP:=172.17.0.1}"
5353

54-
# ── Retry helper ───────────────────────────────────────────────────────────
55-
function retry_command {
56-
local command="$1"
57-
local max_attempts=5
58-
local attempt_num=1
59-
local sleep_seconds=10
60-
61-
while [ $attempt_num -le $max_attempts ]; do
62-
echo "Attempt $attempt_num / $max_attempts to run command: $command"
63-
64-
eval $command &
65-
local pid=$!
66-
67-
sleep $sleep_seconds
68-
69-
if kill -0 $pid 2>/dev/null; then
70-
echo "Failed to connect to server. Retrying..."
71-
((attempt_num++))
72-
else
73-
echo "Connected to server successfully."
74-
return 0
75-
fi
76-
done
77-
78-
echo "Failed to connect to server after $max_attempts attempts."
79-
return 1
80-
}
54+
GAME_TIME=$((GAME_TIME * 60))
8155

8256
# ═══════════════════════════════════════════════════════════════════════════
8357
# SERVER
@@ -173,7 +147,8 @@ elif [ "$TERMINAL" = "CLIENT" ]; then
173147
-t $team_id \
174148
-p $player_idx"
175149

176-
retry_command "$command" > "$output_dir/team${TEAM_SEQ_ID}-$code_name.log" 2>&1 &
150+
echo "Launching: $command"
151+
eval "$command" > "$output_dir/team${TEAM_SEQ_ID}-$code_name.log" 2>&1 &
177152

178153
elif [ -f "./$code_name" ]; then
179154
echo "Found ./$code_name"
@@ -183,7 +158,8 @@ elif [ "$TERMINAL" = "CLIENT" ]; then
183158
-t $team_id \
184159
-p $player_idx"
185160

186-
retry_command "$command" > "$output_dir/team${TEAM_SEQ_ID}-$code_name.log" 2>&1 &
161+
echo "Launching: $command"
162+
eval "$command" > "$output_dir/team${TEAM_SEQ_ID}-$code_name.log" 2>&1 &
187163

188164
else
189165
break

logic/Gaming/Game.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Game(MapStruct mapResource, int numOfTeam)
3737
tradeManager = new(this, gameMap);
3838
uplevelManager = new(this);
3939
teams = new ConcurrentDictionary<long, TeamState>();
40-
InitTeams();
40+
InitTeams(numOfTeam);
4141

4242
tradeManager = new TradeManager(this, gameMap);
4343
uplevelManager = new UplevelManager(this);

logic/Gaming/Teams.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,28 @@ public TeamSnapshot(long teamId, long score, int costTech, int efficiencyTech, i
7777
}
7878
}
7979

80-
private void InitTeams()
80+
private void InitTeams(int numOfTeam)
8181
{
82-
// 工厂位置应该对应地图中 PlaceType.FACTORY 的位置
83-
// MapInfo 中 defaultMap 的工厂位置是 [3,3], [3,46], [46,3], [46,46]
84-
var corners = new (int cx, int cy)[]
82+
if (numOfTeam < 2 || numOfTeam > 4)
8583
{
86-
(3, 3), // 角0 — 左下
87-
(3, 46), // 角1 — 左上
88-
(46, 3), // 角2 — 右下
89-
(46, 46) // 角3 — 右上
90-
};
91-
int[] selected = numOfTeam switch
84+
throw new ArgumentOutOfRangeException(
85+
nameof(numOfTeam),
86+
numOfTeam,
87+
"Team count must be 2, 3, or 4."
88+
);
89+
}
90+
91+
// Team layout order:
92+
// Team 1: top-left, Team 2: bottom-right,
93+
// Team 3: bottom-left, Team 4: top-right.
94+
var corners = new (int cx, int cy)[]
9295
{
93-
2 => new[] { 0, 3 }, // 对角线
94-
3 => new[] { 0, 1, 3 }, // 三角形
95-
_ => Enumerable.Range(0, numOfTeam).ToArray()
96+
(3, 3),
97+
(46, 46),
98+
(3, 46),
99+
(46, 3),
96100
};
101+
97102
for (int i = 0; i < numOfTeam; i++)
98103
{
99104
long teamId = i + 1;

logic/Server/GameServer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ private void OnGameEnd()
403403
SaveGameResult(options.ResultFileName.EndsWith(".json")
404404
? options.ResultFileName
405405
: options.ResultFileName + ".json");
406+
GameServerLogging.logger.LogInfo($"OnGameEnd enters with mode={options.Mode}");
406407
int[] rawMatchScores = GetScore();
407408
double[] competitionScores = rawMatchScores.Select(x => (double)x).ToArray();
408409
if (options.Mode == 2)
@@ -416,9 +417,8 @@ private void OnGameEnd()
416417
}
417418
else
418419
rawMatchScores = ladderDeltas.Select(x => (int)x).ToArray();
419-
endGameSem.Release();
420-
Thread.Sleep(1);
421420
SendGameResult(rawMatchScores, gameCrashed);
421+
endGameSem.Release();
422422
}
423423
else if (options.Mode == 1)
424424
{
@@ -432,7 +432,6 @@ private void OnGameEnd()
432432
s = [2, 0];
433433
*/ // 得分计算方式待定
434434
endGameSem.Release();
435-
Thread.Sleep(1);
436435
//SendGameResult(s);
437436
}
438437
else

logic/Server/HttpSender.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ public async Task SendHttpRequest(int[] scores, string state, string[][] player_
3838
scores,
3939
player_roles = player_role
4040
}));
41-
GameServerLogging.logger.LogInfo("Send to web successfully!");
42-
GameServerLogging.logger.LogInfo($"Web response: {await response.Content.ReadAsStringAsync()}");
41+
var body = await response.Content.ReadAsStringAsync();
42+
if (response.IsSuccessStatusCode)
43+
{
44+
GameServerLogging.logger.LogInfo("Send to web successfully!");
45+
GameServerLogging.logger.LogInfo($"Web response: {body}");
46+
}
47+
else
48+
{
49+
GameServerLogging.logger.LogWarning(
50+
$"Send to web failed with status {(int)response.StatusCode} {response.StatusCode}. Response: {body}"
51+
);
52+
}
4353
}
4454
catch (Exception e)
4555
{

0 commit comments

Comments
 (0)