| Conditions | 100 | 
| Total Lines | 382 | 
| Code Lines | 266 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ige.ospace.ISystem.ISystem.processBATTLEPhase() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #  | 
            ||
| 256 | @public(Const.AL_ADMIN)  | 
            ||
| 257 | def processBATTLEPhase(self, tran, obj, data):  | 
            ||
| 258 | system = obj  | 
            ||
| 259 |         #@log.debug('ISystem', 'BATTLE - system', obj.oid) | 
            ||
| 260 | # we are processing fleets, planets, ...  | 
            ||
| 261 | objects = obj.planets[:] + obj.fleets[:]  | 
            ||
| 262 | # shuffle them to prevent predetermined one-sided battles (temporary hack)  | 
            ||
| 263 | random.shuffle(objects)  | 
            ||
| 264 | # store owners of objects  | 
            ||
| 265 | # find enemies and allies  | 
            ||
| 266 |         attack = {} | 
            ||
| 267 |         allies = {} | 
            ||
| 268 |         owners = {} | 
            ||
| 269 |         ownerIDs = {} | 
            ||
| 270 |         systemAtt = {} | 
            ||
| 271 |         systemDef = {} | 
            ||
| 272 |         hasMine = {} | 
            ||
| 273 | isOwnedObject = 0  | 
            ||
| 274 | for objID in objects:  | 
            ||
| 275 | attack[objID] = []  | 
            ||
| 276 | allies[objID] = []  | 
            ||
| 277 | owner = tran.db[objID].owner  | 
            ||
| 278 | owners[objID] = owner  | 
            ||
| 279 | ownerIDs[owner] = owner  | 
            ||
| 280 | if owner != Const.OID_NONE:  | 
            ||
| 281 | isOwnedObject = 1  | 
            ||
| 282 | for owner in ownerIDs:  | 
            ||
| 283 | tempAtt, tempDef = self.getSystemCombatBonuses(tran, system, owner)  | 
            ||
| 284 | systemAtt[owner] = tempAtt  | 
            ||
| 285 | systemDef[owner] = tempDef  | 
            ||
| 286 | hasMine[owner] = self.getSystemMineSource(tran, system, owner)  | 
            ||
| 287 | if not isOwnedObject:  | 
            ||
| 288 |             #@log.debug('ISystem', 'No combat') | 
            ||
| 289 | # reset combat counters  | 
            ||
| 290 | system.combatCounter = 0  | 
            ||
| 291 | return  | 
            ||
| 292 | # first - direct ones  | 
            ||
| 293 | index = 1  | 
            ||
| 294 | for obj1ID in objects:  | 
            ||
| 295 | obj1 = tran.db[obj1ID]  | 
            ||
| 296 | if obj1.owner == Const.OID_NONE:  | 
            ||
| 297 | index += 1  | 
            ||
| 298 | continue  | 
            ||
| 299 | commander = tran.db[obj1.owner]  | 
            ||
| 300 | # relationships  | 
            ||
| 301 | #for obj2ID in objects[index:]:  | 
            ||
| 302 | for obj2ID in objects:  | 
            ||
| 303 | obj2 = tran.db[obj2ID]  | 
            ||
| 304 | if obj2.owner == Const.OID_NONE or obj1 is obj2:  | 
            ||
| 305 | continue  | 
            ||
| 306 | if obj1.owner == obj2.owner:  | 
            ||
| 307 | allies[obj1ID].append(obj2ID)  | 
            ||
| 308 | allies[obj2ID].append(obj1ID)  | 
            ||
| 309 | continue  | 
            ||
| 310 | # planet and military object  | 
            ||
| 311 | elif obj1.type == Const.T_PLANET and obj2.isMilitary and \  | 
            ||
| 312 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_MILITARY_SHIPS):  | 
            ||
| 313 |                     #@log.debug("ISystem pl - mil", obj1ID, obj2ID) | 
            ||
