def test_MessageHandler_fleet(self): obj = consoleclient.ConsoleClient() f = Fleet(ID=0) f.x = 10. f.y = 10. obj.FleetList = [ "Dummy", ] obj.MessageHandlerBase('FLEETS|' + f.ToString()) self.assertEqual(len(obj.FleetList), 1) self.assertEqual(obj.FleetList[0].x, 10.)
def OrderSplitFleet(self, playerID, fleetID, target_planet, numships): "returns stringif error; none otherwise" if numships < 0: return "-ve ships in split" if numships == 0: # Do nothing return None pos = -1 for i in range(0, len(self.FleetList)): if fleetID == self.FleetList[i].ID: pos = i break if pos == -1: return "No fleet with that ID" fleet = self.FleetList[pos] if fleet.PlayerID <> playerID: return "Not your fleet!" if fleet.AtPlanetID == -1: return "Fleet in space" if self.Galaxy.Distances[(fleet.AtPlanetID, target_planet )] > self.PlayerList[playerID].ShipRange: return "Planet out of range" if numships > fleet.Ships: return "Too many ships in SPLIT" new_f = Fleet() new_f.Ships = numships new_f.AtPlanetID = -1 new_f.TargetID = target_planet new_f.PlayerID = fleet.PlayerID new_f.x = fleet.x new_f.y = fleet.y self.FleetList.append(new_f) fleet.Ships -= numships return None
def MessageHandlerBase(self, msg): if msg.lower().startswith("state"): msg = msg.split(":") self.GameState = msg[1] if len(msg) < 3: return turn = msg[2].split("=") assert (turn[0].lower() == 'turn') self.GameTurn = int(turn[1]) if self.GameTurn > self.LastTurn: self.PlanetList = [] self.FleetList = [] self.LastTurn = self.GameTurn self.CommunicationClient.SendMessage("?PLANETS") self.CommunicationClient.SendMessage("?FLEETS") return if msg.startswith("PLANETS|"): if msg[-1] == '|': # if we have a trailing |, kill it! msg = msg[:-1] msg = msg.split("|") msg.pop(0) self.PlanetList = [Planet.FromString(s) for s in msg] return if msg.startswith("FLEETS|"): if msg[-1] == '|': # if we have a trailing |, kill it! msg = msg[:-1] msg = msg.split("|") msg.pop(0) self.FleetList = [Fleet.FromString(s) for s in msg] return self.MessageHandler(msg)
def ParseFleets(self, msg): plist = msg.split("|") fleetinfo = [] for f in plist: if not ";" in f: continue fleet = Fleet.FromString(f) fleetinfo.append(fleet) self.FleetList = fleetinfo
def Production(self): for p in self.Galaxy.PlanetList: # ignore fractional production for now prod = int(p.Production) if p.PlayerID == self.NumPlayers: prod = int(prod / 4) if prod == 0: continue f_list = self.FindFleetsAtPlanet(p.ID, p.PlayerID) if len(f_list) == 0: # Create a new fleet f = Fleet() f.Ships = prod f.AtPlanetID = p.ID f.PlayerID = p.PlayerID f.x = p.x f.y = p.y self.FleetList.append(f) else: f_list[0].Ships += prod
def CreateFleet1(self): f = Fleet(ID=1) f.PlayerID = 1 f.Ships = 10 f.IsDefending = False return f
def CreateFleet2(self): f = Fleet(ID=2) f.PlayerID = 2 f.Ships = 20 f.IsDefending = False return f