forked from jasonkneen/UTiL
/
gate.js
98 lines (79 loc) · 2.24 KB
/
gate.js
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
var _locked = Ti.App.Properties.getBool('gate_locked', true);
function keeper(_callback) {
// unlocked
if (!exports.locked) {
_callback(true);
return;
}
// random mathematical question
var x = _random();
var y = _random();
var s = x * y;
// question
var dialog = Ti.UI.createAlertDialog({
title: exports.title,
message: String.format(exports.message, x + 'x' + y),
style: Ti.UI.iPhone.AlertDialogStyle.PLAIN_TEXT_INPUT,
buttonNames: [exports.button]
});
// process answer
dialog.addEventListener('click', function(e) {
// ok
if (parseInt(e.text, 10) === s) {
_callback(true);
// wrong
} else {
Ti.UI.createAlertDialog({
title: exports.error_title,
message: String.format(exports.error_message, '' + s),
buttonNames: [exports.error_button]
}).show();
_callback(false);
}
});
// show dialog
dialog.show();
}
function toggle(_callback) {
// lock
if (!exports.locked) {
exports.locked = true;
if (typeof _callback !== 'undefined') {
_callback(exports.locked);
}
return;
}
// gate to unlock
keeper(function (success) {
exports.locked = !success;
if (typeof _callback !== 'undefined') {
_callback(exports.locked);
}
});
}
// get random number
function _random() {
return Math.floor(Math.random() * (exports.to - exports.from + 1) + exports.from);
}
// options
exports.from = 1;
exports.to = 10;
Object.defineProperty(exports, "locked", {
get: function () {
return _locked;
},
set: function (locked) {
_locked = !!locked;
Ti.App.Properties.setBool('gate_locked', _locked);
}
});
// texts
exports.title = L('gate_title', 'Age check');
exports.message = L('gate_message', 'How much is %s ?');
exports.button = L('gate_button', 'Continue');
exports.error_title = L('gate_error_title', 'Age check failed');
exports.error_message = L('gate_error_message', 'The solution was %s');
exports.error_button = L('gate_error_button', 'OK');
// methods
exports.keeper = keeper;
exports.toggle = toggle;