2015.08.29
This commit is contained in:
parent
170c174bdf
commit
6cd585a6cb
6 changed files with 43 additions and 127 deletions
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -13,7 +14,7 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
public static Point GameSize;
|
public static Point GameSize;
|
||||||
public static SqCoord[,] GameField;
|
public static SqCoord[,] GameField;
|
||||||
public static int Length = 4;
|
public static int Length;
|
||||||
public static int UpdateTime = 800;
|
public static int UpdateTime = 800;
|
||||||
public static Direction MoveDirection;
|
public static Direction MoveDirection;
|
||||||
public static Label ScoreLabel;
|
public static Label ScoreLabel;
|
||||||
|
@ -22,8 +23,6 @@ namespace SnakeGame
|
||||||
public static Player Player = new Player("Player", true);
|
public static Player Player = new Player("Player", true);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Opposite of Form1.TimerEnabled
|
/// Opposite of Form1.TimerEnabled
|
||||||
/// TODO: Send pause/resume event
|
|
||||||
/// (View -> Task List -> Comments
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool Paused
|
public static bool Paused
|
||||||
{
|
{
|
||||||
|
@ -90,16 +89,17 @@ namespace SnakeGame
|
||||||
MSGBox.ShowMSGBox("Please change your username from default.", "", MSGBoxType.Text);
|
MSGBox.ShowMSGBox("Please change your username from default.", "", MSGBoxType.Text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Network.DownloadGameList();
|
|
||||||
string matches = Network.Matches.Combine<NetMatch, string>((match, combiner) => combiner += match.Name + " " + match.Players.Count + "/" + match.MaxPlayers + " " + match.OwnerName + "\n");
|
//string matches = Network.Matches.Combine<NetMatch, string>((match, combiner) => combiner += match.Name + " " + match.Players.Count + "/" + match.MaxPlayers + " " + match.OwnerName + "\n");
|
||||||
MSGBox.ShowMSGBox("Connect to game", matches, MSGBoxType.List, new EventHandler<string>(delegate(object s, string input)
|
string inputs = "IP:\n";
|
||||||
|
MSGBox.ShowMSGBox("Connect to game", inputs, MSGBoxType.MultipleInput, new EventHandler<string>(delegate(object s, string input)
|
||||||
{
|
{
|
||||||
int num = 0;
|
IPAddress address;
|
||||||
if (int.TryParse(input, out num) && num != -1)
|
if (IPAddress.TryParse(input.Replace("\n", ""), out address))
|
||||||
{
|
{
|
||||||
Game.Reset();
|
Game.Reset();
|
||||||
Network.Connect(Network.Matches[num]);
|
Network.Connect(new NetMatch { OwnerIP = new IPAddress[] { address } });
|
||||||
Game.Reset(false);
|
//Game.Reset(false); - On successfull connect event
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Game.Paused = true;
|
Game.Paused = true;
|
||||||
|
@ -151,7 +151,7 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
Player.Score = 0;
|
Player.Score = 0;
|
||||||
Player.Lives = 3;
|
Player.Lives = 3;
|
||||||
UpdateTime = 800;
|
UpdateTime = 600;
|
||||||
Length = 4;
|
Length = 4;
|
||||||
}
|
}
|
||||||
Network.SyncUpdate(NetUpdateType.Teleport, Player.Position);
|
Network.SyncUpdate(NetUpdateType.Teleport, Player.Position);
|
||||||
|
@ -209,7 +209,8 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LivesLabel.ForeColor = Color.White;
|
//LivesLabel.ForeColor = Color.White;
|
||||||
|
LivesLabel.ForeColor = Game.Player.Color;
|
||||||
if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point)
|
if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point)
|
||||||
{
|
{
|
||||||
Player.Score += 1000;
|
Player.Score += 1000;
|
||||||
|
@ -217,7 +218,8 @@ namespace SnakeGame
|
||||||
Game.AddPoint();
|
Game.AddPoint();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ScoreLabel.ForeColor = Color.White;
|
//ScoreLabel.ForeColor = Color.White;
|
||||||
|
ScoreLabel.ForeColor = Game.Player.Color;
|
||||||
if (Player.Score > 0)
|
if (Player.Score > 0)
|
||||||
Player.Score -= new Random().Next(1, 20);
|
Player.Score -= new Random().Next(1, 20);
|
||||||
MovePlayerPost(Player, nextcoord);
|
MovePlayerPost(Player, nextcoord);
|
||||||
|
@ -242,6 +244,7 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
MSGBox.ShowMSGBox("Game over!", "", MSGBoxType.Text);
|
MSGBox.ShowMSGBox("Game over!", "", MSGBoxType.Text);
|
||||||
Game.Paused = true;
|
Game.Paused = true;
|
||||||
|
Network.Leave(); //2015.08.29.
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Point MovePlayerPre(Player player, Direction direction)
|
public static Point MovePlayerPre(Player player, Direction direction)
|
||||||
|
|
|
@ -38,13 +38,17 @@ namespace SnakeGame
|
||||||
if (Network.ConnectedMatch == null)
|
if (Network.ConnectedMatch == null)
|
||||||
RenderSquare(new Point { X = i, Y = j }, Color.LimeGreen);
|
RenderSquare(new Point { X = i, Y = j }, Color.LimeGreen);
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if (Game.GameField[i, j].PlayerName == Game.Player.Name)
|
||||||
|
RenderSquare(new Point { X = i, Y = j }, Game.Player.Color);
|
||||||
|
else
|
||||||
{
|
{
|
||||||
Player player = Network.ConnectedMatch.GetPlayerByName(Game.GameField[i, j].PlayerName);
|
Player player = Network.ConnectedMatch.GetPlayerByName(Game.GameField[i, j].PlayerName);
|
||||||
if (player != null)
|
if (player != null)
|
||||||
RenderSquare(new Point { X = i, Y = j }, player.Color);
|
RenderSquare(new Point { X = i, Y = j }, player.Color);
|
||||||
else
|
else
|
||||||
RenderSquare(new Point { X = i, Y = j }, Color.DarkGray);
|
RenderSquare(new Point { X = i, Y = j }, Color.DarkGray);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (Game.GameField[i, j].Type == SquareType.Point)
|
else if (Game.GameField[i, j].Type == SquareType.Point)
|
||||||
RenderSquare(new Point { X = i, Y = j }, Color.Blue);
|
RenderSquare(new Point { X = i, Y = j }, Color.Blue);
|
||||||
|
|
|
@ -12,11 +12,6 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
public string Name = "";
|
public string Name = "";
|
||||||
public int MaxPlayers = 0;
|
public int MaxPlayers = 0;
|
||||||
//public List<string> PlayerNames = new List<string>();
|
|
||||||
/// <summary>
|
|
||||||
/// TODO: Send player join/leave to master server
|
|
||||||
/// </summary>
|
|
||||||
//public List<Player> Players = new List<Player>();
|
|
||||||
public PlayerCollection Players = new PlayerCollection();
|
public PlayerCollection Players = new PlayerCollection();
|
||||||
|
|
||||||
private string ownername = "";
|
private string ownername = "";
|
||||||
|
@ -42,17 +37,7 @@ namespace SnakeGame
|
||||||
public IPAddress[] OwnerIP;
|
public IPAddress[] OwnerIP;
|
||||||
public Player GetPlayerByName(string name)
|
public Player GetPlayerByName(string name)
|
||||||
{
|
{
|
||||||
/*try
|
return Players.FirstOrDefault(entry => entry.Name == name);
|
||||||
{
|
|
||||||
//return Players.Single(entry => entry.ID == id);
|
|
||||||
return Players.Single(entry => entry.Name == name);
|
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}*/
|
|
||||||
return Players.SingleOrDefault(entry => entry.Name == name);
|
|
||||||
}
|
|
||||||
//public int NextID = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
private static void ClientListenerThreadRun()
|
private static void ClientListenerThreadRun()
|
||||||
{
|
{
|
||||||
|
//TODO: Game over disconnect
|
||||||
|
//TODO: Synchronised point placing on respawn
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Connect to the server
|
//Connect to the server
|
||||||
|
@ -39,6 +41,7 @@ namespace SnakeGame
|
||||||
Game.GameSize = new Point((int)readdata["GameSize"]["X"], (int)readdata["GameSize"]["Y"]);
|
Game.GameSize = new Point((int)readdata["GameSize"]["X"], (int)readdata["GameSize"]["Y"]);
|
||||||
Game.GameField = new SqCoord[Game.GameSize.X, Game.GameSize.Y];
|
Game.GameField = new SqCoord[Game.GameSize.X, Game.GameSize.Y];
|
||||||
Game.Length = (int)readdata["Length"];
|
Game.Length = (int)readdata["Length"];
|
||||||
|
ConnectedMatch.OwnerName = (string)readdata["OwnerName"]; //2015.08.29.
|
||||||
for (int i = 0; i < Game.GameSize.X; i++)
|
for (int i = 0; i < Game.GameSize.X; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < Game.GameSize.Y; j++)
|
for (int j = 0; j < Game.GameSize.Y; j++)
|
||||||
|
@ -57,6 +60,7 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
Game.Paused = false;
|
Game.Paused = false;
|
||||||
SendUpdate = true;
|
SendUpdate = true;
|
||||||
|
Game.Reset(false);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
string playername = sr.ReadString();
|
string playername = sr.ReadString();
|
||||||
|
@ -71,6 +75,10 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch(ObjectDisposedException)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Program.HandleException(e);
|
Program.HandleException(e);
|
||||||
|
|
|
@ -78,6 +78,7 @@ namespace SnakeGame
|
||||||
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;
|
||||||
|
senddata["OwnerName"] = Game.Player.Name; //2015.08.29.
|
||||||
senddata["GameSize"] = new JObject();
|
senddata["GameSize"] = new JObject();
|
||||||
senddata["GameSize"]["X"] = Game.GameSize.X;
|
senddata["GameSize"]["X"] = Game.GameSize.X;
|
||||||
senddata["GameSize"]["Y"] = Game.GameSize.Y;
|
senddata["GameSize"]["Y"] = Game.GameSize.Y;
|
||||||
|
|
|
@ -93,82 +93,12 @@ namespace SnakeGame
|
||||||
}
|
}
|
||||||
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()
|
|
||||||
{
|
|
||||||
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)
|
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<IPAddress>(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)
|
public static void Connect(NetMatch match)
|
||||||
{
|
{
|
||||||
//MessageBox.Show("Connect to game: " + match.Name + " (" + match.MaxPlayers + " players)");
|
|
||||||
Join(match, false);
|
Join(match, false);
|
||||||
}
|
}
|
||||||
public static void Join(NetMatch match, bool server)
|
public static void Join(NetMatch match, bool server)
|
||||||
|
@ -183,30 +113,10 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
if (ConnectedMatch == null)
|
if (ConnectedMatch == null)
|
||||||
return;
|
return;
|
||||||
SendUpdate = false;
|
|
||||||
SyncUpdate(NetUpdateType.Leave, null);
|
SyncUpdate(NetUpdateType.Leave, null);
|
||||||
|
SendUpdate = false;
|
||||||
if (ConnectedMatch.OwnerName == Game.Player.Name)
|
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<IPAddress>(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();
|
ReceiverThread.Abort();
|
||||||
|
@ -289,6 +199,7 @@ namespace SnakeGame
|
||||||
break;
|
break;
|
||||||
case NetUpdateType.Teleport:
|
case NetUpdateType.Teleport:
|
||||||
player.Position = new Point(br.ReadInt32(), br.ReadInt32());
|
player.Position = new Point(br.ReadInt32(), br.ReadInt32());
|
||||||
|
Console.WriteLine("Teleporting player " + player.Name + " to point " + player.Position);
|
||||||
Game.MovePlayerPost(player, player.Position);
|
Game.MovePlayerPost(player, player.Position);
|
||||||
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
foreach (BinaryWriter bw in ForwardMessage(player, playername, (int)updatetype))
|
||||||
{
|
{
|
||||||
|
@ -323,6 +234,10 @@ namespace SnakeGame
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
catch(ObjectDisposedException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Program.HandleException(e);
|
Program.HandleException(e);
|
||||||
|
|
Loading…
Reference in a new issue