class UserRedis(walrus.Model): __database__ = WALRUS_DB _id = walrus.AutoIncrementField(walrus.IntegerField) username = walrus.TextField() password = walrus.TextField() age = walrus.IntegerField() place = walrus.TextField() dob = walrus.DateTimeField()
class Task(walrus.Model): __database__ = redis_db id = walrus.TextField(primary_key=True) args = walrus.TextField() stdout = walrus.TextField() stderr = walrus.TextField() returncode = walrus.IntegerField() state = walrus.TextField()
class Ball(walrus.Model): @staticmethod def new(): ball = Ball.create( id=uuid.uuid4(), ballType=random.choice(BALL_TYPES) ) ball.vector['x'] = random.randint(MIN_SPEED, MAX_SPEED) * random.choice([-1, 1]) ball.vector['y'] = random.randint(MIN_SPEED, MAX_SPEED) * random.choice([-1, 1]) ball.position['x'] = 500 + asFloat(ball.vector['x']) ball.position['y'] = 500 + asFloat(ball.vector['y']) ball.save() return Ball.load(ball.id) def move(self, elapsed: float): ''' Move the ball and check if it has collided with a wall. We must explicitly call self.save() to avoid edge conditions with walrus where changes are not preserved. ''' self.position['x'] = asFloat(self.position['x']) + asFloat(self.vector['x']) * elapsed self.position['y'] = asFloat(self.position['y']) + asFloat(self.vector['y']) * elapsed checkPosition(self, elapsed) self.save() def to_json(self): ''' Recursively convert fields to json-friendly output. Return a dict with the following schema: { id: "uuid", pos: { x: float, y: float, }, vec: { x: float, y: float, }, type: "ballType" } ''' return dict( id=str(self.id), pos=dict( x=asFloat(self.position['x']), y=asFloat(self.position['y'])), vec=dict( x=asFloat(self.vector['x']), y=asFloat(self.vector['y'])), type=self.ballType ) __database__ = walrus_conn id = walrus.UUIDField(primary_key=True, index=True) position = walrus.HashField() vector = walrus.HashField() ballType = walrus.TextField()
class Player(walrus.Model): @staticmethod def new(session_id=uuid.uuid4(), user="******", wall=0) -> 'Player': player = Player.create( id=uuid.uuid4(), room=NULL_UUID, username=user, score=0, ) player.paddle['pos'] = .5 player.paddle['wall'] = wall player.save() return Player.load(player.id) def set_room(self, wall: int, room: uuid.UUID) -> 'Player': self.room = room self.paddle['wall'] = wall self.save() return Player.load(self.id) def updatePaddle(self, newPos): self.paddle['pos'] = newPos self.save() def to_json(self): ''' Recursively convert fields to json-friendly output. Return a dict with the following schema: { id: "uuid", score: int, paddle: { wall: int, pos: float, }, } ''' return dict( id=str(self.id), score=self.score, paddle=dict( wall=as_int(self.paddle['wall']), pos=asFloat(self.paddle['pos']), ), ) __database__ = walrus_conn id = walrus.UUIDField(primary_key=True, index=True) room = walrus.UUIDField() username = walrus.TextField() score = walrus.IntegerField() paddle = walrus.HashField()
class Room(walrus.Model): __database__ = walrus.Database.from_url(app.config.get("REDIS_URL")) id = walrus.TextField(default="room_{}".format(generate_uid()), index=True) players = walrus.SetField() spectators = walrus.SetField()