2015.08.29

This commit is contained in:
Norbi Peti 2017-01-07 23:24:42 +01:00
parent 170c174bdf
commit 6cd585a6cb
6 changed files with 43 additions and 127 deletions

View file

@ -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);
/// <summary>
/// Opposite of Form1.TimerEnabled
/// TODO: Send pause/resume event
/// (View -> Task List -> Comments
/// </summary>
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<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 matches = Network.Matches.Combine<NetMatch, string>((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<string>(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)

View file

@ -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);

View file

@ -12,11 +12,6 @@ namespace SnakeGame
{
public string Name = "";
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();
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;
}
}

View file

@ -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);

View file

@ -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;

View file

@ -93,82 +93,12 @@ namespace SnakeGame
}
public static List<NetMatch> Matches = new List<NetMatch>();
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<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)
{
//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<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();
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);