Skip to content

Commit

Permalink
Merge pull request #121 from Panxuc/dev
Browse files Browse the repository at this point in the history
fix: 🐛 let Server run basically
  • Loading branch information
asdawej authored Mar 9, 2024
2 parents 0291416 + cc0ae79 commit f15a87a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 80 deletions.
2 changes: 0 additions & 2 deletions logic/ClientTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public static Task Main(string[] args)
//if (boolRes.ActSuccess == false) break;
tot++;
if (tot % 10 == 0) moveMsg.Angle += 1;

Console.WriteLine("Move!");
}
return Task.CompletedTask;

Check warning on line 43 in logic/ClientTest/Program.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Unreachable code detected
}
Expand Down
4 changes: 2 additions & 2 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public long AddShip(ShipInitInfo shipInitInfo)
break;
}
}
if (gameMap.ProtoGameMap[pos.x, pos.y] != PlaceType.Null &&
gameMap.ProtoGameMap[pos.x, pos.y] != PlaceType.Shadow)
if (gameMap.GetPlaceType(pos) != PlaceType.Null &&
gameMap.GetPlaceType(pos) != PlaceType.Shadow)
{
validBirthPoint = false;
}
Expand Down
24 changes: 12 additions & 12 deletions logic/Server/ArgumentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ public class ArgumentOptions
[Option("civilShipNum", Required = false, HelpText = "The number of civil ship num, 1 by default")]
public ushort CivilShipCount { get; set; } = 1;

[Option("MaxCivilShipNum", Required = false, HelpText = "The number of max civil ship num, 3 by default")]
public ushort MaxCivilShipCount { get; set; } = 3;
[Option("MaxCivilShipNum", Required = false, HelpText = "The number of max civil ship num, 2 by default")]
public ushort MaxCivilShipCount { get; set; } = 2;

[Option("MaxPlayerNumPerTeam", Required = false, HelpText = "The max player number of team, 5 by defualt")]
public ushort MaxPlayerNumPerTeam { get; set; } = 5;
[Option("MaxPlayerNumPerTeam", Required = false, HelpText = "The max player number of team, 4 by defualt")]
public ushort MaxPlayerNumPerTeam { get; set; } = 6;

[Option("warShipNum", Required = false, HelpText = "The number of civil ship num, 0 by default")]
[Option("warShipNum", Required = false, HelpText = "The number of war ship num, 0 by default")]
public ushort WarShipCount { get; set; } = 0;

[Option("MaxWarShipNum", Required = false, HelpText = "The number of civil ship num, 4 by default")]
public ushort MaxWarShipCount { get; set; } = 4;
[Option("MaxWarShipNum", Required = false, HelpText = "The number of war ship num, 2 by default")]
public ushort MaxWarShipCount { get; set; } = 2;

[Option("flagShipNum", Required = false, HelpText = "The number of civil ship num, 0 by default")]
[Option("flagShipNum", Required = false, HelpText = "The number of flag ship num, 0 by default")]
public ushort FlagShipCount { get; set; } = 0;

[Option("MaxFlagShipNum", Required = false, HelpText = "The number of civil ship num, 1 by default")]
[Option("MaxFlagShipNum", Required = false, HelpText = "The number of flag ship num, 1 by default")]
public ushort MaxFlagShipCount { get; set; } = 1;

[Option("MaxShipNum", Required = false, HelpText = "The max number of Ship, 8 by default")]
public ushort MaxShipCount { get; set; } = 8;
[Option("MaxShipNum", Required = false, HelpText = "The max number of Ship, 3 by default")]
public ushort MaxShipCount { get; set; } = 5;

[Option("homeNum", Required = false, HelpText = "The number of Home , 1 by default")]
public ushort HomeCount { get; set; } = 1;
Expand All @@ -64,7 +64,7 @@ public class ArgumentOptions
[Option('u', "url", Required = false, HelpText = "Web Url")]
public string Url { get; set; } = "114514";
[Option('m', "mapResource", Required = false, HelpText = "Map Resource Path")]
public string mapResource { get; set; } = "114514";
public string mapResource { get; set; } = DefaultArgumentOptions.MapResource;
[Option("requestOnly", Required = false, HelpText = "Only send web requests")]
public bool RequestOnly { get; set; } = false;
[Option("finalGame", Required = false, HelpText = "Whether it is the final game")]
Expand Down
2 changes: 1 addition & 1 deletion logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ partial class GameServer : ServerBase
private readonly uint spectatorMinPlayerID = 2023;
public int playerNum;
public int TeamCount => options.TeamCount;
protected long[][] communicationToGameID; // 通信用的ID映射到游戏内的ID,0指向队伍1,1指向队伍2,通信中0-2为民船,3-6为军用船,7为旗舰,8为大本营
protected long[][] communicationToGameID; // 通信用的ID映射到游戏内的ID,0指向队伍1,1指向队伍2,通信中0为大本营,1-5为船
private readonly object messageToAllClientsLock = new();
public static readonly long SendMessageToClientIntervalInMilliseconds = 50;
private MessageWriter? mwr = null;
Expand Down
123 changes: 62 additions & 61 deletions logic/Server/PlaybackServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Playback;
using Grpc.Core;
using Playback;
using Protobuf;
using System.Collections.Concurrent;
using Timothy.FrameRateTask;
Expand Down Expand Up @@ -45,66 +46,66 @@ public int[] FinalScore
public override int[] GetMoney() => new int[0];
public override int[] GetScore() => FinalScore;

