-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.m
311 lines (278 loc) · 9.04 KB
/
Game.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// Game.m
// liar
//
// Created by Scott Nielsen on 1/18/11.
// Copyright 2011 Scott Nielsen. All rights reserved.
//
#import "Game.h"
#import "Player.h"
@interface Game (Private)
- (int)connectionForIndex:(int)index fromIndex:(int)base;
- (int)indexForConnection:(int)conn fromIndex:(int)base;
- (void)start;
- (void)sendStartMessage;
- (void)nextTurn;
- (BOOL)isValidBidCount:(int)count value:(int)value;
- (int)countOfDiceWithValue:(int)value;
- (void)showResultsFromChallenger:(Player *)player;
- (void)resetRoundNextPlayer:(int)player;
@end
@implementation Game
@synthesize started;
- (id)init {
if (self = [super init]) {
nextTag = 0;
players = [[NSMutableArray alloc] init];
waiters = [[NSMutableArray alloc] init];
timeout = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, dispatch_get_main_queue());
}
return self;
}
- (void)dealloc {
[players release];
[waiters release];
[super dealloc];
}
- (void)start {
totalDice = 0;
// Remove players who are not ready
for (Player *p in players) {
if (!p.ready) {
[waiters addObject:p];
}
}
for (Player *p in waiters) {
[players removeObject:p];
}
// Send name commands to each player for each player
for (Player *p in players) {
int base = [players indexOfObject:p];
for (Player *q in players) {
int index = [players indexOfObject:q];
int conn = [self connectionForIndex:index fromIndex:base];
[p sendNameWithConnection:conn player:q];
}
// Each gets 5 dice and rolls
totalDice += 5;
[p resetDice];
[p roll];
}
currentPlayer = rand() % [players count];
[[players objectAtIndex:currentPlayer] yourTurn];
started = TRUE;
}
- (void)reset {
started = FALSE;
startMsgSent = FALSE;
totalDice = 0;
currentPlayer = 0;
lastPlayer = 0;
currentBidCount = 0;
currentBidValue = 0;
for (Player *p in waiters) {
[players addObject:p];
}
[waiters release];
waiters = [[NSMutableArray alloc] init];
for (Player *p in players) {
p.ready = FALSE;
}
}
- (int)connectionForIndex:(int)index fromIndex:(int)base {
if (index < base) {
index += [players count];
}
return index - base + 1;
}
- (int)indexForConnection:(int)conn fromIndex:(int)base {
return (base + conn - 1) % [players count];
}
- (Player *)playerForConnection:(int)conn fromPlayer:(Player *)player {
int base = [players indexOfObject:player];
int index = [self indexForConnection:conn fromIndex:base];
return [players objectAtIndex:index];
}
- (int)connectionForPlayer:(Player *)player fromPlayer:(Player *)requestor {
int base = [players indexOfObject:requestor];
int index = [players indexOfObject:player];
return [self connectionForIndex:index fromIndex:base];
}
- (NSArray *)allPlayersFromPlayer:(Player *)player {
int base = [players indexOfObject:player];
NSMutableArray *list = [[[NSMutableArray alloc] initWithCapacity:[players count]] autorelease];
for(Player *p in players) {
int i = [players indexOfObject:p];
NSNumber *conn = [NSNumber numberWithInt:[self connectionForIndex:i fromIndex:base]];
NSArray *a = [NSArray arrayWithObjects:conn, p, nil];
[list addObject:a];
}
return list;
}
- (NSArray *)currentTurnFromPlayer:(Player *)player {
int base = [players indexOfObject:player];
int index = currentPlayer;
NSNumber *conn = [NSNumber numberWithInt:[self connectionForIndex:index fromIndex:base]];
return [NSArray arrayWithObjects:conn, [players objectAtIndex:currentPlayer], nil];
}
- (void)playerDidDisconnect:(Player *)player {
if ([waiters containsObject:player]) {
[waiters removeObject:player];
return;
}
if ([players containsObject:player]) {
if (!self.started || [player.dice count] == 0) {
[players removeObject:player];
return;
}
// If we made it this far then someone has disconnected with dice
// TODO: How should this be handled?
}
}
- (void)sendChat:(NSString *)message fromPlayer:(Player *)player {
for(Player *p in players) {
if (![p isEqual:player]) {
[p sendChat:message fromPlayer:player];
}
}
}
- (void)playerIsReady:(Player *)player {
if ([players count] < 2) return;
int playersReady = 0;
for(Player *p in players) {
if (p.ready) playersReady++;
}
if (playersReady > 1) {
if (!startMsgSent) [self sendStartMessage];
if (playersReady == [players count]) [self start];
}
}
- (void)sendStartMessage {
startMsgSent = TRUE;
for (Player *p in players) {
[p sendStartMessage];
}
dispatch_time_t fifteen = dispatch_time(DISPATCH_TIME_NOW, 15.0 * NSEC_PER_SEC);
dispatch_after(fifteen, dispatch_get_main_queue(), ^{
if (!self.started) {
[self start];
}
});
}
- (void)nextTurn {
lastPlayer = currentPlayer;
while (TRUE) {
currentPlayer = (currentPlayer + 1) % [players count];
if ([[[players objectAtIndex:currentPlayer] dice] count] > 0) break;
}
[[players objectAtIndex:currentPlayer] yourTurn];
}
- (BOOL)isValidBidCount:(int)count value:(int)value {
if (value < 1 || 6 < value) return FALSE;
int _count = count;
int _currentBidCount = currentBidCount;
if (value == 1) _count *= 2;
if (currentBidValue == 1) _currentBidCount *= 2;
if (_count < _currentBidCount) return FALSE;
if (_count == _currentBidCount && value <= currentBidValue) return FALSE;
return TRUE;
}
- (BOOL)bidCount:(int)count value:(int)value fromPlayer:(Player *)player {
if (![[players objectAtIndex:currentPlayer] isEqual:player]) return FALSE;
if (![self isValidBidCount:count value:value]) return FALSE;
currentBidCount = count;
currentBidValue = value;
for (Player *p in players) {
if (![p isEqual:player]) {
[p sendBidCount:count value:value fromPlayer:player];
}
}
[self nextTurn];
return TRUE;
}
- (BOOL)challengeFromPlayer:(Player *)player {
if(![[players objectAtIndex:currentPlayer] isEqual:player]) return FALSE;
if(currentBidCount == 0) return FALSE;
[self showResultsFromChallenger:player];
int nextPlayer = -1;
int diff = currentBidCount - [self countOfDiceWithValue:currentBidValue];
if (diff > 0) {
[[players objectAtIndex:lastPlayer] loseDice:diff];
for (Player *p in players) {
[p sendLoseDice:diff fromPlayer:[players objectAtIndex:lastPlayer]];
}
nextPlayer = lastPlayer;
}
else if (diff == 0) {
for (Player *p in players) {
if (![p isEqual:[players objectAtIndex:lastPlayer]]) {
[p loseDice:1];
}
[p sendLoseDiceAllExceptPlayer:[players objectAtIndex:lastPlayer]];
}
nextPlayer = currentPlayer;
}
else if (diff < 0) {
[[players objectAtIndex:currentPlayer] loseDice:abs(diff)];
for (Player *p in players) {
[p sendLoseDice:abs(diff) fromPlayer:[players objectAtIndex:currentPlayer]];
}
nextPlayer = currentPlayer;
}
[self resetRoundNextPlayer:nextPlayer];
return TRUE;
}
- (int)countOfDiceWithValue:(int)value {
int count = 0;
for (Player *p in players) {
for (NSNumber *die in p.dice) {
if ([die intValue] == value || [die intValue] == 1) count++;
}
}
return count;
}
- (void)showResultsFromChallenger:(Player *)player {
for (Player *p in players) {
if (![p isEqual:player]) [p sendChallengeFromPlayer:player];
for(Player *q in players) {
if (![p isEqual:q]) [p sendResultForPlayer:q];
}
}
}
- (void)resetRoundNextPlayer:(int)player {
NSMutableArray *hasDice = [NSMutableArray arrayWithCapacity:1];
for (Player *p in players) {
if ([p.dice count] > 0) [hasDice addObject:p];
}
if ([hasDice count] == 1) {
for (Player *p in players) {
[p sendWinner:[hasDice objectAtIndex:0]];
}
[self reset];
}
else {
currentBidCount = 0;
currentBidValue = 0;
currentPlayer = player;
for (Player *p in players) [p roll];
if ([[[players objectAtIndex:currentPlayer] dice] count] == 0) [self nextTurn];
else [[players objectAtIndex:currentPlayer] yourTurn];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: -
// MARK: Async Socket Delegate Methods
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket {
Player *player = [[Player alloc] initWithSocket:newSocket tag:nextTag++];
player.game = self;
if (self.started) {
[waiters addObject:player];
}
else {
[players addObject:player];
}
[player release];
}
@end