<repo_name>aleX3645/PythonServerOfGroupProject<file_sep>/BotMapper.py
class BotMapper:
    __position_x = 0
    __position_y = 0
    __charge = 0.0
    __weight = 0.0
    __speed = 0.0
    __tiredness = 0.0
    __rang = 0.0
    __damage = 0.0
    __game_field = [[]]

    def __init__(self, charge: float, weight: float, speed: float, tiredness: float, damage: float, rang: float,
                 position_x: int, position_y: int, game_field):
        self.__game_field = game_field
        self.__charge = charge
        self.__weight = weight
        self.__tiredness = tiredness
        self.__speed = speed
        self.__rang = rang
        self.__damage = damage
        self.__position_y = position_y
        self.__position_x = position_x

    def get_tiredness(self):
        return self.__tiredness

    def set_tiredness(self, tiredness):
        self.__tiredness = tiredness

    def get_charge(self):
        return self.__charge

    def get_speed(self):
        return self.__speed

    def get_weight(self):
        return self.__weight

    def get_range(self):
        return self.__rang

    def get_damage(self):
        return self.__damage

    def get_position_x(self):
        return self.__position_x

    def get_position_y(self):
        return self.__position_y

    def set_position_xy(self, position_x: int, position_y: int):
        self.__position_x = position_x
        self.__position_y = position_y


<file_sep>/app.py
from flask import Flask, request, jsonify
from flask_restful import Api, Resource

from BotMapper import BotMapper
from GameServer import GameServer

app = Flask(__name__)
api = Api(app)

array = []


class Fight(Resource):
    def post(self):
        return self.start_game_by_json(request.get_json())

    def start_game_by_json(self, req):
        field = self.get_int_field((req['arena'])['field'])
        temp_pos = self.get_position_for(field, 1)

        temp = req['robot1']
        bot_stat1 = BotMapper(temp['charge'], temp['weight'], temp['speed'],
                              temp['tiredness'], temp['damage'], temp['range'],
                              temp_pos[0], temp_pos[1], field)
        bot1_string = (temp['botAlgorithm'])['algorithm']

        temp_pos = self.get_position_for(field, 2)
        temp = req['robot2']
        bot_stat2 = BotMapper(temp['charge'], temp['weight'], temp['speed'],
                              temp['tiredness'], temp['damage'],
                              temp['range'], temp_pos[0], temp_pos[1], field)
        bot2_string = (temp['botAlgorithm'])['algorithm']

        game = GameServer(bot1_string, bot2_string, bot_stat1, bot_stat2, field)
        res = game.start()

        return jsonify(
            {"winner": res[0], "fight_map": res[1]}
        )


    def get_position_for(self, field, t: int):
        for x in range(len(field)):
            for y in range(len(field[x])):
                if field[x][y] == t:
                    return [x, y]

    def get_int_field(self, field):
        temp_field = field.split("\n")
        f_res = []
        for x in range(len(temp_field)):
            list = []
            for y in temp_field[x]:
                if y == '0':
                    list.append(0)
                elif y == '1':
                    list.append(1)
                elif y == '2':
                    list.append(2)
            f_res.append(list)

        print(f_res)
        return f_res


api.add_resource(Fight, '/fight/')
<file_sep>/GameServer.py
import string
from random import gauss

from Bot import Bot
from BotMapper import BotMapper