| 314 | if obj2ID not in attack[obj1ID]:  | 
            ||
| 315 | attack[obj1ID].append(obj2ID)  | 
            ||
| 316 | if obj1ID not in attack[obj2ID]:  | 
            ||
| 317 | attack[obj2ID].append(obj1ID)  | 
            ||
| 318 | # planet and civilian object  | 
            ||
| 319 | elif obj1.type == Const.T_PLANET and not obj2.isMilitary and \  | 
            ||
| 320 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_CIVILIAN_SHIPS):  | 
            ||
| 321 |                     #@log.debug("ISystem pl - civ", obj1ID, obj2ID) | 
            ||
| 322 | if obj2ID not in attack[obj1ID]:  | 
            ||
| 323 | attack[obj1ID].append(obj2ID)  | 
            ||
| 324 | if obj1ID not in attack[obj2ID]:  | 
            ||
| 325 | attack[obj2ID].append(obj1ID)  | 
            ||
| 326 | # military and military object  | 
            ||
| 327 | elif obj1.isMilitary and obj2.isMilitary and \  | 
            ||
| 328 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_MILITARY_SHIPS):  | 
            ||
| 329 |                     #@log.debug("ISystem mil - mil", obj1ID, obj2ID) | 
            ||
| 330 | if obj2ID not in attack[obj1ID]:  | 
            ||
| 331 | attack[obj1ID].append(obj2ID)  | 
            ||
| 332 | if obj1ID not in attack[obj2ID]:  | 
            ||
| 333 | attack[obj2ID].append(obj1ID)  | 
            ||
| 334 | # military and civilian object  | 
            ||
| 335 | elif obj1.isMilitary and not obj2.isMilitary and \  | 
            ||
| 336 | not self.cmd(commander).isPactActive(tran, commander, obj2.owner, Const.PACT_ALLOW_CIVILIAN_SHIPS):  | 
            ||
| 337 |                     #@log.debug("ISystem mil - civ", obj1ID, obj2ID) | 
            ||
| 338 | if obj2ID not in attack[obj1ID]:  | 
            ||
| 339 | attack[obj1ID].append(obj2ID)  | 
            ||
| 340 | if obj1ID not in attack[obj2ID]:  | 
            ||
| 341 | attack[obj2ID].append(obj1ID)  | 
            ||
| 342 | # planet and fleet  | 
            ||
| 343 | #elif obj1.type == Const.T_PLANET and obj2.type == Const.T_FLEET and \  | 
            ||
| 344 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_DEFENCE):  | 
            ||
| 345 | # allies[obj1ID].append(obj2ID)  | 
            ||
| 346 | # allies[obj2ID].append(obj1ID)  | 
            ||
| 347 | # fleet and fleet  | 
            ||
| 348 | #elif obj1.type == Const.T_FLEET and obj2.type == Const.T_FLEET and \  | 
            ||
| 349 | # self.cmd(commander).isPactActive(tran, commander, obj2.owner, PACT_MUTUAL_OFFENCE):  | 
            ||
| 350 | # allies[obj1ID].append(obj2ID)  | 
            ||
| 351 | # allies[obj2ID].append(obj1ID)  | 
            ||
| 352 | index += 1  | 
            ||
| 353 |         #@log.debug('ISystem', 'Targets:', targets) | 
            ||
| 354 |         #@log.debug('ISystem', 'Allies:', allies) | 
            ||
| 355 | # find indirect a/e  | 
            ||
| 356 | #for objID in objects:  | 
            ||
| 357 | # iTargets = []  | 
            ||
| 358 | # iAllies = []  | 
            ||
| 359 | # # find indirect a/e  | 
            ||
| 360 | # todo = allies[objID][:]  | 
            ||
| 361 | # while todo:  | 
            ||
| 362 | # id = todo.pop(0)  | 
            ||
| 363 | # iTargets.extend(targets[id])  | 
            ||
