diff --git a/SnakeGame/Game.cs b/SnakeGame/Game.cs index 9bcf246..d9cf52a 100644 --- a/SnakeGame/Game.cs +++ b/SnakeGame/Game.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -13,7 +14,7 @@ namespace SnakeGame { public static Point GameSize; public static SqCoord[,] GameField; - public static int Length = 4; + public static int Length; public static int UpdateTime = 800; public static Direction MoveDirection; public static Label ScoreLabel; @@ -22,8 +23,6 @@ namespace SnakeGame public static Player Player = new Player("Player", true); /// /// Opposite of Form1.TimerEnabled - /// TODO: Send pause/resume event - /// (View -> Task List -> Comments /// public static bool Paused { @@ -90,16 +89,17 @@ namespace SnakeGame MSGBox.ShowMSGBox("Please change your username from default.", "", MSGBoxType.Text); break; } - Network.DownloadGameList(); - string matches = Network.Matches.Combine((match, combiner) => combiner += match.Name + " " + match.Players.Count + "/" + match.MaxPlayers + " " + match.OwnerName + "\n"); - MSGBox.ShowMSGBox("Connect to game", matches, MSGBoxType.List, new EventHandler(delegate(object s, string input) + + //string matches = Network.Matches.Combine((match, combiner) => combiner += match.Name + " " + match.Players.Count + "/" + match.MaxPlayers + " " + match.OwnerName + "\n"); + string inputs = "IP:\n"; + MSGBox.ShowMSGBox("Connect to game", inputs, MSGBoxType.MultipleInput, new EventHandler(delegate(object s, string input) { - int num = 0; - if (int.TryParse(input, out num) && num != -1) + IPAddress address; + if (IPAddress.TryParse(input.Replace("\n", ""), out address)) { Game.Reset(); - Network.Connect(Network.Matches[num]); - Game.Reset(false); + Network.Connect(new NetMatch { OwnerIP = new IPAddress[] { address } }); + //Game.Reset(false); - On successfull connect event } else Game.Paused = true; @@ -151,7 +151,7 @@ namespace SnakeGame { Player.Score = 0; Player.Lives = 3; - UpdateTime = 800; + UpdateTime = 600; Length = 4; } Network.SyncUpdate(NetUpdateType.Teleport, Player.Position); @@ -209,7 +209,8 @@ namespace SnakeGame } else { - LivesLabel.ForeColor = Color.White; + //LivesLabel.ForeColor = Color.White; + LivesLabel.ForeColor = Game.Player.Color; if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point) { Player.Score += 1000; @@ -217,7 +218,8 @@ namespace SnakeGame Game.AddPoint(); } else - ScoreLabel.ForeColor = Color.White; + //ScoreLabel.ForeColor = Color.White; + ScoreLabel.ForeColor = Game.Player.Color; if (Player.Score > 0) Player.Score -= new Random().Next(1, 20); MovePlayerPost(Player, nextcoord); @@ -242,6 +244,7 @@ namespace SnakeGame { MSGBox.ShowMSGBox("Game over!", "", MSGBoxType.Text); Game.Paused = true; + Network.Leave(); //2015.08.29. } public static Point MovePlayerPre(Player player, Direction direction) diff --git a/SnakeGame/GameRenderer.cs b/SnakeGame/GameRenderer.cs index e800d8a..b1050bf 100644 --- a/SnakeGame/GameRenderer.cs +++ b/SnakeGame/GameRenderer.cs @@ -39,12 +39,16 @@ namespace SnakeGame RenderSquare(new Point { X = i, Y = j }, Color.LimeGreen); else { - Player player = Network.ConnectedMatch.GetPlayerByName(Game.GameField[i, j].PlayerName); - if (player != null) - RenderSquare(new Point { X = i, Y = j }, player.Color); + if (Game.GameField[i, j].PlayerName == Game.Player.Name) + RenderSquare(new Point { X = i, Y = j }, Game.Player.Color); else - RenderSquare(new Point { X = i, Y = j }, Color.DarkGray); - + { + Player player = Network.ConnectedMatch.GetPlayerByName(Game.GameField[i, j].PlayerName); + if (player != null) + RenderSquare(new Point { X = i, Y = j }, player.Color); + else + RenderSquare(new Point { X = i, Y = j }, Color.DarkGray); + } } else if (Game.GameField[i, j].Type == SquareType.Point) RenderSquare(new Point { X = i, Y = j }, Color.Blue); diff --git a/SnakeGame/NetMatch.cs b/SnakeGame/NetMatch.cs index 9204580..4adc3c2 100644 --- a/SnakeGame/NetMatch.cs +++ b/SnakeGame/NetMatch.cs @@ -12,11 +12,6 @@ namespace SnakeGame { public string Name = ""; public int MaxPlayers = 0; - //public List PlayerNames = new List(); - /// - /// TODO: Send player join/leave to master server - /// - //public List Players = new List(); public PlayerCollection Players = new PlayerCollection(); private string ownername = ""; @@ -42,17 +37,7 @@ namespace SnakeGame public IPAddress[] OwnerIP; public Player GetPlayerByName(string name) { - /*try - { - //return Players.Single(entry => entry.ID == id); - return Players.Single(entry => entry.Name == name); - } - catch - { - return null; - }*/ - return Players.SingleOrDefault(entry => entry.Name == name); + return Players.FirstOrDefault(entry => entry.Name == name); } - //public int NextID = 0; } } diff --git a/SnakeGame/Network.Client.cs b/SnakeGame/Network.Client.cs index 836b075..6dd4675 100644 --- a/SnakeGame/Network.Client.cs +++ b/SnakeGame/Network.Client.cs @@ -17,6 +17,8 @@ namespace SnakeGame { private static void ClientListenerThreadRun() { + //TODO: Game over disconnect + //TODO: Synchronised point placing on respawn try { //Connect to the server @@ -39,6 +41,7 @@ namespace SnakeGame 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"]; + ConnectedMatch.OwnerName = (string)readdata["OwnerName"]; //2015.08.29. for (int i = 0; i < Game.GameSize.X; i++) { for (int j = 0; j < Game.GameSize.Y; j++) @@ -57,6 +60,7 @@ namespace SnakeGame } Game.Paused = false; SendUpdate = true; + Game.Reset(false); while (true) { string playername = sr.ReadString(); @@ -71,6 +75,10 @@ namespace SnakeGame } } } + catch(ObjectDisposedException) + { + return; + } catch (Exception e) { Program.HandleException(e); diff --git a/SnakeGame/Network.Server.cs b/SnakeGame/Network.Server.cs index 5dfcb47..baf5da8 100644 --- a/SnakeGame/Network.Server.cs +++ b/SnakeGame/Network.Server.cs @@ -78,6 +78,7 @@ namespace SnakeGame BinaryWriter bwp = new BinaryWriter(ns); var senddata = new JObject(); senddata["Length"] = Game.Length; + senddata["OwnerName"] = Game.Player.Name; //2015.08.29. senddata["GameSize"] = new JObject(); senddata["GameSize"]["X"] = Game.GameSize.X; senddata["GameSize"]["Y"] = Game.GameSize.Y; diff --git a/SnakeGame/Network.cs b/SnakeGame/Network.cs index 474dfe3..2c19097 100644 --- a/SnakeGame/Network.cs +++ b/SnakeGame/Network.cs @@ -93,82 +93,12 @@ namespace SnakeGame } public static List Matches = new List(); public static NetMatch ConnectedMatch { get; private set; } - public static void DownloadGameList() - { - using (var client = new WebClient()) - { - var values = new NameValueCollection(); - values["client"] = "cheesecrescent"; //Sajtoskifli - - try - { - var response = client.UploadValues("http://snakegame.16mb.com", values); - - Matches.Clear(); - var responseString = Encoding.Default.GetString(response); - string[] responses = responseString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); - int x = 0; - while (x < responses.Length) - { - var match = new NetMatch(); - match.Name = responses[x]; - x++; - match.OwnerName = responses[x]; - x++; - if (!Int32.TryParse(responses[x], out match.MaxPlayers)) - MessageBox.Show("Error! The received text is in wrong format."); - x++; - int players; - if (!Int32.TryParse(responses[x], out players)) - MessageBox.Show("Error! The received text is in wrong format."); - x++; - for (int i = x; i < x + players; i++) - match.Players.Add(new Player(responses[i])); - x += players; - //match.OwnerIP = IPAddress.Parse(responses[x]); - match.OwnerIP = responses[x].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(entry => IPAddress.Parse(entry)).ToArray(); - x++; - Matches.Add(match); - } - } - catch (WebException) { } - catch (Exception e) - { - Program.HandleException(e); - } - } - } public static void CreateGame(NetMatch match) { - //MessageBox.Show("Create game: " + match.Name + " (" + match.MaxPlayers + " players)"); - using (var client = new WebClient()) - { - var values = new NameValueCollection(); - values["client"] = Game.Player.Name; - values["name"] = match.Name; - values["maxplayers"] = match.MaxPlayers.ToString(); - values["action"] = "create"; - - /*IPAddress[] ip = GetIPs(); - if (ip == null) - return;*/ - values["ip"] = ""; - Array.ForEach(match.OwnerIP, new Action(entry => values["ip"] += entry.ToString() + ";")); - - var response = client.UploadValues("http://snakegame.16mb.com", values); - - var responseString = Encoding.Default.GetString(response); - if (responseString != "OK") - MessageBox.Show("Error!\n" + responseString); - else - { - Join(match, true); - } - } + Join(match, true); } public static void Connect(NetMatch match) { - //MessageBox.Show("Connect to game: " + match.Name + " (" + match.MaxPlayers + " players)"); Join(match, false); } public static void Join(NetMatch match, bool server) @@ -183,31 +113,11 @@ namespace SnakeGame { if (ConnectedMatch == null) return; - SendUpdate = false; SyncUpdate(NetUpdateType.Leave, null); + SendUpdate = false; if (ConnectedMatch.OwnerName == Game.Player.Name) { - using (var client = new WebClient()) - { - var values = new NameValueCollection(); - values["client"] = Game.Player.Name; - values["name"] = ConnectedMatch.Name; - values["maxplayers"] = ConnectedMatch.MaxPlayers.ToString(); - values["action"] = "remove"; - - /*IPAddress[] ip = GetIPs(); - if (ip == null) - return;*/ - values["ip"] = ""; - Array.ForEach(ConnectedMatch.OwnerIP, new Action(entry => values["ip"] += entry.ToString() + ";")); - - var response = client.UploadValues("http://snakegame.16mb.com", values); - - var responseString = Encoding.Default.GetString(response); - if (responseString != "OK") - MessageBox.Show("Error!\n" + responseString); - } - Listener.Stop(); + Listener.Stop(); } ReceiverThread.Abort(); if (StopEventPerPlayer != null) @@ -289,6 +199,7 @@ namespace SnakeGame break; case NetUpdateType.Teleport: player.Position = new Point(br.ReadInt32(), br.ReadInt32()); + Console.WriteLine("Teleporting player " + player.Name + " to point " + player.Position); Game.MovePlayerPost(player, player.Position); foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype)) { @@ -323,6 +234,10 @@ namespace SnakeGame { return false; } + catch(ObjectDisposedException) + { + return false; + } catch (Exception e) { Program.HandleException(e);