2015.03.11

This commit is contained in:
Norbi Peti 2017-01-07 23:23:23 +01:00
parent 278292e43a
commit f82cc05f82
6 changed files with 180 additions and 109 deletions

View file

@ -183,6 +183,7 @@ namespace SnakeGame
UpdateTime = 800;
Length = 4;
}
Network.SyncUpdate(NetUpdateType.Teleport, Player.Position);
for (int i = 0; i < GameSize.X; i++)
{
for (int j = 0; j < GameSize.Y; j++)
@ -318,6 +319,8 @@ namespace SnakeGame
}
public static void MovePlayerPost(Player player, Point point)
{
if (point.X >= GameSize.X || point.Y >= GameSize.Y)
return;
GameField[point.X, point.Y].Tick = Length;
GameField[point.X, point.Y].Type = SquareType.Player;
GameField[point.X, point.Y].PlayerName = player.Name;

View file

@ -42,7 +42,7 @@ namespace SnakeGame
public IPAddress[] OwnerIP;
public Player GetPlayerByName(string name)
{
try
/*try
{
//return Players.Single(entry => entry.ID == id);
return Players.Single(entry => entry.Name == name);
@ -50,7 +50,8 @@ namespace SnakeGame
catch
{
return null;
}
}*/
return Players.SingleOrDefault(entry => entry.Name == name);
}
//public int NextID = 0;
}

View file

@ -15,6 +15,8 @@ namespace SnakeGame
partial class Network
{
private static void ClientListenerThreadRun()
{
try
{
//Connect to the server
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
@ -40,18 +42,33 @@ namespace SnakeGame
Game.GameField[i, j].Type = (SquareType)Enum.Parse(typeof(SquareType), (string)readdata["GameField"][i + ""][j + ""]["Type"]);
}
}
ConnectedMatch.Players.Clear();
foreach (JProperty item in readdata["Players"])
{
ConnectedMatch.Players.Add(new Player(item.Name, color: Color.FromArgb((int)item.Value["Color"]), x: (int)item.Value["Position"]["X"], y: (int)item.Value["Position"]["Y"]));
if (item.Name == ConnectedMatch.OwnerName)
ConnectedMatch.Players.Last().Client = client;
}
Game.Paused = false;
SendUpdate = true;
while (true)
{
string playername = sr.ReadString();
Player player = ConnectedMatch.Players.Single(entry => entry.Name == playername);
try
{
Player player = ConnectedMatch.Players.SingleOrDefault(entry => entry.Name == playername);
ReceiveAndProcessData(player, sr);
}
catch (Exception e)
{
Program.HandleException(e);
}
}
}
catch (Exception e)
{
Program.HandleException(e);
}
}
}
}

View file

