44a2b3df2e3a6b2a5a0ad54b4a6689dea7172509
[clanbomber-xamarin-client.git] / src / full_system / ConnectionTest / ConnectionTest / ConnectionTest / App.xaml.cs
1 ´╗┐using System;
2 using Xamarin.Forms;
3 using Xamarin.Forms.Xaml;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using System.Net;
9 using static Bomberman.TcpJsonConnection;
10
11 [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
12 namespace Bomberman
13 {
14     public partial class App : Application
15     {
16         TcpJsonConnection con;
17         InGamePage gamePage;
18         int gameStarted = 0;
19
20         public App()
21         {
22             con = new TcpJsonConnection(ServerPkgReceived);
23             MainPage = new ConnectPage(con);
24         }
25
26         protected override void OnStart()
27         {
28             // Handle when your app starts
29         }
30
31         protected override void OnSleep()
32         {
33             // Handle when your app sleeps
34         }
35
36         protected override void OnResume()
37         {
38             // Handle when your app resumes
39         }
40         void ServerPkgReceived(ServerPackage pkg)
41         {
42             Device.BeginInvokeOnMainThread(() =>
43             {
44                 switch (pkg.State)
45                 {
46                     case ServerPackage.ServerState.PreGamePhase:
47                         if(Application.Current.MainPage.GetType() != typeof(PreGamePage))
48                         {
49                             Application.Current.MainPage = new PreGamePage(pkg);
50                             oldPlayersCount = pkg.Players.Count;
51                         }
52                         else
53                         {
54                             if (pkg.Players.Count != oldPlayersCount)
55                             {
56                                 ((PreGamePage)Application.Current.MainPage).UpdatePage(pkg);
57
58                             }
59                             else
60                             {
61                                 ((PreGamePage)Application.Current.MainPage).UpdateTime(pkg);
62                             }
63                             oldPlayersCount = pkg.Players.Count;
64
65                         }
66                         break;
67                     case ServerPackage.ServerState.InGamePhase:
68                         if (gameStarted == 0)
69                         {
70                             gamePage = new InGamePage(con);
71                             Application.Current.MainPage = gamePage;
72                             gamePage.CreateInitializePlayground(pkg.Field.Count);
73                             gameStarted = 1;
74                             updateCnt = 20;
75                         }
76                         else
77                         {
78                             if(updateCnt == 0)
79                             {
80                                 gamePage.UpdatePlayground(pkg);
81                                 updateCnt = 1;
82                             }
83                             else
84                             {
85                                 updateCnt--;
86                             }
87
88                         }
89                         break;
90                     case ServerPackage.ServerState.PostGamePhase:
91                         gameStarted = 0;
92                         if (Application.Current.MainPage.GetType() != typeof(PostGamePage))
93                         {
94                             Application.Current.MainPage = new PostGamePage(pkg);
95                         }
96                         else
97                         {
98                             ((PostGamePage)Application.Current.MainPage).UpdateTime(pkg);
99                         }
100                         break;
101                 }
102             });
103         }
104
105
106         int updateCnt = 0;
107         private int oldPlayersCount;
108     }
109 }