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

View file

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

View file

@ -16,41 +16,58 @@ namespace SnakeGame
{ {
private static void ClientListenerThreadRun() private static void ClientListenerThreadRun()
{ {
//Connect to the server try
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
client.Connect(ConnectedMatch.OwnerIP, Port);
var ns = client.GetStream();
var sw = new BinaryWriter(ns);
sw.Write(52);
JObject writedata = new JObject();
writedata["PlayerName"] = Game.Player.Name;
writedata["Color"] = Game.Player.Color.ToArgb();
sw.Write(writedata.ToString());
var sr = new BinaryReader(ns);
JObject readdata = JObject.Parse(sr.ReadString());
Game.GameSize = new Point((int)readdata["GameSize"]["X"], (int)readdata["GameSize"]["Y"]);
Game.GameField = new SqCoord[Game.GameSize.X, Game.GameSize.Y];
Game.Length = (int)readdata["Length"];
for (int i = 0; i < Game.GameSize.X; i++)
{ {
for (int j = 0; j < Game.GameSize.Y; j++) //Connect to the server
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
client.Connect(ConnectedMatch.OwnerIP, Port);
var ns = client.GetStream();
var sw = new BinaryWriter(ns);
sw.Write(52);
JObject writedata = new JObject();
writedata["PlayerName"] = Game.Player.Name;
writedata["Color"] = Game.Player.Color.ToArgb();
sw.Write(writedata.ToString());
var sr = new BinaryReader(ns);
JObject readdata = JObject.Parse(sr.ReadString());
Game.GameSize = new Point((int)readdata["GameSize"]["X"], (int)readdata["GameSize"]["Y"]);
Game.GameField = new SqCoord[Game.GameSize.X, Game.GameSize.Y];
Game.Length = (int)readdata["Length"];
for (int i = 0; i < Game.GameSize.X; i++)
{ {
Game.GameField[i, j].PlayerName = (string)readdata["GameField"][i + ""][j + ""]["PlayerName"]; for (int j = 0; j < Game.GameSize.Y; j++)
Game.GameField[i, j].Tick = (int)readdata["GameField"][i + ""][j + ""]["Tick"]; {
Game.GameField[i, j].Type = (SquareType)Enum.Parse(typeof(SquareType), (string)readdata["GameField"][i + ""][j + ""]["Type"]); Game.GameField[i, j].PlayerName = (string)readdata["GameField"][i + ""][j + ""]["PlayerName"];
Game.GameField[i, j].Tick = (int)readdata["GameField"][i + ""][j + ""]["Tick"];
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();
try
{
Player player = ConnectedMatch.Players.SingleOrDefault(entry => entry.Name == playername);
ReceiveAndProcessData(player, sr);
}
catch (Exception e)
{
Program.HandleException(e);
}
} }
} }
foreach(JProperty item in readdata["Players"]) catch (Exception e)
{ {
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"])); Program.HandleException(e);
}
Game.Paused = false;
SendUpdate = true;
while (true)
{
string playername = sr.ReadString();
Player player = ConnectedMatch.Players.Single(entry => entry.Name == playername);
ReceiveAndProcessData(player, sr);
} }
} }
} }

View file

@ -57,53 +57,64 @@ namespace SnakeGame
} }
//Read and write inital data //Read and write inital data
//string playername = br.ReadString(); //string playername = br.ReadString();
JObject readdata = JObject.Parse(br.ReadString()); try
string playername = readdata["PlayerName"].ToString();
/*int initialcolor;
if (!int.TryParse(readdata["Color"].ToString(), out initialcolor))
{ {
client.Close(); JObject readdata = JObject.Parse(br.ReadString());
return; string playername = readdata["PlayerName"].ToString();
}*/ /*int initialcolor;
Player player = new Player(playername, color: Color.FromArgb((int)readdata["Color"])); //Login==Connect if (!int.TryParse(readdata["Color"].ToString(), out initialcolor))
player.Client = client;
ConnectedMatch.Players.Add(player);
BinaryWriter bwp = new BinaryWriter(ns);
var senddata = new JObject();
senddata["Length"] = Game.Length;
senddata["GameSize"] = new JObject();
senddata["GameSize"]["X"] = Game.GameSize.X;
senddata["GameSize"]["Y"] = Game.GameSize.Y;
senddata["GameField"] = new JObject();
for (int i = 0; i < Game.GameSize.X; i++)
{
senddata["GameField"][i + ""] = new JObject();
for (int j = 0; j < Game.GameSize.Y; j++)
{ {
senddata["GameField"][i + ""][j + ""] = new JObject(); client.Close();
senddata["GameField"][i + ""][j + ""]["PlayerName"] = Game.GameField[i, j].PlayerName; return;
senddata["GameField"][i + ""][j + ""]["Tick"] = Game.GameField[i, j].Tick; }*/
senddata["GameField"][i + ""][j + ""]["Type"] = Game.GameField[i, j].Type.ToString(); 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;
senddata["GameSize"] = new JObject();
senddata["GameSize"]["X"] = Game.GameSize.X;
senddata["GameSize"]["Y"] = Game.GameSize.Y;
senddata["GameField"] = new JObject();
for (int i = 0; i < Game.GameSize.X; i++)
{
senddata["GameField"][i + ""] = new JObject();
for (int j = 0; j < Game.GameSize.Y; j++)
{
senddata["GameField"][i + ""][j + ""] = new JObject();
senddata["GameField"][i + ""][j + ""]["PlayerName"] = Game.GameField[i, j].PlayerName;
senddata["GameField"][i + ""][j + ""]["Tick"] = Game.GameField[i, j].Tick;
senddata["GameField"][i + ""][j + ""]["Type"] = Game.GameField[i, j].Type.ToString();
}
}
senddata["Players"] = new JObject();
foreach (Player player in ConnectedMatch.Players)
{
if (player.Name == joinedplayer.Name)
continue;
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(joinedplayer, br))
break;
} }
} }
senddata["Players"] = new JObject(); catch
foreach (Player joinedplayer in ConnectedMatch.Players)
{ {
if (joinedplayer.Name == player.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();
}
bwp.Write(senddata.ToString());
Game.Paused = false;
SendUpdate = true;
while (true)
{
if (!ReceiveAndProcessData(player, br))
break;
} }
} }
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype) private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
@ -114,7 +125,7 @@ namespace SnakeGame
while (ConnectedMatch.Players.GetEnumerator().MoveNext()) while (ConnectedMatch.Players.GetEnumerator().MoveNext())
{ {
p = ConnectedMatch.Players.GetEnumerator().Current; p = ConnectedMatch.Players.GetEnumerator().Current;
if (p == player) if (p == null || p.Name == player.Name || p.Name == Game.Player.Name)
continue; continue;
var bw = new BinaryWriter(p.Client.GetStream()); var bw = new BinaryWriter(p.Client.GetStream());
bw.Write(playername); bw.Write(playername);

View file

@ -21,42 +21,58 @@ namespace SnakeGame
public const int Port = 12885; public const int Port = 12885;
public static void SyncUpdate(NetUpdateType updatetype, object data) public static void SyncUpdate(NetUpdateType updatetype, object data)
{ {
if (ConnectedMatch == null) if (ConnectedMatch == null || !SendUpdate)
return; return;
BinaryWriter bw; try
foreach (Player player in ConnectedMatch.Players)
{ {
if (player.Name == Game.Player.Name) BinaryWriter bw;
continue; //Don't send to ourselves foreach (Player player in ConnectedMatch.Players)
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
if (!isserver)
{ {
bw = new BinaryWriter(ConnectedMatch.GetPlayerByName(ConnectedMatch.OwnerName).Client.GetStream()); if (player.Name == Game.Player.Name)
continue; //Don't send to ourselves
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
if (!isserver)
{
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)
{
case NetUpdateType.Name:
string newname = (string)data;
bw.Write(newname);
break;
case NetUpdateType.Color:
int color = ((Color)data).ToArgb();
bw.Write(color);
break;
case NetUpdateType.Move:
int direction = (int)data; //Converting to enum and back to int is unnecessary
bw.Write(direction);
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
} }
else }
{ catch (IOException)
bw = new BinaryWriter(player.Client.GetStream()); {
} }
bw.Write((int)updatetype); catch (Exception e)
switch (updatetype) {
{ Program.HandleException(e);
case NetUpdateType.Name:
string newname = (string)data;
bw.Write(newname);
break;
case NetUpdateType.Color:
int color = ((Color)data).ToArgb();
bw.Write(color);
break;
case NetUpdateType.Move:
int direction = (int)data; //Converting to enum and back to int is unnecessary
bw.Write(direction);
break;
case NetUpdateType.Leave:
break;
}
if (!isserver)
break; //If not server, only send to the server
} }
} }
public static List<NetMatch> Matches = new List<NetMatch>(); public static List<NetMatch> Matches = new List<NetMatch>();
@ -100,6 +116,10 @@ namespace SnakeGame
} }
} }
catch (WebException) { } catch (WebException) { }
catch (Exception e)
{
Program.HandleException(e);
}
} }
} }
public static void CreateGame(NetMatch match) public static void CreateGame(NetMatch match)
@ -171,8 +191,8 @@ namespace SnakeGame
if (responseString != "OK") if (responseString != "OK")
MessageBox.Show("Error!\n" + responseString); MessageBox.Show("Error!\n" + responseString);
} }
}
Listener.Stop(); Listener.Stop();
}
ReceiverThread.Abort(); ReceiverThread.Abort();
if (StopEventPerPlayer != null) if (StopEventPerPlayer != null)
StopEventPerPlayer(null, null); StopEventPerPlayer(null, null);
@ -251,12 +271,25 @@ namespace SnakeGame
Network.ConnectedMatch.Players.RemoveAll(entry => entry.Name == playername); Network.ConnectedMatch.Players.RemoveAll(entry => entry.Name == playername);
player.Client.Close(); player.Client.Close();
break; 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) catch (IOException)
{ {
return false; return false;
} }
catch (Exception e)
{
Program.HandleException(e);
}
return true; return true;
} }
} }
@ -266,6 +299,7 @@ namespace SnakeGame
Color, Color,
Move, Move,
//Login, - Login==Connect //Login, - Login==Connect
Leave Leave,
Teleport
} }
} }

View file

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