def __init__(self, field_size=FIELD_SIZE): logging.info("Created Farm instance") self.field_size = field_size self.field = [[Plant() for _ in range(field_size)] for _ in range(field_size)] self.money = 0 self.lock = Lock()
def plant_handler(col, row, plant_type): user = session['user'] if user.get_plant(col, row).type != 'empty': pass new_plant = Plant((plant_type, time.time())) user.set_plant(col, row, new_plant) db.session.add(user) db.session.commit() emit('set_plant', { 'position': (col, row), 'new_plant': new_plant.info }, room=user.username)
def harvest_handler(col, row): user = session['user'] cur_plant = user.get_plant(col, row) user.money = user.money + cur_plant.cost new_plant = Plant() user.set_plant(col, row, new_plant) db.session.add(user) db.session.commit() emit('set_plant', { 'position': (col, row), 'new_plant': new_plant.info }, room=user.username)
def get_plant(self, col, row): if row >= TILE_NUM or col >= TILE_NUM: raise AttributeError("Plant {} {} doesn't exist".format(col, row)) return Plant(self.field[row * TILE_NUM + col])
def __init__(self, **kwargs): super().__init__(**kwargs) if 'field' not in kwargs: self.field = [Plant().info] * (TILE_NUM**2)
def plant(self, col, row, plant_type): fake_plant = Plant((plant_type, time.time())) self._set_fake_plant(col, row, fake_plant) sio.emit('plant', (col, row, plant_type))
def harvest_plant(self, col, row): plant = self.get_plant(col, row) self._set_money(self.money + plant.cost) self._set_fake_plant(col, row, Plant()) sio.emit('harvest', (col, row))
def set_plant(self, col, row, plant_info): logging.info("New plant at {} {} is {}".format(col, row, plant_info)) with self.lock: self.field[row][col] = Plant(plant_info)