Browse Source

Merge branch 'new' of github.com:shangfengh/THUAI6 into new

tags/0.1.0
shangfengh 2 years ago
parent
commit
713fb09131
4 changed files with 393 additions and 123 deletions
  1. +1
    -0
      logic/Client/Client.csproj
  2. +42
    -0
      logic/Client/CommandLineArgs.cs
  3. +345
    -119
      logic/Client/MainWindow.xaml.cs
  4. +5
    -4
      logic/Client/PlaybackClient.cs

+ 1
- 0
logic/Client/Client.csproj View File

@@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="FrameRateTask" Version="1.2.0" />
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Grpc" Version="2.46.6" />


+ 42
- 0
logic/Client/CommandLineArgs.cs View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using CommandLine;

namespace Client
{
static class DefaultArgumentOptions
{
public static string FileName = "CLGG!@#$%^&*()_+"; // An impossible name of the playback file to indicate -f is not sepcified.
}

public class ArgumentOptions
{
[Option('u', "cl", Required = false, HelpText = "Whether to use command line")]
public bool cl { get; set; } = false;

[Option('i', "ip", Required = false, HelpText = "Client connected ip")]
public string Ip { get; set; } = "127.0.0.1";

[Option('p', "port", Required = false, HelpText = "Client listening port")]
public string Port { get; set; } = "7777";

[Option('t', "teamID", Required = false, HelpText = "Client teamID")]
public string TeamID { get; set; } = "0";

[Option('c', "characterID", Required = false, HelpText = "Client playerID")]
public string PlayerID { get; set; } = "0";

[Option('y', "type", Required = false, HelpText = "Client playerType")]
public string PlayerType { get; set; } = "0";

[Option('o', "occupation", Required = false, HelpText = "Client occupation")]
public string Occupation { get; set; } = "0";

[Option('f', "playbackFile", Required = false, HelpText = "The playback file name.")]
public string PlaybackFile { get; set; } = DefaultArgumentOptions.FileName;

[Option("playbackSpeed", Required = false, HelpText = "The speed of the playback, between 0.25 and 4.0")]
public double PlaybackSpeed { get; set; } = 1.0;

}
}

+ 345
- 119
logic/Client/MainWindow.xaml.cs View File

@@ -17,12 +17,11 @@ using System.Windows.Threading;
using Grpc.Core;
using Protobuf;
using Playback;
using CommandLine;

// 目前MainWindow还未复现的功能:
// 部分errordisplayer
// private void ReactToCommandline(),
// private void Playback(string fileName, double pbSpeed = 2.0)
// 交互:private void ClickToSetMode(object sender, RoutedEventArgs e)
// private void ClickToSetMode(object sender, RoutedEventArgs e)
// private void Bonus()

namespace Client
@@ -58,27 +57,17 @@ namespace Client
listOfDoor = new List<MessageOfDoor>();
listOfGate = new List<MessageOfGate>();
WindowStartupLocation = WindowStartupLocation.CenterScreen;
comInfo[0] = "127.0.0.1";
comInfo[1] = "8888";
comInfo[2] = "0";
comInfo[3] = "1";
comInfo[4] = "1";
ConnectToServer(comInfo);
OnReceive();
//DrawMap();
//ZoomMap();
//MessageOfStudent kurei = new MessageOfStudent();
//kurei.X = 10000;
//kurei.Y = 20000;
//kurei.Speed = 1000;
//kurei.PlayerId = 0;
//listOfHuman.Add(kurei);
//MessageOfAll all= new MessageOfAll();
//all.HiddenGateRefreshed = false;
//listOfAll.Add(all);
// ReactToCommandline();
//comInfo[0] = "127.0.0.1";
//comInfo[1] = "8888";
//comInfo[2] = "0";
//comInfo[3] = "1";
//comInfo[4] = "1";
//ConnectToServer(comInfo);
//OnReceive();
ReactToCommandline();
}


private void SetStatusBar()
{
StatusBarsOfSurvivor = new StatusBarOfSurvivor[4];
@@ -114,24 +103,63 @@ namespace Client
mapFlag = true;
}
}

