-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathauthoredHints.js
More file actions
228 lines (202 loc) · 6.18 KB
/
Copy pathauthoredHints.js
File metadata and controls
228 lines (202 loc) · 6.18 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @overview helper class to manage the state of the Authored Hint UI.
* Used exclusively by StudioApp.
*/
import msg from '@cdo/locale';
import authoredHintUtils from './authoredHintUtils';
import {TestResults} from './constants';
import {getStore} from './redux';
import {
enqueueHints,
showNextHint,
displayMissingBlockHints,
} from './redux/authoredHints';
import {setHasAuthoredHints} from './redux/instructions';
import {
tryGetSessionStorage,
trySetSessionStorage,
showGenericQtip,
createEvent,
} from './utils';
const ONETIME_HINT_PROMPT_SEEN_LEVELS = 'hint_prompt_seen_levels';
export default class AuthoredHints {
constructor(studioApp) {
this.studioApp_ = studioApp;
/**
* @type {number}
*/
this.scriptId_ = undefined;
/**
* @type {number}
*/
this.levelId_ = undefined;
}
/**
* @return {AuthoredHints[]}
*/
getUnseenHints() {
return getStore().getState().authoredHints.unseenHints;
}
/**
* @return {AuthoredHints[]}
*/
getSeenHints() {
return getStore().getState().authoredHints.seenHints;
}
/**
* Creates contextual hints for the specified blocks and adds them to
* the queue of hints to display. Triggers an animation on the hint
* lightbulb if the queue has changed.
* @param {BlockHint[]} blocks {@see authoredHintUtils.createContextualHintsFromBlocks}
*/
displayMissingBlockHints(blocks) {
const newContextualHints =
authoredHintUtils.createContextualHintsFromBlocks(blocks);
getStore().dispatch(displayMissingBlockHints(newContextualHints));
if (newContextualHints.length > 0 && this.getUnseenHints().length > 0) {
getStore().dispatch(setHasAuthoredHints(true));
}
}
/**
* @param {LiveMilestoneResponse} response
*/
finishHints(response) {
authoredHintUtils.finishHints({
time: new Date().getTime() - this.studioApp_.initTime,
attempt: this.studioApp_.attempts,
testResult: this.studioApp_.lastTestResult,
levelSourceId: response && response.level_source_id,
});
}
/**
* @param {string} url
*/
submitHints(url) {
authoredHintUtils.submitHints(url);
}
/**
* @param {AuthoredHint[]} hints
* @param {String[]} hintsUsedIds
* @param {number} scriptId
* @param {number} levelId
*/
init(hints, hintsUsedIds, scriptId, levelId) {
this.scriptId_ = scriptId;
this.levelId_ = levelId;
if (hints && hints.length > 0) {
getStore().dispatch(enqueueHints(hints, hintsUsedIds));
getStore().dispatch(setHasAuthoredHints(true));
}
}
/**
* @return {(AuthoredHint|undefined)} hint
*/
showNextHint() {
if (this.getUnseenHints().length === 0) {
return;
}
const hint = this.getUnseenHints()[0];
this.recordUserViewedHint_(hint);
// Notify game types that implement the `displayHintPath` listener to draw
// hint paths in the visualization area.
if (hint.hintPath && hint.hintPath.length) {
const event = createEvent('displayHintPath');
event.detail = hint.hintPath;
window.dispatchEvent(event);
}
return hint;
}
/**
* Mostly a passthrough to authoredHintUtils.recordUnfinishedHint. Also
* marks the given hint as seen.
* @param {AuthoredHint} hint
*/
recordUserViewedHint_(hint) {
getStore().dispatch(showNextHint(hint));
authoredHintUtils.recordUnfinishedHint({
// level info
scriptId: this.scriptId_,
levelId: this.levelId_,
// hint info
hintId: hint.hintId,
hintClass: hint.hintClass,
hintType: hint.hintType,
});
}
/**
* Get the set of level ids for which the onetime hint prompt has already been
* seen this session
*
* @returns {number[]}
*/
getOnetimeHintPromptSeenLevelIds() {
// default to a JSONified empty array if undefined or on error
const defaultValue = '[]';
const sessionValue = tryGetSessionStorage(
ONETIME_HINT_PROMPT_SEEN_LEVELS,
defaultValue
);
return JSON.parse(sessionValue || defaultValue);
}
/**
* @returns {boolean} whether or not the onetime hint prompt has already been
* seen this level this session
*/
onetimeHintPromptSeenThisLevel() {
const thisLevel = this.levelId_;
const seenLevels = this.getOnetimeHintPromptSeenLevelIds();
return seenLevels.includes(thisLevel);
}
/**
* Method to determine whether or not the user should be shown the onetime
* just-in-time hint prompt suggesting that they use a hint for this level.
*
* Users will see the hint prompt after their program finishes executing if:
* - They have not passed or perfected the puzzle,
* - And there are one or more hints available in the hint well,
* - And they have not seen the hint prompt on this puzzle in the current session,
* - And they have not used a hint on this puzzle yet,
* - And their total number of runs on this puzzle exceeds the run threshold.
*
* @returns {boolean}
*/
shouldShowOnetimeHintPrompt() {
const puzzleUnpassed =
this.studioApp_.lastTestResult < TestResults.MINIMUM_PASS_RESULT;
const hintsAvailable = this.getUnseenHints().length > 0;
const notSeenHintPromptThisLevel = !this.onetimeHintPromptSeenThisLevel();
const noHintsViewed = this.getSeenHints().length === 0;
const runsOverThreshold =
this.studioApp_.attempts >=
this.studioApp_.config.level.hintPromptAttemptsThreshold;
return (
puzzleUnpassed &&
hintsAvailable &&
notSeenHintPromptThisLevel &&
noHintsViewed &&
runsOverThreshold
);
}
considerShowingOnetimeHintPrompt() {
if (this.shouldShowOnetimeHintPrompt()) {
this.showOnetimeHintPrompt();
}
}
showOnetimeHintPrompt() {
// mark prompt as having been seen for this level
const seenLevels = this.getOnetimeHintPromptSeenLevelIds();
seenLevels.push(this.levelId_);
trySetSessionStorage(
ONETIME_HINT_PROMPT_SEEN_LEVELS,
JSON.stringify(seenLevels)
);
// show prompt
const title = msg.onetimeHintPromptTitle();
const message = msg.onetimeHintPromptMessage();
const position = {
my: 'top left',
at: 'bottom right',
};
showGenericQtip('#lightbulb', title, message, position);
}
}