| 364 | # for tmpID in allies[id]:  | 
            ||
| 365 | # if tmpID not in iAllies:  | 
            ||
| 366 | # todo.append(tmpID)  | 
            ||
| 367 | # iAllies.append(tmpID)  | 
            ||
| 368 | # # remove allies from targets  | 
            ||
| 369 | # for id in iAllies:  | 
            ||
| 370 | # if id in iTargets:  | 
            ||
| 371 | # iTargets.remove(id)  | 
            ||
| 372 | # # IMPORTATNT preffer NOT to fire at possible allies  | 
            ||
| 373 | # # add my targets  | 
            ||
| 374 | # #for id in targets[objID]:  | 
            ||
| 375 | # # if id not in iTargets:  | 
            ||
| 376 | # # iTargets.append(id)  | 
            ||
| 377 | # # that's all folks  | 
            ||
| 378 | # for id in iTargets:  | 
            ||
| 379 | # if objID not in attack[id]:  | 
            ||
| 380 | # attack[id].append(objID)  | 
            ||
| 381 | # if id not in attack[objID]:  | 
            ||
| 382 | # attack[objID].append(id)  | 
            ||
| 383 | # NOT VALID: objects with action ACTION_ATTACK will attack only their targets  | 
            ||
| 384 | # check, if there are any targets  | 
            ||
| 385 | isCombat = 0  | 
            ||
| 386 | for objID in objects:  | 
            ||
| 387 | if attack[objID]:  | 
            ||
| 388 | isCombat = 1  | 
            ||
| 389 | break #end loop  | 
            ||
| 390 | if not isCombat:  | 
            ||
| 391 |             #@log.debug('ISystem', 'No combat') | 
            ||
| 392 | # reset combat counters  | 
            ||
| 393 | system.combatCounter = 0  | 
            ||
| 394 | for fleetID in system.fleets:  | 
            ||
| 395 | tran.db[fleetID].combatCounter = 0  | 
            ||
| 396 | return  | 
            ||
| 397 | # increase combat counters  | 
            ||
| 398 | system.combatCounter += 1  | 
            ||
| 399 | for fleetID in system.fleets:  | 
            ||
| 400 | tran.db[fleetID].combatCounter += 1  | 
            ||
| 401 | # debug  | 
            ||
| 402 |         log.debug('ISystem', 'Final attacks in system %d:' % system.oid, attack) | 
            ||
| 403 | # mines detonate before battle  | 
            ||
| 404 |         shots = {} | 
            ||
| 405 |         targets = {} | 
            ||
| 406 |         firing = {} | 
            ||
| 407 |         damageCaused = {} | 
            ||
| 408 |         killsCaused = {} | 
            ||
| 409 |         damageTaken = {} | 
            ||
| 410 |         shipsLost = {} | 
            ||
| 411 |         minesTriggered = {} | 
            ||
| 412 |         fleetOwners = {} | 
            ||
| 413 | isCombat = False  | 
            ||
| 414 | isMineCombat = False  | 
            ||
| 415 | for owner in ownerIDs:  | 
            ||
| 416 | if owner not in hasMine: # no planets  | 
            ||
| 417 | continue  | 
            ||
| 418 | if not hasMine[owner]: # no planet with control structure  | 
            ||
| 419 | continue  | 
            ||
| 420 | controlPlanetID = hasMine[owner][0] # there is list returned, all planets have same effect  | 
            ||
| 421 | if len(self.getMines(system, owner)) == 0:  | 
            ||
| 422 | continue # no mines, something broke  | 
            ||
| 423 | if len(attack[controlPlanetID]) == 0:  | 
            ||
| 424 | continue # no targets  | 
            ||
| 425 | isMineFired = True  | 
            ||
| 426 | mineTargets = copy.copy(attack[controlPlanetID])  | 
            ||
| 427 | while isMineFired:  | 
            ||
| 428 | while len(mineTargets) > 0:  | 
            ||
