26 |
#include <Foundation/NSString.h> |
#include <Foundation/NSString.h> |
27 |
#include <Foundation/NSArray.h> |
#include <Foundation/NSArray.h> |
28 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
29 |
|
#include <Foundation/NSNotification.h> |
30 |
|
|
31 |
#include "Network/NetParty.h" |
#include "Network/NetParty.h" |
32 |
#include "Network/NetClient.h" |
#include "Network/NetClient.h" |
36 |
|
|
37 |
#include "GameEngine/HgGame.h" |
#include "GameEngine/HgGame.h" |
38 |
#include "GameEngine/HgPlayer.h" |
#include "GameEngine/HgPlayer.h" |
39 |
|
#include "GameEngine/HgObject.h" |
40 |
|
|
41 |
|
|
42 |
/** |
/** |
51 |
* player who has created the game. |
* player who has created the game. |
52 |
* The gameName must be unused on the server. |
* The gameName must be unused on the server. |
53 |
*/ |
*/ |
54 |
- (id) initWithGameName: (NSString *)gameName |
- (id) initWithGameName: (NSString *) gameName |
55 |
maxNumPlayers: (unsigned)maxNumPlayers |
maxNumPlayers: (unsigned) maxNumPlayers |
56 |
mapName: (NSString *)mapName |
mapName: (NSString *) mapName |
57 |
master: (NSString *)master |
master: (NSString *) master |
58 |
inServer: (NetServer *)server |
inServer: (NetServer *)server |
59 |
{ |
{ |
60 |
NSParameterAssert (gameName); |
NSParameterAssert (gameName); |
74 |
_maxNumPlayers = maxNumPlayers; |
_maxNumPlayers = maxNumPlayers; |
75 |
_mapName = RETAIN(mapName); |
_mapName = RETAIN(mapName); |
76 |
_master = RETAIN(master); |
_master = RETAIN(master); |
77 |
|
_updatedObjects = [[NSMutableArray alloc] init]; |
78 |
|
|
79 |
[server addParty: self]; |
[server addParty: self]; |
80 |
} |
} |
81 |
|
|
82 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
83 |
|
selector:@selector(createObject:) |
84 |
|
name:@"addObject" object:nil]; |
85 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
86 |
|
selector:@selector(destroyObject:) |
87 |
|
name:@"removeObject" object:nil]; |
88 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
89 |
|
selector:@selector(addUpdatedObject:) |
90 |
|
name:@"updateObject" object:nil]; |
91 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
92 |
|
selector:@selector(createPlayer:) |
93 |
|
name:@"addPlayer" object:nil]; |
94 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
95 |
|
selector:@selector(destroyPlayer:) |
96 |
|
name:@"removePlayer" object:nil]; |
97 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
98 |
|
selector:@selector(updatePlayer:) |
99 |
|
name:@"updatePlayer" object:nil]; |
100 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
101 |
|
selector:@selector(createTeam:) |
102 |
|
name:@"addTeam" object:nil]; |
103 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
104 |
|
selector:@selector(destroyTeam:) |
105 |
|
name:@"removeTeam" object:nil]; |
106 |
|
[[NSNotificationCenter defaultCenter] addObserver:self |
107 |
|
selector:@selector(updateTeam:) |
108 |
|
name:@"updateTeam" object:nil]; |
109 |
|
|
110 |
return self; |
return self; |
111 |
} |
} |
112 |
|
|
120 |
RELEASE (_game); |
RELEASE (_game); |
121 |
RELEASE (_mapName); |
RELEASE (_mapName); |
122 |
RELEASE (_master); |
RELEASE (_master); |
123 |
|
RELEASE (_updatedObjects); |
124 |
|
|
125 |
[super dealloc]; |
[super dealloc]; |
126 |
} |
} |
127 |
|
|
128 |
/** |
/** |
129 |
* Returns an array containing all the clients |
* Returns an array containing all the clients. |
130 |
*/ |
*/ |
131 |
- (NSArray *) clients |
- (NSArray *) clients |
132 |
{ |
{ |
153 |
{ |
{ |
154 |
NSParameterAssert (client); |
NSParameterAssert (client); |
155 |
|
|
|
[_clients removeObject: client]; |
|
|
|
|
156 |
[_game removePlayer: [client player]]; |
[_game removePlayer: [client player]]; |
157 |
|
|
158 |
|
[_clients removeObject: client]; |
159 |
|
|
160 |
[NetServer updateServerListWithParty : self]; |
[NetServer updateServerListWithParty : self]; |
161 |
} |
} |
162 |
|
|
201 |
} |
} |
202 |
|
|
203 |
/** |
/** |
204 |
* Returns YES if the player is already added |
* Returns the current game. |
205 |
|
*/ |
206 |
|
- (HgGame *) game |
207 |
|
{ |
208 |
|
return _game; |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Returns YES if the player is already added. |
213 |
*/ |
*/ |
214 |
- (BOOL) containsPlayerName: (NSString *)playerName |
- (BOOL) containsPlayerName: (NSString *)playerName |
215 |
{ |
{ |
216 |
|
NSParameterAssert (playerName); |
217 |
|
|
218 |
NSEnumerator *enumerator = [_clients objectEnumerator]; |
NSEnumerator *enumerator = [_clients objectEnumerator]; |
219 |
NetInfosClient *client; |
NetInfosClient *client; |
220 |
while ( (client = [enumerator nextObject]) ) |
while ( (client = [enumerator nextObject]) ) |
238 |
{ |
{ |
239 |
NSParameterAssert (playerName); |
NSParameterAssert (playerName); |
240 |
NSParameterAssert ([playerName length] != 0); |
NSParameterAssert ([playerName length] != 0); |
241 |
|
NSParameterAssert (sendPort); |
242 |
|
|
243 |
if (![self containsPlayerName: playerName]) |
if (![self containsPlayerName: playerName]) |
244 |
{ |
{ |
245 |
NSPort *receivePort = [NSPort port]; |
NSPort *receivePort = [NSPort port]; |
281 |
return nil; |
return nil; |
282 |
} |
} |
283 |
|
|
284 |
// - (void)encodeWithCoder: (NSCoder *)encoder |
|
285 |
// { |
/** |
286 |
// [encoder encodeObject: _server]; |
* Create an object from notification on all the clients. |
287 |
// [encoder encodeObject: _clients]; |
*/ |
288 |
// [encoder encodeObject: _game]; |
- (void) createObject:(NSNotification *)notification |
289 |
// } |
{ |
290 |
|
HgObject *obj = [notification object]; |
291 |
// - (id)initWithCoder: (NSCoder *)decoder |
|
292 |
// { |
NSEnumerator *enumerator = [_clients objectEnumerator]; |
293 |
// self = [super init]; |
NetInfosClient *client; |
294 |
// _server = RETAIN([decoder decodeObject]); |
while ( (client = [enumerator nextObject]) ) |
295 |
// _clients = RETAIN([decoder decodeObject]); |
{ |
296 |
// _game = RETAIN([decoder decodeObject]); |
[[client proxy] createObject: obj]; |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
|
* Destroy an object from notification on all the clients. |
302 |
|
*/ |
303 |
|
- (void) destroyObject:(NSNotification *)notification |
304 |
|
{ |
305 |
|
HgObject *obj = [notification object]; |
306 |
|
|
307 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
308 |
|
NetInfosClient *client; |
309 |
|
while ( (client = [enumerator nextObject]) ) |
310 |
|
{ |
311 |
|
[[client proxy] destroyObject: obj]; |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
|
* Add an object from notification to the updated objects list. |
317 |
|
*/ |
318 |
|
- (void) addUpdatedObject:(NSNotification *)notification |
319 |
|
{ |
320 |
|
HgObject *obj = [notification object]; |
321 |
|
|
322 |
|
unsigned i = [_updatedObjects indexOfObject: obj]; |
323 |
|
|
324 |
|
if (i != NSNotFound) |
325 |
|
[_updatedObjects replaceObjectAtIndex: i withObject: obj]; |
326 |
|
else |
327 |
|
[_updatedObjects addObject: obj]; |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
|
* Create a player from notification on all the clients. |
332 |
|
*/ |
333 |
|
- (void) createPlayer:(NSNotification *)notification |
334 |
|
{ |
335 |
|
HgPlayer *player = [notification object]; |
336 |
|
|
337 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
338 |
|
NetInfosClient *client; |
339 |
|
while ( (client = [enumerator nextObject]) ) |
340 |
|
{ |
341 |
|
[[client proxy] createPlayer: player]; |
342 |
|
} |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
|
* Destroy a player from notification on all the clients. |
347 |
|
*/ |
348 |
|
- (void) destroyPlayer:(NSNotification *)notification |
349 |
|
{ |
350 |
|
HgPlayer *player = [notification object]; |
351 |
|
|
352 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
353 |
|
NetInfosClient *client; |
354 |
|
while ( (client = [enumerator nextObject]) ) |
355 |
|
{ |
356 |
|
[[client proxy] destroyPlayer: player]; |
357 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
|
* Update a player from notification on all the clients. |
362 |
|
*/ |
363 |
|
- (void) updatePlayer:(NSNotification *)notification |
364 |
|
{ |
365 |
|
HgPlayer *player = [notification object]; |
366 |
|
|
367 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
368 |
|
NetInfosClient *client; |
369 |
|
while ( (client = [enumerator nextObject]) ) |
370 |
|
{ |
371 |
|
[[client proxy] updatePlayer: player]; |
372 |
|
} |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
/** |
377 |
|
* Create a team from notification on all the clients. |
378 |
|
*/ |
379 |
|
- (void) createTeam:(NSNotification *)notification |
380 |
|
{ |
381 |
|
HgTeam *team = [notification object]; |
382 |
|
|
383 |
// return self; |
NSEnumerator *enumerator = [_clients objectEnumerator]; |
384 |
// } |
NetInfosClient *client; |
385 |
|
while ( (client = [enumerator nextObject]) ) |
386 |
|
{ |
387 |
|
[[client proxy] createTeam: team]; |
388 |
|
} |
389 |
|
} |
390 |
|
|
391 |
|
/** |
392 |
|
* Destroy a team from notification on all the clients. |
393 |
|
*/ |
394 |
|
- (void) destroyTeam:(NSNotification *)notification |
395 |
|
{ |
396 |
|
HgTeam *team = [notification object]; |
397 |
|
|
398 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
399 |
|
NetInfosClient *client; |
400 |
|
while ( (client = [enumerator nextObject]) ) |
401 |
|
{ |
402 |
|
[[client proxy] destroyTeam: team]; |
403 |
|
} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
|
* Update a team from notification on all the clients. |
408 |
|
*/ |
409 |
|
- (void) updateTeam:(NSNotification *)notification |
410 |
|
{ |
411 |
|
HgTeam *team = [notification object]; |
412 |
|
|
413 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
414 |
|
NetInfosClient *client; |
415 |
|
while ( (client = [enumerator nextObject]) ) |
416 |
|
{ |
417 |
|
[[client proxy] updateTeam: team]; |
418 |
|
} |
419 |
|
} |
420 |
|
|
421 |
|
|
422 |
|
/** |
423 |
|
* Send to all the clients a message to update all the updated objects on |
424 |
|
* the server and remove all the objects to the list of updated objects. |
425 |
|
*/ |
426 |
|
- (void) sendUpdatedObjects |
427 |
|
{ |
428 |
|
NSEnumerator *enumerator = [_updatedObjects objectEnumerator]; |
429 |
|
HgObject *obj; |
430 |
|
while ( (obj = [enumerator nextObject]) ) |
431 |
|
{ |
432 |
|
NSEnumerator *enumerator = [_clients objectEnumerator]; |
433 |
|
NetInfosClient *client; |
434 |
|
while ( (client = [enumerator nextObject]) ) |
435 |
|
{ |
436 |
|
[[client proxy] updateObject: obj]; |
437 |
|
} |
438 |
|
} |
439 |
|
[_updatedObjects removeAllObjects]; |
440 |
|
} |
441 |
|
|
442 |
|
|
443 |
@end |
@end |