Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Bibe.GamePages;
- using Bibe.Managers;
- using Bibe.Questions;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace Bibe.HostGamePages
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class HostMillionaireQuestionPage: MillionaireQuestionPage
- {
- private int _clickCounter;
- private MillionaireQuestion _millionair;
- private SocketHandler _handler;
- private bool _isHost;
- private string[] _playerNames;
- private int _answerClicked;
- private int _rightAnswers;
- private QuestionManager _QM;
- private PlayerManager _PM;
- public MillionaireQuestionPage(QuestionManager QM, PlayerManager PM, MillionaireQuestion millionair, SocketHandler handler, bool isHost, string[] playerNames)
- {
- _clickCounter = 0;
- _rightAnswers = 0;
- _QM = QM;
- _PM = PM;
- _millionair = millionair;
- _handler = handler;
- _isHost = isHost;
- _playerNames = playerNames;
- _handler.ServerResponseEvent += HandleServerResponseEvent;
- _handler.InternalErrorEvent += HandleInternalErrorEvent;
- _handler.ConnectionLostEvent += HandleConnectionLostEvent;
- InitializeComponent();
- }
- protected override void OnAppearing()
- {
- if (_isHost)
- {
- ContinueBtn.IsVisible = true;
- ContinueBtn.IsEnabled = true;
- AnswerOne.IsEnabled = true;
- AnswerTwo.IsEnabled = true;
- AnswerThree.IsEnabled = true;
- AnswerFour.IsEnabled = true;
- }
- TextLabel.Text = "Alle VS Handy:\n Schafft ihr gemeinsam 3 Fragen, trinkt ihr nix, sonst... najaaaaaa:";
- }
- private async void ContinueButton(object sender, EventArgs args)
- {
- _clickCounter++;
- if (_clickCounter >= 1 && _clickCounter <= 3)
- {
- if (_isHost)
- {
- _handler.SendNextHandling("Cont");
- }
- ContinueBtn.Text = "Weiter";
- ContinueBtn.IsEnabled = false;
- ContinueBtn.IsVisible = false;
- AnswerOne.IsVisible = true;
- AnswerTwo.IsVisible = true;
- AnswerThree.IsVisible = true;
- AnswerFour.IsVisible = true;
- AnswerClicked();
- return;
- }
- if (_clickCounter >= 5)
- {
- if (_isHost)
- {
- (int qtype, int qId, int playercount, int qIndex) = _QM.NextQuestion<(int, int, int, int)>();
- string[] nameList = _PM.GetPlayers(playercount);
- string allNames = string.Empty;
- foreach (string name in nameList)
- {
- allNames = allNames + name + ':';
- }
- allNames = allNames.Substring(0, allNames.Length - 1);
- //allNames of the form := (qType,qID,qIndex,name1:name2:name3)
- string nextQuestion = qtype.ToString() + ',' + qId.ToString() + ',' + qIndex.ToString() + ',' + allNames;
- _handler.SendQuestion(nextQuestion);
- Device.StartTimer(TimeSpan.FromSeconds(2), () => {
- _handler.ServerResponseEvent -= HandleServerResponseEvent;
- _handler.InternalErrorEvent -= HandleInternalErrorEvent;
- _handler.ConnectionLostEvent -= HandleConnectionLostEvent;
- // TODO wenn selbes nochmal
- _QM.StartNewPageAsnyc(_QM, _PM, qtype, qId, qIndex, nameList, true, _handler);
- return false;
- });
- }
- return;
- }
- }
- private async void Answer1(object sender, EventArgs args)
- {
- if (_isHost)
- {
- _handler.SendNextHandling("1");
- }
- _clickCounter++;
- _answerClicked = 1;
- AnswerClicked();
- }
- private async void Answer2(object sender, EventArgs args)
- {
- if (_isHost)
- {
- _handler.SendNextHandling("2");
- }
- _clickCounter++;
- _answerClicked = 2;
- AnswerClicked();
- }
- private async void Answer3(object sender, EventArgs args)
- {
- if (_isHost)
- {
- _handler.SendNextHandling("3");
- }
- _clickCounter++;
- _answerClicked = 3;
- AnswerClicked();
- }
- private async void Answer4(object sender, EventArgs args)
- {
- if (_isHost)
- {
- _handler.SendNextHandling("4");
- }
- _clickCounter++;
- _answerClicked = 4;
- AnswerClicked();
- }
- private void AnswerClicked()
- {
- var normalColor = (Color)Application.Current.Resources["SecondaryColor"];
- if (_clickCounter > 1 && _clickCounter < 5 && _answerClicked == _millionair.Solutions[_clickCounter - 2])
- {
- _rightAnswers++;
- }
- // TODO display right answers
- AnswerOne.BackgroundColor = Color.Red;
- AnswerTwo.BackgroundColor = Color.Red;
- AnswerThree.BackgroundColor = Color.Red;
- AnswerFour.BackgroundColor = Color.Red;
- if (_clickCounter > 1 && _clickCounter < 5)
- {
- switch (_millionair.Solutions[_clickCounter - 2])
- {
- case 1:
- AnswerOne.BackgroundColor = Color.Green;
- break;
- case 2:
- AnswerTwo.BackgroundColor = Color.Green;
- break;
- case 3:
- AnswerThree.BackgroundColor = Color.Green;
- break;
- case 4:
- AnswerFour.BackgroundColor = Color.Green;
- break;
- default:
- break;
- }
- }
- Device.StartTimer(TimeSpan.FromSeconds(2), () =>
- {
- AnswerOne.BackgroundColor = normalColor;
- AnswerTwo.BackgroundColor = normalColor;
- AnswerThree.BackgroundColor = normalColor;
- AnswerFour.BackgroundColor = normalColor;
- if (_clickCounter >= 4)
- {
- AnswerOne.IsVisible = false;
- AnswerTwo.IsVisible = false;
- AnswerThree.IsVisible = false;
- AnswerFour.IsVisible = false;
- ContinueBtn.IsEnabled = true;
- ContinueBtn.IsVisible = true;
- int result = (Int32.Parse(_playerNames[1]) * 2 * (3 - _rightAnswers));
- TextLabel.Text = _rightAnswers.ToString() + "/3 Antworte waren richtig.\n Das resultiert in " +
- result.ToString() + " Schlücken für euch gemeinsam, teilt sie nach Belieben unter euch auf!";
- return false;
- }
- AnswerOne.Text = _millionair.Guesses[_clickCounter - 1][0];
- AnswerTwo.Text = _millionair.Guesses[_clickCounter - 1][1];
- AnswerThree.Text = _millionair.Guesses[_clickCounter - 1][2];
- AnswerFour.Text = _millionair.Guesses[_clickCounter - 1][3];
- TextLabel.Text = _millionair.QuestionTexts[_clickCounter - 1];
- return false;
- });
- }
- protected override bool OnBackButtonPressed()
- {
- return true;
- }
- private void HandleServerResponseEvent(object sender, ServerResponseEventArgs e)
- {
- string[] playerNames;
- switch (e.Prefix)
- {
- case "erro":
- int errorCode = Int32.Parse(e.Suffix);
- if (errorCode == ErrorCodeInterpreter.SessionClosedByHost)
- {
- Device.BeginInvokeOnMainThread(async () => {
- await DisplayAlert("Verbindung geschlossen", "Der Ersteller eurer Runde hatte wohl keine Lust" +
- " mehr auf euch. Nächstes mal besser benehmen!",
- "Ayay Captain!");
- PlayerManager.PlayerList.Clear();
- if (_handler.Socket != null && _handler.Socket.Connected)
- {
- _handler.Close();
- }
- await PageManager.PopToRootAsyncSingle(this);
- });
- return;
- }
- Device.BeginInvokeOnMainThread(() => {
- string errorMsg = ErrorCodeInterpreter.ErrorCode[errorCode];
- DisplayAlert("Error", errorMsg, "OK");
- });
- break;
- case "nams":
- // TODO Ok so for in game?!
- // Player joined: Update PlayerManager
- if (!_isHost)
- {
- return;
- }
- playerNames = e.Suffix.Split(',');
- foreach (string name in playerNames)
- {
- if (!(PlayerManager.PlayerList.Contains(name)))
- {
- PlayerManager.PlayerList.Add(name);
- }
- }
- break;
- case "left":
- // TODO Ok so for in game?!
- // TODO Update QuestionManager if to few people?!
- // Player left: Update PlayerManager + Update QuestionManager
- if (!_isHost)
- {
- return;
- }
- playerNames = e.Suffix.Split(',');
- PlayerManager.PlayerList = PlayerManager.JustLocalPlayerList;
- foreach (string name in playerNames)
- {
- if (!(PlayerManager.PlayerList.Contains(name)))
- {
- PlayerManager.PlayerList.Add(name);
- }
- }
- // Case Host is alone and has no local Friends
- if (PlayerManager.PlayerList.Count <= 1)
- {
- Device.BeginInvokeOnMainThread(async () => {
- await DisplayAlert("Zu wenig Spieler", "Leider bist du alleine und alleine Trinken ist traurig. " +
- "Daher werden wir jetzt das Spiel beenden...",
- "Na klar, hab euch lieb!");
- PlayerManager.PlayerList.Clear();
- if (_handler.Socket != null && _handler.Socket.Connected)
- {
- _handler.Close();
- }
- await PageManager.PopToRootAsyncSingle(this);
- });
- }
- break;
- case "next":
- if (_isHost)
- {
- return;
- }
- switch (e.Suffix)
- {
- case "Cont":
- Device.BeginInvokeOnMainThread(async () => {
- ContinueButton(null, EventArgs.Empty);
- });
- break;
- case "1":
- Device.BeginInvokeOnMainThread(async () => {
- Answer1(null, EventArgs.Empty);
- });
- break;
- case "2":
- Device.BeginInvokeOnMainThread(async () => {
- Answer2(null, EventArgs.Empty);
- });
- break;
- case "3":
- Device.BeginInvokeOnMainThread(async () => {
- Answer3(null, EventArgs.Empty);
- });
- break;
- case "4":
- Device.BeginInvokeOnMainThread(async () => {
- Answer4(null, EventArgs.Empty);
- });
- break;
- }
- break;
- case "sdqu":
- if (_isHost)
- {
- return;
- }
- string[] tmpArray = e.Suffix.Split(',');
- int qType = Int32.Parse(tmpArray[0]);
- int qID = Int32.Parse(tmpArray[1]);
- int qIndex = Int32.Parse(tmpArray[2]);
- string[] names = tmpArray[3].Split(':');
- _handler.ServerResponseEvent -= HandleServerResponseEvent;
- _handler.InternalErrorEvent -= HandleInternalErrorEvent;
- _handler.ConnectionLostEvent -= HandleConnectionLostEvent;
- PlayerManager playerManager = null;
- _QM.StartNewPageAsnyc(_QM, playerManager, qType, qID, qIndex, names, false, _handler);
- break;
- default:
- break;
- }
- }
- private void HandleInternalErrorEvent(object sender, InternalErrorEventArgs e)
- {
- Device.BeginInvokeOnMainThread(() => {
- DisplayAlert("Error", e.Error, "Send error", "Ignore");
- });
- }
- private void HandleConnectionLostEvent(object sender, ConnectionLostEventArgs e)
- {
- Device.BeginInvokeOnMainThread(() => {
- DisplayAlert("Error Connection Lost", e.Message, "Tut mir Leid", "Shit");
- PageManager.PopToRootAsyncSingle(this);
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement