Browse Source

Merge pull request #142 from gsy1519/dev

feat: try to send MapMsg and remove PlayerType from Message2Server.proto
tags/0.1.0
TCL GitHub 2 years ago
parent
commit
13a58aba25
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 62 deletions
  1. +9
    -17
      dependency/proto/Message2Server.proto
  2. +5
    -7
      logic/ClientTest/Program.cs
  3. +11
    -4
      logic/Gaming/Game.cs
  4. +5
    -1
      logic/Server/CopyInfo.cs
  5. +49
    -32
      logic/Server/GameServer.cs
  6. +1
    -1
      logic/Server/Program.cs

+ 9
- 17
dependency/proto/Message2Server.proto View File

@@ -7,56 +7,48 @@ import "MessageType.proto";
message PlayerMsg message PlayerMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
oneof job_type oneof job_type
{ {
StudentType student_type = 3;
TrickerType tricker_type = 4;
StudentType student_type = 2;
TrickerType tricker_type = 3;
} }
} }


message MoveMsg message MoveMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
double angle = 3;
int64 time_in_milliseconds = 4;
double angle = 2;
int64 time_in_milliseconds = 3;
} }


message PropMsg message PropMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
PropType prop_type = 3;
PropType prop_type = 2;
} }


message SendMsg message SendMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
int64 to_player_id = 3;
PlayerType to_player_type = 4;
string message = 5;
int64 to_player_id = 2;
string message = 3;
} }


message AttackMsg // 相当于攻击 message AttackMsg // 相当于攻击
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
double angle = 3;
double angle = 2;
} }


message IDMsg message IDMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
} }


message SkillMsg message SkillMsg
{ {
int64 player_id = 1; int64 player_id = 1;
PlayerType player_type = 2;
int32 skill_id = 3;
int32 skill_id = 2;
} }


// 基本继承于THUAI5,为了使发送的信息尽可能不被浪费,暂定不发这类大包。 // 基本继承于THUAI5,为了使发送的信息尽可能不被浪费,暂定不发这类大包。


+ 5
- 7
logic/ClientTest/Program.cs View File

@@ -12,12 +12,10 @@ namespace ClientTest
var client = new AvailableService.AvailableServiceClient(channel); var client = new AvailableService.AvailableServiceClient(channel);
PlayerMsg playerInfo = new(); PlayerMsg playerInfo = new();
playerInfo.PlayerId = 0; playerInfo.PlayerId = 0;
playerInfo.PlayerType = PlayerType.StudentPlayer;
playerInfo.StudentType = StudentType.NullStudentType; playerInfo.StudentType = StudentType.NullStudentType;
var call = client.AddPlayer(playerInfo); var call = client.AddPlayer(playerInfo);
MoveMsg moveMsg = new(); MoveMsg moveMsg = new();
moveMsg.PlayerId = 0; moveMsg.PlayerId = 0;
moveMsg.PlayerType = PlayerType.StudentPlayer;
moveMsg.TimeInMilliseconds = 100; moveMsg.TimeInMilliseconds = 100;
moveMsg.Angle = 0; moveMsg.Angle = 0;
while (true) while (true)
@@ -29,15 +27,15 @@ namespace ClientTest


while (await call.ResponseStream.MoveNext()) while (await call.ResponseStream.MoveNext())
{ {
Console.WriteLine("hi");
var currentGameInfo = call.ResponseStream.Current; var currentGameInfo = call.ResponseStream.Current;
if (playerInfo.PlayerType == PlayerType.StudentPlayer)
for (int i = 0; i < currentGameInfo.ObjMessage.Count; i++)
{ {
for (int i = 0; i < currentGameInfo.ObjMessage.Count; i++)
{
//Console.WriteLine($"Human is at ({currentGameInfo.StudentMessage[i].X}, {currentGameInfo.StudentMessage[i].Y})");
}
if (currentGameInfo.ObjMessage[i].MessageOfObjCase == MessageOfObj.MessageOfObjOneofCase.StudentMessage)
Console.WriteLine($"Human is at ({currentGameInfo.ObjMessage[i].StudentMessage.X}, {currentGameInfo.ObjMessage[i].StudentMessage.Y})");
} }
} }

} }
} }
} }

