def executeCode(code): """ Used by execute protocol to run a piece of code """ try: if "yield " in code: newcode = [] for p in code.split("\n"): newcode.append(" " + p) exec(""" @inlineCallbacks def _N(): %s """ % '\n'.join(newcode)) returnValue(otjson.dumps((yield _N()))) else: exec(code) except ReturnValueExit as e: returnValue(otjson.dumps(e.value)) else: yield defer.maybeDeferred()
def saveAll(force=False): """ Save everything, players, houses, global storage etc. """ commited = False t = time.time() for player in game.player.allPlayers.values(): result = player._saveQuery(force) if result: sql.runOperation(*result) commited = True # Global storage if saveGlobalStorage or force: for field in globalStorage: type = "" if field in jsonFields: data = otjson.dumps(globalStorage[field]) type = "json" elif field in pickleFields: data = fastPickler(globalStorage[field]) type = "pickle" else: data = globalStorage[field] sql.runOperation( "INSERT INTO `globals` (`key`, `data`, `type`) VALUES(%s, %s, %s) ON DUPLICATE KEY UPDATE `data` = %s", (field, data, type, data)) commited = True # Houses if game.map.houseTiles: for houseId, house in game.house.houseData.iteritems(): # House is loaded? if houseId in game.map.houseTiles: try: items = house.data["items"].copy() except: log.msg("House id %d have broken items!" % houseId) items = {} # Broken items for tile in game.map.houseTiles[houseId]: _items = [] lastItem = None for item in tile.bottomItems(): ic = item.count if not item.fromMap and (ic == None or ic > 0): if lastItem and lastItem.itemId == item.itemId and lastItem.stackable and lastItem.count < 100: # Stack. lCount = lastItem.count lastItem.count = min(100, lCount + ic) ic -= lastItem.count - lCount item.count = ic if ic or ic == None: _items.append(item) lastItem = item _items.reverse() items[tile.position] = _items if items != house.data["items"]: house.data["items"] = items house.save = True # Force save if house.save: log.msg("Saving house ", houseId) sql.runOperation( "UPDATE `houses` SET `owner` = %s,`guild` = %s,`paid` = %s, `data` = %s WHERE `id` = %s", (house.owner, house.guild, house.paid, fastPickler(house.data) if house.data else '', houseId)) house.save = False commited = True else: log.msg("Not saving house", houseId) if force: log.msg("Full (forced) save took: %f" % (time.time() - t)) elif commited: log.msg("Full save took: %f" % (time.time() - t))
def saveAll(force=False): """ Save everything, players, houses, global storage etc. """ commited = False t = time.time() for player in game.player.allPlayers.values(): result = player._saveQuery(force) if result: sql.runOperation(*result) commited = True # Global storage if saveGlobalStorage or force: for field in globalStorage: type = "" if field in jsonFields: data = otjson.dumps(globalStorage[field]) type = "json" elif field in pickleFields: data = fastPickler(globalStorage[field]) type = "pickle" else: data = globalStorage[field] sql.runOperation("INSERT INTO `globals` (`key`, `data`, `type`) VALUES(%s, %s, %s) ON DUPLICATE KEY UPDATE `data` = %s", (field, data, type, data)) commited = True # Houses if game.map.houseTiles: for houseId, house in game.house.houseData.iteritems(): # House is loaded? if houseId in game.map.houseTiles: try: items = house.data["items"].copy() except: log.msg("House id %d have broken items!" % houseId) items = {} # Broken items for tile in game.map.houseTiles[houseId]: _items = [] lastItem = None for item in tile.bottomItems(): ic = item.count if not item.fromMap and (ic == None or ic > 0): if lastItem and lastItem.itemId == item.itemId and lastItem.stackable and lastItem.count < 100: # Stack. lCount = lastItem.count lastItem.count = min(100, lCount + ic) ic -= lastItem.count - lCount item.count = ic if ic or ic == None: _items.append(item) lastItem = item _items.reverse() items[tile.position] = _items if items != house.data["items"]: house.data["items"] = items house.save = True # Force save if house.save: log.msg("Saving house ", houseId) sql.runOperation("UPDATE `houses` SET `owner` = %s,`guild` = %s,`paid` = %s, `data` = %s WHERE `id` = %s", (house.owner, house.guild, house.paid, fastPickler(house.data) if house.data else '', houseId)) house.save = False commited = True else: log.msg("Not saving house", houseId) if force: log.msg("Full (forced) save took: %f" % (time.time() - t)) elif commited: log.msg("Full save took: %f" % (time.time() - t))