Skip to content

Commit

Permalink
Aggiungere i file di progetto.
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaPulga committed May 17, 2021
1 parent c239888 commit e9334d2
Show file tree
Hide file tree
Showing 30 changed files with 1,476 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
18 changes: 18 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Application x:Class="TcpGameClient.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:TcpGameClient"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen"/>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace TcpGameClient
{
/// <summary>
/// Logica di interazione per App.xaml
/// </summary>
public partial class App : Application
{
}
}
27 changes: 27 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Window x:Class="TcpGameClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TcpGameClient"
mc:Ignorable="d"
Title="Speed clicker - client" Height="450" Width="600" Loaded="Window_Loaded" MouseUp="Window_MouseUp" ResizeMode="NoResize">
<Window.Background>
<ImageBrush ImageSource="speed.jpg"/>
</Window.Background>
<Grid>
<Button Background="Aqua" Foreground="Black" x:Name="btnStart" Content="Click here to start" HorizontalAlignment="Left" Margin="216,177,0,0" VerticalAlignment="Top" Width="152" Click="btnStart_Click" Height="103"/>
<Label Foreground="White" Content="Time" HorizontalAlignment="Left" Margin="18,80,0,0" VerticalAlignment="Top" Width="50"/>
<TextBox BorderBrush="White" BorderThickness="2" Foreground="White" x:Name="txtTime" HorizontalAlignment="Left" Height="28" Margin="18,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="194"/>
<Label BorderThickness="2" BorderBrush="White" Foreground="White" x:Name="lblClicks" Content="Clicks: " HorizontalAlignment="Left" Margin="263,110,0,0" VerticalAlignment="Top" Width="200"/>
<Label Foreground="White" Content="Destination ip address" HorizontalAlignment="Left" Margin="15,10,0,0" VerticalAlignment="Top" FontFamily="Source Serif Pro Black"/>
<TextBox Foreground="White" BorderThickness="2" BorderBrush="White" x:Name="txtIpAdd" HorizontalAlignment="Left" Height="29" Margin="15,34,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="199"/>
<TextBox Foreground="White" BorderThickness="2" BorderBrush="White" x:Name="txtDestPort" HorizontalAlignment="Left" Height="29" Margin="258,34,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="211"/>
<Label FontFamily="Source Serif Pro Black" Foreground="White" Content="Destination port" HorizontalAlignment="Left" Margin="262,11,0,0" VerticalAlignment="Top"/>
<Button Content="Connect" x:Name="btnConnect" HorizontalAlignment="Left" Margin="490,31,0,0" VerticalAlignment="Top" Width="90" Click="btnConnect_Click"/>
<Label HorizontalContentAlignment="Center" x:Name="lblAvversario" Foreground="White" BorderThickness="2" BorderBrush="White" Content="Opponent: " HorizontalAlignment="Left" Margin="106,352,0,0" VerticalAlignment="Top" Width="398"/>
<Label Foreground="White" Content="Created by Pulga Luca 4^L. All rights reserved by Pulga Luca ©." HorizontalAlignment="Left" Margin="142,395,0,0" VerticalAlignment="Top" Height="24"/>
<Label x:Name="lblConnection" Foreground="White" BorderThickness="2" BorderBrush="White" Content="Connection: " HorizontalAlignment="Left" Margin="489,69,0,0" VerticalAlignment="Top" Width="91"/>
<Label HorizontalContentAlignment="Center" x:Name="lblMine" Foreground="White" BorderThickness="2" BorderBrush="White" Content="Me: " HorizontalAlignment="Left" Margin="103,311,0,0" VerticalAlignment="Top" Width="405" />
</Grid>
</Window>
238 changes: 238 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
* Sviluppatore: Pulga Luca;
* Classe: 4^L
* Data di consegna: 2021/05/17
* Scopo: Utilizzando le classi TCPClient e TCPListener realizzare un semplice gioco
* (carte, dadi o altro) utilizzando il protocollo TCP (che a differenza dell'UDP
* visto oggi richiede la creazione di una connessione).
* APP: SPEED CLICKER with TCP protocol
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TcpGameClient
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TcpClient client;
Socket socket;
bool sessionToken;
int time = 10000;
float cps;
int clicks;

public MainWindow()
{
InitializeComponent();

txtDestPort.Text = "55000";
txtIpAdd.Text = GetLocalIPAddress();
txtTime.Text = "0";
}

private void btnConnect_Click(object sender, RoutedEventArgs e)
{
ClientSide(); // Ricezione.
}

