mirror of
https://github.com/Llloooggg/TextSouls.git
synced 2026-03-06 12:36:23 +03:00
backend: прототип дуэли исправлен
This commit is contained in:
@@ -11,7 +11,7 @@ from textsouls.models import CharacterClass
|
|||||||
from textsouls.models import CharacterState
|
from textsouls.models import CharacterState
|
||||||
from textsouls.models import Character
|
from textsouls.models import Character
|
||||||
|
|
||||||
from textsouls.models import DuelParticipants
|
from textsouls.models import DuelParticipant
|
||||||
from textsouls.models import Duel
|
from textsouls.models import Duel
|
||||||
|
|
||||||
ts_admin = Blueprint("ts_admin", __name__)
|
ts_admin = Blueprint("ts_admin", __name__)
|
||||||
@@ -37,5 +37,5 @@ admin.add_view(CommonView(CharacterClass, db.session))
|
|||||||
admin.add_view(CommonView(CharacterState, db.session))
|
admin.add_view(CommonView(CharacterState, db.session))
|
||||||
admin.add_view(CommonView(Character, db.session))
|
admin.add_view(CommonView(Character, db.session))
|
||||||
|
|
||||||
admin.add_view(CommonView(DuelParticipants, db.session))
|
admin.add_view(CommonView(DuelParticipant, db.session))
|
||||||
admin.add_view(CommonView(Duel, db.session))
|
admin.add_view(CommonView(Duel, db.session))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from textsouls.models import CharacterClass
|
|||||||
from textsouls.models import CharacterState
|
from textsouls.models import CharacterState
|
||||||
from textsouls.models import Character
|
from textsouls.models import Character
|
||||||
|
|
||||||
from textsouls.models import DuelParticipants
|
from textsouls.models import DuelParticipant
|
||||||
from textsouls.models import Duel
|
from textsouls.models import Duel
|
||||||
|
|
||||||
main = Blueprint("main", __name__)
|
main = Blueprint("main", __name__)
|
||||||
@@ -21,5 +21,5 @@ register_api(main, CharacterState, "character_states")
|
|||||||
register_api(main, Character, "characters", "owner")
|
register_api(main, Character, "characters", "owner")
|
||||||
|
|
||||||
|
|
||||||
register_api(main, DuelParticipants, "duels_participants")
|
register_api(main, DuelParticipant, "duels_participant")
|
||||||
register_api(main, Duel, "duels")
|
register_api(main, Duel, "duels")
|
||||||
|
|||||||
@@ -117,6 +117,11 @@ class Character(db.Model, SerializerMixin):
|
|||||||
nullable=False,
|
nullable=False,
|
||||||
default=1,
|
default=1,
|
||||||
)
|
)
|
||||||
|
duels_participation = db.relationship(
|
||||||
|
"DuelParticipant",
|
||||||
|
backref=("charachter"),
|
||||||
|
lazy="dynamic",
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
@@ -143,8 +148,8 @@ class Character(db.Model, SerializerMixin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def battle_stats(self):
|
def battle_stats(self):
|
||||||
char_race = CharacterRace.query.get(self.character_race)
|
char_race = CharacterRace.query.get(self.character_race_id)
|
||||||
char_class = CharacterClass.query.get(self.character_class)
|
char_class = CharacterClass.query.get(self.character_class_id)
|
||||||
|
|
||||||
attack_power = (
|
attack_power = (
|
||||||
self.strength_base
|
self.strength_base
|
||||||
@@ -171,7 +176,7 @@ class Character(db.Model, SerializerMixin):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class DuelParticipants(db.Model, SerializerMixin):
|
class DuelParticipant(db.Model, SerializerMixin):
|
||||||
|
|
||||||
__tablename__ = "duels_participants"
|
__tablename__ = "duels_participants"
|
||||||
|
|
||||||
@@ -187,7 +192,7 @@ class DuelParticipants(db.Model, SerializerMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.duel_id, self.participant_id
|
return f"{self.duel_id}: {self.participant_id}"
|
||||||
|
|
||||||
|
|
||||||
class Duel(db.Model, SerializerMixin):
|
class Duel(db.Model, SerializerMixin):
|
||||||
@@ -199,37 +204,52 @@ class Duel(db.Model, SerializerMixin):
|
|||||||
db.DateTime, nullable=False, default=datetime.datetime.now()
|
db.DateTime, nullable=False, default=datetime.datetime.now()
|
||||||
)
|
)
|
||||||
participants = db.relationship(
|
participants = db.relationship(
|
||||||
"DuelParticipants",
|
"DuelParticipant",
|
||||||
backref=backref("duel", order_by="DuelParticipants.turn_order.asc()"),
|
backref=backref("duel", order_by="DuelParticipant.turn_order.asc()"),
|
||||||
lazy="dynamic",
|
lazy="dynamic",
|
||||||
)
|
)
|
||||||
|
|
||||||
def attack(attacked_character, defensive_character):
|
def attack(self, attacked_character, defensive_character):
|
||||||
defensive_character_endurance = (
|
defensive_character_endurance = (
|
||||||
attacked_character.attack_power
|
defensive_character.endurance_base
|
||||||
- defensive_character.endurance_base
|
- attacked_character.battle_stats["attack_power"]
|
||||||
)
|
)
|
||||||
|
|
||||||
return defensive_character_endurance
|
return defensive_character_endurance
|
||||||
|
|
||||||
def defence(defensive_character):
|
def defence(self, defensive_character):
|
||||||
procced = False
|
procced = False
|
||||||
if randint(1 * defensive_character.defence_chance, 50) == 50:
|
if (
|
||||||
|
randint(
|
||||||
|
int(10 * defensive_character.battle_stats["defence_chance"]),
|
||||||
|
50,
|
||||||
|
)
|
||||||
|
== 50
|
||||||
|
):
|
||||||
procced is True
|
procced is True
|
||||||
|
|
||||||
return procced
|
return procced
|
||||||
|
|
||||||
def dodge(defensive_character):
|
def dodge(self, defensive_character):
|
||||||
procced = False
|
procced = False
|
||||||
if randint(1 * defensive_character.dodge_chance, 50) == 50:
|
if (
|
||||||
|
randint(
|
||||||
|
int(10 * defensive_character.battle_stats["dodge_chance"]), 50
|
||||||
|
)
|
||||||
|
== 50
|
||||||
|
):
|
||||||
procced is True
|
procced is True
|
||||||
|
|
||||||
return procced
|
return procced
|
||||||
|
|
||||||
def duel_one_to_one(self):
|
def duel_one_to_one(self):
|
||||||
|
|
||||||
character_first = self.participants[0]
|
character_first = Character.query.get(
|
||||||
character_second = self.participants[1]
|
self.participants[0].participant_id
|
||||||
|
)
|
||||||
|
character_second = Character.query.get(
|
||||||
|
self.participants[1].participant_id
|
||||||
|
)
|
||||||
|
|
||||||
while (
|
while (
|
||||||
character_first.endurance_base > 0
|
character_first.endurance_base > 0
|
||||||
@@ -238,14 +258,18 @@ class Duel(db.Model, SerializerMixin):
|
|||||||
if not self.defence(character_second) or not self.dodge(
|
if not self.defence(character_second) or not self.dodge(
|
||||||
character_second
|
character_second
|
||||||
):
|
):
|
||||||
self.attack(character_first, character_second)
|
character_second.endurance_base = self.attack(
|
||||||
|
character_first, character_second
|
||||||
|
)
|
||||||
if not self.defence(character_first) or not self.dodge(
|
if not self.defence(character_first) or not self.dodge(
|
||||||
character_second
|
character_first
|
||||||
):
|
):
|
||||||
self.attack(character_second, character_first)
|
character_first.endurance_base = self.attack(
|
||||||
|
character_second, character_first
|
||||||
|
)
|
||||||
if character_first.endurance_base > 0:
|
if character_first.endurance_base > 0:
|
||||||
return character_first.name
|
return character_first.name
|
||||||
elif character_second.character_second > 0:
|
elif character_second.endurance_base > 0:
|
||||||
return character_second.name
|
return character_second.name
|
||||||
else:
|
else:
|
||||||
return "Ничья"
|
return "Ничья"
|
||||||
|
|||||||
Reference in New Issue
Block a user