//public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context)
//{
// Console.WriteLine($"AddPlayer: {request.PlayerId}");
// if (request.PlayerId >= spectatorMinPlayerID && options.NotAllowSpectator == false)
// {
// // 观战模式
// lock (spectatorJoinLock) // 具体原因见另一个上锁的地方
// {
// if (semaDict.TryAdd(request.PlayerId, (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1))))
// {
// Console.WriteLine("A new spectator comes to watch this game.");
// IsSpectatorJoin = true;
// }
// else
// {
// Console.WriteLine($"Duplicated Spectator ID {request.PlayerId}");
// return;
// }
// }
// do
// {
// semaDict[request.PlayerId].Item1.Wait();
// try
// {
// if (currentGameInfo != null)
// {
// await responseStream.WriteAsync(currentGameInfo);
// Console.WriteLine("Send!");
// }
// }
// catch (InvalidOperationException)
// {
// if (semaDict.TryRemove(request.PlayerId, out var semas))
// {
// try
// {
// semas.Item1.Release();
// semas.Item2.Release();
// }
// catch { }
// Console.WriteLine($"The spectator {request.PlayerId} exited");
// return;
// }
// }
// catch (Exception)
// {
// // Console.WriteLine(ex);
// }
// finally
// {
// try
// {
// semaDict[request.PlayerId].Item2.Release();
// }
// catch { }
// }
// } while (IsGaming);
// return;
// }
//}
public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context)
{
Console.WriteLine($"AddPlayer: {request.PlayerId}");
if (request.PlayerId >= spectatorMinPlayerID && options.NotAllowSpectator == false)
{
// 观战模式
lock (spectatorJoinLock) // 具体原因见另一个上锁的地方
{
if (semaDict.TryAdd(request.PlayerId, (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1))))
{
Console.WriteLine("A new spectator comes to watch this game.");
IsSpectatorJoin = true;
}
else
{
Console.WriteLine($"Duplicated Spectator ID {request.PlayerId}");
return;
}
}
do
{
semaDict[request.PlayerId].Item1.Wait();
try
{
if (currentGameInfo != null)
{
await responseStream.WriteAsync(currentGameInfo);
Console.WriteLine("Send!");
}
}
catch (InvalidOperationException)
{
if (semaDict.TryRemove(request.PlayerId, out var semas))
{
try
{
semas.Item1.Release();
semas.Item2.Release();
}
catch { }
Console.WriteLine($"The spectator {request.PlayerId} exited");
return;
}
}
catch (Exception)
{
// Console.WriteLine(ex);
}
finally
{
try
{
semaDict[request.PlayerId].Item2.Release();
}
catch { }
}
} while (IsGaming);
return;
}
}

public void ReportGame(MessageToClient? msg)
{
Expand Down
2 changes: 1 addition & 1 deletion logic/Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Server": {
"commandName": "Project",
"commandLineArgs": "--port 8888"
"commandLineArgs": "--port 8888 --teamCount 1 --homeNum 0 --MaxShipNum 1"
}
}
}
4 changes: 3 additions & 1 deletion logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<Mess
if (semaDict.TryAdd(request.PlayerId, temp))
{
start = Interlocked.Increment(ref playerCountNow) == playerNum;
Console.WriteLine($"PlayerCountNow: {playerCountNow}");
Console.WriteLine($"PlayerNum: {playerNum}");
}
}
if (start)
Expand Down Expand Up @@ -227,7 +229,7 @@ public override Task<MoveRes> Move(MoveMsg request, ServerCallContext context)
moveRes.ActSuccess = game.MoveShip(gameID, (int)request.TimeInMilliseconds, request.Angle);
if (!game.GameMap.Timer.IsGaming) moveRes.ActSuccess = false;
#if DEBUG
Console.WriteLine("END Move");
Console.WriteLine($"END Move: {moveRes.ActSuccess}");
#endif
return Task.FromResult(moveRes);
}
Expand Down

0 comments on commit f15a87a

Please sign in to comment.