Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Документация_Оригинал / Пояснювальна_записка_Диплом_Оригинал_1

.pdf
Скачиваний:
7
Добавлен:
11.06.2015
Размер:
1.13 Mб
Скачать

65

{

List<Classes.Items> items; int randNumb;

int countTrueUnsverP1 = 0; int countTrueUnsverP2 = 0; bool P1Unsverd;

bool P2Unsverd; int touch = 0; string category;

public TwoPlayer()

{

this.InitializeComponent();

}

///<summary>

///Invoked when this page is about to be displayed in a Frame.

///</summary>

///<param name="e">Event data that describes how this page was reached.

///This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e)

{

category = e.Parameter.ToString();

}

private void Gen_Quest()

{

Random rand = new Random();

randNumb = rand.Next(0, items.Count);

txtQuestionP1.Text = items[randNumb].question; bttUnsver1P1Text.Text = items[randNumb].unsver1; bttUnsver2P1Text.Text = items[randNumb].unsver2; bttUnsver3P1Text.Text = items[randNumb].unsver3; bttUnsver4P1Text.Text = items[randNumb].unsver4;

txtQuestionP2.Text = items[randNumb].question; bttUnsver1P2Text.Text = items[randNumb].unsver1; bttUnsver2P2Text.Text = items[randNumb].unsver2; bttUnsver3P2Text.Text = items[randNumb].unsver3; bttUnsver4P2Text.Text = items[randNumb].unsver4;

}

private void btnUnsverP1(object sender, RoutedEventArgs e)

{

}

private async void Page_Loaded(object sender, RoutedEventArgs e)

{

using (HttpClient httpClient = new HttpClient())

{

HttpRequestMessage request = new HttpRequestMessage(); httpClient.BaseAddress = new Uri("https://api.parse.com/1/classes/" +

category);

request.Method = new HttpMethod("GET"); request.Headers.Add("X-Parse-Application-Id",

"q8r9XYoXbEw1kXsWIXbyCNQm9ylcWfAS0fqlWWNx");

66

request.Headers.Add("X-Parse-REST-API-Key", "2nke5LSbXQmTiA9FaVytlBihjOOz4fPcoxjEHlee");

var response = await httpClient.SendAsync(request); var text = await response.Content.ReadAsStringAsync();

System.Diagnostics.Debug.WriteLine(text);

JObject results = JObject.Parse(text); items = new List<Classes.Items>();

foreach (var result in results["results"])

{

items.Add(new Classes.Items() { question = (string)result["Question"], unsver1 = (string)result["unsver1"], unsver2 = (string)result["unsver2"], unsver3 = (string)result["unsver3"], unsver4 = (string)result["unsver4"], trueUnsver = (string)result["trueUnsver"] });

//item.Add(new Item() { driveId = (string)result["parentReference"]["driveId"], id = (string)result["parentReference"]["id"], path = (string)result["parentReference"]["path"] });

}

Gen_Quest();

}

}

private async void NextQuest()

{

touch = 0;

if (P1Unsverd == false && P2Unsverd == false)

{

MessageDialog md = new MessageDialog("Игра окончена! Никто не ответил верно! Правильный ответ: " + items[randNumb].trueUnsver + Environment.NewLine + "Результаты игры:" + Environment.NewLine + "Игрок 1: " + countTrueUnsverP1 + Environment.NewLine + "Игрок 2: " + countTrueUnsverP2);

await md.ShowAsync(); Frame.GoBack();

}

P1Unsverd = false;

P2Unsverd = false;

bttUnsver1P1.IsEnabled = true; bttUnsver2P1.IsEnabled = true; bttUnsver3P1.IsEnabled = true; bttUnsver4P1.IsEnabled = true;

bttUnsver1P2.IsEnabled = true; bttUnsver2P2.IsEnabled = true; bttUnsver3P2.IsEnabled = true; bttUnsver4P2.IsEnabled = true;

txtScoreP1.Text = countTrueUnsverP1.ToString(); txtScoreP2.Text = countTrueUnsverP2.ToString();

Gen_Quest();

}

67

//private void btnUnsverP1Text_Tapped(object sender, TappedRoutedEventArgs e) //{

//string unsver = (sender as TextBlock).Text.ToString();

//if (unsver == items[randNumb].trueUnsver)

//{

//countTrueUnsverP1++;

//P1Unsverd = true;

//}

//bttUnsver1P1.IsEnabled = false;

//bttUnsver2P1.IsEnabled = false;

//bttUnsver3P1.IsEnabled = false;

//bttUnsver4P1.IsEnabled = false;

//touch++;

//if (touch == 2)

//{

//NextQuest();

//}

//}

private void btnUnsverP2Text_Tapped(object sender, TappedRoutedEventArgs e)

{

string unsver = (sender as TextBlock).Text.ToString();

if (unsver == items[randNumb].trueUnsver)

{

countTrueUnsverP2++; P2Unsverd = true;

}

bttUnsver1P2.IsEnabled = false; bttUnsver2P2.IsEnabled = false; bttUnsver3P2.IsEnabled = false; bttUnsver4P2.IsEnabled = false;

touch++;

if (touch == 2)

{

NextQuest();

}

}

//private void btnUnsverP1_Click(object sender, RoutedEventArgs e) //{

//string unsver = (sender as Button).Content.ToString();

//if (unsver == items[randNumb].trueUnsver)

//{

//countTrueUnsverP1++;

//P2Unsverd = true;

//}

//bttUnsver1P1.IsEnabled = false;

68

//bttUnsver2P1.IsEnabled = false;

//bttUnsver3P1.IsEnabled = false;

//bttUnsver4P1.IsEnabled = false;

//touch++;

//if (touch == 2)

//{

//NextQuest();

//}

//}

private void btnUnsverP1Text_Tapped(object sender, TappedRoutedEventArgs e)

{

string unsver = (sender as TextBlock).Text.ToString(); if (unsver == items[randNumb].trueUnsver)

{

countTrueUnsverP1++; P1Unsverd = true;

}

bttUnsver1P1.IsEnabled = false; bttUnsver2P1.IsEnabled = false; bttUnsver3P1.IsEnabled = false; bttUnsver4P1.IsEnabled = false;

touch++;

if (touch == 2)

{

NextQuest();

}

}

}

}

