Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PlayButton.m
- //
- // Created by Alexander Behrhof on 5/25/15.
- // Copyright (c) 2015 Alexander Behrhof. All rights reserved.
- //
- #import "PlayButton.h"
- #import "JSONModelLib.h"
- #import "PlayHelper.h"
- @implementation PlayButton
- - (void)createTrackingArea
- {
- NSTrackingAreaOptions focusTrackingAreaOptions = NSTrackingActiveInActiveApp;
- focusTrackingAreaOptions |= NSTrackingMouseEnteredAndExited;
- focusTrackingAreaOptions |= NSTrackingAssumeInside;
- focusTrackingAreaOptions |= NSTrackingInVisibleRect;
- NSTrackingArea *focusTrackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
- options:focusTrackingAreaOptions owner:self userInfo:nil];
- [self addTrackingArea:focusTrackingArea];
- }
- - (void) awakeFromNib {
- [self createTrackingArea];
- }
- - (void) mouseEntered: (NSEvent *) theEvent {
- if ([self isEnabled]) {
- [self setImage:[NSImage imageNamed:@"PLAY_ROLLOVER"]];
- }
- }
- - (void) mouseExited: (NSEvent *) theEvent {
- if ([self isEnabled]) {
- [self setImage:[NSImage imageNamed:@"PLAY_NORMAL"]];
- }
- }
- - (void) mouseUp: (NSEvent*) theEvent {
- if ([self isEnabled]) {
- [self setImage:[NSImage imageNamed:@"PLAY_NORMAL"]];
- }
- }
- - (void) mouseDown: (NSEvent*) theEvent {
- if ([self isEnabled]) {
- [self setImage:[NSImage imageNamed:@"PLAY_DOWN"]];
- AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate];
- NSTextField *_TextUser = appDelegate.TextUser;
- NSTextField *_TextPassword = appDelegate.TextPassword;
- #define allTrim( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]
- [self setEnabled: NO];
- [appDelegate.LabelInfo setTitle:@"Contacting the Toon Council..."];
- [appDelegate.TextUser setEditable: NO];
- [appDelegate.TextUser setTextColor:[NSColor disabledControlTextColor]];
- [appDelegate.TextPassword setEditable: NO];
- [appDelegate.TextPassword setTextColor:[NSColor disabledControlTextColor]];
- NSString *UsernameCheck = _TextUser.stringValue;
- NSString *PasswordCheck = _TextPassword.stringValue;
- NSAlert *alert = [[NSAlert alloc] init];
- [alert addButtonWithTitle:@"Dismiss"];
- if ( [allTrim( UsernameCheck ) length] == 0 || [allTrim( PasswordCheck ) length] == 0 ) {
- [appDelegate.LabelInfo setTitle:@"Please Login!"];
- [alert setMessageText:@"Yikes!"];
- [alert setInformativeText:@"You must enter both your username and password."];
- [alert runModal];
- [self setEnabled: YES];
- [_TextUser setEditable: YES];
- [_TextUser setTextColor:[NSColor controlTextColor]];
- [_TextPassword setEditable: YES];
- [_TextPassword setTextColor:[NSColor controlTextColor]];
- return;
- }
- if ( [allTrim( UsernameCheck ) length] != 0 && [allTrim( PasswordCheck ) length] != 0 ) {
- [[JSONHTTPClient requestHeaders] setValue:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
- //make post, get requests
- [JSONHTTPClient postJSONFromURLWithString:@"http://lw2.ez-webz.com/api/login?format=json" params:@{@"username":_TextUser.stringValue, @"password":_TextPassword.stringValue} completion:^(NSDictionary *json, JSONModelError *err) {
- NSString *success = json[@"success"];
- if ([success isEqualToString:@"true"]) {
- [PlayHelper startGame:json];
- } else if ([success isEqualToString:@"false"]) {
- [self setEnabled: YES];
- [appDelegate.TextUser setEditable: YES];
- [appDelegate.TextUser setTextColor:[NSColor controlTextColor]];
- [appDelegate.TextPassword setEditable: YES];
- [appDelegate.TextPassword setTextColor:[NSColor controlTextColor]];
- _TextPassword.stringValue=@"";
- [appDelegate.LabelInfo setTitle:@"Please Login!"];
- [alert setMessageText:@"Yikes!"];
- [alert setInformativeText:json[@"banner"]];
- [alert runModal];
- } else if ([success isEqualToString:@"partial"]) {
- [appDelegate.twoAuthWindow makeKeyAndOrderFront:self];
- appDelegate.TextAuthToken=json[@"responseToken"];
- } else if ([success isEqualToString:@"delayed"]) {
- NSString *string_eta = [NSString stringWithFormat:@"You'll be in-game in around %@s", json[@"eta"]];
- [appDelegate.LabelInfo setTitle:string_eta];
- [JSONHTTPClient postJSONFromURLWithString:@"http://lw2.ez-webz.com/api/login?format=json" params:@{@"queueToken":json[@"queueToken"]} completion:^(NSDictionary *json, JSONModelError *err) {
- NSString *success = json[@"success"];
- if ([success isEqualToString:@"true"]) {
- [PlayHelper startGame:json];
- } else {
- [self setEnabled: YES];
- [appDelegate.TextUser setEditable: YES];
- [appDelegate.TextUser setTextColor:[NSColor controlTextColor]];
- [appDelegate.TextPassword setEditable: YES];
- [appDelegate.TextPassword setTextColor:[NSColor controlTextColor]];
- _TextPassword.stringValue=@"";
- [appDelegate.LabelInfo setTitle:@"Please Login!"];
- [alert setMessageText:@"Yikes!"];
- [alert setInformativeText:json[@"banner"]];
- [alert runModal];
- }
- }];
- } else {
- [alert setMessageText:@"Yikes!"];
- [alert setInformativeText:@"An error has occurred"];
- [alert runModal];
- }
- if (err) {
- [alert setMessageText:@"Yikes!"];
- [alert setInformativeText:@"An error has occurred"];
- [alert runModal];
- }
- }];
- }
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement