Skip to content

Commit

Permalink
Add their IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter committed Jun 8, 2011
1 parent c2938cf commit d53941c
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 188 deletions.
17 changes: 7 additions & 10 deletions Battlefields/Objective-C/BFAppDelegate.h
Expand Up @@ -10,11 +10,6 @@
#import "BFGrid.h"

@interface BFAppDelegate : NSObject <BFGridDelegate> {
NSTextField *statusLabel;
NSTextView *logView;
NSTextField *yourIDLabel;
NSTextField *yourPrefixLabel;

// Players' own stuff
BFGrid *yourGrid;
BFGrid *theirGrid;
Expand All @@ -29,19 +24,21 @@

}


@property (retain) NSWindow *window;
@property (assign) IBOutlet NSTextField *statusLabel;
@property (assign) IBOutlet NSTextView *logView;

// Setup window
@property (retain) IBOutlet NSTextField *theirIDField;
@property (retain) IBOutlet NSTextField *theirPrefixField;
@property (assign) IBOutlet NSTextField *yourIDLabel;
@property (assign) IBOutlet NSTextField *yourPrefixLabel;

// Game Window
@property (retain) NSWindow *window;
@property (assign) IBOutlet NSTextView *logView;
@property (assign) IBOutlet NSView *yourGridSuperview;
@property (assign) IBOutlet NSView *theirGridSuperview;

@property (assign) IBOutlet NSTextField *yourCurrentIP;
@property (assign) IBOutlet NSTextField *theirCurrentIP;

@property (assign) BFGrid *yourGrid;
@property (assign) BFGrid *theirGrid;

Expand Down
32 changes: 23 additions & 9 deletions Battlefields/Objective-C/BFAppDelegate.m
Expand Up @@ -17,12 +17,14 @@ @interface BFAppDelegate ()
@property (retain) BFBBattlefields *field;

- (void)setUpGrid;
- (void)setTheirIP:(NSString *)theIPAddress;
- (void)setOurIP:(NSString *)theIPAddress;

@end

@implementation BFAppDelegate
@synthesize statusLabel;
@synthesize yourGrid, theirGrid, yourGridSuperview, theirGridSuperview;

@synthesize yourGrid, theirGrid, yourGridSuperview, theirGridSuperview, yourCurrentIP, theirCurrentIP;
@synthesize logView;
@synthesize yourIDLabel;
@synthesize yourPrefixLabel;
Expand All @@ -46,23 +48,20 @@ - (void)applicationDidFinishLaunching:(NSNotification *)theNotification
[NSApp setPresentationOptions:[NSApp presentationOptions] | NSApplicationPresentationFullScreen];
[[self window] setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];

// Set up label and prefix
// Set up the setup window ;)
[[self yourPrefixLabel] setStringValue:[NSString stringWithFormat:@"Your Prefix: %@", [[self field] interfacePrefix]]];
[[self yourIDLabel] setStringValue:[NSString stringWithFormat:@"ID: %@", [[self field] gameID]]];
[[self theirPrefixField] setStringValue:[[self field] interfacePrefix]];

// Set up the main window
[[self window] setBackgroundColor:[NSColor colorWithCalibratedRed:66.0/255 green:39.0/255 blue:0.0 alpha:1.0]];
[self setTheirIP:@""];
[self setOurIP:@""];
[self setUpGrid];
}

- (void)setUpGrid;
{
// Set up state
// for (size_t i = 0; i < 100; ++i) {
// theirState[i] = BFGridStateEmpty;
// yourState[i] = BFGridStateEmpty;
// }

// Set up your grid
[self setYourGrid:[[BFGrid alloc] initWithFrame:[[self yourGridSuperview] bounds] delegate:self]];
[[self yourGridSuperview] addSubview:[self yourGrid]];
Expand All @@ -83,6 +82,8 @@ - (void)addLogMessage:(NSString *)theMessage;
font = [NSFont fontWithName:@"Courier" size:18.0];
[newLine setAttributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] range:NSMakeRange(0, [newLine length])];
[[[self logView] textStorage] appendAttributedString:newLine];

[[self logView] scrollRangeToVisible:NSMakeRange([[[self logView] textStorage] length], 0)];
}

- (IBAction)startGame:(id)sender;
Expand All @@ -95,13 +96,26 @@ - (IBAction)startGame:(id)sender;
[self performSelector:@selector(setUpFinished) withObject:nil afterDelay:0.1];
}

- (void)setTheirIP:(NSString *)theIPAddress;
{
[[self theirCurrentIP] setStringValue:[[self field] prettifyIP:theIPAddress]];
}

- (void)setOurIP:(NSString *)theIPAddress;
{
[[self yourCurrentIP] setStringValue:[[self field] prettifyIP:theIPAddress]];
}

- (void)setUpFinished;
{
[self addLogMessage:@"Ready to play the game"];
[[self field] monitorICMP:self];

[[self yourGrid] setShips:[[self field] ships]];
[[self yourGrid] setNeedsDisplay:TRUE];

[self setOurIP: [[self field] addressForX:[NSNumber numberWithInt:0] Y:[NSNumber numberWithInt:0]]];
[self setTheirIP: [[self field] theirAddressForX:[NSNumber numberWithInt:0] Y:[NSNumber numberWithInt:0]]];
}
- (IBAction)stopGame:(id)sender {
[[self field] cleanUp];
Expand Down
2 changes: 1 addition & 1 deletion Battlefields/Ruby Bridge/BFBBattlefields.h
Expand Up @@ -30,5 +30,5 @@
- (NSNumber *)playerHasShipAtX:(NSNumber *)theX Y:(NSNumber *)theY;

- (void)monitorICMP:(id)theDelegate;

- (NSString *)prettifyIP:(NSString *)theIPAddress;
@end
4 changes: 4 additions & 0 deletions Battlefields/Ruby Bridge/BFBBattlefields.rb
Expand Up @@ -10,6 +10,10 @@

class BFBBattlefields

def prettifyIP(ip_address)
ip_address.gsub(/::/, ":0000:").gsub(/:([0-9][0-9][0-9]):/, ":0\\1:").sub(/^(([a-f0-9]{1,4}:?){1,4}):/, "\\1\n")
end

def initWithInterface(interface, prefix: prefix, gameID:gameID)
initialize
@queue = Dispatch::Queue.new("nl.frim.Battlefields.queue")
Expand Down

0 comments on commit d53941c

Please sign in to comment.