00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "Postman.h"
00010
00011
00012 @implementation Postman
00013
00014
00015 - (void) awakeFromNib {
00016
00017 [notificationCenter addObserver:self selector:@selector(messageSelected:) name:@"MV_MESSAGE_SELECTION"];
00018
00019 [notificationCenter addObserver:self selector:@selector(playerSelected:) name:@"PV_PLAYER_SELECTION"];
00020
00021 [notificationCenter addObserver:self selector:@selector(manualSelection:) name:@"GV_MESSAGE_DEST"];
00022 }
00023
00024
00025 - (void) messageSelected:(NSString*) str {
00026
00027 if ([str length] == 2) {
00028
00029
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
00037
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
00046
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
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
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
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
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
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
00143
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
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
00188 - (void)controlTextDidEndEditing:(NSNotification *)aNotification {
00189 if ([[self message] length] > 0) {
00190 NSLog(@"Postman.controlTextDidEndEditing sending message %@", [self message]);
00191 [self sendCurrentMessage];
00192
00193
00194
00195
00196 [self setMessage:@""];
00197 } else {
00198 NSLog(@"Postman.controlTextDidEndEditing ignoring message %@", [self message]);
00199 }
00200 }
00201
00202 @end