2015.03.11
This commit is contained in:
parent
278292e43a
commit
f82cc05f82
6 changed files with 180 additions and 109 deletions
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ namespace SnakeGame
|
||||||
partial class Network
|
partial class Network
|
||||||
{
|
{
|
||||||
private static void ClientListenerThreadRun()
|
private static void ClientListenerThreadRun()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
//Connect to the server
|
//Connect to the server
|
||||||
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
|
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"]);
|
Game.GameField[i, j].Type = (SquareType)Enum.Parse(typeof(SquareType), (string)readdata["GameField"][i + ""][j + ""]["Type"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach(JProperty item in readdata["Players"])
|
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"]));
|
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;
|
Game.Paused = false;
|
||||||
SendUpdate = true;
|
SendUpdate = true;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
string playername = sr.ReadString();
|
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);
|
ReceiveAndProcessData(player, sr);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Program.HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Program.HandleException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,8 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
//Read and write inital data
|
//Read and write inital data
|
||||||
//string playername = br.ReadString();
|
//string playername = br.ReadString();
|
||||||
|
try
|
||||||
|
{
|
||||||
JObject readdata = JObject.Parse(br.ReadString());
|
JObject readdata = JObject.Parse(br.ReadString());
|
||||||
string playername = readdata["PlayerName"].ToString();
|
string playername = readdata["PlayerName"].ToString();
|
||||||
/*int initialcolor;
|
/*int initialcolor;
|
||||||
|
@ -65,9 +67,14 @@ namespace SnakeGame
|
||||||
client.Close();
|
client.Close();
|
||||||
return;
|
return;
|
||||||
}*/
|
}*/
|
||||||
Player player = new Player(playername, color: Color.FromArgb((int)readdata["Color"])); //Login==Connect
|
if (Network.ConnectedMatch.Players.SingleOrDefault(entry => entry.Name == playername) != null)
|
||||||
player.Client = client;
|
{
|
||||||
ConnectedMatch.Players.Add(player);
|
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);
|
BinaryWriter bwp = new BinaryWriter(ns);
|
||||||
var senddata = new JObject();
|
var senddata = new JObject();
|
||||||
senddata["Length"] = Game.Length;
|
senddata["Length"] = Game.Length;
|
||||||
|
@ -87,25 +94,29 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
senddata["Players"] = new JObject();
|
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;
|
continue;
|
||||||
senddata["Players"][joinedplayer.Name] = new JObject();
|
senddata["Players"][player.Name] = new JObject();
|
||||||
senddata["Players"][joinedplayer.Name]["Position"] = new JObject();
|
senddata["Players"][player.Name]["Position"] = new JObject();
|
||||||
senddata["Players"][joinedplayer.Name]["Position"]["X"] = joinedplayer.Position.X;
|
senddata["Players"][player.Name]["Position"]["X"] = player.Position.X;
|
||||||
senddata["Players"][joinedplayer.Name]["Position"]["Y"] = joinedplayer.Position.Y;
|
senddata["Players"][player.Name]["Position"]["Y"] = player.Position.Y;
|
||||||
senddata["Players"][joinedplayer.Name]["Color"] = joinedplayer.Color.ToArgb();
|
senddata["Players"][player.Name]["Color"] = player.Color.ToArgb();
|
||||||
}
|
}
|
||||||
bwp.Write(senddata.ToString());
|
bwp.Write(senddata.ToString());
|
||||||
Game.Paused = false;
|
Game.Paused = false;
|
||||||
SendUpdate = true;
|
SendUpdate = true;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (!ReceiveAndProcessData(player, br))
|
if (!ReceiveAndProcessData(joinedplayer, br))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
|
private static IEnumerable<BinaryWriter> ForwardMessage(Player player, string playername, int updatetype)
|
||||||
{
|
{
|
||||||
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
||||||
|
@ -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);
|
||||||
|
|
|
@ -21,8 +21,10 @@ 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;
|
||||||
|
try
|
||||||
|
{
|
||||||
BinaryWriter bw;
|
BinaryWriter bw;
|
||||||
foreach (Player player in ConnectedMatch.Players)
|
foreach (Player player in ConnectedMatch.Players)
|
||||||
{
|
{
|
||||||
|
@ -31,11 +33,12 @@ namespace SnakeGame
|
||||||
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
|
bool isserver = ConnectedMatch.OwnerName == Game.Player.Name;
|
||||||
if (!isserver)
|
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
|
else
|
||||||
{
|
{
|
||||||
bw = new BinaryWriter(player.Client.GetStream());
|
bw = new BinaryWriter(player.Client.GetStream());
|
||||||
|
bw.Write(Game.Player.Name); //Otherwise write playername as listener expects
|
||||||
}
|
}
|
||||||
bw.Write((int)updatetype);
|
bw.Write((int)updatetype);
|
||||||
switch (updatetype)
|
switch (updatetype)
|
||||||
|
@ -54,11 +57,24 @@ namespace SnakeGame
|
||||||
break;
|
break;
|
||||||
case NetUpdateType.Leave:
|
case NetUpdateType.Leave:
|
||||||
break;
|
break;
|
||||||
|
case NetUpdateType.Teleport:
|
||||||
|
Point point = (Point)data;
|
||||||
|
bw.Write(point.X);
|
||||||
|
bw.Write(point.Y);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (!isserver)
|
if (!isserver)
|
||||||
break; //If not server, only send to the server
|
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 List<NetMatch> Matches = new List<NetMatch>();
|
||||||
public static NetMatch ConnectedMatch { get; private set; }
|
public static NetMatch ConnectedMatch { get; private set; }
|
||||||
public static void DownloadGameList()
|
public static void DownloadGameList()
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue