-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHubViewController.m
378 lines (288 loc) · 13.8 KB
/
HubViewController.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// HubViewController.m
// Remember
//
// Created by Lea Marolt on 1/12/14.
// Copyright (c) 2014 Lea Marolt Sonnenschein. All rights reserved.
//
#import "HubViewController.h"
#import "FirstViewController.h"
#import <Parse/Parse.h>
#import "Option.h"
#import "DailyDareViewController.h"
@interface HubViewController () {
int num;
int gamezToday;
}
@end
@implementation HubViewController {
NSString *name;
NSArray *theWords;
NSArray *theShapes;
int randomWord;
int randomShape;
UIColor *randomColor;
Option *dailyOption;
NSNumber *shapeWithShapeAndColor;
NSNumber *wordWithTextAndColor;
NSMutableArray *optionsArr;
BOOL chosenOption;
CGFloat hue;
CGFloat brightness;
CGFloat saturation;
NSMutableArray *options;
NSMutableArray *stuffForParse;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.a
PFUser *currentUser = [PFUser currentUser];
name = [currentUser objectForKey:@"name"];
self.welcomeLabel.text = [NSString stringWithFormat:@"Welcome back, %@!", name];
theWords = [[NSMutableArray alloc] initWithObjects:@"bird", @"snake", @"table", @"keys", @"picture", @"radio", @"folder", @"hanger", @"post", @"cucumber", @"elephant", @"crocodile", @"plastic", @"mortgage", @"sinister", @"sleep", @"park", @"prison", @"level", @"smile", @"stop", @"spot", @"elastic", @"gorge", @"mister", @"slap", @"fonder", @"hamper", @"rake", @"lake", @"letter", @"better", @"dark", @"eagle", @"eager", nil];
theShapes = [[NSMutableArray alloc] initWithObjects:@"circleEmptyReverse", @"triangleEmptyReverse", @"squareEmptyReverse", @"circleFullReverse", @"triangleFullReverse", @"squareFullReverse", nil];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [defaults dictionaryRepresentation];
// NSLog(@"my defaults: %@", dict);
}
- (void) viewDidAppear:(BOOL)animated {
}
- (void) viewWillAppear:(BOOL)animated {
[self winkyToSmiely];
[NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(animateSmiley)
userInfo:nil
repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:2.3
target:self
selector:@selector(smileyToWinky)
userInfo:nil
repeats:NO];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//NSLog(@"Games played today: %li", (long)[userDefaults integerForKey:@"GamesToday"]);
self.gamesPlayed.text = [NSString stringWithFormat:@"%li", (long)[userDefaults integerForKey:@"GamesToday"]];
[self resetNumOfGamesPlayedInDay];
if (![userDefaults boolForKey:@"dareAccepted"]) {
self.dailyDareState.text = @"Not accepted";
}
// if ([userDefaults boolForKey:@"firstRunOfDay"]) {
// [self setGamesPlayedToZeroOnNewDay];
// }
}
- (void) smileyToWinky {
self.smileyView.image = [UIImage imageNamed:@"winky"];
}
- (void) winkyToSmiely {
self.smileyView.image = [UIImage imageNamed:@"smiley"];
}
- (float)timeToDailyReset {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
int hour = [components hour];
int minute = [components minute];
int seconds = [components second];
float timeToReset = 24*60*60 - hour*60*60 - minute*60 - seconds;
return timeToReset;
}
- (void) animateSmiley {
CABasicAnimation *layerAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
layerAnimation.duration = 0.8;
layerAnimation.beginTime = CACurrentMediaTime() + 1.5;
layerAnimation.valueFunction = [CAValueFunction functionWithName:kCAValueFunctionRotateZ];
layerAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
layerAnimation.fromValue = [NSNumber numberWithFloat:0.0];
layerAnimation.toValue = [NSNumber numberWithFloat:6];
layerAnimation.byValue = [NSNumber numberWithFloat:1];
[self.smileyView.layer addAnimation:layerAnimation forKey:@"layerAnimation"];
}
- (void) resetNumOfGamesPlayedInDay {
// get the current time, to know when the next day starts to reset the amount
// of total games played during the day back to 0
float timeToReset = [self timeToDailyReset];
[NSTimer scheduledTimerWithTimeInterval:timeToReset
target:self
selector:@selector(setGamesPlayedToZeroOnNewDay)
userInfo:nil
repeats:NO];
}
- (void) setGamesPlayedToZeroOnNewDay {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
PFUser *currentUser = [PFUser currentUser];
[currentUser addObject:[NSNumber numberWithInt:[userDefaults integerForKey:@"GamesToday"]] forKey:@"gamesToday"];
[currentUser addObject:[NSNumber numberWithInt:[userDefaults integerForKey:@"GamesWonToday"]] forKey:@"gamesWonToday"];
[currentUser saveInBackground];
[currentUser addObject:[NSNumber numberWithInt:[userDefaults integerForKey:@"dailyDareWon"]] forKey:@"dailyDareWon"];
[currentUser saveInBackground];
//NSLog(@"Games today: %li and Games won today: %li", [userDefaults integerForKey:@"GamesToday"], (long)[userDefaults integerForKey:@"GamesWonToday"]);
[userDefaults setInteger:0 forKey:@"GamesToday"];
[userDefaults setInteger:0 forKey:@"GamesWonToday"];
[userDefaults setBool:YES forKey:@"firstRunOfDay"];
[userDefaults setBool:YES forKey:@"firstSelectionRunOfDay"];
// NSLog(@"First selection run of day: %d", [userDefaults boolForKey:@"firstSelectionRunOfDay"]);
[userDefaults setBool:YES forKey:@"lastRunOfDay"];
[userDefaults setBool:YES forKey:@"dailyDareOpenedForFirstTime"];
[userDefaults setBool:YES forKey:@"firstAppRun"];
[userDefaults setBool:YES forKey:@"throughSelection"];
[userDefaults setBool:YES forKey:@"resetCalled"];
NSLog(@"I JUST RESET EVERYTHING!!!");
// NSLog(@"After the reset: Games today: %li and Games won today: %li", [userDefaults integerForKey:@"GamesToday"], [userDefaults integerForKey:@"GamesWonToday"]);
//
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"deviceType" equalTo:@"ios"];
// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
withMessage:@"Did you remember to activate your daily dare?"];
}
- (void) setUpDailyDare {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL firstDailyDareRunOfDay = [userDefaults boolForKey:@"dailyDareOpenedForFirstTime"];
if (firstDailyDareRunOfDay) {
// NSLog(@"AM I EVER GETTING HERE?");
[self saveInfoInUserDefaults];
}
[self setUpDailyOption];
}
- (void) setUpDailyAnswers {
options = [[NSMutableArray alloc] init];
[self setUpDailyOption];
[options addObject:dailyOption];
for (int i = 0; i < 2; i++) {
int secondShape = arc4random() % ([theShapes count] - 1);
int secondWord = arc4random() % ([theWords count] - 1);
UIColor *secondColor = [self randomColor];
Option *option = [[Option alloc] optionWithShape:theShapes[secondShape] andWord:theWords[secondWord] andColor:secondColor];
[options addObject:option];
}
}
- (Option *)setUpDailyOption {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
long int shape = [userDefaults integerForKey:@"randomShape"];
long int word = [userDefaults integerForKey:@"randomWord"];
long int iH = [userDefaults integerForKey:@"hue"];
long int iS = [userDefaults integerForKey:@"saturation"];
long int iB = [userDefaults integerForKey:@"brightness"];
CGFloat h = iH / 255.0;
CGFloat s = iS / 255.0;
CGFloat b = iB / 255.0;
UIColor *color = [UIColor colorWithHue:h saturation:s brightness:b alpha:1.0];
dailyOption = [[Option alloc] optionWithShape:[UIImage imageNamed:theShapes[shape]] andWord:theWords[word] andColor:color];
[userDefaults setObject:theShapes[shape] forKey:@"theDailyShape"];
return dailyOption;
}
- (UIColor *)randomColor
{
hue = (arc4random() % 256 / 256.0f);
saturation = (arc4random() % 128 / 256.0f) + 0.5f;
brightness = (arc4random() % 128 / 256.0f) + 0.5f;
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0f];
return color;
}
- (void) saveInfoInUserDefaults {
shapeWithShapeAndColor = [NSNumber numberWithBool:YES];
wordWithTextAndColor = [NSNumber numberWithBool:YES];
optionsArr = [[NSMutableArray alloc] init];
[optionsArr addObject:shapeWithShapeAndColor];
[optionsArr addObject:wordWithTextAndColor];
int choseOption = arc4random() % 1;
chosenOption = [optionsArr[choseOption] boolValue];
randomWord = arc4random()% ([theWords count] - 1);
randomShape = arc4random()% ([theShapes count] - 1);
randomColor = [self randomColor];
// converting the color into CGFloats, and then ints, so it can be stores in
// NSUserdefaults, and then converted back to the correct color
// There's probably an easier way to do this using categories, but everything I've
// found up to now deals with RGB nos HSB
CGFloat hFloat,sFloat,bFloat,aFloat;
[randomColor getHue:&hFloat saturation:&sFloat brightness:&bFloat alpha:&aFloat];
int h, s, b;
h = (int)(255.0 * hFloat);
s = (int)(255.0 * sFloat);
b = (int)(255.0 * bFloat);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:randomWord forKey:@"randomWord"];
[userDefaults setInteger:randomShape forKey:@"randomShape"];
[userDefaults setInteger:h forKey:@"hue"];
[userDefaults setInteger:s forKey:@"saturation"];
[userDefaults setInteger:b forKey:@"brightness"];
[userDefaults setBool:chosenOption forKey:@"chosenOption"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) recieveDataForDailyeDare:(BOOL)dareAccepted {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:@"dareAccepted"];
self.acceptedDare = YES;
self.dailyDareState.text = @"Accepted";
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"gameSegue"]) {
FirstViewController *fvc = [segue destinationViewController];
fvc.delegate = self;
// NSLog(@"FVC.DELEGATE IS %@", fvc.delegate);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL firstRunOfDay = [userDefaults boolForKey:@"firstRunOfDay"];
if (firstRunOfDay) {
fvc.games = num;
}
}
if ([segue.identifier isEqualToString:@"dailyDareSegue"]) {
DailyDareViewController *ddvc = [segue destinationViewController];
ddvc.delegate = self;
// is user hasn't clicked on accept dare, set it up, otherwise set up lockedView
NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
BOOL acceptedDare = [userDefauls boolForKey:@"dareAccepted"];
BOOL shapeSelected = [userDefauls boolForKey:@"chosenOption"];
if (!acceptedDare) {
[self setUpDailyDare];
ddvc.recievedDailyOption = dailyOption;
ddvc.selectedOption = shapeSelected;
[userDefauls setBool:YES forKey:@"dailyDareOpenedForFirstTime"];
}
else if (acceptedDare) {
[self setUpDailyAnswers];
ddvc.optionsArray = options;
ddvc.selectedOption = shapeSelected;
}
}
}
//- (void)recieveDataForGamesPlayed:(int) games andGamesWon:(int)gamesWon; {
// // get the amount od games played during the last session, and adds it to the total
// // amount of games played today
//
// NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(@"games this round: %i", games);
//
// gamezToday = [userDefaults integerForKey:@"GamesToday"] + games;
// NSLog(@"games played today: %i", gamezToday);
// [userDefaults setInteger:gamezToday forKey:@"GamesToday"];
//
// //NSLog(@"Games won today: %i", gamesWon);
//}
- (void)recieveOverallData:(int) gamesToday andWonData:(int) gamesWon {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(@"games this round in HUB: %i", gamesToday);
// NSLog(@"Before adding to GamesToday: %i", [userDefaults integerForKey:@"GamesToday"]);
//
// gamezToday = [userDefaults integerForKey:@"GamesToday"] + gamesToday;
// NSLog(@"games played today: %i", gamezToday);
[userDefaults setInteger:gamesToday forKey:@"GamesToday"];
[userDefaults setInteger:gamesWon forKey:@"GamesWonToday"];
// NSLog(@"What's actually in GamesToday: %li", (long)[userDefaults integerForKey:@"GamesToday"]);
// NSLog(@"What's actually in GamesWonToday: %li", (long)[userDefaults integerForKey:@"GamesWonToday"]);
}
@end