display Lives on Screen
[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                             gamePage.UpdateLableLives(pkg);
74                             gameStarted = 1;
75                             updateCnt = 20;
76                         }
77                         else
78                         {
79                             if(updateCnt == 0)
80                             {
81                                 gamePage.UpdatePlayground(pkg);
82                                 gamePage.UpdateLableLives(pkg);
83                                 updateCnt = 1;
84                             }
85                             else
86                             {
87                                 updateCnt--;
88                             }
89
90                         }
91                         break;
92                     case ServerPackage.ServerState.PostGamePhase:
93                         gameStarted = 0;
94                         if (Application.Current.MainPage.GetType() != typeof(PostGamePage))
95                         {
96                             Application.Current.MainPage = new PostGamePage(pkg);
97                         }
98                         else
99                         {
100                             ((PostGamePage)Application.Current.MainPage).UpdateTime(pkg);
101                         }
102                         break;
103                 }
104             });
105         }
106
107
108         int updateCnt = 0;
109         private int oldPlayersCount;
110     }
111 }