//private void Playback(string fileName, double pbSpeed = 2.0)
//{
// var pbClient = new PlaybackClient(fileName, pbSpeed);
// int[,]? map;
// if ((map = pbClient.ReadDataFromFile(dataDict, drawPicLock)) != null)
// {
// isClientStocked = false;
// isPlaybackMode = true;
// defaultMap = map;
// mapFlag = true;
// }
// else
// {
// MessageBox.Show("Failed to read the playback file!");
// isClientStocked = true;
// }
//}
private void ReactToCommandline()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length == 2)
{
Playback(args[1]);
return;
}
_ = Parser.Default.ParseArguments<ArgumentOptions>(args).WithParsed(o =>
{ options = o; });
if (options == null || options.cl == false)
{
OnReceive();
}
else
{
if (options.PlaybackFile == DefaultArgumentOptions.FileName)
{
try
{
OnReceive();
string[] comInfo = new string[5];
comInfo[0] = options.Ip;
comInfo[1] = options.Port;
comInfo[2] = options.PlayerID;
comInfo[3] = options.PlayerType;
comInfo[4] = options.Occupation;
ConnectToServer(comInfo);
}
catch
{
OnReceive();
}
}
else
{
Playback(options.PlaybackFile, options.PlaybackSpeed);
}
}
}
private void Playback(string fileName, double pbSpeed = 2.0)
{
var pbClient = new PlaybackClient(fileName, pbSpeed);
int[,]? map;
if ((map = pbClient.ReadDataFromFile(listOfProp, listOfHuman, listOfButcher, listOfBullet,listOfBombedBullet, listOfAll, listOfChest, listOfClassroom,listOfDoor, listOfGate, drawPicLock)) != null)
{
isClientStocked = false;
isPlaybackMode = true;
defaultMap = map;
mapFlag = true;
}
else
{
MessageBox.Show("Failed to read the playback file!");
isClientStocked = true;
}
}

// 连接Server,comInfo[]的格式:0-ip 1- port 2-playerID 3-human/TrickerType 4-occupation
private void ConnectToServer(string[] comInfo)
@@ -890,86 +918,283 @@ namespace Client
{
if (!isPlaybackMode)
{
switch (e.Key)
if(Keyboard.Modifiers == ModifierKeys.Control)
{
case Key.W:
case Key.NumPad8:
MoveMsg msgW = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = Math.PI
};
client.Move(msgW);
break;
case Key.NumPad2:
case Key.S:
MoveMsg msgS = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = 0
};
client.Move(msgS);
break;
case Key.D:
case Key.NumPad6:
MoveMsg msgD = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = Math.PI / 2
};
client.Move(msgD);
break;
case Key.A:
case Key.NumPad4:
MoveMsg msgA = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = 3 * Math.PI / 2
};
client.Move(msgA);
break;
case Key.J:
AttackMsg msgJ = new()
{
PlayerId = playerID,
Angle = Math.PI
};
client.Attack(msgJ);
break;
case Key.F:
PropMsg msgF = new()
{
PlayerId = playerID,
};
client.PickProp(msgF);
break;
case Key.E:
PropMsg msgE = new()
{
PlayerId = playerID,
};
client.UseProp(msgE);
break;
case Key.Q:
SkillMsg msgQ = new()
{
PlayerId = playerID,
};
client.UseSkill(msgQ);
break;
case Key.K:
IDMsg msgK = new()
{
PlayerId = playerID,
};
client.StartLearning(msgK);
break;
default:
break;
switch (e.Key)
{
case Key.D0:
PropMsg msgP0 = new()
{
PlayerId = playerID,
PropType = PropType.Key3,
};
client.PickProp(msgP0);
break;
case Key.D1:
PropMsg msgP1 = new()
{
PlayerId = playerID,
PropType = PropType.Key5,
};
client.PickProp(msgP1);
break;
case Key.D2:
PropMsg msgP2 = new()
{
PlayerId = playerID,
PropType = PropType.Key6,
};
client.PickProp(msgP2);
break;
case Key.D3:
PropMsg msgP3 = new()
{
PlayerId = playerID,
PropType = PropType.Ptype4,
};
client.PickProp(msgP3);
break;
default:
break;
}
}
else if (Keyboard.Modifiers == ModifierKeys.Alt)
{
switch (e.Key)
{
case Key.D0:
PropMsg msgP0 = new()
{
PlayerId = playerID,
PropType = PropType.Key3,
};
client.UseProp(msgP0);
break;
case Key.D1:
PropMsg msgP1 = new()
{
PlayerId = playerID,
PropType = PropType.Key5,
};
client.UseProp(msgP1);
break;
case Key.D2:
PropMsg msgP2 = new()
{
PlayerId = playerID,
PropType = PropType.Key6,
};
client.UseProp(msgP2);
break;
case Key.D3:
PropMsg msgP3 = new()
{
PlayerId = playerID,
PropType = PropType.Ptype4,
};
client.UseProp(msgP3);
break;
default:
break;
}
}
else if(Keyboard.Modifiers == ModifierKeys.Shift)
{
switch (e.Key)
{
case Key.D0:
PropMsg msgP0 = new()
{
PlayerId = playerID,
PropType = PropType.Key3,
};
client.ThrowProp(msgP0);
break;
case Key.D1:
PropMsg msgP1 = new()
{
PlayerId = playerID,
PropType = PropType.Key5,
};
client.ThrowProp(msgP1);
break;
case Key.D2:
PropMsg msgP2 = new()
{
PlayerId = playerID,
PropType = PropType.Key6,
};
client.ThrowProp(msgP2);
break;
case Key.D3:
PropMsg msgP3 = new()
{
PlayerId = playerID,
PropType = PropType.Ptype4,
};
client.ThrowProp(msgP3);
break;
default:
break;
}
}
else if (Keyboard.Modifiers == ModifierKeys.Windows)
{
switch (e.Key)
{
case Key.D0:
SkillMsg msgS0 = new()
{
PlayerId = playerID,
SkillId = 0,
};
client.UseSkill(msgS0);
break;
case Key.D1:
SkillMsg msgS1 = new()
{
PlayerId = playerID,
SkillId = 1,
};
client.UseSkill(msgS1);
break;
case Key.D2:
SkillMsg msgS2 = new()
{
PlayerId = playerID,
SkillId = 2,
};
client.UseSkill(msgS2);
break;
default:
break;
}
}
else
{
switch (e.Key)
{
case Key.W:
case Key.NumPad8:
MoveMsg msgW = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = Math.PI
};
client.Move(msgW);
break;
case Key.S:
case Key.NumPad2:
MoveMsg msgS = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = 0
};
client.Move(msgS);
break;
case Key.D:
case Key.NumPad6:
MoveMsg msgD = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = Math.PI / 2
};
client.Move(msgD);
break;
case Key.A:
case Key.NumPad4:
MoveMsg msgA = new()
{
PlayerId = playerID,
TimeInMilliseconds = 50,
Angle = 3 * Math.PI / 2
};
client.Move(msgA);
break;
case Key.J:
AttackMsg msgJ = new()
{
PlayerId = playerID,
Angle = Math.PI
};
client.Attack(msgJ);
break;
case Key.K:
IDMsg msgK = new()
{
PlayerId = playerID,
};
client.StartLearning(msgK);
break;
case Key.R:
IDMsg msgR = new()
{
PlayerId = playerID,
};
client.StartRescueMate(msgR);
break;
case Key.T:
IDMsg msgT = new()
{
PlayerId = playerID,
};
client.StartTreatMate(msgT);
break;
case Key.G:
IDMsg msgG = new()
{
PlayerId = playerID,
};
client.Graduate(msgG);
break;
case Key.H:
IDMsg msgH = new()
{
PlayerId = playerID,
};
client.StartOpenGate(msgH);
break;
case Key.O:
IDMsg msgO = new()
{
PlayerId = playerID,
};
client.OpenDoor(msgO);
break;
case Key.P:
IDMsg msgP = new()
{
PlayerId = playerID,
};
client.CloseDoor(msgP);
break;
case Key.U:
IDMsg msgU = new()
{
PlayerId = playerID,
};
client.SkipWindow(msgU);
break;
case Key.I:
IDMsg msgI = new()
{
PlayerId = playerID,
};
client.StartOpenChest(msgI);
break;
case Key.E:
IDMsg msgE = new()
{
PlayerId = playerID,
};
client.EndAllAction(msgE);
break;
default:
break;
}
}
}
}
@@ -1201,5 +1426,6 @@ namespace Client
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }};

