24 |
#include <Foundation/NSHost.h> |
#include <Foundation/NSHost.h> |
25 |
#include <Foundation/NSPort.h> |
#include <Foundation/NSPort.h> |
26 |
#include <Foundation/NSString.h> |
#include <Foundation/NSString.h> |
27 |
#include <Foundation/NSDictionary.h> |
#include <Foundation/NSArray.h> |
28 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
29 |
|
|
30 |
#include "Network/NetParty.h" |
#include "Network/NetParty.h" |
57 |
if (self != nil) |
if (self != nil) |
58 |
{ |
{ |
59 |
_server = RETAIN (server); |
_server = RETAIN (server); |
60 |
_clients = AUTORELEASE ([NSMutableDictionary dictionary]); |
_clients = [[NSMutableArray alloc] init]; |
61 |
_game = AUTORELEASE ([[HgGame alloc] initWithName: gameName]); |
_game = [[HgGame alloc] initWithName: gameName]; |
62 |
|
|
63 |
[server addParty: self]; |
[server addParty: self]; |
64 |
} |
} |
79 |
} |
} |
80 |
|
|
81 |
/** |
/** |
|
* Returns the NetInfosClient object corresponding to the playerName. |
|
|
* If the playerName is not a key of the client's list, it returns nil. |
|
|
*/ |
|
|
- (NetInfosClient *) clientWithName: (NSString *)playerName |
|
|
{ |
|
|
NSParameterAssert (playerName); |
|
|
NSParameterAssert ([playerName length] != 0); |
|
|
|
|
|
return [_clients objectForKey: playerName]; |
|
|
} |
|
|
|
|
|
/** |
|
82 |
* Returns an array containing all the clients |
* Returns an array containing all the clients |
83 |
*/ |
*/ |
84 |
- (NSDictionary *) allClients |
- (NSArray *) clients |
85 |
{ |
{ |
86 |
return _clients; |
return _clients; |
87 |
} |
} |
93 |
- (void) terminate |
- (void) terminate |
94 |
{ |
{ |
95 |
[_server removeParty: self]; |
[_server removeParty: self]; |
96 |
[self dealloc]; |
|
97 |
|
[_clients makeObjectsPerformSelector: @selector(disconnect)]; |
98 |
|
|
99 |
|
DESTROY(self); |
100 |
} |
} |
101 |
|
|
102 |
/** |
/** |
106 |
{ |
{ |
107 |
NSParameterAssert (client); |
NSParameterAssert (client); |
108 |
|
|
109 |
[_clients removeObjectForKey: [[client player] name]]; |
[_clients removeObject: client]; |
110 |
|
|
111 |
[_game removePlayer: [client player]]; |
[_game removePlayer: [client player]]; |
112 |
} |
} |
113 |
|
|
114 |
/** |
/** |
115 |
* Returns the name of the party. |
* Returns the name of the game played in the party. |
116 |
*/ |
*/ |
117 |
- (NSString *) name |
- (NSString *) gameName |
118 |
{ |
{ |
119 |
return [_game name]; |
return [_game name]; |
120 |
} |
} |
121 |
|
|
122 |
/** |
/** |
123 |
* Returns the number of clients in the party |
* Returns the current number of players playing on the map. |
124 |
*/ |
*/ |
125 |
- (unsigned) nbClients |
- (unsigned) nbPlayers |
126 |
{ |
{ |
127 |
return [_clients count]; |
return [_clients count]; |
128 |
} |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* Returns an array with the clients name. |
* Returns the maximum number of player in this party. |
132 |
*/ |
*/ |
133 |
- (NSArray *) allClientName |
- (unsigned) maxNbPlayers |
134 |
{ |
{ |
135 |
return [_clients allKeys]; |
/* FIXME */ |
136 |
|
return -1; |
137 |
} |
} |
138 |
|
|
139 |
/** |
/** |
140 |
|
* Returns the name of the map used is the party. |
141 |
|
*/ |
142 |
|
- (NSString *) mapName |
143 |
|
{ |
144 |
|
/* FIXME */ |
145 |
|
return @"map name"; |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
|
* Returns the name of the player which created the party. |
150 |
|
*/ |
151 |
|
- (NSString *) master |
152 |
|
{ |
153 |
|
/* FIXME */ |
154 |
|
return @"master"; |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* Returns YES if the player is already added |
159 |
|
*/ |
160 |
|
- (BOOL) containsPlayerName: (NSString *)playerName |
161 |
|
{ |
162 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
163 |
|
NetInfosClient *client; |
164 |
|
while ( (client = [enumerator nextObject]) ) |
165 |
|
{ |
166 |
|
if ( [playerName isEqualToString: [[client player] name]] ) |
167 |
|
return YES; |
168 |
|
} |
169 |
|
|
170 |
|
return NO; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
/** |
175 |
* Connects the player named playerName to the NetParty |
* Connects the player named playerName to the NetParty |
176 |
* and returns the receive port on the server. |
* and returns the receive port on the server. |
177 |
* If the name is already in use, returns nil. |
* If the name is already in use, returns nil. |
183 |
NSParameterAssert (playerName); |
NSParameterAssert (playerName); |
184 |
NSParameterAssert ([playerName length] != 0); |
NSParameterAssert ([playerName length] != 0); |
185 |
|
|
186 |
if ([_clients objectForKey: playerName] == nil) |
if (![self containsPlayerName: playerName]) |
187 |
{ |
{ |
188 |
NSPort *receivePort = [NSPort port]; |
NSPort *receivePort = [NSPort port]; |
189 |
|
|
193 |
|
|
194 |
/* creating a NetInfosClient associated with the player */ |
/* creating a NetInfosClient associated with the player */ |
195 |
NetInfosClient *newClient = [[NetInfosClient alloc] |
NetInfosClient *newClient = [[NetInfosClient alloc] |
196 |
initWithParty: self |
initWithParty: self |
197 |
withConnection: conn |
withConnection: conn |
198 |
playerName: playerName]; |
playerName: playerName]; |
199 |
|
|
200 |
[conn setRootObject: newClient]; |
[conn setRootObject: newClient]; |
201 |
|
|
202 |
/* update the clients list */ |
/* update the clients list */ |
203 |
[_clients setObject: newClient forKey: playerName]; |
[_clients addObject: newClient]; |
204 |
|
|
205 |
/* add a new player to the game */ |
/* add a new player to the game */ |
206 |
[_game addPlayer: [newClient player]]; |
[_game addPlayer: [newClient player]]; |
207 |
|
|
208 |
|
RELEASE(newClient); |
209 |
|
|
210 |
printf(" Etat du jeu avant mise a jour : %s\n", [[*game name] cString]); |
printf(" Etat du jeu avant mise a jour : %s\n", [[*game name] cString]); |
211 |
|
|
212 |
//[*game modifWithGame: _game]; |
/* this game is returned through the network via bycopy, |
213 |
/* game renvoye bycopy */ |
* so we shouldn't retain it */ |
214 |
*game = _game; |
*game = _game; |
215 |
|
|
216 |
printf(" Etat du jeu apres mise a jour : %s\n", [[*game name] cString]); |
printf(" Etat du jeu apres mise a jour : %s\n", [[*game name] cString]); |
217 |
|
|
218 |
return receivePort; |
return receivePort; |
219 |
} |
} |
220 |
else |
|
221 |
{ |
return nil; |
|
return nil; |
|
|
} |
|
222 |
} |
} |
223 |
|
|
224 |
@end |
@end |