Exemplo n.º 1
0
def application(environ, start_response):

    with open('index.html', encoding='utf-8') as f:
        html = f.read()

    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0

    request_body = environ['wsgi.input'].read(request_body_size).decode(encoding='utf-8')
    d = parse_qs(request_body)

    armies = 2 if type (d.get('armies', 0)) == int else int(d.get('armies', 0).pop(0))
    solders = 1 if type (d.get('solders', 0)) == int else int(d.get('solders', 0).pop(0))
    vehicles = 1 if type (d.get('vehicles', 0)) == int else int(d.get('vehicles', 0).pop(0))
    squads =  1 if type (d.get('squads', 0)) == int else int(d.get('squads', 0).pop(0))

    if environ['REQUEST_METHOD'] == 'POST':
        f = io.StringIO()
        battlefield = battle.BattleField(*[battle.Army('random', *[battle.Squad(solders, vehicles) for _ in range(squads)]) for _ in range(armies)])
        with redirect_stdout(f):
            battlefield.start()
        response_body = html.format(result=f.getvalue())
    else:
        response_body = html.format(result='Пусто')

    start_response('200 OK', [('Content-Type', 'text/html')])
    return [bytes(response_body, 'utf-8')]
Exemplo n.º 2
0
def test_last_hero():
    sq11 = battle.Squad(1, 1)
    sq12 = battle.Squad(1, 1)
    sq21 = battle.Squad(1, 1)
    sq22 = battle.Squad(1, 1)
    for _ in range(1000):
        with redirect_stdout(open(os.devnull, 'w')):
            sq12.take_damage(1)
            sq22.take_damage(1)
            sq22.take_damage(1)
    for _ in range(99):
        with redirect_stdout(open(os.devnull, 'w')):
            sq11.take_damage(1)

    army1 = battle.Army('random', sq11, sq12)
    army2 = battle.Army('random', sq21, sq22)

    assert last_hero([army1, army2])
Exemplo n.º 3
0
def index():
    armies = int(request.args.get('armies', 2))
    solders = int(request.args.get('solders', 1))
    vehicles = int(request.args.get('vehicles', 1))
    squads = int(request.args.get('squads', 1))
    f = io.StringIO()
    battlefield = battle.BattleField(*[
        battle.Army('random', *
                    [battle.Squad(solders, vehicles) for _ in range(squads)])
        for _ in range(armies)
    ])
    with redirect_stdout(f):
        battlefield.start()
        result = f.getvalue()
    resp = Response(result, status=200, mimetype='plain/text')
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp
Exemplo n.º 4
0
def index():
    if request.method == 'POST':
        armies = request.args.get('armies', 2)
        solders = request.args.get('solders', 1)
        vehicles = request.args.get('vehicles', 1)
        squads = request.args.get('squads', 1)
        f = io.StringIO()
        battlefield = battle.BattleField(*[
            battle.Army(
                'random', *
                [battle.Squad(solders, vehicles) for _ in range(squads)])
            for _ in range(armies)
        ])
        with redirect_stdout(f):
            battlefield.start()
        session['result'] = f.getvalue()
        return redirect(url_for('index'))
    return render_template('index.html', result=session.get('result', ''))
Exemplo n.º 5
0
def application(request):
    if request.method == 'POST':
        armies = request.args.get('armies', 2)
        solders = request.args.get('solders', 1)
        vehicles = request.args.get('vehicles', 1)
        squads = request.args.get('squads', 1)
        f = io.StringIO()
        battlefield = battle.BattleField(*[
            battle.Army(
                'random', *
                [battle.Squad(solders, vehicles) for _ in range(squads)])
            for _ in range(armies)
        ])
        with redirect_stdout(f):
            battlefield.start()
        global result
        result = f.getvalue()
        return redirect('/')
    return Response(template.render(result=result), mimetype='text/html')
Exemplo n.º 6
0
 def test_strategy(self, strategy):
     a = battle.Army(strategy, battle.Squad(1, 1), battle.Squad(1, 1))
     assert isinstance(a, battle.Army)
Exemplo n.º 7
0
 def test_take_damage_type(self, damage):
     sq = battle.Squad()
     sq.take_damage(damage)
     assert type(sq.get_health()) == int or type(sq.get_health()) == float
Exemplo n.º 8
0
 def test_health(self):
     sq = battle.Squad()
     for _ in range(900):
         with redirect_stdout(open(os.devnull, 'w')):
             sq.take_damage(random.random())
     assert 0 <= sq.get_health() <= 100