| 429 | targetID = random.choice(mineTargets) # select random target  | 
            ||
| 430 | targetobj = tran.db.get(targetID, None)  | 
            ||
| 431 | try:  | 
            ||
| 432 | if targetobj.type == Const.T_FLEET:  | 
            ||
| 433 | fleetOwners[targetID] = targetobj.owner  | 
            ||
| 434 | break # target found  | 
            ||
| 435 | mineTargets.remove(targetID) # remove an object type that a mine can't hit from the temporary targets list  | 
            ||
| 436 | except:  | 
            ||
| 437 | mineTargets.remove(targetID) # remove a dead fleet from the temporary targets list  | 
            ||
| 438 | |||
| 439 | if len(mineTargets) == 0:  | 
            ||
| 440 | break # no fleet targets for mines  | 
            ||
| 441 | temp, temp, firing[targetID] = self.cmd(targetobj).getPreCombatData(tran, targetobj) # fix firing for "surrender to" section  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 442 | damage, att, ignoreshield, mineID = self.cmd(obj).fireMine(system, owner)  | 
            ||
| 443 | if not damage: # no more mines  | 
            ||
| 444 | isMineFired = False  | 
            ||
| 445 | break  | 
            ||
| 446 |                 log.debug('ISystem', 'Mine Shooting (damage, att, ignore shield):', damage, att, ignoreshield) | 
            ||
| 447 | isMineCombat = True  | 
            ||
| 448 | minesTriggered[mineID] = minesTriggered.get(mineID, 0) + 1  | 
            ||
| 449 | # Process Combat  | 
            ||
| 450 | # for now we assume only one ship can be destroyed by one mine  | 
            ||
| 451 | dmg, destroyed = self.cmd(targetobj).applyMine(tran, targetobj, att, damage, ignoreshield)  | 
            ||
| 452 |                 #log.debug('ISystem-Mines', 'Actual Damage Done:',dmg) | 
            ||
| 453 | if dmg > 0:  | 
            ||
| 454 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg  | 
            ||
| 455 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed  | 
            ||
| 456 | killsCaused[mineID] = killsCaused.get(mineID, 0) + destroyed  | 
            ||
| 457 | if dmg > 0:  | 
            ||
| 458 | damageCaused[mineID] = damageCaused.get(mineID, 0) + dmg  | 
            ||
| 459 | # send messages about mine effects to the owner of the minefield  | 
            ||
| 460 | # collect hit players  | 
            ||
| 461 |             players = {} | 
            ||
| 462 | for triggerID in firing.keys():  | 
            ||
| 463 | players[owners[triggerID]] = None  | 
            ||
| 464 | controllerPlanet = tran.db.get(controlPlanetID, None)  | 
            ||
| 465 | damageCausedSum = 0  | 
            ||
| 466 | killsCausedSum = 0  | 
            ||
| 467 | for mineID in damageCaused.keys():  | 
            ||
| 468 | damageCausedSum = damageCausedSum + damageCaused.get(mineID, 0)  | 
            ||
| 469 | killsCausedSum = killsCausedSum + killsCaused.get(mineID, 0)  | 
            ||
| 470 | Utils.sendMessage(tran, controllerPlanet, Const.MSG_MINES_OWNER_RESULTS, system.oid, (players.keys(),(damageCaused, killsCaused, minesTriggered),damageCausedSum,killsCausedSum))  | 
            ||
| 471 | # send messages to the players whose fleets got hit by minefields  | 
            ||
| 472 | for targetID in damageTaken.keys():  | 
            ||
| 473 | targetFleet = tran.db.get(targetID, None)  | 
            ||
| 474 | if targetFleet:  | 
            ||
| 475 | Utils.sendMessage(tran, targetFleet, Const.MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID]))  | 
            ||
| 476 | else:  | 
            ||
| 477 | targetFleet = IDataHolder()  | 
            ||