private string[] comInfo = new string[5];
ArgumentOptions? options = null;
}
}

+ 5
- 4
logic/Client/PlaybackClient.cs View File

@@ -16,12 +16,14 @@ namespace Client
private readonly double playbackSpeed;
private readonly int frameTimeInMilliseconds;
public MessageReader? Reader;
private SemaphoreSlim sema;
public SemaphoreSlim Sema => sema;
public PlaybackClient(string fileName, double playbackSpeed = 1.0, int frameTimeInMilliseconds = 50)
{
this.fileName = fileName;
this.playbackSpeed = playbackSpeed;
this.frameTimeInMilliseconds = frameTimeInMilliseconds;
//this.sema = new SemaphoreSlim(1, 1);
this.sema = new SemaphoreSlim(1, 1);
try
{
Reader = new MessageReader(this.fileName);
@@ -40,7 +42,7 @@ namespace Client
{
if (Reader == null)
return null;
//Sema.Wait();
Sema.Wait();
bool endFile = false;
bool mapFlag = false; // 是否获取了地图
int[,] map = new int[50, 50];
@@ -239,7 +241,7 @@ namespace Client
frame,
() =>
{
//Sema.Release();
Sema.Release();
//MessageBox.Show("Game Over!");
return 1;
}
@@ -248,7 +250,6 @@ namespace Client
})
{ IsBackground = true }.Start();
return map;

}
}
}

Loading…
Cancel
Save