private async void ClientSide()
{
try
{
await Task.Run(() =>
{
client = new TcpClient("127.0.0.1", 55000);
//client.Connect("127.0.0.1", 55000);
this.Dispatcher.BeginInvoke(new Action(() =>
{
if (client.Connected) // Controllo se i 2 host sono connessi.
{
lblConnection.Content = "CONNECTED";
btnConnect.IsEnabled = false;
}
}));

while (true)
{
string textToTransmit = "";
this.Dispatcher.BeginInvoke(new Action(() =>
{
textToTransmit = lblMine.Content.ToString();
}));
socket = client.Client; // Parte client.

byte[] ba = Encoding.ASCII.GetBytes(textToTransmit); // string to byte to send.


byte[] bb = new byte[100];
int j = socket.Receive(bb, bb.Length, 0); // Ricezione dati.

if(j > 0)
this.Dispatcher.BeginInvoke(new Action(() =>
{
lblAvversario.Content = "Opponent: " + Encoding.ASCII.GetString(bb);
}));
}
});
}
catch (Exception ex)
{
MessageBox.Show("Errore:\n" + ex.Message, "Client", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
finally
{
if (client != null)
client.Close();
}
}

private void Session()
{
while (true)
{
if (sessionToken == true)
{
Thread.Sleep(time);
sessionToken = false;
cps = time / 1000;
cps = clicks / cps;

string n = "";

this.Dispatcher.BeginInvoke(new Action(() =>
{
lblMine.Content = "Click: " + clicks.ToString() + ", clicks per second: " + cps.ToString();
}));

this.Dispatcher.BeginInvoke(new Action(() =>
{
n = lblMine.Content.ToString();
}));

Thread.Sleep(100);
SendData(n);

Thread.Sleep(2000);
this.Dispatcher.BeginInvoke(new Action(() =>
{
btnStart.Visibility = Visibility.Visible;
}));
}
}
}

/// <summary>
/// Invio dei dati.
/// </summary>
/// <param name="n"></param>
private void SendData(string n)
{
socket.Send(Encoding.ASCII.GetBytes(n));
}

/// <summary>
/// Creazione window e avvio thread per gestione click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread sn = new Thread(Session) { IsBackground = true }; // Se rilascia il button, incremento numero di click.
sn.Start();
}

/// <summary>
/// elease del click del mouse per incremento numero click.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released)
{
clicks++;
lblClicks.Content = "Click: " + clicks.ToString();
}
}

private void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
if (!int.TryParse(txtTime.Text, out int t))
throw new Exception("Inserire un tempo valido.");
time = t * 1000;
if (sessionToken == false)
{
clicks = 0;
sessionToken = true;
this.Dispatcher.BeginInvoke(new Action(() =>
{
btnStart.Visibility = Visibility.Hidden;
}));
}
}
catch (Exception ex)
{
MessageBox.Show("Errore :\n" + ex.Message, "Client", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

}

/// <summary>
/// get automated local ip.
/// </summary>
/// <returns></returns>
public static string GetLocalIPAddress()
{
try
{
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
// Get the IP
string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString(); // Get local ip address

Uri uri = new Uri("http://" + myIP);

return uri.Host.ToString(); // return dell'ip dell'host.
}
catch (Exception ex)
{
throw new Exception("No network adapters with an IPv4 address in the system!");
}
}

/// <summary>
/// control ipv4 address.
/// </summary>
/// <param name="ipAddress"></param>
/// <returns></returns>
public static bool IsIPv4(string ipAddress)
{
return Regex.IsMatch(ipAddress, @"^\d{1,3}(\.\d{1,3}){3}$") && ipAddress.Split('.').SingleOrDefault(s => int.Parse(s) > 255) == null; // Regex per controllare ip.
}

}
}
55 changes: 55 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// Le informazioni generali relative a un assembly sono controllate dal seguente
// set di attributi. Modificare i valori di questi attributi per modificare le informazioni
// associate a un assembly.
[assembly: AssemblyTitle("TcpGameClient")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TcpGameClient")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Se si imposta ComVisible su false, i tipi in questo assembly non saranno visibili
// ai componenti COM. Se è necessario accedere a un tipo in questo assembly da
// COM, impostare su true l'attributo ComVisible per tale tipo.
[assembly: ComVisible(false)]

//Per iniziare a creare applicazioni localizzabili, impostare
//<UICulture>CultureYouAreCodingWith</UICulture> nel file .csproj
//all'interno di un <PropertyGroup>. Ad esempio, se si utilizza l'inglese (Stati Uniti)
//nei file di origine, impostare <UICulture> su en-US. Rimuovere quindi il commento dall'attributo
//NeutralResourceLanguage riportato di seguito. Aggiornare "en-US" nella
//riga sottostante in modo che corrisponda all'impostazione UICulture nel file di progetto.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //dove si trovano i dizionari delle risorse specifiche del tema
//(da usare se nella pagina non viene trovata una risorsa,
// oppure nei dizionari delle risorse dell'applicazione)
ResourceDictionaryLocation.SourceAssembly //dove si trova il dizionario delle risorse generiche
//(da usare se nella pagina non viene trovata una risorsa,
// nell'applicazione o nei dizionari delle risorse specifiche del tema)
)]


// Le informazioni sulla versione di un assembly sono costituite dai seguenti quattro valori:
//
// Versione principale
// Versione secondaria
// Numero di build
// Revisione
//
// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build
// usando l'asterisco '*' come illustrato di seguito:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit e9334d2

Please sign in to comment.