| 478 | targetFleet.oid = fleetOwners[targetID]  | 
            ||
| 479 | Utils.sendMessage(tran, targetFleet, Const.MSG_MINES_FLEET_RESULTS, system.oid, (damageTaken[targetID], shipsLost[targetID]))  | 
            ||
| 480 | Utils.sendMessage(tran, targetFleet, Const.MSG_DESTROYED_FLEET, system.oid, ())  | 
            ||
| 481 |         damageCaused = {} | 
            ||
| 482 |         killsCaused = {} | 
            ||
| 483 |         damageTaken = {} | 
            ||
| 484 |         shipsLost = {} | 
            ||
| 485 | # now to battle  | 
            ||
| 486 | for objID in objects:  | 
            ||
| 487 | obj = tran.db.get(objID, None)  | 
            ||
| 488 | # get shots from object, should be sorted by weaponClass  | 
            ||
| 489 | # shots = [ shot, ...], shot = (combatAtt, weaponID)  | 
            ||
| 490 | # get target classes and numbers  | 
            ||
| 491 | # (class1, class2, class3, class4)  | 
            ||
| 492 | # cls0 == fighters, cls1 == midships, cls2 == capital ships, cls3 == planet installations  | 
            ||
| 493 | #@log.debug(objID, obj.name, "getting pre combat data")  | 
            ||
| 494 | if obj: # source already destroyed; ignore  | 
            ||
| 495 | shots[objID], targets[objID], firing[objID] = self.cmd(obj).getPreCombatData(tran, obj)  | 
            ||
| 496 | if firing[objID]:  | 
            ||
| 497 | isCombat = True  | 
            ||
| 498 | if not isCombat and not isMineCombat:  | 
            ||
| 499 | # no shots has been fired  | 
            ||
| 500 |             #@log.debug('ISystem', 'No combat') | 
            ||
| 501 | # reset combat counters  | 
            ||
| 502 | system.combatCounter = 0  | 
            ||
| 503 | for fleetID in system.fleets:  | 
            ||
| 504 | tran.db[fleetID].combatCounter = 0  | 
            ||
| 505 | return  | 
            ||
| 506 |         #@log.debug("Shots:", shots) | 
            ||
| 507 |         #@log.debug("Targets", targets) | 
            ||
| 508 | if isCombat:  | 
            ||
| 509 | for shotIdx in (3, 2, 1, 0):  | 
            ||
| 510 | for objID in objects:  | 
            ||
| 511 | # obj CAN be deleted at this point  | 
            ||
| 512 | obj = tran.db.get(objID, None)  | 
            ||
| 513 | if obj == None:  | 
            ||
| 514 | continue # source already destroyed; move to next source  | 
            ||
| 515 | # if object is fleet, then it's signature is max  | 
            ||
| 516 | if obj and obj.type == Const.T_FLEET:  | 
            ||
| 517 | obj.signature = Rules.maxSignature  | 
            ||
| 518 | # target preselection  | 
            ||
| 519 | totalClass = [0, 0, 0, 0]  | 
            ||
| 520 | total = 0  | 
            ||
| 521 | for targetID in attack[objID]:  | 
            ||
| 522 | totalClass[0] += targets[targetID][0]  | 
            ||
| 523 | totalClass[1] += targets[targetID][1]  | 
            ||
| 524 | totalClass[2] += targets[targetID][2]  | 
            ||
| 525 | totalClass[3] += targets[targetID][3]  | 
            ||
| 526 | total = totalClass[0] + totalClass[1] + totalClass[2] + totalClass[3]  | 
            ||
| 527 | # process shots  | 
            ||
| 528 | for combatAtt, weaponID in shots[objID][shotIdx]:  | 
            ||
| 529 | weapon = Rules.techs[weaponID]  | 
            ||
| 530 | weaponClass = weapon.weaponClass  | 
            ||
| 531 | if total == 0:  | 
            ||
