Skip to content

Commit 3b8f823

Browse files
authored
Merge pull request #199 from Grange007/dev
README: 📝 Added Readme for client
2 parents 647d616 + 510c0aa commit 3b8f823

File tree

8 files changed

+141
-64
lines changed

8 files changed

+141
-64
lines changed

logic/Client/Model/DrawCircLabel.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Client.Util;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -41,8 +42,8 @@ public Thickness Thick
4142
{
4243
get
4344
{
44-
thick.Left = x;
45-
thick.Top = y;
45+
thick.Left = (y - 0.5) * UtilInfo.unitWidth >= 0 ? (y - 0.5) * UtilInfo.unitWidth : 0;
46+
thick.Top = (x - 0.5) * UtilInfo.unitHeight >= 0 ? (x - 0.5) * UtilInfo.unitHeight : 0;
4647
return thick;
4748
}
4849
set

logic/Client/README.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Client 调试客户端使用指南
2+
3+
## 1. 启动 Server
4+
可使用命令行或 Visual Studio 启动 Server,启动后会监听本地 8888 端口。
5+
相关命令行参数如下所示,可在 `\THUAI7\logic\Server\ArgumentOptions.cs` 中查看。
6+
7+
```Csharp
8+
public class ArgumentOptions
9+
{
10+
[Option("ip", Required = false, HelpText = "Server listening ip")]
11+
public string ServerIP { get; set; } = "localhost";
12+
13+
[Option('p', "port", Required = true, HelpText = "Server listening port")]
14+
public ushort ServerPort { get; set; } = 8888;
15+
16+
[Option("teamCount", Required = false, HelpText = "The number of teams, 2 by defualt")]
17+
public ushort TeamCount { get; set; } = 2;
18+
19+
[Option("shipNum", Required = false, HelpText = "The max number of Ship, 4 by default")]
20+
public ushort ShipCount { get; set; } = 4;
21+
22+
[Option("homeNum", Required = false, HelpText = "The number of Home , 1 by default")]
23+
public ushort HomeCount { get; set; } = 1;
24+
25+
[Option('g', "gameTimeInSecond", Required = false, HelpText = "The time of the game in second, 10 minutes by default")]
26+
public uint GameTimeInSecond { get; set; } = 10 * 60;
27+
[Option('f', "fileName", Required = false, HelpText = "The file to store playback file or to read file.")]
28+
public string FileName { get; set; } = "114514";
29+
[Option("notAllowSpectator", Required = false, HelpText = "Whether to allow a spectator to watch the game.")]
30+
public bool NotAllowSpectator { get; set; } = false;
31+
[Option('b', "playback", Required = false, HelpText = "Whether open the server in a playback mode.")]
32+
public bool Playback { get; set; } = false;
33+
[Option("playbackSpeed", Required = false, HelpText = "The speed of the playback, between 0.25 and 4.0")]
34+
public double PlaybackSpeed { get; set; } = 1.0;
35+
[Option("resultOnly", Required = false, HelpText = "In playback mode to get the result directly")]
36+
public bool ResultOnly { get; set; } = false;
37+
[Option('k', "token", Required = false, HelpText = "Web API Token")]
38+
public string Token { get; set; } = "114514";
39+
[Option('u', "url", Required = false, HelpText = "Web Url")]
40+
public string Url { get; set; } = "114514";
41+
[Option('m', "mapResource", Required = false, HelpText = "Map Resource Path")]
42+
public string MapResource { get; set; } = DefaultArgumentOptions.MapResource;
43+
[Option("requestOnly", Required = false, HelpText = "Only send web requests")]
44+
public bool RequestOnly { get; set; } = false;
45+
[Option("finalGame", Required = false, HelpText = "Whether it is the final game")]
46+
public bool FinalGame { get; set; } = false;
47+
[Option("cheatMode", Required = false, HelpText = "Whether to open the cheat code")]
48+
public bool CheatMode { get; set; } = false;
49+
[Option("resultFileName", Required = false, HelpText = "Result file name, saved as .json")]
50+
public string ResultFileName { get; set; } = "114514";
51+
[Option("startLockFile", Required = false, HelpText = "Whether to create a file that identifies whether the game has started")]
52+
public string StartLockFile { get; set; } = "114514";
53+
[Option("mode", Required = false, HelpText = "Whether to run final competition")]
54+
public int Mode { get; set; } = 0;
55+
}
56+
```
57+
58+
其中,主要需要改动的是 `TeamCount``ShipCount``HomeCount` ,分别代表队伍总数量,每队船只总数量和基地总数量。只有加入的 Client 对应数据满足这些条件,Server 才能开始游戏并发送数据。
59+
进行游戏的最小条件是有一支队伍(默认红队)与一个基地,即设置 `TeamCount` 为 1, `ShipCount` 为 0, `HomeCount` 为 1 。只有加入了基地才能加入船只。如果需要测试船只,则最小条件为 `TeamCount` 为 1, `ShipCount` 为 1, `HomeCount` 为 1 。
60+
61+
## 2. 启动 Client
62+
目前 Client 只能通过 Visual Studio 启动,其与 Server 的连接函数位于 `\THUAI7\logic\Client\ViewModel\GeneralViewModel.cs` 中的 `ConnectToServer` 函数中,如下所示。
63+
64+
```Csharp
65+
// 连接Server,comInfo[]的格式:0-ip 1- port 2-playerID 3-teamID 4-ShipType
66+
ConnectToServer(new string[]{
67+
"localhost",
68+
"8888",
69+
"0",
70+
"0",
71+
"1"
72+
});
73+
```
74+
75+
其中,传入参数对应的含义已在上方标明。修改对应的参数并运行 Client ,即可按照所加参数连接 Server 并添加相关的 player 信息。
76+
77+
## 3. 启动游戏
78+
如果希望启动游戏,则需进行以下步骤:
79+
80+
1. 按照对应的参数启动 Server 。
81+
2. 如果 Server 中设置 `TeamCount` 为 1, `ShipCount` 为 0, `HomeCount` 为 1 ,则可以直接启动 Client ,即可看到游戏开始。
82+
3. 如果 Server 中的参数要求有更多的 Client 加入,则需要依次启动相应参数的 Client ,直到满足 Server 的条件。由于 Server 只要求有 Client 加入,而无需对应 Client 进程保持运行,可在加入一个 Client 后关闭其进程,再启动下一个 Client 。
83+
例如,如果希望加入一支队伍,这支队伍有一只船只,则可以按照如下步骤启动:
84+
1. Server 设置 `TeamCount` 为 1, `ShipCount` 为 1, `HomeCount` 为 1 。
85+
2. 启动一个 Client ,设置 `playerID` 为 0,`teamID` 为 0, `ShipType` 为 1 ,即加入一个 `home`
86+
3. 关闭该 Client 进程,再重新启动一个 Client ,设置 `playerID` 为 1,`teamID` 为 0, `ShipType` 为 1 ,即加入一个 `ship`
87+
此时即可看到游戏开始。

logic/Client/Util/UtilFunctions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public static int getGridIndex(float i, float j)
3737
return 50 * (int)i / 1000 + (int)j / 1000;
3838
}
3939

