@@ -235,9 +235,118 @@ public ErrorCode RenamePlayer(String oldName, String newName) {
235
235
if ( existingPlayerEntry == null )
236
236
return ErrorCode . NoExistingPlayerByThatName ; // Player not found
237
237
238
+ return RenamePlayer ( existingPlayerEntry , newName ) ;
239
+ }
240
+
241
+ /// <summary>
242
+ /// Renames the player at the given index with the given name.
243
+ /// </summary>
244
+ /// <param name="index">The index of the player (the rank - 1).</param>
245
+ /// <param name="newName">The new name for the player.</param>
246
+ /// <param name="existingPlayer">Set to the existing player if there wasn't an error. Set to null if there was an error.</param>
247
+ /// <returns>Returns Success or an error code for what went wrong.</returns>
248
+ public ErrorCode RenamePlayer ( int index , String newName , out BeefEntry existingPlayer ) {
249
+ existingPlayer = null ;
250
+ List < BeefEntry > entries = ReadBracket ( ) ;
251
+ if ( entries . Count == 0 )
252
+ return ErrorCode . CouldNotReadTheLadder ; // There was an error reading the bracket.
253
+
254
+ if ( index <= 0 || index >= entries . Count ) {
255
+ return ErrorCode . RankNotOnLadder ;
256
+ }
257
+
258
+ BeefEntry playerToRename = entries [ index ] ;
259
+ ErrorCode result = RenamePlayer ( playerToRename , newName ) ;
260
+
261
+ if ( result . Ok ( ) )
262
+ existingPlayer = playerToRename ;
263
+
264
+ return result ;
265
+ }
266
+
267
+ /// <summary>
268
+ /// Renames the given entry to the new name.
269
+ /// </summary>
270
+ /// <param name="existingEntry">The existing entry in the ladder.</param>
271
+ /// <param name="newName">The new name to give the player</param>
272
+ /// <returns>Returns Success or what went wrong if it failed.</returns>
273
+ private ErrorCode RenamePlayer ( BeefEntry existingEntry , String newName ) {
238
274
// Remove the existing and add the new one
239
- AddDeleteRequest ( existingPlayerEntry ) ;
240
- AddInsertRequest ( existingPlayerEntry , newName ) ;
275
+ AddDeleteRequest ( existingEntry ) ;
276
+ AddInsertRequest ( existingEntry , newName ) ;
277
+
278
+ return SubmitRequests ( ) ;
279
+ }
280
+
281
+ /// <summary>
282
+ /// Removes the player from the ladder with the given name and shuffles everyone else down.
283
+ /// </summary>
284
+ /// <param name="name">The name of the player to remove.</param>
285
+ /// <returns>Returns Success if it was successful or the error code if there was an issue.</returns>
286
+ public ErrorCode RemovePlayer ( String name ) {
287
+ List < BeefEntry > entries = ReadBracket ( ) ;
288
+ if ( entries . Count == 0 )
289
+ return ErrorCode . CouldNotReadTheLadder ; // There was an error reading the bracket.
290
+
291
+ // Get the object ID for this player
292
+ BeefEntry playerToRemove = null ;
293
+ for ( int i = 0 ; i < entries . Count ; i ++ ) {
294
+ BeefEntry entry = entries [ i ] ;
295
+ if ( name . Equals ( entry . PlayerName ) ) {
296
+ playerToRemove = entry ;
297
+ break ;
298
+ }
299
+ }
300
+
301
+ if ( playerToRemove == null ) {
302
+ return ErrorCode . NoExistingPlayerByThatName ;
303
+ }
304
+
305
+ return RemovePlayer ( entries , playerToRemove ) ;
306
+ }
307
+
308
+ /// <summary>
309
+ /// Removes the player at the given index.
310
+ /// </summary>
311
+ /// <param name="index">The index of the player to remove. Note this is the INDEX not the RANK.</param>
312
+ /// <param name="playerToRemove">This is set to the player being removed if successful and null otherwise.</param>
313
+ /// <returns>Returns Success if it was successful or the error code if there was an issue.</returns>
314
+ public ErrorCode RemovePlayer ( int index , out BeefEntry playerToRemove ) {
315
+ playerToRemove = null ;
316
+ List < BeefEntry > entries = ReadBracket ( ) ;
317
+ if ( entries . Count == 0 )
318
+ return ErrorCode . CouldNotReadTheLadder ; // There was an error reading the bracket.
319
+
320
+ if ( index <= 0 || index >= entries . Count ) {
321
+ return ErrorCode . RankNotOnLadder ;
322
+ }
323
+
324
+ playerToRemove = entries [ index ] ;
325
+ ErrorCode result = RemovePlayer ( entries , playerToRemove ) ;
326
+ if ( result . Ok ( ) ) {
327
+ playerToRemove = entries [ index ] ;
328
+ }
329
+ return result ;
330
+ }
331
+
332
+ /// <summary>
333
+ /// Builds the list of requests to remove the given BeefEntry (player) and submits it.
334
+ /// </summary>
335
+ /// <param name="entries">The list of entries</param>
336
+ /// <param name="playerToRemove">The player to remove</param>
337
+ /// <returns>Returns Success if it worked or the error code if it didn't.</returns>
338
+ private ErrorCode RemovePlayer ( List < BeefEntry > entries , BeefEntry playerToRemove ) {
339
+ // Add a delete request for each player and insert the player one rank above
340
+ // so it shuffles everyone up the bracket leaving the last one black.
341
+ for ( int index = playerToRemove . PlayerRank - 1 ; index < entries . Count ; index ++ ) {
342
+ AddDeleteRequest ( entries [ index ] ) ;
343
+
344
+ if ( index + 1 < entries . Count ) {
345
+ AddInsertRequest ( entries [ index ] , entries [ index + 1 ] . PlayerName ) ;
346
+ } else {
347
+ AddInsertRequest ( entries [ index ] , "" ) ;
348
+ }
349
+ }
241
350
242
351
return SubmitRequests ( ) ;
243
352
}
0 commit comments