| 532 | # there are no targets  | 
            ||
| 533 | break  | 
            ||
| 534 |                         #@log.debug('ISystem', 'Processing shot', objID, weapon.name, weaponClass) | 
            ||
| 535 | # process from weaponClass up  | 
            ||
| 536 | # never shoot on smaller ships than weaponClass  | 
            ||
| 537 | applied = 0  | 
            ||
| 538 | for tmpWpnClass in xrange(weaponClass, 4):  | 
            ||
| 539 |                             #@log.debug('ISystem', 'Trying target class', tmpWpnClass, totalClass[tmpWpnClass]) | 
            ||
| 540 | # select target  | 
            ||
| 541 | if totalClass[tmpWpnClass]:  | 
            ||
| 542 | target = Utils.rand(0, totalClass[tmpWpnClass])  | 
            ||
| 543 |                                 #@log.debug('ISystem', 'Target rnd num', target, totalClass[tmpWpnClass]) | 
            ||
| 544 | for targetID in attack[objID]:  | 
            ||
| 545 | if target < targets[targetID][tmpWpnClass]:  | 
            ||
| 546 | #@log.debug(objID, 'attacks', targetID, tmpWpnClass)  | 
            ||
| 547 | # targetID can be deleted at this point  | 
            ||
| 548 | anObj = tran.db.get(targetID, None)  | 
            ||
| 549 | if anObj:  | 
            ||
| 550 | dmg, destroyed, destroyedClass = self.cmd(anObj).applyShot(tran, anObj, systemDef[owners[targetID]], combatAtt + systemAtt[owners[objID]], weaponID, tmpWpnClass, target)  | 
            ||
| 551 |                                             #@log.debug("ISystem result", dmg, destroyed, destroyedClass, tmpWpnClass) | 
            ||
| 552 | #@print objID, 'dmg, destroyed', dmg, destroyed  | 
            ||
| 553 | damageTaken[targetID] = damageTaken.get(targetID, 0) + dmg  | 
            ||
| 554 | if destroyed > 0:  | 
            ||
| 555 | shipsLost[targetID] = shipsLost.get(targetID, 0) + destroyed  | 
            ||
| 556 | total -= destroyed  | 
            ||
| 557 | totalClass[destroyedClass] -= destroyed  | 
            ||
| 558 | if dmg > 0 and obj:  | 
            ||
| 559 | obj.combatExp += dmg  | 
            ||
| 560 | damageCaused[objID] = damageCaused.get(objID, 0) + dmg  | 
            ||
| 561 | applied = 1  | 
            ||
| 562 | else:  | 
            ||
| 563 | continue # target already destroyed, move to next target  | 
            ||
| 564 | break  | 
            ||
| 565 | else:  | 
            ||
| 566 |                                         #@log.debug('ISystem', 'Lovering target by', targets[targetID][tmpWpnClass]) | 
            ||
| 567 | target -= targets[targetID][tmpWpnClass]  | 
            ||
| 568 | if applied:  | 
            ||
| 569 | break  | 
            ||
| 570 | # send messages and modify diplomacy relations  | 
            ||
| 571 | # distribute experience pts  | 
            ||
| 572 | for objID in objects:  | 
            ||
| 573 | obj = tran.db.get(objID, None)  | 
            ||
| 574 | if obj:  | 
            ||
| 575 | self.cmd(obj).distributeExp(tran, obj)  | 
            ||
| 576 | if attack[objID]:  | 
            ||
| 577 | source = obj or tran.db[owners[objID]]  | 
            ||
| 578 | # collect players  | 
            ||
| 579 |                 players = {} | 
            ||
| 580 | for attackerID in attack[objID]:  | 
            ||
| 581 | players[owners[attackerID]] = None  | 
            ||
| 582 | d1 = damageTaken.get(objID,0)  | 
            ||
| 583 | d2 = damageCaused.get(objID,0)  | 
            ||
