def findPostByName(self, postName): postList = [] try: # 定义sql语句, 采用模糊检索 sql = "select * from tab_post where postname like '%%%s%%';" % postName self.cur.execute(sql) results = self.cur.fetchall() for row in results: postid = row[0] postname = row[1] postintro = row[2] # 创建taff对象 post = Post(postid, postname, postintro) # 加入列表 postList.append(post) return postList except Exception as e: print(e) return postList finally: # 关闭数据库连接 DbUtil.close(self.conn, self.cur)
def findAll(self): postList = [] try: # 定义sql语句 sql = "select * from tab_post;" self.cur.execute(sql) results = self.cur.fetchall() for row in results: postid = row[0] postname = row[1] postintro = row[2] # 创建Post对象 post = Post(postid, postname, postintro) # 加入列表 postList.append(post) return postList except Exception as e: print(e) return postList finally: # 关闭数据库连接 DbUtil.close(self.conn, self.cur)
def from_json_str(self, string_data): data = json.loads(string_data) if data.get('idx'): self.idx = data['idx'] if data.get('name'): self.name = data['name'] if data.get('size'): self.size = tuple(data['size']) if data.get('line'): self.line = {l['idx']: Line(l['idx'], l['length'], l['point'][0], l['point'][1]) for l in data['line']} if data.get('point'): self.point = {p['idx']: Point(p['idx'], post_id=p.get('post_id', None)) for p in data['point']} if data.get('post'): self.post = { p['idx']: Post( p['idx'], p['name'], p['type'], population=p.get('population', None), armor=p.get('armor', None), product=p.get('product', None), replenishment=p.get('replenishment', None), level=p.get('level', None), player_id=p.get('player_id', None), point_id=p.get('point_id', None) ) for p in data['post'] } if data.get('train'): self.train = { t['idx']: Train( t['idx'], line_idx=t['line_idx'], position=t['position'], speed=t['speed'], player_id=t['player_id'], level=t['level'], goods=t['goods'], post_type=t['post_type'] ) for t in data['train'] } if data.get('coordinate'): self.coordinate = {c['idx']: {'idx': c['idx'], 'x': c['x'], 'y': c['y']} for c in data['coordinate']} self.okey = True
def init_map(self): with map_session_ctx() as session: _map = session.query(MapModel).filter(MapModel.name == self.name).first() self.idx = _map.id self.size = (_map.size_x, _map.size_y) lines = _map.lines.order_by(LineModel.id).all() self.line = {l.id: Line(l.id, l.len, l.p0, l.p1) for l in lines} points = session.query(PointModel, func.max(PostModel.id)).outerjoin( PostModel, PointModel.id == PostModel.point_id).filter(PointModel.map_id == _map.id).group_by( PointModel.id).order_by(PointModel.id).all() for point, post_id in points: self.coordinate[point.id] = {'idx': point.id, 'x': point.x, 'y': point.y} self.point[point.id] = Point(point.id, post_id=post_id) posts = _map.posts.order_by(PostModel.id).all() self.post = { p.id: Post( p.id, p.name, p.type, p.population, p.armor, p.product, replenishment=p.replenishment, point_id=p.point_id ) for p in posts } self.markets = [m for m in self.post.values() if m.type == PostType.MARKET] self.storages = [s for s in self.post.values() if s.type == PostType.STORAGE] self.towns = [t for t in self.post.values() if t.type == PostType.TOWN] self.okey = True
def init_from_db(self): with session_ctx() as session: if self.name: _map = session.query(MapModel).filter( MapModel.name == self.name).first() elif self.use_active: _map = session.query(MapModel).filter( MapModel.active == true()).first() else: raise errors.WgForgeServerError('Unable to initialize the map') if _map is None: raise errors.WgForgeServerError('The map is not found') self.idx = _map.id self.name = _map.name self.size = (_map.size_x, _map.size_y) lines = _map.lines.order_by(LineModel.id).all() self.lines = { l.id: Line(l.id, l.length, l.p0, l.p1) for l in lines } points = session.query( PointModel, func.max(PostModel.id)).outerjoin( PostModel, PointModel.id == PostModel.point_id).filter( PointModel.map_id == _map.id).group_by( PointModel.id).order_by(PointModel.id).all() for point, post_id in points: self.coordinates[point.id] = { 'idx': point.id, 'x': point.x, 'y': point.y } self.points[point.id] = Point(point.id, post_idx=post_id) posts = _map.posts.order_by(PostModel.id).all() self.posts = { p.id: Post(p.id, p.name, p.type, p.population, p.armor, p.product, p.replenishment, p.point_id) for p in posts } self.markets = [ m for m in self.posts.values() if m.type == PostType.MARKET ] self.storages = [ s for s in self.posts.values() if s.type == PostType.STORAGE ] self.towns = [ t for t in self.posts.values() if t.type == PostType.TOWN ] self.initialized = True
def from_json_str(self, string_data): """ loads object from json string """ data = json.loads(string_data) if data.get('idx'): self.idx = data['idx'] if data.get('name'): self.name = data['name'] if data.get('train'): self.train = { t['idx']: Train(t['idx'], line_idx=t['line_idx'], position=t['position'], speed=t['speed'], player_id=t['player_id'], level=t['level'], goods=t['goods'], post_type=t['post_type']) for t in data['train'] } if data.get('home'): home = data['home'] self.home = Point(home['idx'], home['post_id']) if data.get('town'): town = data['town'] self.town = Post(town['idx'], town['name'], town['type'], population=town['population'], armor=town['armor'], product=town['product'], level=town['level'], player_id=town['player_id'], point_id=town['point_id']) if data.get('turn_done'): self.turn_done = data['turn_done'] if data.get('in_game'): self.in_game = data['in_game'] if data.get('security_key'): self.security_key = data['security_key']