40-
public static PointF getMapCenter(float i, float j)
40+
public static PointF Grid2CellPoint(float i, float j)
4141
{
42-
return new PointF(10 * i + 5, 10 * j + 5);
42+
return new PointF(i / 1000, j / 1000);
4343
}
4444

4545
public static bool IsShipEqual(Ship a, Ship b)

logic/Client/Util/UtilInfo.cs

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static class UtilInfo
7373

7474
public static bool isRedPlayerShipsEmpty = true;
7575
public static bool isBluePlayerShipsEmpty = false;
76+
77+
public static int unitWidth = 10;
78+
public static int unitHeight = 10;
7679
}
7780

7881

logic/Client/View/MainPage.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
<Button x:Name="MoveDown" Text="" Grid.Column="1" Grid.Row="2" Command="{Binding MoveDownCommand}"></Button>
254254
<Button x:Name="MoveLeftDown" Text="" Grid.Column="0" Grid.Row="2" Command="{Binding MoveLeftDownCommand}"></Button>
255255
<Button x:Name="MoveRightDown" Text="" Grid.Column="2" Grid.Row="2" Command="{Binding MoveRightDownCommand}"></Button>
256-
<Button x:Name="Shoot" Text="" Grid.Column="1" Grid.Row="1"></Button>
256+
<Button x:Name="Shoot" Text="" Grid.Column="1" Grid.Row="1" Command="{Binding AttackCommand}"></Button>
257257
</Grid>
258258

