Beispiel #1
0
def company():

    gid = request.args.get('gid', None)
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=gid).first()

    if request.method == 'GET':
        company_schema = CompanySchema()
        serialized_company = company_schema.dump(company).data
        return jsonify({'player_company': serialized_company})
    else:
        company.state = request.args.get('pcstate')
        db.session.commit()

        if company.state == "turn":
            companies_ready = Company.query.filter_by(id_game=gid,
                                                      state="turn").count()

            if companies_ready == 1:
                return redirect(url_for('game.runturn', gid=gid))
            else:
                return "Waiting For Players"

        else:
            return ("Success - state = " + company.state)
Beispiel #2
0
def player_company():

    gid = request.args.get('gid', None)

    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=gid).first()
    company_schema = CompanySchema()
    serialized_company = company_schema.dump(company).data

    return jsonify({'company': serialized_company})
Beispiel #3
0
def companies():

    gid = request.args.get('gid', None)
    companies = Company.query.filter_by(id_game=gid).all()
    companies_schema = CompanySchema()
    serialized_companies = company_schema.dump(companies).data
    return jsonify({'companies': serialized_companies})
Beispiel #4
0
def on_get_company(data):
    data = json.loads(data)
    game = Game.query.filter_by(id=data['gameId']).first()
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()
    company_serialized = CompanySchema().dump(company).data

    return {'company': company_serialized}
Beispiel #5
0
def on_join_gameroom(data):
    data = json.loads(data)
    company = Company.query.filter_by(id_user=current_user.id,
                                      id_game=data['gameId']).first()
    company_serialized = CompanySchema().dump(company).data
    join_room("game" + str(data['gameId']))

    if not company.joined_game:
        company.joined_game = True
        db.session.commit()
        shout_players_message(data['gameId'],
                              "Welcome to Energize! A power grid simulator.",
                              [company.player_number])
        shout_company_joined_game(data['gameId'], company_serialized)

    return
Beispiel #6
0
from app import db
from flask_restplus import Namespace, Resource, fields
from app.models import Company, CompanySchema, User, Post, Service, ServiceSchema, PostSchema
from flask import jsonify, request
from .dto import companyDTO, postDTO, serviceDTO

api = Namespace('companies', description='Companies api')

#currentUser = User.query.get_or_404(1)

#currentUser = User(id=1, name='Test', email='test')
#db.session.add(currentUser)
#db.session.commit()

company_schema = CompanySchema()
company_schema_list = CompanySchema(many=True)

service_schema = ServiceSchema()
service_schema_list = ServiceSchema(many=True)

post_schema = PostSchema()
post_schema_list = PostSchema(many=True)


@api.route('/')
class CompaniesListResource(Resource):
    def get(self):
        comps = Company.query.all()
        return company_schema_list.jsonify(comps)

    @api.marshal_with(companyDTO, code=201)
Beispiel #7
0
def player_facility():

    gid = request.args.get('gid', None)
    game = Game.query.filter_by(id=gid).first()
    fid = request.args.get('fid', None)
    owned_facility = False

    facility = Facility.query.filter_by(id=fid).first()
    facility_schema = FacilitySchema()
    facility_serialized = facility_schema.dump(facility).data

    company = facility.company
    company_schema = CompanySchema()
    company_serialized = company_schema.dump(company).data

    if company.user.id == current_user.id:
        owned_facility = True

    facility_type = facility.facility_type
    facility_type_schema = FacilityTypeSchema()
    facility_type_serialized = facility_type_schema.dump(facility_type).data

    generators = Generator.query.filter_by(id_facility=facility.id,
                                           id_game=gid).all()

    generators_schema = GeneratorSchema(many=True)
    generators_serialized = generators_schema.dump(generators).data

    # Add generator age, condition, and profit to each generator.
    for gen in generators_serialized:
        gen['generator_age'] = str(get_age(game, gen['start_prod_date'], True))

    # logging.debug(f"generator_serialized info = {generators_serialized[0]}")

    generator_modifications = [{
        'id': gen.id,
        'mods': gen.modifications
    } for gen in generators]
    generator_modifications_schema = GeneratorModificationSchema(many=True)
    generator_modifications_serialized = generator_modifications_schema.dump(
        generator_modifications).data

    generator_types = GeneratorType.query.filter_by(
        id_facility_type=facility_type.id).all()
    generator_types_schema = GeneratorTypeSchema(many=True)
    generator_types_serialized = generator_types_schema.dump(
        generator_types).data

    power_types = [gen_type.power_type for gen_type in generator_types]
    power_type_schema = PowerTypeSchema(many=True)
    power_type_serialized = power_type_schema.dump(power_types).data

    resource_types = [gen_type.resource_type for gen_type in generator_types]
    resource_type_schema = ResourceTypeSchema(many=True)
    resource_type_serialized = resource_type_schema.dump(resource_types).data

    generator_modification_types = GeneratorModificationType.query.filter_by(
        id_facility_type=facility.id_type).all()
    generator_modification_types_schema = GeneratorModificationTypeSchema(
        many=True)
    generator_modification_types_serialized = generator_modification_types_schema.dump(
        generator_modification_types).data

    return jsonify({
        'owned_facility':
        owned_facility,
        'company':
        company_serialized,
        'facility':
        facility_serialized,
        'facility_type':
        facility_type_serialized,
        'generators':
        generators_serialized,
        'modifications':
        generator_modifications_serialized,
        'generator_types':
        generator_types_serialized,
        'power_types':
        power_type_serialized,
        'resource_types':
        resource_type_serialized,
        'modification_types':
        generator_modification_types_serialized
    })