1
0
Fork 0

Separation des projets

This commit is contained in:
sheychen 2016-10-17 13:09:45 +02:00
parent 8eb1f0742b
commit 8d433a6a08
9 changed files with 562 additions and 416 deletions

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Galactic_Colors_Control_Console</RootNamespace>
<AssemblyName>Galactic Colors Control Console</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Galactic Colors Control\Galactic Colors Control.csproj">
<Project>{93582ce8-c8c8-4e19-908b-d671ecbade25}</Project>
<Name>Galactic Colors Control</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,98 @@
using System;
using Galactic_Colors_Control;
using System.Threading;
using System.Reflection;
namespace Galactic_Colors_Control_Console
{
internal class Program
{
private static Client client;
private static bool run = true;
private static Thread Writer;
private static void Main()
{
client = new Client();
Console.Title = "Galactic Colors Control Client";
Console.Write(">");
Write("Galactic Colors Control Client");
Write("Console " + Assembly.GetEntryAssembly().GetName().Version.ToString());
bool hostSet = false;
while(!hostSet)
{
Write("Enter server host:");
string host = client.ValidateHost(Console.ReadLine());
if(host == null)
{
foreach (string output in client.Output.ToArray())
{
Write(output);
}
client.Output.Clear();
client.ResetHost();
}
else
{
Write("Use " + host + "? y/n");
ConsoleKeyInfo c = new ConsoleKeyInfo();
while(c.Key != ConsoleKey.Y && c.Key != ConsoleKey.N)
{
c = Console.ReadKey();
}
if(c.Key == ConsoleKey.Y)
{
hostSet = true;
}
else
{
client.ResetHost();
}
}
}
if (client.ConnectHost())
{
run = true;
Writer = new Thread(OutputWriter);
Writer.Start();
while (run)
{
client.SendRequest(Console.ReadLine());
if (!client.isRunning) { run = false; }
}
Writer.Join();
Console.Read();
}
else
{
foreach (string output in client.Output.ToArray())
{
Write(output);
}
client.Output.Clear();
Console.Read();
}
}
private static void OutputWriter()
{
while (run || client.Output.Count > 0)
{
if (client.Output.Count > 0)
{
string text = client.Output[0];
Write(text);
client.Output.Remove(text);
}
Thread.Sleep(200);
}
}
private static void Write( string text)
{
Console.Write("\b");
Console.WriteLine(text);
Console.Write(">");
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Les informations générales relatives à un assembly dépendent de
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
// associées à un assembly.
[assembly: AssemblyTitle("Galactic Colors Control Console")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("sheychen")]
[assembly: AssemblyProduct("Galactic Colors Control Console")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
[assembly: ComVisible(false)]
// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
[assembly: Guid("5d6a09d1-dcab-4fd8-b4e6-62d9f41ae8f0")]
// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
//
// Version principale
// Version secondaire
// Numéro de build
// Révision
//
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.1")]
[assembly: AssemblyFileVersion("1.0.1.1")]

View File

@ -110,6 +110,12 @@
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Galactic Colors Control\Galactic Colors Control.csproj">
<Project>{93582ce8-c8c8-4e19-908b-d671ecbade25}</Project>
<Name>Galactic Colors Control</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -1,19 +1,15 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System;
using MyMonoGame.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using Galactic_Colors_Control;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Microsoft.Xna.Framework.Input;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace Galactic_Colors_Control_GUI
{
@ -42,34 +38,32 @@ namespace Galactic_Colors_Control_GUI
private Texture2D[] pointerSprites = new Texture2D[1];
private boxSprites[] buttonsSprites = new boxSprites[1];
private List<String> chat = new List<string>();
private Client client;
private Manager GUI = new Manager();
private string skinName;
private bool isFullScren = false;
private Manager GUI = new Manager();
private enum dataType { message, data };
Version version;
private enum GameStatus { Home, Connect, Connection, Options, Game, Pause, End, Thanks, Exit, Error}
private enum GameStatus { Home, Connect, Options, Game, Pause, End, Thanks,
Title,
Indentification,
Kick
}
private GameStatus gameStatus = GameStatus.Home;
private int ScreenWidth = 1280;
private int ScreenHeight = 720;
private string addressText;
private static Socket ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private string username = null;
private static int PORT = 0;
private static string IP = null;
private string username;
private Thread ReceiveThread;
private static bool _run = true;
private string errorText;
private int _errorCount = 0;
private static Thread Writer;
private bool showOKMessage = false;
private string messageTitle;
private List<string> messageText = new List<string>();
private bool showYNMessage = false;
private bool showLoading = false;
private bool showChat = true;
private string chatText;
public Game1()
{
@ -95,7 +89,6 @@ namespace Galactic_Colors_Control_GUI
/// </summary>
protected override void Initialize()
{
version = Assembly.GetEntryAssembly().GetName().Version;
nullSprite = new Texture2D(GraphicsDevice, 1, 1);
nullSprite.SetData(new Color[1 * 1] { Color.White });
@ -206,13 +199,15 @@ namespace Galactic_Colors_Control_GUI
switch (gameStatus)
{
case GameStatus.Home:
case GameStatus.Title:
case GameStatus.Connect:
case GameStatus.Indentification:
backgroundX[0] -= 1 * acceleratorX;
backgroundX[1] -= 2 * acceleratorX;
break;
case GameStatus.Connect:
backgroundX[0] -= 1 * acceleratorX;
backgroundX[1] -= 2 * acceleratorX;
case GameStatus.Game:
if (!client.isRunning) { gameStatus = GameStatus.Kick; }
break;
}
@ -235,67 +230,157 @@ namespace Galactic_Colors_Control_GUI
switch (gameStatus)
{
case GameStatus.Title:
DrawBackground(0);
DrawBackground(1);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 2), "Galactic Colors Control", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
break;
case GameStatus.Home:
DrawBackground(0);
DrawBackground(1);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Galactic Colors Control", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 40), "GUI " + Assembly.GetEntryAssembly().GetName().Version.ToString(), basicFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
if (GUI.Button(new Rectangle(ScreenWidth - 64, ScreenHeight - 74,64,64), logoSprite)) { System.Diagnostics.Process.Start("https://sheychen.shost.ca/"); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), buttonsSprites[0], "Connect", basicFont, new MyMonoGame.Colors(Color.White, Color.Green))) { new Thread(ChangeTo).Start(GameStatus.Connect); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Options", basicFont, new MyMonoGame.Colors(Color.White, Color.Blue))) { GUI.ResetFocus(); gameStatus = GameStatus.Options; }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Exit", basicFont, new MyMonoGame.Colors(Color.White, Color.Red))) { new Thread(ChangeTo).Start(GameStatus.Exit); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), buttonsSprites[0], "Play", basicFont, new MyMonoGame.Colors(Color.White, Color.Green))) {
GUI.ResetFocus();
client = new Client();
new Thread(() => {
while (acceleratorX < 5)
{
Thread.Sleep(20);
acceleratorX += 0.1d;
}
gameStatus = GameStatus.Connect;
}).Start();
}
//if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Options", basicFont, new MyMonoGame.Colors(Color.White, Color.Blue))) {
// GUI.ResetFocus();
// gameStatus = GameStatus.Options;
//}
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Exit", basicFont, new MyMonoGame.Colors(Color.White, Color.Red))) {
GUI.ResetFocus();
gameStatus = GameStatus.Title;
new Thread(() => {
while (acceleratorX > 0)
{
Thread.Sleep(10);
acceleratorX -= 0.01d;
}
Exit();
}).Start();
}
break;
case GameStatus.Connect:
DrawBackground(0);
DrawBackground(1);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Connnect", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
if (GUI.TextField(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), ref addressText, basicFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter, "Server address")) { new Thread(ChangeTo).Start(GameStatus.Connection); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Connection", basicFont)) { new Thread(ChangeTo).Start(GameStatus.Connection); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Back", basicFont, new MyMonoGame.Colors(Color.White, Color.Red))) { new Thread(ChangeTo).Start(GameStatus.Home); }
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Galactic Colors Control", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
if (showLoading)
{
GUI.Box(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 50), buttonsSprites[0]);
GUI.Label(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 50), "Loading", basicFont);
}
else
{
if (showOKMessage)
{
GUI.Box(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 150), buttonsSprites[0]);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 60), messageTitle, basicFont, null, Manager.textAlign.bottomCenter);
DrawList(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 100), messageText, smallFont, null, Manager.textAlign.bottomCenter);
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 140, ScreenHeight / 4 + 150, 280, 40), buttonsSprites[0], "Ok", basicFont)) { GUI.ResetFocus(); showOKMessage = false; }
}
else {
if (showYNMessage)
{
GUI.Box(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 100), buttonsSprites[0]);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 60), messageTitle, basicFont, null, Manager.textAlign.bottomCenter);
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 140, ScreenHeight / 4 + 100, 135, 40), buttonsSprites[0], "Yes", basicFont))
{
GUI.ResetFocus();
new Thread(ConnectHost).Start();
showYNMessage = false;
}
if (GUI.Button(new Rectangle(ScreenWidth / 2 + 5, ScreenHeight / 4 + 100, 135, 40), buttonsSprites[0], "No", basicFont))
{
client.Output.Clear();
client.ResetHost();
GUI.ResetFocus();
showYNMessage = false;
}
}
else {
if (GUI.TextField(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), ref username, basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White), Manager.textAlign.centerCenter, "Server address")) { new Thread(ValidateHost).Start(); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Connect", basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White))) { new Thread(ValidateHost).Start(); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Back", basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White)))
{
GUI.ResetFocus();
new Thread(() =>
{
while (acceleratorX > 1)
{
Thread.Sleep(20);
acceleratorX -= 0.1d;
}
gameStatus = GameStatus.Home;
username = null;
}).Start();
}
}
}
}
break;
case GameStatus.Connection:
case GameStatus.Indentification:
DrawBackground(0);
DrawBackground(1);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Connnection", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
GUI.Label(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 4 + 30, 150, 40), addressText, basicFont, new MyMonoGame.Colors(Color.White));
if (GUI.TextField(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), ref username, basicFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter, "Username")) { if (username != null) { new Thread(ChangeTo).Start(GameStatus.Game); } }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Connection", basicFont)) { if (username != null) { new Thread(ChangeTo).Start(GameStatus.Game); } }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Back", basicFont, new MyMonoGame.Colors(Color.White, Color.Red))) { _run = false; ReceiveThread.Join(); new Thread(ChangeTo).Start(GameStatus.Home); }
break;
case GameStatus.Error:
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Error", titleFont, null, Manager.textAlign.centerCenter);
GUI.Label(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 4 + 30, 150, 40), errorText, basicFont);
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Exit", basicFont, new MyMonoGame.Colors(Color.White, Color.Red))) { _run = false; ReceiveThread.Join(); new Thread(ChangeTo).Start(GameStatus.Exit); }
break;
case GameStatus.Options:
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4), "Galactic Colors Control", titleFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.centerCenter);
if (showLoading)
{
GUI.Box(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 50), buttonsSprites[0]);
GUI.Label(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 50), "Loading", basicFont);
}
else
{
if (showOKMessage)
{
GUI.Box(new Rectangle(ScreenWidth / 2 - 150, ScreenHeight / 4 + 50, 300, 150), buttonsSprites[0]);
GUI.Label(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 60), messageTitle, basicFont, null, Manager.textAlign.bottomCenter);
DrawList(new MyMonoGame.Vector(ScreenWidth / 2, ScreenHeight / 4 + 100), messageText, smallFont, null, Manager.textAlign.bottomCenter);
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 140, ScreenHeight / 4 + 150, 280, 40), buttonsSprites[0], "Ok", basicFont)) { GUI.ResetFocus(); showOKMessage = false; }
}
else {
if (GUI.TextField(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 - 30, 150, 40), ref username, basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White), Manager.textAlign.centerCenter, "Username")) { new Thread(IdentifiacateHost).Start(); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 20, 150, 40), buttonsSprites[0], "Validate", basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White))) { new Thread(IdentifiacateHost).Start(); }
if (GUI.Button(new Rectangle(ScreenWidth / 2 - 75, ScreenHeight / 2 + 70, 150, 40), buttonsSprites[0], "Back", basicFont, new MyMonoGame.Colors(Color.LightGray, Color.White)))
{
GUI.ResetFocus();
new Thread(() =>
{
while (acceleratorX > 1)
{
Thread.Sleep(20);
acceleratorX -= 0.1d;
}
gameStatus = GameStatus.Home;
username = null;
}).Start();
}
}
}
break;
case GameStatus.Game:
DrawBackground(0);
DrawBackground(1);
int i = 1;
foreach(string ligne in chat.ToArray().Reverse())
if (showChat)
{
GUI.Label(new MyMonoGame.Vector(10, ScreenHeight - 12 * i), ligne, smallFont, new MyMonoGame.Colors(Color.White), Manager.textAlign.topRight);
i++;
GUI.Box(new Rectangle(5, 5, 310, 310), buttonsSprites[0]);
if(GUI.TextField(new Rectangle(10,10,300,20), ref chatText, basicFont, null, Manager.textAlign.centerLeft, "Enter message")) { if(messageText != null) { client.SendRequest(chatText); chatText = null; } }
DrawList(new MyMonoGame.Vector(10, 30), client.Output, smallFont, null, Manager.textAlign.bottomRight);
}
break;
case GameStatus.Pause:
break;
case GameStatus.End:
break;
case GameStatus.Thanks:
break;
}
Color ActiveColor = IsActive ? Color.Green : Color.Red;
@ -307,130 +392,90 @@ namespace Galactic_Colors_Control_GUI
base.Draw(gameTime);
}
private void ChangeTo( object target )
private void ValidateHost()
{
GUI.ResetFocus();
switch ((GameStatus)target)
showLoading = true;
if ( username == null) { username = ""; }
string Host = client.ValidateHost(username);
if (Host == null)
{
case GameStatus.Home:
if(gameStatus == GameStatus.Connect)
{
while (acceleratorX > 1)
{
Thread.Sleep(20);
acceleratorX -= 0.1d;
}
}
if (gameStatus == GameStatus.Connection || gameStatus == GameStatus.Error)
{
ClientSocket.Close();
ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
while (acceleratorX > 1)
{
Thread.Sleep(20);
acceleratorX -= 0.1d;
}
}
break;
case GameStatus.Connect:
addressText = null;
while (acceleratorX < 5)
{
Thread.Sleep(20);
acceleratorX += 0.1d;
}
break;
case GameStatus.Connection:
if(addressText == null) { addressText = ""; }
string text = addressText;
string[] parts = text.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
parts = new string[] { "" };
PORT = 25001;
}
else
{
if (parts.Length > 1)
{
if (!int.TryParse(parts[1], out PORT)) { PORT = 0; }
if (PORT < 0 || PORT > 65535) { PORT = 0; }
}
else
{
PORT = 25001;
}
}
if (PORT != 0)
{
try
{
IPHostEntry ipHostEntry = Dns.GetHostEntry(parts[0]);
IPAddress host = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
IP = host.ToString();
}
catch (Exception e)
{
addressText = e.Message;
PORT = 0;
Thread.CurrentThread.Abort();
}
}
else
{
addressText = "Incorrect port";
Thread.CurrentThread.Abort();
}
if(IP != null)
{
int attempts = 0;
while (!ClientSocket.Connected && attempts < 5)
{
try
{
attempts++;
addressText = "Connection attempt " + attempts;
ClientSocket.Connect(IP, PORT);
}
catch (SocketException)
{
addressText = "Error";
}
}
if (attempts < 5)
{
addressText = "Connected to " + IP.ToString();
_run = true;
chat.Clear();
ReceiveThread = new Thread(ReceiveLoop);
ReceiveThread.Start();
}
else
{
addressText = "Can't connected to " + IP.ToString();
Thread.CurrentThread.Abort();
}
}
break;
case GameStatus.Game:
Send("/connect " + username, dataType.message);
break;
case GameStatus.Exit:
while (acceleratorX > 0)
{
Thread.Sleep(20);
acceleratorX -= 0.1d;
}
Thread.Sleep(500);
Exit();
break;
messageTitle = "Error";
messageText.Clear();
foreach(string line in client.Output.ToArray()) { messageText.Add(line); }
showOKMessage = true;
client.Output.Clear();
client.ResetHost();;
}
gameStatus = (GameStatus)target;
else
{
messageTitle = "Use " + Host + "?";
showYNMessage = true;
}
showLoading = false;
}
private void ConnectHost()
{
showLoading = true;
if (client.ConnectHost())
{
gameStatus = GameStatus.Indentification;
}
else
{
messageTitle = "Error";
messageText.Clear();
foreach (string line in client.Output.ToArray()) { messageText.Add(line); }
showOKMessage = true;
client.Output.Clear();
client.ResetHost();
}
showLoading = false;
}
private void IdentifiacateHost()
{
showLoading = true;
if (username != null)
{
if(username.Length > 3)
{
client.Output.Clear();
client.SendRequest("/connect " + username);
bool wait = true;
while (wait)
{
if (client.Output.Count > 0)
{
wait = false;
}
}
if(client.Output.Contains("Identifiaction succes"))
{
gameStatus = GameStatus.Game;
}
else
{
messageTitle = "Error";
messageText.Clear();
foreach (string line in client.Output.ToArray()) { messageText.Add(line); }
showOKMessage = true;
showLoading = false;
client.Output.Clear();
}
}
}
showLoading = false;
}
private void DrawList(MyMonoGame.Vector vector, List<string> list, SpriteFont font, MyMonoGame.Colors colors, Manager.textAlign align)
{
string text = "";
foreach(string line in list)
{
text += (line + Environment.NewLine);
}
spriteBatch.DrawString(font, text, new Vector2(vector.X, vector.Y), Color.Black);
}
private void DrawBackground(int index)
@ -447,113 +492,5 @@ namespace Galactic_Colors_Control_GUI
}
}
}
private void ReceiveLoop()
{
while (_run)
{
var buffer = new byte[2048];
int received = 0;
try
{
received = ClientSocket.Receive(buffer, SocketFlags.None);
}
catch
{
errorText = "Server Timeout";
new Thread(ChangeTo).Start(GameStatus.Error);
}
if (received == 0) return;
_errorCount = 0;
var data = new byte[received];
Array.Copy(buffer, data, received);
byte[] type = new byte[4];
type = data.Take(4).ToArray();
type.Reverse();
dataType dtype = (dataType)BitConverter.ToInt32(type, 0);
byte[] bytes = null;
bytes = data.Skip(4).ToArray();
switch (dtype)
{
case dataType.message:
string text = Encoding.ASCII.GetString(bytes);
if (text[0] == '/')
{
text = text.Substring(1);
text = text.ToLower();
string[] array = text.Split(new char[1] { ' ' }, 4, StringSplitOptions.RemoveEmptyEntries);
switch (array[0])
{
case "kick":
if (array.Length > 1)
{
errorText = "Kick : " + array[1];
new Thread(ChangeTo).Start(GameStatus.Error);
}
else
{
errorText = "Kick by server";
new Thread(ChangeTo).Start(GameStatus.Error);
}
_run = false;
break;
default:
chat.Add("Unknown action from server");
break;
}
}
else
{
chat.Add(text);
}
break;
case dataType.data:
chat.Add("data");
break;
}
Thread.Sleep(200);
}
}
private void Send(object data, dataType dtype)
{
byte[] type = new byte[4];
type = BitConverter.GetBytes((int)dtype);
byte[] bytes = null;
switch (dtype)
{
case dataType.message:
bytes = Encoding.ASCII.GetBytes((string)data);
break;
case dataType.data:
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, data);
bytes = ms.ToArray();
}
break;
}
byte[] final = new byte[type.Length + bytes.Length];
type.CopyTo(final, 0);
bytes.CopyTo(final, type.Length);
try
{
ClientSocket.Send(final);
}
catch
{
chat.Add("Can't contact server : " + _errorCount);
_errorCount++;
}
if (_errorCount >= 5)
{
errorText = "Can't contact server";
new Thread(ChangeTo).Start(GameStatus.Error);
}
}
}
}

