ungleich-game/game-sql.py
Nico Schottelius de768b2ae7 Add ungleichapi
2019-05-25 22:45:44 +02:00

111 lines
2.4 KiB
Python

#!/usr/bin/env python3
USERLENGTH = 50
import ipaddress
import random
import sys
from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse
# ORM support -- should be in database.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('postgresql://localhost:5432/ungleichgame?host=/var/run/postgresql/')
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
Base.metadata.create_all(bind=engine)
class Network(Base):
__tablename__ = 'network'
username = Column(String(USERLENGTH), primary_key=True)
network = Column(postgresql.CIDR)
def __init__(self, username, network):
self.username = username
self.network = network
class Address(Base):
__tablename__ = 'address'
username = Column(String(USERLENGTH), primary_key=True)
address = Column(postgresql.INET)
def __init__(self, username, address):
self.username = username
self.address = address
app = Flask(__name__)
api = Api(app)
class Level(Resource):
points = 0
def test(self):
pass
class Ping6(Level):
points = 10
def test(self):
"""
ping6 -c3
"""
class Game(Resource):
def get(self):
return {'hello': 'list of levels'}
api.add_resource(Game, '/game')
@app.route("/")
def high_score():
return "High score!"
if __name__ == '__main__':
init_db()
#net = ipaddress.IPv6Network("2a0a:e5c1:101::/64")
# net_base = ipaddress.IPv6Network("2a0a:e5c1::/64")
# net_offset = random.randrange(2**80, 2**95)
net_base = "2a0a:e5c1:{}::/64"
net_offset = random.randrange(0, 9999)
net = ipaddress.IPv6Network(net_base.format(net_offset))
print(net)
n = Network('nico{}'.format(net_offset), str(net))
print(n)
addr_offset = random.randrange(0, 2**64)
addr = net[0] + addr_offset
a = Address('nico{}'.format(net_offset), str(addr))
print(a)
db_session.add(n)
db_session.add(a)
db_session.commit()
sys.exit(0)
app.run(port='5002')