259259

logic/Client/View/MainPage.xaml.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public MainPage()
3535
Application.Current.UserAppTheme = AppTheme.Light; //Light Theme Mode
3636
InitializeComponent();
3737

38-
38+
unitWidth = viewModel.unitWidth;
39+
unitHeight = viewModel.unitHeight;
3940
for (int i = 0; i < 50; i++)
4041
{
4142
for (int j = 0; j < 50; j++)
@@ -46,16 +47,16 @@ public MainPage()
4647
HeightRequest = unitHeight,
4748
HorizontalOptions = LayoutOptions.Start,
4849
VerticalOptions = LayoutOptions.Start,
49-
Margin = new Thickness(unitWidth * j, unitHeight * i, 0, 0),
50+
Margin = new Thickness(unitWidth * (49 - j), unitHeight * (49 - i), 0, 0),
5051
FontSize = 5
5152
};
5253
int index = i * 50 + j;
5354
mapPatches_[i, j].SetBinding(Label.BackgroundColorProperty, new Binding($"MapPatchesList[{index}].PatchColor"));
5455
mapPatches_[i, j].SetBinding(Label.TextProperty, new Binding($"MapPatchesList[{index}].Text"));
5556
mapPatches_[i, j].SetBinding(Label.TextColorProperty, new Binding($"MapPatchesList[{index}].TextColor"));
5657
MapGrid.Children.Add(mapPatches_[i, j]);
57-
MapGrid.SetColumn(mapPatches_[i, j], i);
58-
MapGrid.SetRow(mapPatches_[i, j], j);
58+
//MapGrid.SetColumn(mapPatches_[i, j], i);
59+
//MapGrid.SetRow(mapPatches_[i, j], j);
5960
}
6061
}
6162

@@ -115,8 +116,8 @@ public MainPage()
115116
private List<CircleLabel> bulletCirc = new List<CircleLabel>();
116117
private readonly IDispatcherTimer timer;
117118
private long counter;
118-
private double unitWidth = 10;
119-
private double unitHeight = 10;
119+
public float unitWidth = 10;
120+
public float unitHeight = 10;
120121

