Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.67 KB | None | 0 0
  1. using Bibe.GamePages;
  2. using Bibe.Managers;
  3. using Bibe.Questions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9.  
  10. namespace Bibe.HostGamePages
  11. {
  12.     [XamlCompilation(XamlCompilationOptions.Compile)]
  13.     public partial class HostMillionaireQuestionPage: MillionaireQuestionPage
  14.     {
  15.         private int _clickCounter;
  16.         private MillionaireQuestion _millionair;
  17.         private SocketHandler _handler;
  18.         private bool _isHost;
  19.         private string[] _playerNames;
  20.         private int _answerClicked;
  21.         private int _rightAnswers;
  22.         private QuestionManager _QM;
  23.         private PlayerManager _PM;
  24.  
  25.         public MillionaireQuestionPage(QuestionManager QM, PlayerManager PM, MillionaireQuestion millionair, SocketHandler handler, bool isHost, string[] playerNames)
  26.         {
  27.             _clickCounter = 0;
  28.             _rightAnswers = 0;
  29.             _QM = QM;
  30.             _PM = PM;
  31.             _millionair = millionair;
  32.             _handler = handler;
  33.             _isHost = isHost;
  34.             _playerNames = playerNames;
  35.  
  36.             _handler.ServerResponseEvent += HandleServerResponseEvent;
  37.             _handler.InternalErrorEvent += HandleInternalErrorEvent;
  38.             _handler.ConnectionLostEvent += HandleConnectionLostEvent;
  39.  
  40.             InitializeComponent();
  41.         }
  42.  
  43.  
  44.         protected override void OnAppearing()
  45.         {
  46.             if (_isHost)
  47.             {
  48.                 ContinueBtn.IsVisible = true;
  49.                 ContinueBtn.IsEnabled = true;
  50.                 AnswerOne.IsEnabled = true;
  51.                 AnswerTwo.IsEnabled = true;
  52.                 AnswerThree.IsEnabled = true;
  53.                 AnswerFour.IsEnabled = true;
  54.             }
  55.             TextLabel.Text = "Alle VS Handy:\n Schafft ihr gemeinsam 3 Fragen, trinkt ihr nix, sonst... najaaaaaa:";
  56.         }
  57.  
  58.         private async void ContinueButton(object sender, EventArgs args)
  59.         {
  60.             _clickCounter++;
  61.             if (_clickCounter >= 1 && _clickCounter <= 3)
  62.             {
  63.                 if (_isHost)
  64.                 {
  65.                     _handler.SendNextHandling("Cont");
  66.                 }
  67.                 ContinueBtn.Text = "Weiter";
  68.                 ContinueBtn.IsEnabled = false;
  69.                 ContinueBtn.IsVisible = false;
  70.                 AnswerOne.IsVisible = true;
  71.                 AnswerTwo.IsVisible = true;
  72.                 AnswerThree.IsVisible = true;
  73.                 AnswerFour.IsVisible = true;
  74.                 AnswerClicked();
  75.                 return;
  76.             }
  77.  
  78.             if (_clickCounter >= 5)
  79.             {
  80.                 if (_isHost)
  81.                 {
  82.                     (int qtype, int qId, int playercount, int qIndex) = _QM.NextQuestion<(int, int, int, int)>();
  83.  
  84.  
  85.                     string[] nameList = _PM.GetPlayers(playercount);
  86.                     string allNames = string.Empty;
  87.                     foreach (string name in nameList)
  88.                     {
  89.                         allNames = allNames + name + ':';
  90.                     }
  91.                     allNames = allNames.Substring(0, allNames.Length - 1);
  92.  
  93.                     //allNames of the form := (qType,qID,qIndex,name1:name2:name3)
  94.                     string nextQuestion = qtype.ToString() + ',' + qId.ToString() + ',' + qIndex.ToString() + ',' + allNames;
  95.                     _handler.SendQuestion(nextQuestion);
  96.  
  97.                     Device.StartTimer(TimeSpan.FromSeconds(2), () => {
  98.                         _handler.ServerResponseEvent -= HandleServerResponseEvent;
  99.                         _handler.InternalErrorEvent -= HandleInternalErrorEvent;
  100.                         _handler.ConnectionLostEvent -= HandleConnectionLostEvent;
  101.  
  102.  
  103.                         // TODO wenn selbes nochmal
  104.                         _QM.StartNewPageAsnyc(_QM, _PM, qtype, qId, qIndex, nameList, true, _handler);
  105.                         return false;
  106.                     });
  107.                 }
  108.                 return;
  109.             }
  110.         }
  111.  
  112.         private async void Answer1(object sender, EventArgs args)
  113.         {
  114.             if (_isHost)
  115.             {
  116.                 _handler.SendNextHandling("1");
  117.             }
  118.             _clickCounter++;
  119.             _answerClicked = 1;
  120.             AnswerClicked();
  121.         }
  122.  
  123.         private async void Answer2(object sender, EventArgs args)
  124.         {
  125.             if (_isHost)
  126.             {
  127.                 _handler.SendNextHandling("2");
  128.             }
  129.             _clickCounter++;
  130.             _answerClicked = 2;
  131.             AnswerClicked();
  132.         }
  133.  
  134.         private async void Answer3(object sender, EventArgs args)
  135.         {
  136.             if (_isHost)
  137.             {
  138.                 _handler.SendNextHandling("3");
  139.             }
  140.             _clickCounter++;
  141.             _answerClicked = 3;
  142.             AnswerClicked();
  143.         }
  144.  
  145.         private async void Answer4(object sender, EventArgs args)
  146.         {
  147.             if (_isHost)
  148.             {
  149.                 _handler.SendNextHandling("4");
  150.             }
  151.             _clickCounter++;
  152.             _answerClicked = 4;
  153.             AnswerClicked();
  154.         }
  155.  
  156.         private void AnswerClicked()
  157.         {
  158.             var normalColor = (Color)Application.Current.Resources["SecondaryColor"];
  159.             if (_clickCounter > 1 && _clickCounter < 5 && _answerClicked == _millionair.Solutions[_clickCounter - 2])
  160.             {
  161.                 _rightAnswers++;
  162.             }
  163.  
  164.             // TODO display right answers
  165.             AnswerOne.BackgroundColor = Color.Red;
  166.             AnswerTwo.BackgroundColor = Color.Red;
  167.             AnswerThree.BackgroundColor = Color.Red;
  168.             AnswerFour.BackgroundColor = Color.Red;
  169.  
  170.             if (_clickCounter > 1 && _clickCounter < 5)
  171.             {
  172.                 switch (_millionair.Solutions[_clickCounter - 2])
  173.                 {
  174.                     case 1:
  175.                         AnswerOne.BackgroundColor = Color.Green;
  176.                         break;
  177.                     case 2:
  178.                         AnswerTwo.BackgroundColor = Color.Green;
  179.                         break;
  180.                     case 3:
  181.                         AnswerThree.BackgroundColor = Color.Green;
  182.                         break;
  183.                     case 4:
  184.                         AnswerFour.BackgroundColor = Color.Green;
  185.                         break;
  186.                     default:
  187.                         break;
  188.                 }
  189.             }
  190.  
  191.             Device.StartTimer(TimeSpan.FromSeconds(2), () =>
  192.             {
  193.                 AnswerOne.BackgroundColor = normalColor;
  194.                 AnswerTwo.BackgroundColor = normalColor;
  195.                 AnswerThree.BackgroundColor = normalColor;
  196.                 AnswerFour.BackgroundColor = normalColor;
  197.  
  198.                 if (_clickCounter >= 4)
  199.                 {
  200.                     AnswerOne.IsVisible = false;
  201.                     AnswerTwo.IsVisible = false;
  202.                     AnswerThree.IsVisible = false;
  203.                     AnswerFour.IsVisible = false;
  204.                     ContinueBtn.IsEnabled = true;
  205.                     ContinueBtn.IsVisible = true;
  206.  
  207.                     int result = (Int32.Parse(_playerNames[1]) * 2 * (3 - _rightAnswers));
  208.                     TextLabel.Text = _rightAnswers.ToString() + "/3 Antworte waren richtig.\n Das resultiert in " +
  209.                         result.ToString() + " Schlücken für euch gemeinsam, teilt sie nach Belieben unter euch auf!";
  210.                     return false;
  211.                 }
  212.                 AnswerOne.Text = _millionair.Guesses[_clickCounter - 1][0];
  213.                 AnswerTwo.Text = _millionair.Guesses[_clickCounter - 1][1];
  214.                 AnswerThree.Text = _millionair.Guesses[_clickCounter - 1][2];
  215.                 AnswerFour.Text = _millionair.Guesses[_clickCounter - 1][3];
  216.                 TextLabel.Text = _millionair.QuestionTexts[_clickCounter - 1];
  217.                 return false;
  218.             });
  219.         }
  220.  
  221.  
  222.         protected override bool OnBackButtonPressed()
  223.         {
  224.             return true;
  225.         }
  226.  
  227.         private void HandleServerResponseEvent(object sender, ServerResponseEventArgs e)
  228.         {
  229.             string[] playerNames;
  230.  
  231.             switch (e.Prefix)
  232.             {
  233.                 case "erro":
  234.                     int errorCode = Int32.Parse(e.Suffix);
  235.                     if (errorCode == ErrorCodeInterpreter.SessionClosedByHost)
  236.                     {
  237.                         Device.BeginInvokeOnMainThread(async () => {
  238.                             await DisplayAlert("Verbindung geschlossen", "Der Ersteller eurer Runde hatte wohl keine Lust" +
  239.                             " mehr auf euch. Nächstes mal besser benehmen!",
  240.                             "Ayay Captain!");
  241.                             PlayerManager.PlayerList.Clear();
  242.                             if (_handler.Socket != null && _handler.Socket.Connected)
  243.                             {
  244.                                 _handler.Close();
  245.                             }
  246.                             await PageManager.PopToRootAsyncSingle(this);
  247.                         });
  248.  
  249.                         return;
  250.                     }
  251.                     Device.BeginInvokeOnMainThread(() => {
  252.                         string errorMsg = ErrorCodeInterpreter.ErrorCode[errorCode];
  253.                         DisplayAlert("Error", errorMsg, "OK");
  254.                     });
  255.                     break;
  256.  
  257.                 case "nams":
  258.                     // TODO Ok so for in game?!
  259.                     // Player joined: Update PlayerManager
  260.  
  261.                     if (!_isHost)
  262.                     {
  263.                         return;
  264.                     }
  265.  
  266.                     playerNames = e.Suffix.Split(',');
  267.                     foreach (string name in playerNames)
  268.                     {
  269.                         if (!(PlayerManager.PlayerList.Contains(name)))
  270.                         {
  271.                             PlayerManager.PlayerList.Add(name);
  272.                         }
  273.                     }
  274.                     break;
  275.  
  276.                 case "left":
  277.                     // TODO Ok so for in game?!
  278.                     // TODO Update QuestionManager if to few people?!
  279.                     // Player left: Update PlayerManager + Update QuestionManager
  280.  
  281.                     if (!_isHost)
  282.                     {
  283.                         return;
  284.                     }
  285.  
  286.                     playerNames = e.Suffix.Split(',');
  287.                     PlayerManager.PlayerList = PlayerManager.JustLocalPlayerList;
  288.                     foreach (string name in playerNames)
  289.                     {
  290.                         if (!(PlayerManager.PlayerList.Contains(name)))
  291.                         {
  292.                             PlayerManager.PlayerList.Add(name);
  293.                         }
  294.                     }
  295.  
  296.                     // Case Host is alone and has no local Friends
  297.                     if (PlayerManager.PlayerList.Count <= 1)
  298.                     {
  299.                         Device.BeginInvokeOnMainThread(async () => {
  300.                             await DisplayAlert("Zu wenig Spieler", "Leider bist du alleine und alleine Trinken ist traurig. " +
  301.                             "Daher werden wir jetzt das Spiel beenden...",
  302.                             "Na klar, hab euch lieb!");
  303.                             PlayerManager.PlayerList.Clear();
  304.                             if (_handler.Socket != null && _handler.Socket.Connected)
  305.                             {
  306.                                 _handler.Close();
  307.                             }
  308.                             await PageManager.PopToRootAsyncSingle(this);
  309.                         });
  310.                     }
  311.                     break;
  312.  
  313.                 case "next":
  314.                     if (_isHost)
  315.                     {
  316.                         return;
  317.                     }
  318.  
  319.                     switch (e.Suffix)
  320.                     {
  321.                         case "Cont":
  322.                             Device.BeginInvokeOnMainThread(async () => {
  323.                                 ContinueButton(null, EventArgs.Empty);
  324.                             });
  325.  
  326.                             break;
  327.                         case "1":
  328.                             Device.BeginInvokeOnMainThread(async () => {
  329.                                 Answer1(null, EventArgs.Empty);
  330.                             });
  331.                             break;
  332.                         case "2":
  333.                             Device.BeginInvokeOnMainThread(async () => {
  334.                                 Answer2(null, EventArgs.Empty);
  335.                             });
  336.                             break;
  337.                         case "3":
  338.                             Device.BeginInvokeOnMainThread(async () => {
  339.                                 Answer3(null, EventArgs.Empty);
  340.                             });
  341.                             break;
  342.                         case "4":
  343.                             Device.BeginInvokeOnMainThread(async () => {
  344.                                 Answer4(null, EventArgs.Empty);
  345.                             });
  346.                             break;
  347.                     }
  348.  
  349.                     break;
  350.  
  351.                 case "sdqu":
  352.                     if (_isHost)
  353.                     {
  354.                         return;
  355.                     }
  356.                     string[] tmpArray = e.Suffix.Split(',');
  357.                     int qType = Int32.Parse(tmpArray[0]);
  358.                     int qID = Int32.Parse(tmpArray[1]);
  359.                     int qIndex = Int32.Parse(tmpArray[2]);
  360.                     string[] names = tmpArray[3].Split(':');
  361.  
  362.                     _handler.ServerResponseEvent -= HandleServerResponseEvent;
  363.                     _handler.InternalErrorEvent -= HandleInternalErrorEvent;
  364.                     _handler.ConnectionLostEvent -= HandleConnectionLostEvent;
  365.  
  366.                     PlayerManager playerManager = null;
  367.                     _QM.StartNewPageAsnyc(_QM, playerManager, qType, qID, qIndex, names, false, _handler);
  368.                     break;
  369.  
  370.                 default:
  371.                     break;
  372.             }
  373.         }
  374.  
  375.         private void HandleInternalErrorEvent(object sender, InternalErrorEventArgs e)
  376.         {
  377.             Device.BeginInvokeOnMainThread(() => {
  378.                 DisplayAlert("Error", e.Error, "Send error", "Ignore");
  379.             });
  380.         }
  381.  
  382.         private void HandleConnectionLostEvent(object sender, ConnectionLostEventArgs e)
  383.         {
  384.             Device.BeginInvokeOnMainThread(() => {
  385.                 DisplayAlert("Error Connection Lost", e.Message, "Tut mir Leid", "Shit");
  386.                 PageManager.PopToRootAsyncSingle(this);
  387.             });
  388.         }
  389.     }
  390.  
  391.  
  392.  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement