forked from microsoft/Windows-appsample-networkhelper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientViewModel.cs
191 lines (173 loc) · 7.25 KB
/
ClientViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using QuizGame.Common;
using QuizGame.Model;
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml;
namespace QuizGame.ViewModel
{
public class ClientViewModel : BindableBase
{
private IClientCommunicator ClientCommunicator { get; set; }
private bool IsQuestionAnswered { get; set; }
public ClientViewModel(IClientCommunicator clientCommunicator)
{
if (clientCommunicator == null) throw new ArgumentNullException("clientCommunicator");
this.ClientCommunicator = clientCommunicator;
Func<DispatchedHandler, Task> callOnUiThread = async (handler) => await
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, handler);
this.ClientCommunicator.GameAvailable += async (s, e) =>
await callOnUiThread(() => this.CanJoin = true);
this.ClientCommunicator.NewQuestionAvailable += async (s, e) =>
await callOnUiThread(() => this.CurrentQuestion = e.Question);
this.ClientCommunicator.PlayerJoined += async (s, e) =>
await callOnUiThread(() =>
{
this.IsJoined = e.IsJoined;
this.ErrorMessageVisibility = e.IsJoined ? Visibility.Collapsed : Visibility.Visible;
});
this.ClientCommunicator.InitializeAsync();
}
public string StateName
{
get { return this.stateName; }
set { this.SetProperty(ref this.stateName, value); }
}
private string stateName;
public string PlayerName
{
get { return this.playerName ?? string.Empty; }
set
{
if (this.SetProperty(ref this.playerName, value))
{
this.ErrorMessageVisibility = Visibility.Collapsed;
this.JoinGameCommand.RaiseCanExecuteChanged();
}
}
}
private string playerName;
public bool CanJoin
{
get { return this.canJoin && this.PlayerName != null && this.PlayerName.Length != 0; }
set
{
if (this.SetProperty(ref this.canJoin, value))
{
this.JoinGameCommand.RaiseCanExecuteChanged();
}
}
}
private bool canJoin;
public bool IsJoined
{
get { return this.isJoined; }
set
{
if (this.SetProperty(ref this.isJoined, value))
{
this.OnPropertyChanged(nameof(JoinVisibility));
this.OnPropertyChanged(nameof(GameUnderwayVisibility));
this.OnPropertyChanged(nameof(QuestionAvailableVisibility));
}
this.StateName = this.isJoined ? "stand by..." : "lobby";
}
}
private bool isJoined;
public Visibility ErrorMessageVisibility
{
get { return this.errorMessageVisibility; }
set { this.SetProperty(ref this.errorMessageVisibility, value); }
}
private Visibility errorMessageVisibility = Visibility.Collapsed;
public Visibility JoinVisibility
{
get { return !this.IsJoined ? Visibility.Visible : Visibility.Collapsed; }
}
public Visibility GameUnderwayVisibility
{
get { return this.IsJoined ? Visibility.Visible : Visibility.Collapsed; }
}
public Visibility QuestionAvailableVisibility
{
get { return this.IsJoined && this.CurrentQuestion != null ? Visibility.Visible : Visibility.Collapsed; }
}
public Question CurrentQuestion
{
get { return this.currentQuestion; }
set
{
if (this.IsJoined && this.SetProperty(ref this.currentQuestion, value))
{
this.OnPropertyChanged(nameof(QuestionAvailableVisibility));
this.StateName = this.currentQuestion == null ? "lobby" : "make a selection...";
this.IsQuestionAnswered = false;
this.AnswerQuestionCommand.RaiseCanExecuteChanged();
}
}
}
private Question currentQuestion;
public DelegateCommand JoinGameCommand
{
get
{
return this.joinGameCommand ?? (this.joinGameCommand = new DelegateCommand(
() => this.ClientCommunicator.JoinGameAsync(this.PlayerName),
() => this.CanJoin));
}
}
private DelegateCommand joinGameCommand;
public DelegateCommand LeaveGameCommand
{
get
{
return this.leaveGameCommand ?? (this.leaveGameCommand = new DelegateCommand(
() => {
this.ClientCommunicator.LeaveGameAsync(this.PlayerName);
this.IsJoined = false;
this.CurrentQuestion = null;
}));
}
}
private DelegateCommand leaveGameCommand;
public DelegateCommand<string> AnswerQuestionCommand
{
get
{
return this.answerQuestionCommand ?? (this.answerQuestionCommand = new DelegateCommand<string>(
option => {
this.ClientCommunicator.AnswerQuestionAsync(PlayerName, Int32.Parse(option));
this.IsQuestionAnswered = true;
this.AnswerQuestionCommand.RaiseCanExecuteChanged();
this.StateName = "thank you";
},
option => !this.IsQuestionAnswered));
}
}
private DelegateCommand<string> answerQuestionCommand;
}
}