121122
private void TestRefresh(object sender, EventArgs e)
122123
{

logic/Client/ViewModel/GeneralViewModel.cs

+29-8
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,10 @@ private void UpdateTest(object sender, EventArgs e)
626626
public readonly int numOfShips = 16;
627627
public readonly int numOfBullets = 40;
628628

629+
public double lastMoveAngle;
629630

631+
public readonly float unitWidth = UtilInfo.unitWidth;
632+
public readonly float unitHeight = UtilInfo.unitHeight;
630633

631634
public GeneralViewModel()
632635
{
@@ -646,7 +649,8 @@ public GeneralViewModel()
646649
MoveMsg movemsg = new MoveMsg();
647650
movemsg.PlayerId = playerID;
648651
movemsg.TeamId = teamID;
649-
movemsg.Angle = double.Pi * 3 / 2;
652+
movemsg.Angle = double.Pi;
653+
lastMoveAngle = movemsg.Angle;
650654
movemsg.TimeInMilliseconds = 50;
651655
client.Move(movemsg);
652656
}
@@ -668,7 +672,8 @@ Show the error message
668672
MoveMsg movemsg = new MoveMsg();
669673
movemsg.PlayerId = playerID;
670674
movemsg.TeamId = teamID;
671-
movemsg.Angle = double.Pi / 2;
675+
movemsg.Angle = double.NegativeZero;
676+
lastMoveAngle = movemsg.Angle;
672677
movemsg.TimeInMilliseconds = moveTime;
673678
client.Move(movemsg);
674679
});
@@ -678,7 +683,8 @@ Show the error message
678683
MoveMsg movemsg = new MoveMsg();
679684
movemsg.PlayerId = playerID;
680685
movemsg.TeamId = teamID;
681-
movemsg.Angle = double.Pi;
686+
movemsg.Angle = double.Pi * 3 / 2;
687+
lastMoveAngle = movemsg.Angle;
682688
movemsg.TimeInMilliseconds = moveTime;
683689
client.Move(movemsg);
684690
});
@@ -688,7 +694,8 @@ Show the error message
688694
MoveMsg movemsg = new MoveMsg();
689695
movemsg.PlayerId = playerID;
690696
movemsg.TeamId = teamID;
691-
movemsg.Angle = double.NegativeZero;
697+
movemsg.Angle = double.Pi / 2;
698+
lastMoveAngle = movemsg.Angle;
692699
movemsg.TimeInMilliseconds = moveTime;
693700
client.Move(movemsg);
694701
});
@@ -699,6 +706,7 @@ Show the error message
699706
movemsg.PlayerId = playerID;
700707
movemsg.TeamId = teamID;
701708
movemsg.Angle = double.Pi * 5 / 4;
709+
lastMoveAngle = movemsg.Angle;
702710
movemsg.TimeInMilliseconds = moveTime;
703711
client.Move(movemsg);
704712
});
@@ -708,7 +716,8 @@ Show the error message
708716
MoveMsg movemsg = new MoveMsg();
709717
movemsg.PlayerId = playerID;
710718
movemsg.TeamId = teamID;
711-
movemsg.Angle = double.Pi * 7 / 4;
719+
movemsg.Angle = double.Pi * 3 / 4;
720+
lastMoveAngle = movemsg.Angle;
712721
movemsg.TimeInMilliseconds = moveTime;
713722
client.Move(movemsg);
714723
});
@@ -718,7 +727,8 @@ Show the error message
718727
MoveMsg movemsg = new MoveMsg();
719728
movemsg.PlayerId = playerID;
720729
movemsg.TeamId = teamID;
721-
movemsg.Angle = double.Pi * 3 / 4;
730+
movemsg.Angle = double.Pi * 7 / 4;
731+
lastMoveAngle = movemsg.Angle;
722732
movemsg.TimeInMilliseconds = moveTime;
723733
client.Move(movemsg);
724734
});
@@ -729,9 +739,20 @@ Show the error message
729739
movemsg.PlayerId = playerID;
730740
movemsg.TeamId = teamID;
731741
movemsg.Angle = double.Pi / 4;
742+
lastMoveAngle = movemsg.Angle;
732743
movemsg.TimeInMilliseconds = moveTime;
733744
client.Move(movemsg);
734745
});
746+
747+
AttackCommand = new Command(() =>
748+
{
749+
AttackMsg attackMsg = new AttackMsg();
750+
attackMsg.PlayerId = playerID;
751+
attackMsg.TeamId = teamID;
752+
attackMsg.Angle = 0;
753+
client.Attack(attackMsg);
754+
});
755+
735756
//Links = [
736757
// new Link { Name = "天梯信息", Url = "" },
737758
// new Link { Name = "获取更新", Url = "" },
@@ -787,9 +808,9 @@ Show the error message
787808

788809
// 连接Server,comInfo[]的格式:0-ip 1- port 2-playerID 3-teamID 4-ShipType
789810
ConnectToServer(new string[]{
790-
"127.0.0.1",
811+
"localhost",
791812
"8888",
792-
"1",
813+
"0",
793814
"0",
794815
"1"
795816
});

