|
@@ -1,5 +1,9 @@
|
|
|
+import uuid
|
|
|
+
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
+from lib import generate_coord
|
|
|
+
|
|
|
db = SQLAlchemy()
|
|
|
session = db.session
|
|
|
|
|
@@ -12,6 +16,40 @@ class Game(db.Model):
|
|
|
coord_set = db.relationship("CoordSet")
|
|
|
guess_sets = db.relationship("GuessSet", backref="game", lazy=True)
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def create(timer, creator):
|
|
|
+ game_id = str(uuid.uuid4())
|
|
|
+ while Game.query.get(game_id) is not None:
|
|
|
+ # basically impossible collision, but let's be safe
|
|
|
+ game_id = str(uuid.uuid4())
|
|
|
+
|
|
|
+ cs = CoordSet()
|
|
|
+ for round_num in "12345":
|
|
|
+ coord = generate_coord()
|
|
|
+ cs.set_coord(round_num, *coord)
|
|
|
+ db.session.add(cs)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ new_game = Game(
|
|
|
+ game_id=game_id,
|
|
|
+ timer=timer,
|
|
|
+ creator=creator,
|
|
|
+ coord_set=cs
|
|
|
+ )
|
|
|
+ db.session.add(new_game)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ gs = GuessSet(
|
|
|
+ game_id=game_id,
|
|
|
+ player_name=creator,
|
|
|
+ coord_set=CoordSet()
|
|
|
+ )
|
|
|
+ db.session.add(gs)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ return new_game
|
|
|
+
|
|
|
+
|
|
|
def __str__(self):
|
|
|
return f"Game({self.game_id}, {self.timer})"
|
|
|
|
|
@@ -32,35 +70,6 @@ class Game(db.Model):
|
|
|
}
|
|
|
|
|
|
|
|
|
-def create_game():
|
|
|
- game_id = str(uuid.uuid4())
|
|
|
- while Game.query.get(game_id) is not None:
|
|
|
- # basically impossible collision, but let's be safe
|
|
|
- game_id = str(uuid.uuid4())
|
|
|
-
|
|
|
- cs = CoordSet()
|
|
|
- for round_num in "12345":
|
|
|
- coord = generate_coord()
|
|
|
- cs.set_coord(round_num, *coord)
|
|
|
- db.session.add(cs)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- new_game = Game(
|
|
|
- game_id=game_id,
|
|
|
- timer=timer,
|
|
|
- creator=name,
|
|
|
- coord_set=cs
|
|
|
- )
|
|
|
- db.session.add(new_game)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- gs = GuessSet(game_id=game_id, player_name=name, coord_set=CoordSet())
|
|
|
- db.session.add(gs)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- return new_game
|
|
|
-
|
|
|
-
|
|
|
class CoordSet(db.Model):
|
|
|
coord_id = db.Column(db.Integer, primary_key=True)
|
|
|
coord_1 = db.Column(db.String)
|