Клас Ragistration.xaml.cs

using System;

using System.Collections.Generic; using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime; using System.Xml.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections; using Windows.Storage;

using Windows.UI.Popups; using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation;

69

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Quiz

{

///<summary>

///An empty page that can be used on its own or navigated to within a Frame.

///</summary>

public sealed partial class Registration : Page

{

public Registration()

{

this.InitializeComponent();

}

///<summary>

///Invoked when this page is about to be displayed in a Frame.

///</summary>

///<param name="e">Event data that describes how this page was reached.

///This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e)

{

}

private async void btnRegist_Click(object sender, RoutedEventArgs e)

{

Classes.User[] com;

Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync("Users.xml");

string users_text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

XDocument xDoc = XDocument.Parse(users_text);

com = xDoc.Root.Elements("user").Select(p =>

{

return new Classes.User()

{

login = p.Element("login").Value, pass = p.Element("pass").Value

}; }).ToArray();

foreach (var item in com)

{

if (item.login == txtLogin.Text && item.pass ==txtPass.Password)

{

MessageDialog md = new MessageDialog("Пользователья с таким именем

уже существует!");

await md.ShowAsync();

}

return;

}

string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

70

string newText = text.Replace("</users>", "\n<user>"

+"\n<login>"

+txtLogin.Text

+"</login>"

+"\n<pass>"

+txtPass.Password

+"</pass>"

+"\n</user>"

+"\n</users>");

StorageFile sampleFile2 = await storageFolder.CreateFileAsync("Users.xml", CreationCollisionOption.OpenIfExists);

await Windows.Storage.FileIO.WriteTextAsync(sampleFile2, newText);

System.Diagnostics.Debug.WriteLine(newText);

MessageDialog md1 = new MessageDialog("Пользователь успешно добавлен!"); await md1.ShowAsync();

Frame.GoBack();

}

private void btnBack_Click(object sender, RoutedEventArgs e)

{

Frame.GoBack();

}

}

}

Клас Records.xaml.cs

using System;

using System.Collections.Generic; using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime; using System.Xml.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections; using Windows.Storage;

using Windows.UI.Popups; using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Quiz

{

/// <summary>

71

///An empty page that can be used on its own or navigated to within a Frame.

///</summary>

public sealed partial class Registration : Page

{

public Registration()

{

this.InitializeComponent();

}

///<summary>

///Invoked when this page is about to be displayed in a Frame.

///</summary>

///<param name="e">Event data that describes how this page was reached.

///This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e)

{

}

private async void btnRegist_Click(object sender, RoutedEventArgs e)

{

Classes.User[] com;

Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync("Users.xml");

string users_text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

XDocument xDoc = XDocument.Parse(users_text);

com = xDoc.Root.Elements("user").Select(p =>

{

return new Classes.User()

{

login = p.Element("login").Value, pass = p.Element("pass").Value

}; }).ToArray();

foreach (var item in com)

{

if (item.login == txtLogin.Text && item.pass ==txtPass.Password)

{

MessageDialog md = new MessageDialog("Пользователья с таким именем

уже существует!");

await md.ShowAsync();

}

return;

}

string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

string newText = text.Replace("</users>", "\n<user>"

+"\n<login>"

+txtLogin.Text

+"</login>"

+"\n<pass>"

72

+txtPass.Password

+"</pass>"

+"\n</user>"

+"\n</users>");

StorageFile sampleFile2 = await storageFolder.CreateFileAsync("Users.xml", CreationCollisionOption.OpenIfExists);

await Windows.Storage.FileIO.WriteTextAsync(sampleFile2, newText);

System.Diagnostics.Debug.WriteLine(newText);

MessageDialog md1 = new MessageDialog("Пользователь успешно добавлен!"); await md1.ShowAsync();

Frame.GoBack();

}

private void btnBack_Click(object sender, RoutedEventArgs e)

{

Frame.GoBack();

}

}

}

Клас OnePlayer2.xaml.cs

using Newtonsoft.Json.Linq; using System;

using System.Collections.Generic; using System.IO;

using System.Linq; using System.Net.Http;

using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation;

using Windows.Foundation.Collections; using Windows.Storage;

using Windows.UI.Popups; using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Quiz

{

///<summary>

///An empty page that can be used on its own or navigated to within a Frame.

///</summary>

public sealed partial class OnePlayer2 : Page

{

List<Classes.Items> items;

73

int randNumb;

int countTrueUnsver = 0; string category;

public OnePlayer2()

{

this.InitializeComponent();

}

///<summary>

///Invoked when this page is about to be displayed in a Frame.

///</summary>

///<param name="e">Event data that describes how this page was reached.

///This parameter is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e)

{

category = e.Parameter.ToString();

}

private void Gen_Quest()

{

Random rand = new Random();

randNumb = rand.Next(0, items.Count);

txtQuestion.Text = items[randNumb].question;

}

private async void Page_Loaded(object sender, RoutedEventArgs e)

{

using (HttpClient httpClient = new HttpClient())

{

HttpRequestMessage request = new HttpRequestMessage(); httpClient.BaseAddress = new Uri("https://api.parse.com/1/classes/" +

category);

request.Method = new HttpMethod("GET"); request.Headers.Add("X-Parse-Application-Id",

"q8r9XYoXbEw1kXsWIXbyCNQm9ylcWfAS0fqlWWNx"); request.Headers.Add("X-Parse-REST-API-Key",

"2nke5LSbXQmTiA9FaVytlBihjOOz4fPcoxjEHlee");

var response = await httpClient.SendAsync(request); var text = await response.Content.ReadAsStringAsync();

System.Diagnostics.Debug.WriteLine(text);

JObject results = JObject.Parse(text); items = new List<Classes.Items>();

foreach (var result in results["results"])

{

items.Add(new Classes.Items() { question = (string)result["Question"], trueUnsver = (string)result["trueUnsver"] });

}

Gen_Quest();

}

}

74

private async void Button_Click(object sender, RoutedEventArgs e)

{

if (string.Equals(txtBox.Text, items[randNumb].trueUnsver, StringComparison.CurrentCultureIgnoreCase))

{

MessageDialog md1 = new MessageDialog("Это был правильный ответ!"); await md1.ShowAsync();

txtBox.Text = ""; countTrueUnsver++;

txtScore.Text = countTrueUnsver.ToString(); Gen_Quest();

}

else

{

MessageDialog md1 = new MessageDialog("Вы отетили не верно! Правильный ответ: " + items[randNumb].trueUnsver);

await md1.ShowAsync();

MessageDialog md = new MessageDialog("Игра окночена! Сохранить результат

в рекордах?");

UICommand cancelBtn = new UICommand("Нет"); cancelBtn.Invoked = cancelBtn_CLick; UICommand yesBtn = new UICommand("Да"); yesBtn.Invoked = yesBtn_CLick;

md.Commands.Add(cancelBtn);

md.Commands.Add(yesBtn);

await md.ShowAsync();

}

}

private async void yesBtn_CLick(IUICommand command)

{

HttpClient httpClient = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage();

httpClient.BaseAddress = new Uri("https://api.parse.com/1/classes/Record"); httpClient.DefaultRequestHeaders.Accept.Add(new

System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

string data = "{\"Name\":\"" + ApplicationData.Current.LocalSettings.Values["UserName"].ToString() + "\",\"Score\":" + countTrueUnsver + ",\"Mode\":\"MyUnsver\"}";

request.Headers.Add("X-Parse-Application-Id", "q8r9XYoXbEw1kXsWIXbyCNQm9ylcWfAS0fqlWWNx");

request.Headers.Add("X-Parse-REST-API-Key", "2nke5LSbXQmTiA9FaVytlBihjOOz4fPcoxjEHlee");

request.Method = new HttpMethod("POST");

// var response = await httpClient.PostAsync(request, new StringContent(data, System.Text.Encoding.UTF8, "application/json"));

request.Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json");

await httpClient.SendAsync(request);