logic/Client/ViewModel/MapViewModel.cs

+8-44
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
using Client.Model;
2+
using Client.Util;
23
using Protobuf;
3-
using System;
4-
using System.Collections.Generic;
54
using System.Collections.ObjectModel;
6-
using System.ComponentModel;
7-
using System.Linq;
8-
using System.Runtime.CompilerServices;
9-
using System.Text;
10-
using System.Threading.Tasks;
11-
using Microsoft.Maui.Dispatching;
12-
using Grpc.Core;
13-
using Client.Util;
145

156

167
namespace Client.ViewModel
@@ -280,11 +271,11 @@ private void DrawShip()
280271
{
281272
MessageOfShip data = listOfShip[i];
282273
DrawCircLabel shipinfo = ShipCircList[i];
283-
PointF point = UtilFunctions.getMapCenter(data.X, data.Y);
284-
shipinfo.X = point.X / 1000;
285-
shipinfo.Y = point.Y / 1000;
286-
System.Diagnostics.Debug.WriteLine(String.Format("shipinfo.X:{0}", data.X));
287-
System.Diagnostics.Debug.WriteLine(String.Format("shipinfo.Y:{0}", data.Y));
274+
PointF point = UtilFunctions.Grid2CellPoint(data.X, data.Y);
275+
shipinfo.X = point.X;
276+
shipinfo.Y = point.Y;
277+
System.Diagnostics.Debug.WriteLine(String.Format("shipinfo.X:{0}", shipinfo.X));
278+
System.Diagnostics.Debug.WriteLine(String.Format("shipinfo.Y:{0}", shipinfo.Y));
288279
shipinfo.Text = Convert.ToString(data.Hp);
289280
long team_id = data.TeamId;
290281
switch (team_id)
@@ -336,7 +327,7 @@ private void DrawBullet()
336327
{
337328
MessageOfBullet data = listOfBullet[i];
338329
DrawCircLabel bulletinfo = BulletCircList[i];
339-
PointF point = UtilFunctions.getMapCenter(data.X, data.Y);
330+
PointF point = UtilFunctions.Grid2CellPoint(data.X, data.Y);
340331
bulletinfo.X = point.X;
341332
bulletinfo.Y = point.Y;
342333
long team_id = data.TeamId;
@@ -670,34 +661,6 @@ private void DrawResource(MessageOfResource data)
670661
MapPatchesList[index].TextColor = Colors.White;
671662
}
672663

673-
674-
private void DrawShip(MessageOfShip data, ICanvas canvas)
675-
{
676-
PointF point = UtilFunctions.getMapCenter(data.X, data.Y);
677-
float x = point.X;
678-
float y = point.Y;
679-
int hp = data.Hp;
680-
long team_id = data.TeamId;
681-
switch (team_id)
682-
{
683-
case (long)PlayerTeam.Red:
684-
canvas.FillColor = Colors.Red;
685-
break;
686-
687-
case (long)PlayerTeam.Blue:
688-
canvas.FillColor = Colors.Blue;
689-
break;
690-
691-
default:
692-
canvas.FillColor = Colors.Black;
693-
break;
694-
}
695-
canvas.FillCircle(x, y, (float)4.5);
696-
canvas.FontSize = 5.5F;
697-
canvas.FontColor = Colors.White;
698-
canvas.DrawString(Convert.ToString(hp), x - 5, y - 5, 10, 10, HorizontalAlignment.Left, VerticalAlignment.Top);
699-
}
700-
701664
private bool isClientStocked = false;
702665
private bool hasDrawn = false;
703666
private bool getMapFlag = false;
@@ -824,5 +787,6 @@ public int MapWidth
824787
public Command MoveLeftDownCommand { get; }
825788
public Command MoveRightUpCommand { get; }
826789
public Command MoveRightDownCommand { get; }
790+
public Command AttackCommand { get; }
827791
}
828792
}

0 commit comments

Comments
 (0)