@ -57,6 +57,8 @@ namespace SnakeGame
}
//Read and write inital data
//string playername = br.ReadString();
try
{
JObject readdata = JObject.Parse(br.ReadString());
string playername = readdata["PlayerName"].ToString();
/*int initialcolor;
@ -65,9 +67,14 @@ namespace SnakeGame
client.Close();
return;
}*/
Player player = new Player(playername, color: Color.FromArgb((int)readdata["Color"])); //Login==Connect
player.Client = client;
ConnectedMatch.Players.Add(player);
if (Network.ConnectedMatch.Players.SingleOrDefault(entry => entry.Name == playername) != null)
{
client.Close();
return;
}
Player joinedplayer = new Player(playername, color: Color.FromArgb((int)readdata["Color"])); //Login==Connect
joinedplayer.Client = client;
ConnectedMatch.Players.Add(joinedplayer);
BinaryWriter bwp = new BinaryWriter(ns);
var senddata = new JObject();
senddata["Length"] = Game.Length;
@ -87,25 +94,29 @@ namespace SnakeGame
}
}
senddata["Players"] = new JObject();
foreach (Player joinedplayer in ConnectedMatch.Players)
foreach (Player player in ConnectedMatch.Players)
{
if (joinedplayer.Name == player.Name)
if (player.Name == joinedplayer.Name)
continue;
senddata["Players"][joinedplayer.Name] = new JObject();
senddata["Players"][joinedplayer.Name]["Position"] = new JObject();
senddata["Players"][joinedplayer.Name]["Position"]["X"] = joinedplayer.Position.X;
senddata["Players"][joinedplayer.Name]["Position"]["Y"] = joinedplayer.Position.Y;
senddata["Players"][joinedplayer.Name]["Color"] = joinedplayer.Color.ToArgb();
senddata["Players"][player.Name] = new JObject();
senddata["Players"][player.Name]["Position"] = new JObject();
senddata["Players"][player.Name]["Position"]["X"] = player.Position.X;
senddata["Players"][player.Name]["Position"]["Y"] = player.Position.Y;
senddata["Players"][player.Name]["Color"] = player.Color.ToArgb();
}
bwp.Write(senddata.ToString());
Game.Paused = false;
SendUpdate = true;
while (true)
{
if (!ReceiveAndProcessData(player, br))
if (!ReceiveAndProcessData(joinedplayer, br))
break;
}
}
catch
{
}
}
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
{
if (ConnectedMatch.OwnerName == Game.Player.Name)
@ -114,7 +125,7 @@ namespace SnakeGame
while (ConnectedMatch.Players.GetEnumerator().MoveNext())
{
p = ConnectedMatch.Players.GetEnumerator().Current;
if (p == player)
if (p == null || p.Name == player.Name || p.Name == Game.Player.Name)
continue;
var bw = new BinaryWriter(p.Client.GetStream());
bw.Write(playername);

View file

@ -21,8 +21,10 @@ namespace SnakeGame
public const int Port = 12885;
public static void SyncUpdate(NetUpdateType updatetype, object data)
{
if (ConnectedMatch == null)
if (ConnectedMatch == null || !SendUpdate)
return;
try
{
BinaryWriter bw;
foreach (Player player in ConnectedMatch.Players)
{
@ -31,11 +33,12 @@ namespace SnakeGame
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
if (!isserver)
{
bw = new BinaryWriter(ConnectedMatch.GetPlayerByName(ConnectedMatch.OwnerName).Client.GetStream());
bw = new BinaryWriter(ConnectedMatch.GetPlayerByName(ConnectedMatch.OwnerName).Client.GetStream()); //If not server, send only to server
}
else
{
bw = new BinaryWriter(player.Client.GetStream());
bw.Write(Game.Player.Name); //Otherwise write playername as listener expects
}
bw.Write((int)updatetype);
switch (updatetype)
@ -54,11 +57,24 @@ namespace SnakeGame
break;
case NetUpdateType.Leave:
break;
case NetUpdateType.Teleport:
Point point = (Point)data;
bw.Write(point.X);
bw.Write(point.Y);
break;
}
if (!isserver)
break; //If not server, only send to the server
}
}
catch (IOException)
{
}
catch (Exception e)
{
Program.HandleException(e);
}
}
public static List<NetMatch> Matches = new List<NetMatch>();
public static NetMatch ConnectedMatch { get; private set; }
public static void DownloadGameList()
@ -100,6 +116,10 @@ namespace SnakeGame
}
}
catch (WebException) { }
catch (Exception e)
{
Program.HandleException(e);
}
}
}
public static void CreateGame(NetMatch match)
@ -171,8 +191,8 @@ namespace SnakeGame
if (responseString != "OK")
MessageBox.Show("Error!\n" + responseString);
}
}
Listener.Stop();
}
ReceiverThread.Abort();
if (StopEventPerPlayer != null)
StopEventPerPlayer(null, null);
@ -251,12 +271,25 @@ namespace SnakeGame
Network.ConnectedMatch.Players.RemoveAll(entry => entry.Name == playername);
player.Client.Close();
break;
case NetUpdateType.Teleport:
player.Position = new Point(br.ReadInt32(), br.ReadInt32());
Game.MovePlayerPost(player, player.Position);
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
{
bw.Write(player.Position.X);
bw.Write(player.Position.Y);
}
break;
}
}
catch (IOException)
{
return false;
}
catch (Exception e)
{
Program.HandleException(e);
}
return true;
}
}
@ -266,6 +299,7 @@ namespace SnakeGame
Color,
Move,
//Login, - Login==Connect
Leave
Leave,
Teleport
}
}

View file

@ -18,5 +18,10 @@ namespace SnakeGame
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static void HandleException(Exception e)
{
MessageBox.Show("Error!\n" + e.Message);
}
}
}