class GameServer:
    r = 0

    __fight_map = ""

    bot1: Bot
    bot2: Bot
    bot_stats1: BotMapper
    bot_stats2: BotMapper
    game_field: [[]]

    def __init__(self, bot1: str, bot2: str, bot_stats1: BotMapper, bot_stats2: BotMapper, game_field: []):
        self.game_field = game_field

        bot1 += "\n" + "self.bot1 = Bot(game_field)"
        bot2 += "\n" + "self.bot2 = Bot(game_field)"
        exec(bot1)
        exec(bot2)

        self.bot_stats1 = bot_stats1
        self.bot_stats2 = bot_stats2

    # 1-bot1 winner, 2-bot2 winner, 0-draw
    def start(self):
        while self.r < 60:
            step = self.bot1.consider_step(self.game_field)

            if step[0] == "attack":
                res = self.attack(self.bot_stats1, self.bot_stats2, 1, step[1], step[2])
                self.__fight_map += "1," + "attack," + str(step[1]) +"," + str(step[2]) + ";"

            elif step[0] == "move":
                res = self.attack(self.bot_stats1, self.bot_stats2, 1, step[1], step[2])
                self.__fight_map += "1," + "move," + str(step[1]) + "," + str(step[2]) + ";"
            else:
                res = 0

            if res == 2:
                self.__fight_map += "1," + "kill," + str(step[1]) + "," + str(step[2]) + ";"
                return [1, self.__fight_map]

            step = self.bot2.consider_step(self.game_field)

            if step[0] == "attack":
                res = self.attack(self.bot_stats2, self.bot_stats1, 2, step[1], step[2])
                self.__fight_map += "2," + "attack," + str(step[1]) + "," + str(step[2]) + ";"

            elif step[0] == "move":
                res = self.attack(self.bot_stats2, self.bot_stats1, 2, step[1], step[2])
                self.__fight_map += "2," + "move," + str(step[1]) + "," + str(step[2]) + ";"
            else:
                res = 0

            if res == 2:
                self.__fight_map += "1," + "kill," + str(step[1]) + "," + str(step[2]) + ";"
                return [2, self.__fight_map]

            self.r += 1
        return 0

    # -1 - error, 1- hit, 2 - kill, 0 - miss
    def attack(self, bot: BotMapper, attacked_bot: BotMapper, bot_n: int, x: int, y: int):
        range_of_attack = abs(bot.get_position_x()) - abs(x) + abs(bot.get_position_y()) - abs(y)
        print(str(bot_n) + " " + str(x) + " " + str(y) + str(self.game_field[0][2]) + ";")
        if bot.get_range() < range_of_attack | x < 0 | y < 0:
            return -1
        else:
            if (bot_n == 1 and self.game_field[x][y] == 2) | (bot_n == 2 and self.game_field[x][y] == 1):
                dmg = gauss(bot.get_damage(), 25)
                hp = attacked_bot.get_tiredness() - dmg
                if hp <= 0:
                    return 2
                attacked_bot.set_tiredness(hp)
                return 1
            else:
                return 0

    # -1 - error, 1 - move
    def move(self, bot: BotMapper, x: int, y: int):
        range_of_move = abs(bot.get_position_x()) - abs(x) + abs(bot.get_position_y()) - abs(y)
        if self.game_field[x][y] == 0 and range_of_move < bot.get_speed():
            bot.set_position_xy(x, y)
            return 1
        else:
            return -1
<file_sep>/Game.py
from random import gauss

from Bot import Bot


class Game:
    r = 0

    bot1: Bot
    bot2: Bot
    game_field: [[]]

    def __init__(self, bot1: Bot, bot2: Bot):
        self.bot1 = bot1
        self.bot2 = bot2

    # 1-bot1 winner, 2-bot2 winner, 0-draw
    def start(self):
        while self.r < 60:
            step = self.bot1.consider_step(self.game_field)

            if step[0] == "attack":
                res = self.attack(self.bot1, self.bot2, 1, step[1], step[2])
            elif step[0] == "move":
                res = self.move(self.bot1, step[1], step[2])
            else:
                res = 0

            if res == 2:
                return 1

            step = self.bot2.consider_step(self.game_field)

            if step[0] == "attack":
                res = self.attack(self.bot2, self.bot1, 2, step[1], step[2])
            elif step[0] == "move":
                res = self.move(self.bot2, step[1], step[2])
            else:
                res = 0

            if res == 2:
                return 2

            self.r += 1
        return 0

    # -1 - error, 1- hit, 2 - kill, 0 - miss
    def attack(self, bot: Bot, attacked_bot: Bot, bot_n: int, x: int, y: int):
        range_of_attack = abs(bot.get_position_x()) - abs(x) + abs(bot.get_position_y()) - abs(y)
        if bot.get_range() < range_of_attack | x < 0 | y < 0 | len(self.game_field) < x | len(self.game_field[x]) < y:
            return -1
        else:
            if (bot_n == 1 & self.game_field[x, y] == 2) | (bot_n == 2 & self.game_field[x, y] == 1):
                dmg = gauss(bot.get_damage(), 25)
                hp = attacked_bot.get_tiredness() - dmg
                if hp <= 0:
                    return 2
                attacked_bot.set_tiredness(hp)
                return 1
            else:
                return 0

    def move(self, bot: Bot, x: int, y: int):
        range_of_move = abs(bot.get_position_x()) - abs(x) + abs(bot.get_position_y()) - abs(y)
        if self.game_field[x, y] != 0 & range_of_move < bot.get_speed() | len(self.game_field) < x | len(self.game_field[x]) < y:
            return -1
        else:
            bot.set_position_xy(x, y)
            return 1
