class Controller: def __init__(self): self.gui = Gui() self.dao = ClientDAO() self.selected = None #cliente selecionado self.currentClient = Client() def view_command(self): "método para visualização dos resultados" try: rows = self.dao.view() self.gui.listClientes.delete(0, END) for r in rows: self.gui.listClientes.insert(END, r) except Exception as e: print(e) def __fill_current_client(self): self.currentClient.nome = self.gui.txtNome.get() self.currentClient.sobrenome = self.gui.txtSobrenome.get() self.currentClient.email = self.gui.txtEmail.get() self.currentClient.cpf = self.gui.txtCPF.get() def search_command(self): "método para buscar registros" self.gui.listClientes.delete(0, END) self.__fill_current_client() try: rows = self.dao.search(self.currentClient) for r in rows: self.gui.listClientes.insert(END, r) except Exception as e: print(e) def insert_command(self): "método para inserir registros" self.__fill_current_client() self.dao.insert(self.currentClient) self.view_command() def get_selected_row(self, event): "método que seleciona na listbox e popula os campos de input" if self.gui.listClientes.curselection(): index = self.gui.listClientes.curselection()[0] self.selected = self.gui.listClientes.get(index) self.gui.entNome.delete(0, END) self.gui.entNome.insert(END, self.selected[1]) self.gui.entSobrenome.delete(0, END) self.gui.entSobrenome.insert(END, self.selected[2]) self.gui.entEmail.delete(0, END) self.gui.entEmail.insert(END, self.selected[3]) self.gui.entCPF.delete(0, END) self.gui.entCPF.insert(END, self.selected[4]) def update_command(self): "método para atualizar registro" id = self.selected[0] self.__fill_current_client() self.dao.update(id, self.currentClient) self.view_command() def del_command(self): "método para remover registro" id = self.selected[0] self.dao.delete(id) self.view_command() def close_command(self): self.dao.close() self.gui.window.destroy() def start(self): self.gui.listClientes.bind('<<ListboxSelect>>', self.get_selected_row) #associando o comportamento à interface self.gui.btnViewAll.configure(command=self.view_command) self.gui.btnBuscar.configure(command=self.search_command) self.gui.btnInserir.configure(command=self.insert_command) self.gui.btnUpdate.configure(command=self.update_command) self.gui.btnDel.configure(command=self.del_command) self.gui.btnClose.configure(command=self.close_command) self.gui.run()
class Controller: def __init__(self): self.gui = Gui() self.dao = VeiculoDAO() self.selected = None #cliente selecionado self.currentVeiculo = Veiculo() def view_command(self): "método para visualização dos resultados" try: rows = self.dao.view() self.gui.listVeiculos.delete(0, END) for r in rows: self.gui.listVeiculos.insert(END, r) except Exception as e: print(e) def __fill_current_veiculo(self): self.currentVeiculo.marca = self.gui.txtMarca.get() self.currentVeiculo.modelo = self.gui.txtModelo.get() self.currentVeiculo.ano = self.gui.txtAno.get() self.currentVeiculo.cor = self.gui.txtCor.get() self.currentVeiculo.tanque = self.gui.txtTanque.get() self.currentVeiculo.combustivel = self.gui.txtCombustivel.get() self.currentVeiculo.consumo_cidade = self.gui.txtConsumo_Cidade.get() self.currentVeiculo.consumo_estrada = self.gui.txtConsumo_Estrada.get() self.currentVeiculo.tempo_0_100 = self.gui.txtTempo_0_100.get() self.currentVeiculo.chassi = self.gui.txtChassi.get() self.currentVeiculo.placa = self.gui.txtPlaca.get() self.currentVeiculo.tamanho_pneu = self.gui.txtTamanho_Pneu.get() self.currentVeiculo.som = self.gui.txtSom.get() self.currentVeiculo.valor_diaria = self.gui.txtValor_Diaria.get() def search_command(self): "método para buscar registros" self.gui.listVeiculos.delete(0, END) self.__fill_current_veiculo() try: rows = self.dao.search(self.currentVeiculo) for r in rows: self.gui.listVeiculos.insert(END, r) except Exception as e: print(e) def insert_command(self): "método para inserir registros" self.__fill_current_veiculo() self.dao.insert(self.currentVeiculo) self.view_command() def get_selected_row(self, event): "método que seleciona na listbox e popula os campos de input" if self.gui.listVeiculos.curselection(): index = self.gui.listVeiculos.curselection()[0] self.selected = self.gui.listVeiculos.get(index) self.gui.entMarca.delete(0, END) self.gui.entMarca.insert(END, self.selected[1]) self.gui.entModelo.delete(0, END) self.gui.entModelo.insert(END, self.selected[2]) self.gui.entAno.delete(0, END) self.gui.entAno.insert(END, self.selected[3]) self.gui.entCor.delete(0, END) self.gui.entCor.insert(END, self.selected[4]) self.gui.entTanque.delete(0, END) self.gui.entTanque.insert(END, self.selected[1]) self.gui.entCombustivel.delete(0, END) self.gui.entCombustivel.insert(END, self.selected[2]) self.gui.entConsumo_Cidade.delete(0, END) self.gui.entConsumo_Cidade.insert(END, self.selected[3]) self.gui.entConsumo_Estrada.delete(0, END) self.gui.entConsumo_Estrada.insert(END, self.selected[4]) self.gui.entTempo_0_100.delete(0, END) self.gui.entTempo_0_100.insert(END, self.selected[1]) self.gui.entChassi.delete(0, END) self.gui.entChassi.insert(END, self.selected[1]) self.gui.entPlaca.delete(0, END) self.gui.entPlaca.insert(END, self.selected[1]) self.gui.entTamanho_Pneu.delete(0, END) self.gui.entTamanho_Pneu.insert(END, self.selected[1]) self.gui.entSom.delete(0, END) self.gui.entSom.insert(END, self.selected[1]) self.gui.entValor_Diaria.delete(0, END) self.gui.entValor_Diaria.insert(END, self.selected[1]) def update_command(self): "método para atualizar registro" id = self.selected[0] self.__fill_current_veiculo() self.dao.update(id, self.currentVeiculo) self.view_command() def del_command(self): "método para remover registro" id = self.selected[0] self.dao.delete(id) self.view_command() def close_command(self): self.dao.close() self.gui.window.destroy() def start(self): self.gui.listVeiculos.bind('<<ListboxSelect>>', self.get_selected_row) #associando o comportamento à interface self.gui.btnViewAll.configure(command=self.view_command) self.gui.btnBuscar.configure(command=self.search_command) self.gui.btnInserir.configure(command=self.insert_command) self.gui.btnUpdate.configure(command=self.update_command) self.gui.btnDel.configure(command=self.del_command) self.gui.btnClose.configure(command=self.close_command) self.gui.run()
class Engine: def __init__(self, Id, name, player, seed, client, nplayers, savedGame): #temp self.gameId = random.randint(0,1000) self.turn = 1 self.rounds = 0 self.roundsPlayed = 0 self.width = 1100 self.height = 650 self.initPlayers(nplayers) self.seednumber = seed random.seed(self.seednumber) self.grid = Grid(1, self.width, self.height, self) self.grid.populateMap(self.players) self.Gui = Gui(self, self.width, self.height, name, player, client, savedGame) self.run() def initPlayers(self, n): self.players = {x : Player(1,"name",x) for x in range(1,n+1)} def applyMove(self,destNumber, unitHexNumber): unit = self.grid.hexes[unitHexNumber].occupant h = self.grid.hexes[destNumber] self.movePath(h,unit) def applyBuildMeadow(self, hex): self.grid.hexes[hex].occupant.setBuildingMeadow() def applyBuildRoad(self, hex): self.grid.hexes[hex].occupant.setBuildingRoad() def applyUpgradeUnit(self, unitHex, level): self.grid.hexes[unitHex].occupant.upgrade(level) def applyBuildWatchtower(self, hex): self.grid.hexes[hex].buildWatchTower() def applySpawnUnit(self,hex,type): selected = self.grid.hexes[hex] selected.village.spawnUnit(selected,type) def applyUpgradeVillage(self, hex): self.grid.hexes[hex].village.upgrade() def applyCombine(self, hex1, hex2): selected = self.grid.hexes[hex1] selected.village.combine(selected, self.grid.hexes[hex2]) def canCapture(self, h, unit): if not h.village: return True if unit.type==0 or unit.type == 4: return False if unit.type == 1 and h.village.hex == h: return False if h.occupant and h.occupant.type==4 and unit.type != 3: return False for g in h.neighbours + [h]: if ((g.occupant and g.occupant.type>=unit.type and g.village.owner == h.village.owner) #neighbour hex has unit of higher level or (g.hasWatchTower and unit.type<2 and g.village.owner == h.village.owner) #watch tower in neighbours and unit level<2 or (h.village.type ==2 and g==h.village.hex and unit.type<3) or (h.village.hex == g and h.village.type == 3)): return False return True def join(self, h, unit): joinVillages = {g.village for g in h.neighbours if g.village and g.village.owner == unit.village.owner} if len(joinVillages)>1: temp = max(joinVillages, key = lambda v: v.type) temp2 = [t for t in joinVillages if t.type == temp.type] main = max(temp2, key = lambda v: len(v.territory)) joinVillages.remove(main) for v in joinVillages: main.addVillage(v) v.hex.putMeadow() main.owner.villages.remove(v) del list(joinVillages)[:] def splitTerritory(self,h,unit): splitStarts = [g for g in h.neighbours if g.village == h.village] #maybe fix later if len(splitStarts)>=2: temp = self.grid.BFS(splitStarts[0], lambda g: True if g != splitStarts[0] else False, lambda g: True if g in splitStarts else False) for x in temp: splitStarts.remove(x) if len(splitStarts)>=2: temp = self.grid.BFS(splitStarts[1], lambda g: True if g != splitStarts[1] else False, lambda g: True if g in splitStarts else False) for x in temp: splitStarts.remove(x) splitTerrities = [self.grid.BFS(start, lambda g: True, lambda g: True if g.village == start.village and g != h else False) for start in splitStarts] for split in splitTerrities: if len(split)<3: for g in split: h.village.territory.remove(g) g.hasWatchTower = False g.village = None g.owner = 0 elif h.village.hex not in split: t = Village(random.choice(split), h.village.owner, split) h.village.owner.addVillage(t) for g in split: h.village.territory.remove(g) if g.occupant: t.units.append(g.occupant) g.occupant.village = t h.village.units.remove(g.occupant) if h.occupant: h.village.killUnit(h.occupant) del h.occupant h.occupant = None h.village.territory.remove(h) if h.village.hex == h and len(h.village.territory)>2: h.village.hex = random.choice(h.village.territory) h.village.reset() for unit in h.village.units: unit.village= unit.hex.village if unit.village and unit.village != h.village: h.village.units.remove(unit) unit.village.units.append(unit) if h.village and h.village.hex.owner ==0 or h.village.hex == h: h.village.owner.villages.remove(h.village) h.village.killUnits() if h.village.hex != h: h.village.hex.putTree() if h.village.hex == h: unit.hex.village.gold += h.village.gold unit.hex.village.wood += h.village.wood h.putMeadow() del h.village def movePath(self, h, unit): if h == unit.hex: return False path = self.grid.Astar(unit.hex, h) if not path: return False temp = False if unit.type==4: if path and path[0] == unit.hex: unit.hex.occupant = None unit.hex = h h.occupant = unit unit.moved = True temp = True elif path and unit.village.wood>=2: temp =True #fire cannon if path[1].occupant: path[1].village.killUnitPlaceTomb(path[1].occupant) unit.village.wood-=2 unit.moved = True elif path[1].village and path[1].village.hex == path[1]: path[1].village.hitPoints-=1 unit.village.wood-=2 unit.moved = True if path[1].village.hitPoints==0: h.village.hex = random.choice(h.village.territory) h.village.reset() else: temp = self.moveUnit(h, unit) if temp and not (unit.type ==4 and path and not path[0] == unit.hex): if unit.type>=2: for g in path[1:]: if g.hasMeadow and not g.hasRoad: g.trample() return temp def moveUnit(self, h, unit): ret = False captured = False if unit.moved: return False if h.hasTree and unit.type == 3: return False if h.village == unit.village and not h.occupant: unit.hex.occupant = None unit.hex = h h.occupant = unit ret = True elif not h.village and not h.occupant: self.join(h,unit) unit.hex.occupant = None unit.hex = h unit.village.territory.append(h) h.village = unit.village h.owner = unit.village.hex.owner h.occupant = unit ret = True captured = True elif self.canCapture(h, unit): self.join(h,unit) self.splitTerritory(h,unit) unit.hex.occupant = None unit.hex = h unit.village.territory.append(h) h.village = unit.village h.owner = unit.village.hex.owner h.occupant = unit h.hasWatchTower = False ret = True captured = True if ret: unit.moved = captured if h.hasTree and unit.type<3: unit.gatherWood() if h.hasTombstone and unit.type<3: unit.removeTombstone() return ret def newGame(self, players, mapData): pass def growthPhase(self): toPlant = [] for h in self.grid.hexes.values(): if h.hasTree: #temp = [g for g in h.neighbours if not g.hasTree and not g.hasRoad and not g.hasWatchTower and not g.hasTombstone and not (g.village and g.village.hex == g) and not g.occupant] #if temp: if h.neighbours: g= random.choice(h.neighbours) if random.random()>.5 and not g.hasTree and not g.hasRoad and not g.hasWatchTower and not g.hasTombstone and not (g.village and g.village.hex == g) and not g.occupant: toPlant.append(g) for h in toPlant: h.putTree() def tombPhase(self, player): for v in player.villages: for t in v.territory: if t.hasTombstone: t.removeTomb() if not t.occupant: t.putTree() def buildPhase(self,player): for v in player.villages: for u in v.units: u.update() def incomePhase(self,player): for v in player.villages: v.gold += sum(t.getIncome() for t in v.territory) def paymentPhase(self, player): for v in player.villages: temp = sum(u.getUpkeep() for u in v.units) +(80 if v.type == 3 else 0) if v.gold<temp: v.killUnits() else: v.gold -= temp def beginTurn(self, p): #player or turn? #recieve data beforehand player = self.players[p] self.tombPhase(player) self.buildPhase(player) self.incomePhase(player) self.paymentPhase(player) def run(self): self.Gui.run() def __getstate__(self): odict = self.__dict__.copy() try: del odict['Gui'] except: pass return odict def __setstate__(self, dict): self.__dict__.update(dict) random.seed(self.seednumber)
class Engine: def __init__(self, Id, name, player, seed, client, nplayers, savedGame): #temp self.gameId = random.randint(0, 1000) self.turn = 1 self.rounds = 0 self.roundsPlayed = 0 self.width = 1100 self.height = 650 self.initPlayers(nplayers) self.seednumber = seed random.seed(self.seednumber) self.grid = Grid(1, self.width, self.height, self) self.grid.populateMap(self.players) self.Gui = Gui(self, self.width, self.height, name, player, client, savedGame) self.run() def initPlayers(self, n): self.players = {x: Player(1, "name", x) for x in range(1, n + 1)} def applyMove(self, destNumber, unitHexNumber): unit = self.grid.hexes[unitHexNumber].occupant h = self.grid.hexes[destNumber] self.movePath(h, unit) def applyBuildMeadow(self, hex): self.grid.hexes[hex].occupant.setBuildingMeadow() def applyBuildRoad(self, hex): self.grid.hexes[hex].occupant.setBuildingRoad() def applyUpgradeUnit(self, unitHex, level): self.grid.hexes[unitHex].occupant.upgrade(level) def applyBuildWatchtower(self, hex): self.grid.hexes[hex].buildWatchTower() def applySpawnUnit(self, hex, type): selected = self.grid.hexes[hex] selected.village.spawnUnit(selected, type) def applyUpgradeVillage(self, hex): self.grid.hexes[hex].village.upgrade() def applyCombine(self, hex1, hex2): selected = self.grid.hexes[hex1] selected.village.combine(selected, self.grid.hexes[hex2]) def canCapture(self, h, unit): if not h.village: return True if unit.type == 0 or unit.type == 4: return False if unit.type == 1 and h.village.hex == h: return False if h.occupant and h.occupant.type == 4 and unit.type != 3: return False for g in h.neighbours + [h]: if ((g.occupant and g.occupant.type >= unit.type and g.village.owner == h.village.owner ) #neighbour hex has unit of higher level or (g.hasWatchTower and unit.type < 2 and g.village.owner == h.village.owner ) #watch tower in neighbours and unit level<2 or (h.village.type == 2 and g == h.village.hex and unit.type < 3) or (h.village.hex == g and h.village.type == 3)): return False return True def join(self, h, unit): joinVillages = { g.village for g in h.neighbours if g.village and g.village.owner == unit.village.owner } if len(joinVillages) > 1: temp = max(joinVillages, key=lambda v: v.type) temp2 = [t for t in joinVillages if t.type == temp.type] main = max(temp2, key=lambda v: len(v.territory)) joinVillages.remove(main) for v in joinVillages: main.addVillage(v) v.hex.putMeadow() main.owner.villages.remove(v) del list(joinVillages)[:] def splitTerritory(self, h, unit): splitStarts = [g for g in h.neighbours if g.village == h.village] #maybe fix later if len(splitStarts) >= 2: temp = self.grid.BFS( splitStarts[0], lambda g: True if g != splitStarts[0] else False, lambda g: True if g in splitStarts else False) for x in temp: splitStarts.remove(x) if len(splitStarts) >= 2: temp = self.grid.BFS( splitStarts[1], lambda g: True if g != splitStarts[1] else False, lambda g: True if g in splitStarts else False) for x in temp: splitStarts.remove(x) splitTerrities = [ self.grid.BFS( start, lambda g: True, lambda g: True if g.village == start.village and g != h else False) for start in splitStarts ] for split in splitTerrities: if len(split) < 3: for g in split: h.village.territory.remove(g) g.hasWatchTower = False g.village = None g.owner = 0 elif h.village.hex not in split: t = Village(random.choice(split), h.village.owner, split) h.village.owner.addVillage(t) for g in split: h.village.territory.remove(g) if g.occupant: t.units.append(g.occupant) g.occupant.village = t h.village.units.remove(g.occupant) if h.occupant: h.village.killUnit(h.occupant) del h.occupant h.occupant = None h.village.territory.remove(h) if h.village.hex == h and len(h.village.territory) > 2: h.village.hex = random.choice(h.village.territory) h.village.reset() for unit in h.village.units: unit.village = unit.hex.village if unit.village and unit.village != h.village: h.village.units.remove(unit) unit.village.units.append(unit) if h.village and h.village.hex.owner == 0 or h.village.hex == h: h.village.owner.villages.remove(h.village) h.village.killUnits() if h.village.hex != h: h.village.hex.putTree() if h.village.hex == h: unit.hex.village.gold += h.village.gold unit.hex.village.wood += h.village.wood h.putMeadow() del h.village def movePath(self, h, unit): if h == unit.hex: return False path = self.grid.Astar(unit.hex, h) if not path: return False temp = False if unit.type == 4: if path and path[0] == unit.hex: unit.hex.occupant = None unit.hex = h h.occupant = unit unit.moved = True temp = True elif path and unit.village.wood >= 2: temp = True #fire cannon if path[1].occupant: path[1].village.killUnitPlaceTomb(path[1].occupant) unit.village.wood -= 2 unit.moved = True elif path[1].village and path[1].village.hex == path[1]: path[1].village.hitPoints -= 1 unit.village.wood -= 2 unit.moved = True if path[1].village.hitPoints == 0: h.village.hex = random.choice(h.village.territory) h.village.reset() else: temp = self.moveUnit(h, unit) if temp and not (unit.type == 4 and path and not path[0] == unit.hex): if unit.type >= 2: for g in path[1:]: if g.hasMeadow and not g.hasRoad: g.trample() return temp def moveUnit(self, h, unit): ret = False captured = False if unit.moved: return False if h.hasTree and unit.type == 3: return False if h.village == unit.village and not h.occupant: unit.hex.occupant = None unit.hex = h h.occupant = unit ret = True elif not h.village and not h.occupant: self.join(h, unit) unit.hex.occupant = None unit.hex = h unit.village.territory.append(h) h.village = unit.village h.owner = unit.village.hex.owner h.occupant = unit ret = True captured = True elif self.canCapture(h, unit): self.join(h, unit) self.splitTerritory(h, unit) unit.hex.occupant = None unit.hex = h unit.village.territory.append(h) h.village = unit.village h.owner = unit.village.hex.owner h.occupant = unit h.hasWatchTower = False ret = True captured = True if ret: unit.moved = captured if h.hasTree and unit.type < 3: unit.gatherWood() if h.hasTombstone and unit.type < 3: unit.removeTombstone() return ret def newGame(self, players, mapData): pass def growthPhase(self): toPlant = [] for h in self.grid.hexes.values(): if h.hasTree: #temp = [g for g in h.neighbours if not g.hasTree and not g.hasRoad and not g.hasWatchTower and not g.hasTombstone and not (g.village and g.village.hex == g) and not g.occupant] #if temp: if h.neighbours: g = random.choice(h.neighbours) if random.random( ) > .5 and not g.hasTree and not g.hasRoad and not g.hasWatchTower and not g.hasTombstone and not ( g.village and g.village.hex == g) and not g.occupant: toPlant.append(g) for h in toPlant: h.putTree() def tombPhase(self, player): for v in player.villages: for t in v.territory: if t.hasTombstone: t.removeTomb() if not t.occupant: t.putTree() def buildPhase(self, player): for v in player.villages: for u in v.units: u.update() def incomePhase(self, player): for v in player.villages: v.gold += sum(t.getIncome() for t in v.territory) def paymentPhase(self, player): for v in player.villages: temp = sum(u.getUpkeep() for u in v.units) + (80 if v.type == 3 else 0) if v.gold < temp: v.killUnits() else: v.gold -= temp def beginTurn(self, p): #player or turn? #recieve data beforehand player = self.players[p] self.tombPhase(player) self.buildPhase(player) self.incomePhase(player) self.paymentPhase(player) def run(self): self.Gui.run() def __getstate__(self): odict = self.__dict__.copy() try: del odict['Gui'] except: pass return odict def __setstate__(self, dict): self.__dict__.update(dict) random.seed(self.seednumber)