home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Games / Empire-0.6-MIS / ClientController.m < prev    next >
Encoding:
Text File  |  1997-10-30  |  6.3 KB  |  216 lines

  1. //
  2. // $Id: ClientController.m,v 1.11 1997/10/31 04:51:38 nygard Exp $
  3. //
  4.  
  5. //
  6. //  This file is a part of Empire, a game of exploration and conquest.
  7. //  Copyright (C) 1996  Steve Nygard
  8. //
  9. //  This program is free software; you can redistribute it and/or modify
  10. //  it under the terms of the GNU General Public License as published by
  11. //  the Free Software Foundation; either version 2 of the License, or
  12. //  (at your option) any later version.
  13. //
  14. //  This program is distributed in the hope that it will be useful,
  15. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. //  GNU General Public License for more details.
  18. //
  19. //  You should have received a copy of the GNU General Public License
  20. //  along with this program; if not, write to the Free Software
  21. //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. //
  23. //  You may contact the author by:
  24. //     e-mail:  nygard@telusplanet.net
  25. //
  26.  
  27. #import "Empire.h"
  28.  
  29. RCSID ("$Id: ClientController.m,v 1.11 1997/10/31 04:51:38 nygard Exp $");
  30.  
  31. #import "ClientController.h"
  32.  
  33. #import "Brain.h"
  34. #import "DistributedGameManager.h"
  35. #import "NewGameController.h"
  36.  
  37. //======================================================================
  38. // Provides control over the connection to the server.  It also has
  39. // to show the client player selection panel, and create the client
  40. // game manager.
  41. //======================================================================
  42.  
  43. @implementation ClientController
  44.  
  45. - (void) awakeFromNib
  46. {
  47.     [clientWindow setTitle:[NSString stringWithFormat:@"Client  %c  %@", 208, [[NSHost currentHost] name]]];
  48.  
  49.     empireServer = nil;
  50. }
  51.  
  52. //----------------------------------------------------------------------
  53.  
  54. - initWithBrain:(Brain *)theBrain
  55. {
  56.     NSString *nibFile;
  57.     BOOL loaded;
  58.  
  59.     [super init];
  60.  
  61.     nibFile = @"ClientWindow.nib";
  62.     loaded = [NSBundle loadNibNamed:nibFile owner:self];
  63.     if (loaded == NO)
  64.     {
  65.         NSLog (@"Could not load %@.", nibFile);
  66.         [super dealloc];
  67.         return nil;
  68.     }
  69.  
  70.     brain = [theBrain retain];
  71.  
  72.     return self;
  73. }
  74.  
  75. //----------------------------------------------------------------------
  76.  
  77. - (void) dealloc
  78. {
  79.     SNRelease (brain);
  80.     
  81.     [super dealloc];
  82. }
  83.  
  84. //----------------------------------------------------------------------
  85.  
  86. - (void) showPanel
  87. {
  88.     [clientWindow makeKeyAndOrderFront:self];
  89. }
  90.  
  91. //----------------------------------------------------------------------
  92.  
  93. - (void) connectAction:sender
  94. {
  95.     NSProtocolChecker *protocolChecker;
  96.  
  97.     // Note: When disconnected, the empireServer remains set... and probably shouldn't.
  98.     NSAssert (empireServer == nil, @"Already connected.");
  99.  
  100.     // Note: The host should end up as @"*"...
  101.   
  102.     empireServer = [[NSConnection rootProxyForConnectionWithRegisteredName:SERVER_PORT_NAME
  103.                                   host:[serverHostTextfield stringValue]] retain];
  104.  
  105.     NSAssert (empireServer != nil, @"Connection failed.");
  106.  
  107.     [[NSNotificationCenter defaultCenter] addObserver:self
  108.                                           selector:@selector (connectionDidDie:)
  109.                                           name:NSConnectionDidDieNotification
  110.                                           object:[empireServer connectionForProxy]];
  111.  
  112.     [statusTextfield setStringValue:@"Connected."];
  113.  
  114.     serverConnection = [[empireServer connectionForProxy] retain];
  115.     NSAssert (serverConnection != nil, @"serverConnection is nil.");
  116.   
  117.     [empireServer setProtocolForProxy:@protocol (EmpireServerConnectionProtocol)];
  118.  
  119.     protocolChecker = [NSProtocolChecker protocolCheckerWithTarget:self
  120.                                          protocol:@protocol (EmpireClientConnectionProtocol)];
  121.     NSAssert (protocolChecker != nil, @"A protocol checker could not be allocated.\n");
  122.  
  123.     NS_DURING
  124.         {
  125.             //[empireServer showText:@"Hello server, said the client."];
  126.             [empireServer client:protocolChecker onHost:[[NSHost currentHost] name]];
  127.             [empireServer ping];
  128.         }
  129.     NS_HANDLER
  130.         {
  131.             EHAND;
  132.         }
  133.     NS_ENDHANDLER;
  134. }
  135.   
  136. //----------------------------------------------------------------------
  137.  
  138. - (void) disconnectAction:sender
  139. {
  140.     NSAssert (serverConnection != nil, @"Client not connected.");
  141.  
  142.     [empireServer aboutToDisconnect:self];
  143.     
  144.     [serverConnection invalidate];
  145.     SNRelease (serverConnection);
  146.  
  147.     SNRelease (empireServer);
  148.  
  149.     //[statusTextfield setStringValue:@"Not connected."];
  150. }
  151.   
  152. //----------------------------------------------------------------------
  153.  
  154. - (void) showText:(NSString *)format, ...
  155. {
  156.     NSMutableString *message;
  157.     NSRange selected;
  158.     va_list ap;
  159.  
  160.     va_start(ap, format);
  161.     message = [[[NSMutableString alloc] initWithFormat:format arguments:ap] autorelease];
  162.     [message appendString:@"\n"];
  163.     va_end(ap);
  164.  
  165.     [messageText selectAll:nil];
  166.     selected = [messageText selectedRange];
  167.     selected.location = selected.length;
  168.     selected.length = 0;
  169.     [messageText setSelectedRange:selected];
  170.     [messageText replaceCharactersInRange:selected withString:message];
  171.     [messageText scrollRangeToVisible:selected];
  172. }
  173.  
  174. //----------------------------------------------------------------------
  175.  
  176. - (void) ping
  177. {
  178.     [self showText:@"We were pinged..."];
  179. }
  180.  
  181. //----------------------------------------------------------------------
  182.  
  183. - (void) aboutToDisconnect:sender
  184. {
  185.     [[sender connectionForProxy] invalidate];
  186. }
  187.  
  188. //----------------------------------------------------------------------
  189.  
  190. - (void) connectionDidDie:(NSNotification *)notification
  191. {
  192.     [statusTextfield setStringValue:@"Not connected."];
  193.  
  194.     [[NSNotificationCenter defaultCenter] removeObserver:self
  195.                                           name:nil
  196.                                           object:[notification object]];
  197. }
  198.  
  199. //----------------------------------------------------------------------
  200.  
  201. - (void) choosePlayer:(Player)number forMap:(Map *)worldMap master:masterGameManager
  202. {
  203.     DistributedGameManager *gameManager;
  204.     NewGameController *newGameController;
  205.  
  206.     [masterGameManager setProtocolForProxy:@protocol (EmpireGameManagerProtocol)];
  207.  
  208.     gameManager = [[[DistributedGameManager alloc] init] autorelease];
  209.     [gameManager startGameWithMap:worldMap master:masterGameManager];
  210.  
  211.     newGameController = [brain newGameController];
  212.     [newGameController choosePlayer:number forGameManager:gameManager];
  213. }
  214.  
  215. @end
  216.