def inputButtonClicked(self): text = self.inputScores.toPlainText().strip() if text == "": return try: b64 = bytes(text, encoding='utf8') decoded = scores_decode(b64).decode('utf-8') scores = json.loads(decoded) except: errorDialog(self, "Error", message="Failed to decode high scores") return scores_msg = "The string you added contains the following scores:<br><br>" scores_msg += "<br>".join(['%s (%s)' % (x[0], x[1]) for x in scores]) scores_msg += "<br><br>" scores_msg += "Are you sure you want to add them to your high scores?" proceed = yesNoDialog(self, "Add scores?", message=scores_msg) if not proceed: return for name, score in scores: config.add_highscore(name, score) infoDialog(self, "Success", "Scores added successfully")
def introduceNewItem(self, itemname): quantity = self.parent.state.items.items[itemname].quantity rand_quantity = random.randrange(*const.ITEM_SAMPLE_QUANTITY_RANGE) planet = self.parent.state.current_planet if quantity <= rand_quantity: rand_quantity = max(1, int(quantity / 2)) msg = ( "{0} has never been seen on {1}, and you will have to persuade them " "that it is worth buying. If you provide a free sample of {2} {0}, " "this may help your cause.<br><br>Provide a free sample of " "{2} {0}?".format(itemname, planet.full_name, rand_quantity)) proceed = yesNoDialog(self, "Provide sample?", message=msg) if not proceed: return if itemname in planet.samples_today: errorDialog( self, "Already sampled today", message="%s has already sampled %s today, try again on " "a different day" % (planet.full_name, itemname)) return planet.samples_today.append(itemname) # Add new items to planet-- we might remove them in a sec, but this # also handles deleteing them from the player's items, so, meh planet.items.add_items(itemname, self.parent.state.items, rand_quantity) self.parent.updatePlayerItemsLabel() self.update() successful = random.randrange(0, 100) < const.ITEM_SAMPLE_SUCCESS_PERCENT if successful: # Sample succesful, update planet item browser to show new item we added self.parent.planetItemBrowser.update() # Reset item's value history item = planet.items.items[itemname] item.value_history = [item.value] title = "Good news!" msg = ("Your sample achieved its intended purpose! " "%s is now actively trading in %s." % (planet.full_name, itemname)) else: # Sample unsuccessful, delete items from planet planet.items.remove_items(itemname, rand_quantity) title = "Bad news!" msg = ( "Your sample was not well received, and %s has decided not to " "trade in %s." % (planet.full_name, itemname)) infoDialog(self, title, message=msg)
def travelButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select a planet to travel to first!") return planetname = self.table.item(selectedRow, 0).text() self.travelToPlanet(planetname)
def travelButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select a planet to travel to first!") return planetname = self.table.item(selectedRow, 0).text() if self.parent.state.current_planet.full_name == planetname: errorDialog(self, message="You are already on %s!" % planetname) return self.travelToPlanet(planetname)
def dumpButtonClicked(self): if self.parent.state.items.count() == 0: errorDialog(self, "No items", "You have no items to dump.") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first!") return itemname = self.table.item(selectedRow, 0).text() dialog = DumpPlayerItem(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def sellAllButtonClicked(self): planet = self.parent.state.current_planet gain = 0 if self.parent.state.items.count() == 0: errorDialog(self, "No items", "You have no items to sell.") return items_for_sale_on_planet = False for name in self.parent.state.items.items: if name not in planet.items.items: continue items_for_sale_on_planet = True price = planet.items.items[name].value quantity = self.parent.state.items.items[name].quantity gain += price * quantity if not items_for_sale_on_planet: errorDialog( self, "Items cannot be sold", "This planet is not buying any of the items you are selling.") return proceed = yesNoDialog( self, "Sell all?", message="Are you sure you want to sell all items " "that are currently being traded on {0}? (total " "gain: {1:,})".format(planet.full_name, gain)) if not proceed: return for name in list(self.parent.state.items.items.keys()): if name not in planet.items.items: continue quantity = self.parent.state.items.items[name].quantity planet.items.add_items(name, self.parent.state.items, quantity) self.parent.state.money += gain checkForMoneyBonus(self.parent) self.parent.infoBar.update() self.parent.planetItemBrowser.update() self.parent.updatePlayerItemsLabel() self.update()
def sellButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item to sell first!") return itemname = self.table.item(selectedRow, 0).text() if itemname not in self.parent.state.current_planet.items.items: errorDialog(self, message="%s is not currently in demand on %s" % (itemname, self.parent.state.current_planet.full_name)) return dialog = Sell(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def dumpAllButtonClicked(self): if self.parent.state.warehouse.count() == 0: errorDialog(self, "No items", "You have no items to dump.") return proceed = yesNoDialog( self, "Dump everything?", message="Are you sure you want to dump all your items? You " "will lose all the items in your warehouse, and you " "will not be able to get them back.") if not proceed: return self.parent.state.warehouse.remove_all_items() self.update()
def warehouseButtonClicked(self): if self.parent.state.warehouse_puts == const.WAREHOUSE_PUTS_PER_DAY: errorDialog(self, "Warehouse", message="You cannot put anything else " "in the warehouse until tomorrow") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first!") return itemname = self.table.item(selectedRow, 0).text() dialog = PlayerToWarehouse(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def removeAllButtonClicked(self): totalitemcount = self.parent.state.warehouse.count() if totalitemcount == 0: errorDialog( self, "Warehouse", message="There is nothing in your warehouse to retrieve.") return if self.parent.state.warehouse_gets == self.parent.state.warehouse_gets_per_day: errorDialog(self, "Warehouse", message="You cannot take anything else " "from the warehouse until tomorrow.") return capacity = self.parent.state.capacity - self.parent.state.items.count() itemcount = min(capacity, totalitemcount) if itemcount < totalitemcount: msg = ( "You do not have room for all items, the maximum number of items " "that can be retrieved is {0:,}. Are you sure you want to retrieve {0:,} " "items? ".format(itemcount)) else: msg = "Are you sure you want to retrieve all items?" proceed = yesNoDialog(self.parent, "Are you sure?", message=msg) if not proceed: return for name in list(self.parent.state.warehouse.items.keys()): if itemcount == 0: break item = self.parent.state.warehouse.items[name] quantity = min(itemcount, item.quantity) self.parent.state.items.add_items(name, self.parent.state.warehouse, quantity) itemcount -= quantity self.parent.state.warehouse_gets += 1 self.parent.warehouseItemBrowser.update() self.parent.playerItemBrowser.update() self.parent.updatePlayerItemsLabel()
def dumpButtonClicked(self): totalitemcount = self.parent.state.warehouse.count() if totalitemcount == 0: errorDialog(self, "Warehouse", message="There is nothing in your warehouse to dump.") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first!") return itemname = self.table.item(selectedRow, 0).text() dialog = DumpWarehouseItem(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def sellButtonClicked(self): if self.parent.state.items.count() == 0: errorDialog(self, "No items", "You have no items to sell.") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item to sell first!") return itemname = self.table.item(selectedRow, 0).text() if itemname not in self.parent.state.current_planet.items.items: self.introduceNewItem(itemname) return dialog = Sell(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def config_store(): cfg = {} if config[SCORES_KEY]: string = json.dumps(config[SCORES_KEY]) encoded = scores_encode(bytes(string, encoding='utf8')).decode('utf-8') else: encoded = '' cfg[SCORES_KEY] = encoded try: with open(FILENAME, 'w') as fh: json.dump(cfg, fh) except: errorDialog(None, "Error", message="Unable to write file %s" % FILENAME)
def buyButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first") return item = store_items[selectedRow] if self.parent.state.money < item.price: errorDialog(self, message="You don't have enough money to buy '%s'" % item.name) return if not item.use(self.parent): return self.parent.state.money -= item.price self.parent.infoBar.update()
def travelToPlanet(self, planetname): if self.parent.state.money < self.parent.state.travel_cost: errorDialog(self, message="You don't have enough money! (%d required)" % self.parent.state.travel_cost) return accepted = yesNoDialog( self, "Travel", "Travel to %s? (cost is %d, you have %d)" % (planetname, self.parent.state.travel_cost, self.parent.state.money)) if not accepted: return self.parent.state.money -= self.parent.state.travel_cost self.parent.state.change_current_planet(planetname) self.parent.locationBrowser.update() self.parent.planetItemBrowser.update() self.parent.infoBar.update()
def buyButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, "No item selected", message="Please select an item to buy first!") return itemname = self.table.item(selectedRow, 0).text() if self.parent.state.current_planet.items.items[ itemname].quantity == 0: errorDialog(self, "None available", message="%s has no %s left to sell" % (self.parent.state.current_planet.full_name, itemname)) return dialog = Buy(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def checkHighScore(self): scores = config.get_highscores() # High scores are sorted in descending order if (len(scores) > 0) and (self.state.money <= scores[0][1]): return proceed = yesNoDialog( self, "High score!", message="You have achieved a high score ({:,}) ! " "would you like to enter your name? (high " "scores are only stored locally)".format(self.state.money)) if not proceed: return initial_text = '' if len(scores) == 0 else scores[0][0] name = None while True: name, accepted = QtWidgets.QInputDialog.getText( self, 'Enter name', "Enter your name for the high score table", text=initial_text) if not accepted: return if len(name) > const.MAX_HIGHSCORE_NAME_LEN: errorDialog( self, "Too long", "Name is too long (max %d characters)" % const.MAX_HIGHSCORE_NAME_LEN) else: break config.add_highscore(name, self.state.money) config.config_store() self.showHighScores()
def removeButtonClicked(self): totalitemcount = self.parent.state.warehouse.count() if totalitemcount == 0: errorDialog( self, "Warehouse", message="There is nothing in your warehouse to retrieve.") return if self.parent.state.warehouse_gets == self.parent.state.warehouse_gets_per_day: errorDialog(self, "Warehouse", message="You cannot take anything else " "from the warehouse until tomorrow") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first!") return itemname = self.table.item(selectedRow, 0).text() dialog = WarehouseToPlayer(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def selectButtonClicked(self): selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select a planet to travel to first!") return planet = self.parent.state.planets[selectedRow] if self.parent.state.current_planet.full_name == planet.full_name: errorDialog(self, message="You are currently on %s, you cannot " "destroy a planet that you are on" % planet.full_name) return proceed = yesNoDialog(self, "Are you sure?", message="Are you sure you want to destroy the " "planet {0}? {0} will cease to exist, and " "all tradeable items that currrently exist " "on {0} will be shipped to your " "warehouse.".format(planet.full_name)) if not proceed: return self.parent.state.warehouse.add_all_items(planet.items) del self.parent.state.planets[selectedRow] self.parent.infoBar.update() self.parent.locationBrowser.update() self.parent.warehouseItemBrowser.update() self.parent.infoBar.update() self.close() self.accepted = True infoDialog(self.parent, "Success", message="Your destruction of %s is complete." % planet.full_name)
def warehouseButtonClicked(self): if self.parent.state.items.count() == 0: errorDialog(self, "No items", "You have no items to put in the warehouse.") return if self.parent.state.warehouse_puts == self.parent.state.warehouse_puts_per_day: errorDialog(self, "Warehouse", message="You cannot put anything else " "in the warehouse until tomorrow") return selectedRow = self.table.currentRow() if selectedRow < 0: errorDialog(self, message="Please select an item first!") return itemname = self.table.item(selectedRow, 0).text() dialog = PlayerToWarehouse(self.parent, itemname) dialog.setWindowModality(QtCore.Qt.ApplicationModal) dialog.exec_()
def previousButtonClicked(self): if self.parent.state.previous_planet is None: errorDialog(self, message="No previous planet to travel to!") return self.travelToPlanet(self.parent.state.previous_planet.full_name)
def travelToPlanet(self, planetname): if self.parent.state.current_planet.full_name == planetname: errorDialog(self, message="You are already on %s!" % planetname) return if self.parent.state.money < self.parent.state.travel_cost: errorDialog(self, message="You don't have enough money! (%d required)" % self.parent.state.travel_cost) return accepted = yesNoDialog( self, "Travel", "Travel to {0}?<br><br>(cost is {1:,}, you have {2:,})".format( planetname, self.parent.state.travel_cost, self.parent.state.money)) if not accepted: return self.parent.state.money -= self.parent.state.travel_cost if random.randint( 0, 100) <= self.parent.state.chance_of_being_robbed_in_transit(): accepted = yesNoDialog( self, "Attacked by pirates!", "You have encountered a pirate fleet while travelling " + "between planets!<br><br>Your battle fleet must defeat them if " + "you want to continue.<br><br>If you do not fight, then the only other " + "option is surrender; you will not die, but you may lose some of " + "your money and resources.<br><br>Do you want to fight?") if accepted: if self.parent.state.battle_won(): infoDialog( self, "Battle won!", "You have defeated the pirate fleet, " + "and can continue with your travels.") else: infoDialog( self, "Battle lost!", "You have been defeated by the pirate fleet." + "<br><br>You are dead.") self.parent.reset() return else: if (self.parent.state.items.count() == 0) or (random.randint(0, 100) >= 80): # take 95-99% percent of players money percent_to_take = random.randint(95, 99) money_to_take = (float(self.parent.state.money) / 100.0) * percent_to_take self.parent.state.money -= int(money_to_take) self.parent.state.items.remove_all_items() self.parent.playerItemBrowser.update() self.parent.infoBar.update() infoDialog( self, "Surrender", "You decide not to fight the pirate fleet. " + "<br><br>The pirates spare your life, but they rob you of everything you've got!" ) self.parent.state.change_current_planet(planetname) self.parent.advanceDay() currentRow = self.table.currentRow() self.update() self.table.selectRow(currentRow) self.parent.planetItemBrowser.update() self.parent.infoBar.update()
def _malformed_config(): errorDialog(None, "Error", message="Malformed config file: %s" % FILENAME)