-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayGameViewController.m
356 lines (282 loc) · 11.7 KB
/
PlayGameViewController.m
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// PlayGameViewController.m
// StereotypeQuiz
//
// Created by pavan krishnamurthy on 6/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "PlayGameViewController.h"
#import "ProfileViewController.h"
#import "AppDelegate.h"
#import "AFJSONRequestOperation.h"
//#import "CustomBadge.h"
@interface PlayGameViewController ()
@end
@implementation PlayGameViewController
@synthesize option1Song;
@synthesize option1artist;
@synthesize option2Song;
@synthesize option2artist;
@synthesize option3Song;
@synthesize option3artist;
@synthesize option4Song;
@synthesize option4artist;
@synthesize questionText;
@synthesize option1Button;
@synthesize option2Button;
@synthesize option3Button;
@synthesize option4Button;
@synthesize scoreLabel;
@synthesize multiplierLabel;
@synthesize lifeLineButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg2.png"]];
lifeline_remaining = 2;
totalscore = 0;
multiplier = 1;
question_num = 0;
NSData *ImagenUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a5.mzstatic.com/us/r1000/025/Music/c0/c4/7f/mzi.dfvpkjls.30x30-50.jpg"]];
[questionText setText:@"What Song do i Like"];
[option1Button setImage:[UIImage imageWithData:ImagenUrl] forState:UIControlStateNormal];
[self loadQuestions];
//[option1Button setTitle:@"Hiii" forState:UIControlStateNormal];
// Do any additional setup after loading the view.
}
- (void)loadQuestions
{
NSArray *tabcontrollers = self.tabBarController.viewControllers;
ProfileViewController *profile = [tabcontrollers objectAtIndex:0];
//NSLog(@"Questions = %@",[profile profiledata]);
NSArray * quiz = [[profile profiledata]objectForKey:@"quiz"];
if(question_num < [quiz count])
{
[questionText setText:[[quiz objectAtIndex:question_num]objectForKey:@"question"]];
NSMutableDictionary *answers = [[quiz objectAtIndex:question_num]objectForKey:@"answers"];
NSLog(@"%@",answers);
NSLog(@"Current Image URL=%@",[[answers objectForKey:@"1"]objectForKey:@"url"]);
NSData *ImagenUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[answers objectForKey:@"1"]objectForKey:@"url"]]];
[option1Button setImage:[UIImage imageWithData:ImagenUrl] forState:UIControlStateNormal];
[option1Song setText:[[answers objectForKey:@"1"]objectForKey:@"track"]];
[option1artist setText:[[answers objectForKey:@"1"]objectForKey:@"artist"]];
ImagenUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[answers objectForKey:@"2"]objectForKey:@"url"]]];
[option2Button setImage:[UIImage imageWithData:ImagenUrl] forState:UIControlStateNormal];
[option2Song setText:[[answers objectForKey:@"2"]objectForKey:@"track"]];
[option2artist setText:[[answers objectForKey:@"2"]objectForKey:@"artist"]];
ImagenUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[answers objectForKey:@"3"]objectForKey:@"url"]]];
[option3Button setImage:[UIImage imageWithData:ImagenUrl] forState:UIControlStateNormal];
[option3Song setText:[[answers objectForKey:@"3"]objectForKey:@"track"]];
[option3artist setText:[[answers objectForKey:@"3"]objectForKey:@"artist"]];
ImagenUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[answers objectForKey:@"4"]objectForKey:@"url"]]];
[option4Button setImage:[UIImage imageWithData:ImagenUrl] forState:UIControlStateNormal];
[option4Song setText:[[answers objectForKey:@"4"]objectForKey:@"track"]];
[option4artist setText:[[answers objectForKey:@"4"]objectForKey:@"artist"]];
current_answer_num = [[[quiz objectAtIndex:question_num]objectForKey:@"correct"]intValue];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"GameOver" message:[NSString stringWithFormat:@"TotalScore: %i",totalscore] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ShareFacebook", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[alert addSubview:myTextField];
[alert show];
}
}
- (void)viewDidUnload
{
[self setQuestionText:nil];
[self setOption1Button:nil];
[self setOption2Button:nil];
[self setOption3Button:nil];
[self setOption4Button:nil];
[self setScoreLabel:nil];
[self setMultiplierLabel:nil];
[self setLifeLineButton:nil];
[self setOption1Song:nil];
[self setOption1artist:nil];
[self setOption2Song:nil];
[self setOption2artist:nil];
[self setOption3Song:nil];
[self setOption3artist:nil];
[self setOption4Song:nil];
[self setOption4artist:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) evaluateanswer {
/*if(question_num == 1 && clicked_button == 1) {
totalscore = totalscore + (multiplier * 100);
multiplier ++;
}
else if (question_num == 2 && clicked_button == 2) {
totalscore = totalscore + (multiplier * 100);
multiplier ++;
}
else if (question_num == 3 && clicked_button == 3) {
totalscore = totalscore + (multiplier * 100);
multiplier ++;
}
else if (question_num == 4 && clicked_button == 4) {
totalscore = totalscore + (multiplier * 100);
multiplier ++;
}*/
NSString *result;
if(clicked_button == current_answer_num) {
totalscore = totalscore + (multiplier * 100);
multiplier ++;
result = @"Correct";
}
else {
multiplier = 1;
result = @"Wrong";
}
[self.navigationItem setTitle:result];
scoreLabel.text = [NSString stringWithFormat:@"Score : %i",totalscore];
multiplierLabel.text = [NSString stringWithFormat:@"Multiplier : %i",multiplier];
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result",
message:result,
delegate:self,
cancelButtonTitle:@"OK"
otherButtonTitles:nil];*/
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:result delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.title != @"GameOver")
{
question_num ++;
option1Button.hidden = false;
option2Button.hidden = false;
option3Button.hidden = false;
option4Button.hidden = false;
option1Song.hidden = false;
option1artist.hidden = false;
option2Song.hidden = false;
option2artist.hidden = false;
option3Song.hidden = false;
option3artist.hidden = false;
option4Song.hidden = false;
option4artist.hidden = false;
[self loadQuestions];
}
else {
/*AppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
//[appdelegate facebook]
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//https://graph.facebook.com/me/stereotypesfm:play
NSString *urlString = @"http://dev.stereotypes.fm/angelhack/mobileauth.php?auth=";
NSString *urlstr = [NSString stringWithFormat:@"%@%@",urlString,[defaults objectForKey:@"FBAccessTokenKey"]];
NSURL *url = [NSURL URLWithString:urlstr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(@"%@",urlstr);
NSString *usertext = [[alertView textFieldAtIndex:0]text];
void (^successBlock)(NSURLRequest *,NSHTTPURLResponse *,id) = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"json = %@",[JSON objectForKey:@"success"]);
};
void (^failureBlock)(NSURLRequest *,NSHTTPURLResponse *,NSError *, id) = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"error: %@", error);
};
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:successBlock failure:failureBlock];
[operation start];*/
}
}
- (IBAction)option1click:(id)sender {
//question_num ++;
clicked_button = 1;
[self evaluateanswer];
}
- (IBAction)option2click:(id)sender {
//question_num ++;
clicked_button = 2;
[self evaluateanswer];
}
- (IBAction)option3click:(id)sender {
//question_num ++;
clicked_button = 3;
[self evaluateanswer];
}
- (IBAction)option4click:(id)sender {
//question_num ++;
clicked_button = 4;
[self evaluateanswer];
}
- (IBAction)lifelineClick:(id)sender {
/*if(question_num == 1) {
option2Button.hidden = TRUE;
option4Button.hidden = TRUE;
}
else if (question_num == 2) {
option1Button.hidden = TRUE;
option3Button.hidden = TRUE;
}
else if (question_num == 3) {
option1Button.hidden = TRUE;
option2Button.hidden = TRUE;
}
else if (question_num == 4) {
option1Button.hidden = TRUE;
option3Button.hidden = TRUE;
}*/
if(lifeline_remaining > 0 )
{
int remove_two = 2;
int already_tried = 0;
while(remove_two > 0)
{
int rand_num = rand()%4;
rand_num ++ ;
if(rand_num != already_tried && rand_num != current_answer_num) {
remove_two --;
already_tried = rand_num;
if(rand_num == 1) {
option1Button.hidden = true;
option1Song.hidden = true;
option1artist.hidden = true;
}
else if(rand_num == 2) {
option2Button.hidden = true;
option2Song.hidden = true;
option2artist.hidden = true;
}
else if(rand_num == 3) {
option3Button.hidden = true;
option3Song.hidden = true;
option3artist.hidden = true;
}
else if(rand_num == 4) {
option4Button.hidden = true;
option4Song.hidden = true;
option4artist.hidden = true;
}
}
}
lifeline_remaining--;
[lifeLineButton setTitle:[NSString stringWithFormat:@"%i",lifeline_remaining] forState:UIControlStateNormal];
/*CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:[NSString stringWithFormat:@"LifeLine:%i",lifeline_remaining]
withStringColor:[UIColor whiteColor]
withInsetColor:[UIColor redColor]
withBadgeFrame:YES
withBadgeFrameColor:[UIColor whiteColor]
withScale:1.0
withShining:YES];
[customBadge1 setFrame:CGRectMake(10,10,10,10)];
[lifeLineButton addSubview:customBadge1];
*/
}
if(lifeline_remaining == 0) {
[lifeLineButton setTitle:[NSString stringWithFormat:@"Buy Lifeline",lifeline_remaining] forState:UIControlStateNormal];
}
}
@end