-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScoreViewController.m
129 lines (98 loc) · 3.55 KB
/
ScoreViewController.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
//
// ScoreViewController.m
// Bluetooth2
//
// Created by Gurupur Sushma Pai & Aparna Srinivasan on 2/19/13.
// Copyright (c) Gurupur Sushma Pai & Aparna Srinivasan All rights reserved.
//
#import "ScoreViewController.h"
@implementation ScoreViewController
// Generates the setter and getter methods automatically.
@synthesize totalGamesPlayed,
totalGamesWon,
bestScoreTillDate,
plistGamesPlayed,
plistGamesWon,
scoresList;
- (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.
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"gameScoreList.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"gameScoreList" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
// assign values
plistGamesPlayed = [temp objectForKey:@"totalGames"];
plistGamesWon = [temp objectForKey:@"totalWon"];
scoresList = [NSMutableArray arrayWithArray:[temp objectForKey:@"scores"]];
NSLog(@"%d",[scoresList count]);
// display values
totalGamesWon.text = plistGamesWon.stringValue;
totalGamesPlayed.text = plistGamesPlayed.stringValue;
// pick up the best score from the list.
int maxValue = 0;
for(int i = 0 ; i < [scoresList count]; i++)
{
NSNumber *score = [scoresList objectAtIndex:i];
int currentValue = score.intValue;
if(currentValue > maxValue)
maxValue = currentValue;
}
// NSNumber *maximum = [scoresList valueForKeyPath:@"@max.self"];
bestScoreTillDate.text = [NSString stringWithFormat:@"%d",maxValue];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationLandscapeRight);
}
//to go back to the main menu
-(IBAction)goToMainMenu:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end