Gui/Postman.m

00001 //
00002 //  Postman.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 06/08/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "Postman.h"
00010 
00011 
00012 @implementation Postman
00013 
00014 // setup 
00015 - (void) awakeFromNib {
00016     // user clicked on message list
00017     [notificationCenter addObserver:self selector:@selector(messageSelected:) name:@"MV_MESSAGE_SELECTION"];
00018     // user clicked on player list
00019     [notificationCenter addObserver:self selector:@selector(playerSelected:) name:@"PV_PLAYER_SELECTION"];
00020     // user manually choose a destination
00021     [notificationCenter addObserver:self selector:@selector(manualSelection:) name:@"GV_MESSAGE_DEST"];
00022 }
00023 
00024 // external event handling
00025 - (void) messageSelected:(NSString*) str {
00026     // the sender may be a player 
00027     if ([str length] == 2) {
00028         // it is is [F|R|O|I] followed by index
00029         // check first
00030         if ([self individualIdOfAdress:[str substringWithRange:NSMakeRange(0,2)]] == nil) {
00031             NSLog(@"Postman.messageSelected can't find a sender in %@", str);
00032             return;
00033         }
00034         [self setDestination:[str substringWithRange:NSMakeRange(0,2)]];        
00035     } else if ([str characterAtIndex:0] == ' ') {
00036         // it is [F|R|O|I] followed by index
00037         // check first
00038         if ([self individualIdOfAdress:[str substringWithRange:NSMakeRange(1,2)]] == nil) {
00039             NSLog(@"Postman.messageSelected can't find a sender in %@", str);
00040             return;
00041         }
00042         [self setDestination:[str substringWithRange:NSMakeRange(1,2)]];
00043     } else if (([str characterAtIndex:2] == ' ') ||
00044                ([str characterAtIndex:2] == '-')) {
00045         // it is is [F|R|O|I] followed by index
00046         // check first
00047         if ([self individualIdOfAdress:[str substringWithRange:NSMakeRange(0,2)]] == nil) {
00048             NSLog(@"Postman.messageSelected can't find a sender in %@", str);
00049             return;
00050         }
00051         [self setDestination:[str substringWithRange:NSMakeRange(0,2)]];
00052     } else {
00053         // must be GOD or ALL or FED.. try:
00054         if ([self individualIdOfAdress:[str substringWithRange:NSMakeRange(0,3)]] == nil) {
00055             NSLog(@"Postman.messageSelected can't find a sender in %@", str);
00056             return;
00057         }
00058         [self setDestination:[str substringWithRange:NSMakeRange(0,3)]];        
00059     }
00060 }
00061 
00062 - (void) playerSelected:(NSString*) str {
00063     // player strings has the id between brackets
00064     NSRange temp = [str rangeOfString:@"("];
00065     if (temp.location == NSNotFound) {
00066         NSLog(@"Postman.playerSelected can't find a sender in %@", str);
00067         return;
00068     }
00069     int start = temp.location + 1;
00070     temp = [str rangeOfString:@")"];
00071     if (temp.location == NSNotFound) {
00072         NSLog(@"Postman.playerSelected can't find a sender in %@", str);
00073         return;
00074     }
00075     int size = temp.location - start; 
00076     
00077     [self messageSelected:[str substringWithRange:NSMakeRange(start, size)]];
00078 }
00079 
00080 - (void) manualSelection:(NSString*) str {
00081     [self setDestination:str];
00082 }
00083 
00084 // getters / setters
00085 - (void) setDestination:(NSString*) dst {
00086     [toField setStringValue:dst];
00087     [commTextField becomeFirstResponder];
00088 }
00089 
00090 - (NSString*) destination {
00091     return [toField stringValue];
00092 }
00093 
00094 - (void) setMessage:(NSString*)msg {
00095     [commTextField setStringValue:msg];
00096 }
00097 
00098 - (NSString*) message {
00099     return [commTextField stringValue];
00100 }
00101 
00102 
00103 // sending logic
00104 - (void) sendCurrentMessage {
00105     [self sendMessage:[self message] to:[self destination]];
00106 }
00107 
00108 - (void) sendMessage:(NSString*)msg to:(NSString*) dst {
00109     
00110     NSNumber *group = [self groupOfAdress:dst];
00111     NSNumber *indiv = [self individualIdOfAdress:dst];
00112     
00113     if (indiv == nil) {
00114         return;
00115     }
00116     
00117     [notificationCenter postNotificationName:@"COMM_SEND_MESSAGE" userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
00118         group, @"group", indiv, @"indiv", msg, @"message", nil]];
00119     
00120     // it won't be echoed by the server, show it here
00121     NSString *localEcho = [NSString stringWithFormat:@"%@ -> %@ %@", 
00122         [[universe playerThatIsMe] mapChars], dst, msg];
00123     [notificationCenter postNotificationName:@"PM_MESSAGE" userInfo:localEcho];    
00124 }
00125 
00126 - (NSNumber *) individualIdOfAdress:(NSString*) address {
00127     if        ([address isEqualToString:@"TEAM"]) {
00128         return [NSNumber numberWithChar:[[[universe playerThatIsMe] team] bitMask]];
00129     } else if ([address isEqualToString:@"ALL"]) {
00130         return [NSNumber numberWithChar:0];
00131     } else if ([address isEqualToString:@"FED"]) {
00132         return [NSNumber numberWithChar:[[universe teamWithId:TEAM_FED] bitMask]];
00133     } else if ([address isEqualToString:@"KLI"]) {
00134         return [NSNumber numberWithChar:[[universe teamWithId:TEAM_KLI] bitMask]];
00135     } else if ([address isEqualToString:@"ORI"]) {
00136         return [NSNumber numberWithChar:[[universe teamWithId:TEAM_ORI] bitMask]];
00137     } else if ([address isEqualToString:@"ROM"]) {
00138         return [NSNumber numberWithChar:[[universe teamWithId:TEAM_ROM] bitMask]];
00139     } else if ([address isEqualToString:@"GOD"]) {
00140         return [NSNumber numberWithChar:255];
00141     } else {
00142         // it must be a player [F|O|R|K][0..f]
00143         // find out the player id
00144         char playerId = [address characterAtIndex:1];
00145         if (playerId >= '0' && playerId <= '9') {
00146             playerId -= '0';
00147         } else if (playerId >= 'a' && playerId <= 'f'){
00148             playerId -= 'a';
00149             playerId += 10;
00150         } else {
00151             [notificationCenter postNotificationName:@"PM_WARNING" userInfo:@"Unknown player. message not sent."];
00152             NSLog(@"Postman.individualIdOfAdress Unknown player %@ message not sent.", address);
00153             return nil;  
00154         }
00155 
00156         // simple sanity check
00157         if ([[universe playerWithId:playerId] status] == PLAYER_FREE) {
00158             [notificationCenter postNotificationName:@"PM_WARNING" userInfo:@"That player left the game. message not sent."];
00159             NSLog(@"Postman.individualIdOfAdress player %@ left game message not sent.", address);
00160             return nil;
00161         }
00162         return [NSNumber numberWithChar:playerId];
00163     }    
00164 }
00165 
00166 - (NSNumber *) groupOfAdress:(NSString*) address {
00167     
00168     if        ([address isEqualToString:@"TEAM"]) {
00169         return [NSNumber numberWithChar:TEAM];
00170     } else if ([address isEqualToString:@"ALL"]) {
00171         return [NSNumber numberWithChar:ALL];
00172     } else if ([address isEqualToString:@"FED"]) {
00173         return [NSNumber numberWithChar:TEAM];
00174     } else if ([address isEqualToString:@"KLI"]) {
00175         return [NSNumber numberWithChar:TEAM];
00176     } else if ([address isEqualToString:@"ORI"]) {
00177         return [NSNumber numberWithChar:TEAM];
00178     } else if ([address isEqualToString:@"ROM"]) {
00179         return [NSNumber numberWithChar:TEAM];
00180     } else if ([address isEqualToString:@"GOD"]) {
00181         return [NSNumber numberWithChar:GOD];
00182     } else {
00183         return [NSNumber numberWithChar:INDIV];
00184     }    
00185 }
00186      
00187 // delegate functions of textfield
00188 - (void)controlTextDidEndEditing:(NSNotification *)aNotification {
00189     if ([[self message] length] > 0) {
00190         NSLog(@"Postman.controlTextDidEndEditing sending message %@", [self message]);
00191         [self sendCurrentMessage];
00192         // clean up since the change of focus when the mouse moves creates a 
00193         // second event that we do not wish to send.
00194         // $$ alternatively check if we are losing first responder status since that is not
00195         // the same as pressing enter
00196         [self setMessage:@""];  
00197     } else {
00198         NSLog(@"Postman.controlTextDidEndEditing ignoring message %@", [self message]);
00199     }
00200 }
00201 
00202 @end

Generated on Sat Aug 26 21:14:15 2006 for MacTrek by  doxygen 1.4.7