| 584 | l = shipsLost.get(objID, 0)  | 
            ||
| 585 | if d1 or d2 or l:  | 
            ||
| 586 | # send only if damage is taken/caused  | 
            ||
| 587 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_RESULTS, system.oid, (d1, d2, l, players.keys()))  | 
            ||
| 588 | if not obj:  | 
            ||
| 589 | # report DESTROYED status  | 
            ||
| 590 | Utils.sendMessage(tran, source, Const.MSG_DESTROYED_FLEET, system.oid, ())  | 
            ||
| 591 | # modify diplomacy relations  | 
            ||
| 592 | objOwner = tran.db[owners[objID]]  | 
            ||
| 593 | for attackerID in attack[objID]:  | 
            ||
| 594 | attOwner = tran.db.get(owners[attackerID], None)  | 
            ||
| 595 | # owner of the fleet  | 
            ||
| 596 | rel = self.cmd(objOwner).getDiplomacyWith(tran, objOwner, attOwner.oid)  | 
            ||
| 597 | rel.relChng = Rules.relLostWhenAttacked  | 
            ||
| 598 | # attacker  | 
            ||
| 599 | rel = self.cmd(attOwner).getDiplomacyWith(tran, attOwner, objOwner.oid)  | 
            ||
| 600 | rel.rechChng = Rules.relLostWhenAttacked  | 
            ||
| 601 | # check if object surrenders  | 
            ||
| 602 | for objID in objects:  | 
            ||
| 603 | # object surrender IFF it and its allies had target and was not able  | 
            ||
| 604 | # to fire at it, planet is not counted as ally in this case  | 
            ||
| 605 | obj = tran.db.get(objID, None)  | 
            ||
| 606 | if firing[objID] and obj:  | 
            ||
| 607 | continue  | 
            ||
| 608 | surrenderTo = []  | 
            ||
| 609 | for attID in attack[objID]:  | 
            ||
| 610 | if firing[attID] and tran.db.has_key(attID):  | 
            ||
| 611 | surrenderTo.append(tran.db[attID].owner)  | 
            ||
| 612 | for allyID in allies[objID]:  | 
            ||
| 613 | if not tran.db.has_key(allyID):  | 
            ||
| 614 | continue  | 
            ||
| 615 | ally = tran.db[allyID]  | 
            ||
| 616 | if firing[allyID] and ally.type != Const.T_PLANET:  | 
            ||
| 617 | surrenderTo = []  | 
            ||
| 618 | break  | 
            ||
| 619 | if surrenderTo:  | 
            ||
| 620 | index = Utils.rand(0, len(surrenderTo))  | 
            ||
| 621 | if obj:  | 
            ||
| 622 | if self.cmd(obj).surrenderTo(tran, obj, surrenderTo[index]):  | 
            ||
| 623 | winner = tran.db[surrenderTo[index]]  | 
            ||
| 624 | source = tran.db.get(owners[objID], None)  | 
            ||
| 625 |                         log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) | 
            ||
| 626 | if source:  | 
            ||
| 627 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_LOST, system.oid, winner.oid)  | 
            ||
| 628 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, source.oid)  | 
            ||
| 629 | else:  | 
            ||
| 630 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, obj.oid)  | 
            ||
| 631 | else:  | 
            ||
| 632 | winner = tran.db[surrenderTo[index]]  | 
            ||
| 633 | source = tran.db[owners[objID]]  | 
            ||
| 634 |                     log.debug('ISystem', 'BATTLE - surrender', objID, surrenderTo[index], surrenderTo) | 
            ||
| 635 | Utils.sendMessage(tran, source, Const.MSG_COMBAT_LOST, system.oid, winner.oid)  | 
            ||
| 636 | Utils.sendMessage(tran, winner, Const.MSG_COMBAT_WON, system.oid, source.oid)  | 
            ||
| 637 | return  | 
            ||
| 638 | |||
| 889 |