Fixed split packages bug.
[clanbomber-xamarin-client.git] / src / communication / ConnectionTest / TcpJsonConnectionTest / Program.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Net.Sockets;
6 using System.Text;
7 using System.Threading.Tasks;
8 using ConnectionTest;
9
10 namespace TcpJsonConnectionTest
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             TcpJsonConnection con = new TcpJsonConnection(DisplayServerInformation);
17
18             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
19             // con.Connect(IPAddress.Parse("fe80::c0a:44f1:63e7:bfb8%4"));
20             // con.Connect(IPAddress.Parse("127.0.0.1"));
21
22             while (true)
23             {
24                 con.SendEvent(TcpJsonConnection.InputState.Right);
25                 System.Threading.Thread.Sleep(100);
26                 con.SendEvent(TcpJsonConnection.InputState.Down);
27                 System.Threading.Thread.Sleep(100);
28                 con.SendEvent(TcpJsonConnection.InputState.Left);
29                 System.Threading.Thread.Sleep(100);
30                 con.SendEvent(TcpJsonConnection.InputState.Up);
31                 System.Threading.Thread.Sleep(100);
32
33                 con.SendEvent(TcpJsonConnection.InputState.Idle);
34                 con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
35                 System.Threading.Thread.Sleep(100);
36                 con.SendEvent(TcpJsonConnection.InputBomb.Idle);
37                 System.Threading.Thread.Sleep(100);
38
39                 if(Console.KeyAvailable)
40                 {
41                     break;
42                 }
43             }
44
45
46             con.Close();
47
48         }
49
50
51
52         static void DisplayServerInformation(TcpJsonConnection.ServerPackage pkg)
53         {
54             char[,] playground = new char[pkg.Field.Count,pkg.Field.Count];
55
56
57             for (int i = 0; i < pkg.Field.Count; i++)
58             {
59                 for (int j = 0; j < pkg.Field[i].Count; j++)
60                 {
61                     playground[i,j]= FieldTypeToChar(pkg.Field[i][j]);
62                 }
63             }
64
65             foreach(var player in pkg.Players)
66             {
67                 playground[(int)player.Y, (int)player.X] = System.Convert.ToChar(player.Id + '0');
68             }
69
70             foreach (var bomb in pkg.Bombs)
71             {
72                 playground[(int)bomb.Y, (int)bomb.X] = 'o';
73             }
74
75             foreach (var flame in pkg.Flames)
76             {
77                 playground[(int)flame.Y, (int)flame.X] = '/';
78             }
79
80
81
82             for (int i = 0; i < pkg.Field.Count; i++)
83             {
84                 for (int j = 0; j < pkg.Field[i].Count; j++)
85                 {
86                     Console.Write(playground[i, j]);
87                 }
88                 Console.WriteLine();
89             }
90
91         }
92
93
94         static char FieldTypeToChar(TcpJsonConnection.ServerPackage.FieldTypes field)
95         {
96             switch(field)
97             {
98                 case TcpJsonConnection.ServerPackage.FieldTypes.DynamicOrDestroyableBlock:
99                     return '%';
100                 case TcpJsonConnection.ServerPackage.FieldTypes.StaticOrUndestroyableBlock:
101                     return '#';
102                 case TcpJsonConnection.ServerPackage.FieldTypes.ItemOrMoreBombs:
103                     return 'O';
104             }
105
106             return ' ';
107         }
108
109
110
111
112     }
113 }