View File

@ -30,13 +30,13 @@ namespace Galactic_Colors_Control_Server.Commands
Program.clients[soc].status = 0;
//args[1] = args[1][0].ToString().ToUpper()[0] + args[1].Substring(1);
Program.clients[soc].pseudo = args[1];
Utilities.Send(soc, "Identified as " + args[1], Utilities.dataType.message);
Utilities.Send(soc, "/connected", Utilities.dataType.message);
Utilities.Broadcast(args[1] + " joined the server", Utilities.dataType.message);
Logger.Write("Identified as " + Utilities.GetName(soc) + " form " + ((IPEndPoint)soc.LocalEndPoint).Address.ToString(), Logger.logType.info);
}
else
{
Utilities.Send(soc, "/kick username_allready_taken", Utilities.dataType.message);
Utilities.Send(soc, "/allreadytaken", Utilities.dataType.message);
}
}
else

View File

@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Galactic Colors Control GUI
{93582CE8-C8C8-4E19-908B-D671ECBADE25} = {93582CE8-C8C8-4E19-908B-D671ECBADE25}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Galactic Colors Control Console", "Galactic Colors Control Console\Galactic Colors Control Console.csproj", "{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -44,6 +46,14 @@ Global
{F6CDDF1B-5A57-4A84-B57C-749FFF9AE031}.Release|Any CPU.Build.0 = Release|x86
{F6CDDF1B-5A57-4A84-B57C-749FFF9AE031}.Release|x86.ActiveCfg = Release|x86
{F6CDDF1B-5A57-4A84-B57C-749FFF9AE031}.Release|x86.Build.0 = Release|x86
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Debug|x86.Build.0 = Debug|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Release|Any CPU.Build.0 = Release|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Release|x86.ActiveCfg = Release|Any CPU
{5D6A09D1-DCAB-4FD8-B4E6-62D9F41AE8F0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{93582CE8-C8C8-4E19-908B-D671ECBADE25}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Galactic_Colors_Control</RootNamespace>
<AssemblyName>Galactic Colors Control</AssemblyName>
@ -49,7 +49,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Galactic_Colors_Control_Client.Program</StartupObject>
<StartupObject>
</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
@ -8,156 +9,146 @@ using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
namespace Galactic_Colors_Control_Client
namespace Galactic_Colors_Control
{
internal class Program
public class Client
{
private static readonly Socket ClientSocket = new Socket
private Socket ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static int PORT = 0;
private static int _errorCount = 0;
private static bool _run = true;
private static string IP = null;
public int PORT = 0;
private int _errorCount = 0;
private bool _run = true;
public string IP = null;
public bool isRunning { get { return _run; } }
private enum dataType { message, data };
private static void Main()
public List<string> Output = new List<string>();
private Thread RecieveThread;
public void ResetHost()
{
Console.Title = "Galactic Colors Control Client";
Console.Write(">");
ConnectToServer();
RequestLoop();
Exit();
IP = null;
PORT = 0;
}
private static void ConnectToServer()
public string ValidateHost(string text)
{
int attempts = 0;
while (IP == null)
if (text == null) { text = ""; }
string[] parts = text.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
ConsoleWrite(Environment.NewLine + "Enter server host:");
string text = Console.ReadLine();
string[] parts = text.Split(new char[] {':'}, 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
parts = new string[] { "" };
PORT = 25001;
}
else
{
if (parts.Length > 1)
{
if (!int.TryParse(parts[1], out PORT)) { PORT = 0; }
if (PORT < 0 || PORT > 65535) { PORT = 0; }
}
else
{
parts = new string[] { "" };
PORT = 25001;
}
else
}
if (PORT != 0)
{
try
{
if (parts.Length > 1)
{
if (!int.TryParse(parts[1], out PORT)) { PORT = 0; }
if (PORT < 0 || PORT > 65535) { PORT = 0; }
}
else
{
PORT = 25001;
}
IPHostEntry ipHostEntry = Dns.GetHostEntry(parts[0]);
IPAddress host = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
IP = host.ToString();
return IP + ":" + PORT;
}
if (PORT != 0)
catch (Exception e)
{
try
{
IPHostEntry ipHostEntry = Dns.GetHostEntry(parts[0]);
IPAddress host = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
ConsoleWrite("Use " + host.ToString() + ":" + PORT + "? y/n");
ConsoleKey key = ConsoleKey.NoName;
while (key != ConsoleKey.Y && key != ConsoleKey.N)
{
key = Console.ReadKey().Key;
}
if (key == ConsoleKey.Y)
{
IP = host.ToString();
}
else
{
PORT = 0;
}
}
catch (Exception e)
{
ConsoleWrite(e.Message);
PORT = 0;
}
}
else
{
ConsoleWrite("Incorrect port");
Output.Add(e.Message);
PORT = 0;
return null;
}
}
else
{
Output.Add("Incorrect port");
return null;
}
}
/// <summary>
/// Set IP and PORT before
/// </summary>
/// <returns>Connection succes</returns>
public bool ConnectHost()
{
int attempts = 0;
while (!ClientSocket.Connected && attempts < 5)
{
try
{
attempts++;
ConsoleWrite("Connection attempt " + attempts);
Output.Add("Connection attempt " + attempts);
ClientSocket.Connect(IP, PORT);
}
catch (SocketException)
{
Console.Clear();
Output.Clear();
}
}
if (attempts < 5)
{
Console.Clear();
ConsoleWrite("Connected to " + IP.ToString());
Output.Clear();
Output.Add("Connected to " + IP.ToString());
_run = true;
RecieveThread = new Thread(ReceiveLoop);
RecieveThread.Start();
return true;
}
else
{
Console.Clear();
ConsoleWrite("Can't connected to " + IP.ToString());
ClientSocket.Close();
ConsoleWrite("Press Enter to quit");
Console.Read();
Environment.Exit(0);
Output.Clear();
Output.Add("Can't connected to " + IP.ToString());
ResetSocket();
return false;
}
}
private static void RequestLoop()
private void ResetSocket()
{
Thread ReceiveThread = new Thread(new ThreadStart(ReceiveLoop));
ReceiveThread.Start();
while (_run)
{
SendRequest();
}
ReceiveThread.Join();
if (_errorCount >= 5)
{
ConsoleWrite("Exit: Too much network errors");
}
ClientSocket.Close();
ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
/// <summary>
/// Close socket and exit program.
/// </summary>
private static void Exit()
public void ExitHost()
{
Send("/exit", dataType.message); // Tell the server we are exiting
_run = false;
RecieveThread.Join();
ClientSocket.Shutdown(SocketShutdown.Both);
ClientSocket.Close();
ConsoleWrite("Bye");
Console.ReadLine();
Environment.Exit(0);
Output.Add("Bye");
ResetHost();
}
private static void SendRequest()
public void SendRequest(string request)
{
string request = Console.ReadLine();
switch (request.ToLower())
{
case "/exit":
Exit();
ExitHost();
break;
case "/ping":
Ping();
PingHost();
break;
default:
@ -166,7 +157,7 @@ namespace Galactic_Colors_Control_Client
}
}
private static void Ping()
private void PingHost()
{
Ping p = new Ping();
PingReply r;
@ -175,15 +166,15 @@ namespace Galactic_Colors_Control_Client
if (r.Status == IPStatus.Success)
{
Console.WriteLine(r.RoundtripTime.ToString() + " ms.");
Output.Add(r.RoundtripTime.ToString() + " ms.");
}
else
{
Console.WriteLine("Time out");
Output.Add("Time out");
}
}
private static void Send(object data, dataType dtype)
private void Send(object data, dataType dtype)
{
byte[] type = new byte[4];
type = BitConverter.GetBytes((int)dtype);
@ -212,23 +203,17 @@ namespace Galactic_Colors_Control_Client
}
catch
{
ConsoleWrite("Can't contact server : " + _errorCount);
Output.Add("Can't contact server : " + _errorCount);
_errorCount++;
}
if (_errorCount >= 5)
{
Output.Add("Kick : too_much_errors");
_run = false;
}
}
private static void ConsoleWrite(string v)
{
Console.Write("\b");
Console.WriteLine(v);
Console.Write(">");
}
private static void ReceiveLoop()
private void ReceiveLoop()
{
while (_run)
{
@ -240,7 +225,7 @@ namespace Galactic_Colors_Control_Client
}
catch
{
ConsoleWrite("Server timeout");
Output.Add("Server timeout");
}
if (received == 0) return;
_errorCount = 0;
@ -263,26 +248,34 @@ namespace Galactic_Colors_Control_Client
string[] array = text.Split(new char[1] { ' ' }, 4, StringSplitOptions.RemoveEmptyEntries);
switch (array[0])
{
case "connected":
Output.Add("Identifiaction succes");
break;
case "allreadytaken":
Output.Add("Username Allready Taken");
break;
case "kick":
if (array.Length > 1)
{
ConsoleWrite("Kick : " + array[1]);
Output.Add("Kick : " + array[1]);
}
else
{
ConsoleWrite("Kick by server");
Output.Add("Kick by server");
}
_run = false;
break;
default:
Console.WriteLine("Unknown action from server");
Output.Add("Unknown action from server");
break;
}
}
else
{
ConsoleWrite(text);
Output.Add(text);
}
break;
@ -292,6 +285,7 @@ namespace Galactic_Colors_Control_Client
}
Thread.Sleep(200);
}
Output.Add("/*exit*/");
}
}
}