+ 11
- 4
logic/Gaming/Game.cs View File

@@ -150,7 +150,7 @@ namespace Gaming


finally finally
{ {
gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
} }
}, },
timeInterval: GameData.checkInterval, timeInterval: GameData.checkInterval,
@@ -180,10 +180,17 @@ namespace Gaming
propManager.StartProducing(); propManager.StartProducing();


// 开始游戏 // 开始游戏
if (!gameMap.Timer.StartGame(milliSeconds))
return false;
new Thread
(
() =>
{
if (!gameMap.Timer.StartGame(milliSeconds))
return;


EndGame(); // 游戏结束时要做的事
EndGame(); // 游戏结束时要做的事
}
)
{ IsBackground = true }.Start();


return true; return true;
} }


+ 5
- 1
logic/Server/CopyInfo.cs View File

@@ -32,6 +32,7 @@ namespace Server
{ {
MessageOfObj msg = new MessageOfObj(); MessageOfObj msg = new MessageOfObj();
if (player.IsGhost()) return null; if (player.IsGhost()) return null;
msg.StudentMessage = new();


msg.StudentMessage.X = player.Position.x; msg.StudentMessage.X = player.Position.x;
msg.StudentMessage.Y = player.Position.y; msg.StudentMessage.Y = player.Position.y;
@@ -114,6 +115,7 @@ namespace Server
{ {
MessageOfObj msg = new MessageOfObj(); MessageOfObj msg = new MessageOfObj();
if (!player.IsGhost()) return null; if (!player.IsGhost()) return null;
msg.TrickerMessage = new();


msg.TrickerMessage.X = player.Position.x; msg.TrickerMessage.X = player.Position.x;
msg.TrickerMessage.Y = player.Position.y; msg.TrickerMessage.Y = player.Position.y;
@@ -212,6 +214,7 @@ namespace Server
private static MessageOfObj Bullet(Bullet bullet) private static MessageOfObj Bullet(Bullet bullet)
{ {
MessageOfObj msg = new MessageOfObj(); MessageOfObj msg = new MessageOfObj();
msg.BulletMessage = new();
msg.BulletMessage.X = bullet.Position.x; msg.BulletMessage.X = bullet.Position.x;
msg.BulletMessage.Y = bullet.Position.y; msg.BulletMessage.Y = bullet.Position.y;
//msg.BulletMessage.FacingDirection = bullet.FacingDirection; // XY转double? //msg.BulletMessage.FacingDirection = bullet.FacingDirection; // XY转double?
@@ -263,6 +266,7 @@ namespace Server
private static MessageOfObj Prop(Prop prop) private static MessageOfObj Prop(Prop prop)
{ {
MessageOfObj msg = new MessageOfObj(); MessageOfObj msg = new MessageOfObj();
msg.PropMessage = new();
//msg.PropMessage.Type = PropType.NullPropType; 下面写 //msg.PropMessage.Type = PropType.NullPropType; 下面写
msg.PropMessage.X = prop.Position.x; msg.PropMessage.X = prop.Position.x;
msg.PropMessage.Y = prop.Position.y; msg.PropMessage.Y = prop.Position.y;
@@ -326,7 +330,7 @@ namespace Server
private static MessageOfObj BombedBullet(BombedBullet bombedBullet) private static MessageOfObj BombedBullet(BombedBullet bombedBullet)
{ {
MessageOfObj msg = new MessageOfObj(); MessageOfObj msg = new MessageOfObj();
msg.BombedBulletMessage = new();
msg.BombedBulletMessage.X = bombedBullet.bulletHasBombed.Position.x; msg.BombedBulletMessage.X = bombedBullet.bulletHasBombed.Position.x;
msg.BombedBulletMessage.Y = bombedBullet.bulletHasBombed.Position.y; msg.BombedBulletMessage.Y = bombedBullet.bulletHasBombed.Position.y;
//msg.BombedBulletMessage.FacingDirection = bombedBullet.FacingDirection; XY类型转double? //msg.BombedBulletMessage.FacingDirection = bombedBullet.FacingDirection; XY类型转double?


+ 49
- 32
logic/Server/GameServer.cs View File

@@ -32,7 +32,7 @@ namespace Server
protected readonly ArgumentOptions options; protected readonly ArgumentOptions options;
protected readonly Game game; protected readonly Game game;
private uint spectatorMinPlayerID = 2022; private uint spectatorMinPlayerID = 2022;
private List<Tuple<PlayerType, uint>> spectatorList = new List<Tuple<PlayerType, uint>>();
private List<uint> spectatorList = new List<uint>();
public int TeamCount => options.TeamCount; public int TeamCount => options.TeamCount;
protected long[,] communicationToGameID; // 通信用的ID映射到游戏内的ID,[i,j]表示team:i,player:j的id。 protected long[,] communicationToGameID; // 通信用的ID映射到游戏内的ID,[i,j]表示team:i,player:j的id。
private readonly object messageToAllClientsLock = new(); private readonly object messageToAllClientsLock = new();
@@ -82,8 +82,8 @@ namespace Server
} }
public void StartGame() public void StartGame()
{ {
bool gameState = game.StartGame((int)options.GameTimeInSecond * 1000);
var waitHandle = new SemaphoreSlim(gameState == true ? 1 : 0); // 注意修改
game.StartGame((int)options.GameTimeInSecond * 1000);
Thread.Sleep(1);
new Thread(() => new Thread(() =>
{ {
bool flag = true; bool flag = true;
@@ -109,13 +109,6 @@ namespace Server
).Start(); ).Start();
}) })
{ IsBackground = true }.Start(); { IsBackground = true }.Start();
new Thread(() =>
{
waitHandle.Wait();
this.endGameSem.Release();
})
{ IsBackground = true }.Start();

} }
public void WaitForEnd() public void WaitForEnd()
{ {
@@ -136,9 +129,11 @@ namespace Server
public void ReportGame(GameState gameState, bool requiredGaming = true) public void ReportGame(GameState gameState, bool requiredGaming = true)
{ {
var gameObjList = game.GetGameObj(); var gameObjList = game.GetGameObj();
currentGameInfo = new();
lock (messageToAllClientsLock) lock (messageToAllClientsLock)
{ {
//currentGameInfo.MapMessage = (Messa(game.GameMap)); //currentGameInfo.MapMessage = (Messa(game.GameMap));
if (gameState == GameState.GameStart) currentGameInfo.MapMessage = MapMsg(game.GameMap.ProtoGameMap);
switch (gameState) switch (gameState)
{ {
case GameState.GameRunning: case GameState.GameRunning:
@@ -149,6 +144,7 @@ namespace Server
currentGameInfo.ObjMessage.Add(CopyInfo.Auto(gameObj)); currentGameInfo.ObjMessage.Add(CopyInfo.Auto(gameObj));
} }
currentGameInfo.GameState = gameState; currentGameInfo.GameState = gameState;
currentGameInfo.AllMessage = new(); // 还没写
mwr?.WriteOne(currentGameInfo); mwr?.WriteOne(currentGameInfo);
break; break;
default: default:
@@ -167,25 +163,40 @@ namespace Server
} }
} }


private int PlayerTypeToTeamID(PlayerType playerType)
private int PlayerIDToTeamID(long playerID)
{ {
if (playerType == PlayerType.StudentPlayer) return 0;
if (playerType == PlayerType.TrickerPlayer) return 1;
if (0 <= playerID && playerID < options.PlayerCountPerTeam) return 0;
if (playerID == options.PlayerCountPerTeam) return 1;
return -1; return -1;
} }
private uint GetBirthPointIdx(PlayerType playerType, long playerID) // 获取出生点位置
private uint GetBirthPointIdx(long playerID) // 获取出生点位置
{ {
return (uint)((PlayerTypeToTeamID(playerType) * options.PlayerCountPerTeam) + playerID + 1);
return (uint)(playerID + 1);
} }
private bool ValidPlayerTypeAndPlayerID(PlayerType playerType, long playerID)
private bool ValidPlayerID(long playerID)
{ {
if (playerType == PlayerType.StudentPlayer && 0 <= playerID && playerID < options.PlayerCountPerTeam)
return true; // 人数待修改
if (playerType == PlayerType.TrickerPlayer && 0 <= playerID && playerID < options.PlayerCountPerTeam)
if (0 <= playerID && playerID < options.PlayerCountPerTeam + 1)
return true; return true;
return false; return false;
} }
private MessageOfMap MapMsg(Map map)

private Protobuf.PlaceType IntToPlaceType(uint n)
{
switch (n)
{
case 0: return Protobuf.PlaceType.Land;
case 6: return Protobuf.PlaceType.Wall;
case 7: return Protobuf.PlaceType.Grass;
case 8: return Protobuf.PlaceType.Classroom;
case 9: return Protobuf.PlaceType.Gate;
case 10: return Protobuf.PlaceType.HiddenGate;
case 11: return Protobuf.PlaceType.Window;
case 12: return Protobuf.PlaceType.Door;
case 13: return Protobuf.PlaceType.Chest;
default: return Protobuf.PlaceType.NullPlaceType;
}
}
private MessageOfMap MapMsg(uint[,] map)
{ {
MessageOfMap msgOfMap = new MessageOfMap(); MessageOfMap msgOfMap = new MessageOfMap();
for (int i = 0; i < GameData.rows; i++) for (int i = 0; i < GameData.rows; i++)
@@ -193,7 +204,7 @@ namespace Server
msgOfMap.Row.Add(new MessageOfMap.Types.Row()); msgOfMap.Row.Add(new MessageOfMap.Types.Row());
for (int j = 0; j < GameData.cols; j++) for (int j = 0; j < GameData.cols; j++)
{ {
//msgOfMap.Row[i].Col.Add((int)map.ProtoGameMap[i, j]); int转placetype
msgOfMap.Row[i].Col.Add(IntToPlaceType(map[i, j]));
} }
} }
return msgOfMap; return msgOfMap;
@@ -219,10 +230,10 @@ namespace Server
public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context) public override async Task AddPlayer(PlayerMsg request, IServerStreamWriter<MessageToClient> responseStream, ServerCallContext context)
{ {
Console.WriteLine($"AddPlayer: {request.PlayerId}"); Console.WriteLine($"AddPlayer: {request.PlayerId}");
if (request.PlayerId >= spectatorMinPlayerID && request.PlayerType == PlayerType.NullPlayerType)
if (request.PlayerId >= spectatorMinPlayerID)
{ {
// 观战模式 // 观战模式
Tuple<PlayerType, uint> tp = new Tuple<PlayerType, uint>(request.PlayerType, (uint)request.PlayerId);
uint tp = (uint)request.PlayerId;
if (!spectatorList.Contains(tp)) if (!spectatorList.Contains(tp))
{ {
spectatorList.Add(tp); spectatorList.Add(tp);
@@ -233,7 +244,7 @@ namespace Server


if (game.GameMap.Timer.IsGaming) if (game.GameMap.Timer.IsGaming)
return; return;
if (!ValidPlayerTypeAndPlayerID(request.PlayerType, request.PlayerId)) //玩家id是否正确
if (!ValidPlayerID(request.PlayerId)) //玩家id是否正确
return; return;
//if (communicationToGameID[PlayerTypeToTeamID(request.PlayerType), request.PlayerId] != GameObj.invalidID) //是否已经添加了该玩家 //if (communicationToGameID[PlayerTypeToTeamID(request.PlayerType), request.PlayerId] != GameObj.invalidID) //是否已经添加了该玩家
//return; //return;
@@ -242,15 +253,15 @@ namespace Server


lock (addPlayerLock) lock (addPlayerLock)
{ {
Game.PlayerInitInfo playerInitInfo = new(GetBirthPointIdx(request.PlayerType, request.PlayerId), PlayerTypeToTeamID(request.PlayerType), request.PlayerId, characterType);
Game.PlayerInitInfo playerInitInfo = new(GetBirthPointIdx(request.PlayerId), PlayerIDToTeamID(request.PlayerId), request.PlayerId, characterType);
long newPlayerID = game.AddPlayer(playerInitInfo); long newPlayerID = game.AddPlayer(playerInitInfo);
if (newPlayerID == GameObj.invalidID) if (newPlayerID == GameObj.invalidID)
return; return;
communicationToGameID[PlayerTypeToTeamID(request.PlayerType), request.PlayerId] = newPlayerID;
communicationToGameID[PlayerIDToTeamID(request.PlayerId), request.PlayerId] = newPlayerID;
// 内容待修改 // 内容待修改
var temp = (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1)); var temp = (new SemaphoreSlim(0, 1), new SemaphoreSlim(0, 1));
bool start = false; bool start = false;
Console.WriteLine($"PlayerType: {request.PlayerType} Id: {request.PlayerId} joins.");
Console.WriteLine($"Id: {request.PlayerId} joins.");
lock (semaDict) lock (semaDict)
{ {
semaDict.Add(request.PlayerId, temp); semaDict.Add(request.PlayerId, temp);
@@ -270,14 +281,13 @@ namespace Server
if (currentGameInfo != null) if (currentGameInfo != null)
{ {
await responseStream.WriteAsync(currentGameInfo); await responseStream.WriteAsync(currentGameInfo);
Console.WriteLine("Send!");
//Console.WriteLine("Send!");
} }
semaDict[request.PlayerId].Item2.Release(); semaDict[request.PlayerId].Item2.Release();
} while (game.GameMap.Timer.IsGaming); } while (game.GameMap.Timer.IsGaming);
} }


public override Task<BoolRes> Attack(AttackMsg request, ServerCallContext context) public override Task<BoolRes> Attack(AttackMsg request, ServerCallContext context)

{ {
game.Attack(request.PlayerId, request.Angle); game.Attack(request.PlayerId, request.Angle);
BoolRes boolRes = new(); BoolRes boolRes = new();
@@ -292,8 +302,10 @@ namespace Server


public override Task<MoveRes> Move(MoveMsg request, ServerCallContext context) public override Task<MoveRes> Move(MoveMsg request, ServerCallContext context)
{ {
Console.WriteLine($"SetPos ID: {request.PlayerId}, TimeInMilliseconds: {request.TimeInMilliseconds}");
var gameID = communicationToGameID[PlayerTypeToTeamID(request.PlayerType), request.PlayerId];
#if DEBUG
Console.WriteLine($"Move ID: {request.PlayerId}, TimeInMilliseconds: {request.TimeInMilliseconds}");
#endif
var gameID = communicationToGameID[PlayerIDToTeamID(request.PlayerId), request.PlayerId];
game.MovePlayer(gameID, (int)request.TimeInMilliseconds, request.Angle); game.MovePlayer(gameID, (int)request.TimeInMilliseconds, request.Angle);
// 之后game.MovePlayer可能改为bool类型 // 之后game.MovePlayer可能改为bool类型
MoveRes moveRes = new(); MoveRes moveRes = new();
@@ -336,7 +348,12 @@ namespace Server
} }
public override Task<BoolRes> StartLearning(IDMsg request, ServerCallContext context) public override Task<BoolRes> StartLearning(IDMsg request, ServerCallContext context)
{ {
return base.StartLearning(request, context);
#if DEBUG
Console.WriteLine($"StartLearning ID: {request.PlayerId}");
#endif
BoolRes boolRes = new();
boolRes.ActSuccess = game.Fix(request.PlayerId);
return Task.FromResult(boolRes);
} }


public GameServer(ArgumentOptions options) public GameServer(ArgumentOptions options)


+ 1
- 1
logic/Server/Program.cs View File

@@ -34,7 +34,7 @@ namespace Server
Grpc.Core.Server server = new Grpc.Core.Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) Grpc.Core.Server server = new Grpc.Core.Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
{ {
Services = { AvailableService.BindService(gameServer) }, Services = { AvailableService.BindService(gameServer) },
Ports = { new ServerPort("127.0.0.1", 8888, ServerCredentials.Insecure) }
Ports = { new ServerPort("0.0.0.0", 8888, ServerCredentials.Insecure) }
}; };
server.Start(); server.Start();




Loading…
Cancel
Save