-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·145 lines (131 loc) · 3.6 KB
/
main.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
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
var apiKey
, mongoURL
, booty
;
function main (stage) {
require([ 'config' ], function(config) {
apiKey = config.apiKey;
mongoURL = config.mongoURL;
});
var headsButton = addButton(0, 0, 'Heads', 'heads.png', handleButtonClick)
, tailsButton = addButton(150, 0, 'Tails', 'tails.png', handleButtonClick)
, resultsTextField = addTextField(10, 50, 100)
, scoreTextField = addTextField(10, 100, 100)
, yourNameTextField = addYourName()
, submitButton = addButton(0, 200, 'Submit', 'submit.png', handleSubmitClick)
, highScoreList = addTextField(10, 250, 500)
;
scoreTextField.score = 0;
function addButton (x, y, guess, url, onClick) {
var buttonLoader = new sp.Loader();
var buttonURL = url;
var buttonURLRequest = new sp.URLRequest(buttonURL);
buttonLoader.load(buttonURLRequest);
buttonLoader.x = x;
buttonLoader.y = y;
buttonLoader.guess = guess;
buttonLoader.addEventListener(sp.MouseEvent.CLICK, onClick);
stage.addChild(buttonLoader);
return buttonLoader;
}
function addTextField (x, y, width) {
var textField = new sp.TextField();
textField.x = x;
textField.y = y;
stage.addChild(textField);
return textField;
}
function addYourName() {
var yourNameLoader = new sp.Loader()
, yourNameURL = 'your_name.swf'
, yourNameURLRequest = new sp.URLRequest(yourNameURL)
;
yourNameLoader.addEventListener(sp.MouseEvent.CLICK, function (event) {
yourNameLoader.content.keystring.text='';
});
yourNameLoader.load(yourNameURLRequest);
yourNameLoader.y = 150;
stage.addChild(yourNameLoader);
return yourNameLoader;
}
function handleButtonClick (event) {
console.log(event.target.guess);
placeBet(event.target.guess, resultsTextField, scoreTextField);
}
function handleSubmitClick (event) {
console.log('handleSubmitClick');
console.log(yourNameTextField);
console.log(booty);
var name = yourNameTextField.content.keystring.text
, score = scoreTextField.score
;
saveScore(name, score);
showScores(highScoreList);
}
}
function showScores(highScoreList) {
$.getJSON(mongoURL + '?apiKey=' + apiKey, function(data) {
highScoreList.text = 'HIGH SCORES\n\n';
for (var i = 0; i < data.length; i++) {
highScoreList.text += data[i].score + ' by ' + data[i].name + '\n';
}
});
}
function saveScore (name, score) {
var mongoQuery = '?apiKey=' + apiKey;
console.log('adding to mongo: name: ' + name + ' score: ' + score);
$.ajax (mongoURL + mongoQuery, {
"data": JSON.stringify( {
"_id" : name
, "name" : name
, "score" : score
} )
, "type": "POST"
, "contentType": "application/json"
});
}
function placeBet (guess, resultsTextField, scoreTextField) {
console.log('placeBet');
var coinState = flipCoin();
if (guess == coinState) {
var winner = true;
}
else {
var winner = false;
}
resultsTextField.text = printResult(coinState, winner);
updateScore(scoreTextField, winner);
}
function flipCoin() {
var toss = Math.random();
console.log(toss);
if (toss < 0.5) {
return 'Heads';
}
else if (toss > 0.5) {
return 'Tails';
}
else {
return flipCoin();
}
}
function printResult (coinState, winner) {
console.log('printResult');
var output = coinState + '!';
if (winner) {
output += ' You Win!';
}
else {
output += ' You Lose :(';
}
return output;
}
function updateScore(scoreTextField, winner) {
if (winner) {
scoreTextField.score++;
}
else {
scoreTextField.score--;
}
scoreTextField.text = 'Your Score: ' + scoreTextField.score;
}