Skip to content

Commit

Permalink
generator: initialize also backup teams and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed Oct 15, 2024
1 parent cc6457f commit 4b23017
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 53 deletions.
112 changes: 69 additions & 43 deletions game/src/generators/generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,43 @@ var names: Dictionary = {}
var date: Dictionary
var max_timestamp: int
var min_timestamp: int
var world: World


func generate_world() -> World:
_generate_world_from_csv()
var world: World = _generate_world_from_csv()

# generate players
_load_person_names()
for c: Continent in world.continents:
for n: Nation in c.nations:
for l: League in n.leagues:
for t: Team in l.teams:
_initialize_team(n, l, t)
_load_person_names(world)
for continent: Continent in world.continents:
for nation: Nation in continent.nations:
# backup teams
for team: Team in nation.backup_teams:
_initialize_team(world, nation, nation.leagues.size() + 1, team)
# league teams
for league: League in nation.leagues:
for team: Team in league.teams:
_initialize_team(world, nation, league.pyramid_level, team)

# first generate clubs history with promotions, delegations, cup wins
_generate_club_history()
_generate_club_history(world)
# then generate player histroy with trasnfers and statistics
_generate_player_history()
_generate_player_history(world)

return world


func _initialize_team(nation: Nation, league: League, team: Team) -> void:
func _initialize_team(
world: World,
nation: Nation,
league_pyramid_level: int,
team: Team,
) -> void:
# create date ranges
# starts from current year and subtracts min/max years
# youngest player can be 15 and oldest 45
date = Global.save_states.temp_state.start_date

var temp_team_prestige: int = _get_team_prestige(league.pyramid_level)
var temp_team_prestige: int = _get_team_prestige(league_pyramid_level)

var max_date: Dictionary = date.duplicate()
max_date.month = 1
Expand All @@ -64,9 +72,9 @@ func _initialize_team(nation: Nation, league: League, team: Team) -> void:

team.create_stadium(team.name + " Stadium", 1234, 1990)

_assign_players_to_team(team, league, nation, temp_team_prestige)

team.staff = _create_staff(team.get_prestige(), nation, league.pyramid_level)
_assign_players_to_team(world, team, league_pyramid_level, nation, temp_team_prestige)
team.staff = _create_staff(world, team.get_prestige(), nation, league_pyramid_level)

# assign manager preffered formation to team
team.formation = team.staff.manager.formation
Expand All @@ -77,7 +85,13 @@ func _initialize_team(nation: Nation, league: League, team: Team) -> void:
team.salary_budget = _get_salary_budget(team.players, team.staff, temp_team_prestige)


func _assign_players_to_team(p_team: Team, p_league: League, p_nation: Nation, prestige: int) -> Team:
func _assign_players_to_team(
world: World,
p_team: Team,
league_pyramid_level: int,
p_nation: Nation,
prestige: int
) -> Team:
var nr: int = 1

for position_type: int in Position.Type.values():
Expand All @@ -87,14 +101,19 @@ func _assign_players_to_team(p_team: Team, p_league: League, p_nation: Nation, p

for i in amount:
var random_nation: Nation = _get_random_nationality(
p_nation, prestige, p_league.pyramid_level
world, p_nation, prestige, league_pyramid_level
)
var player: Player = _create_player(
world,
random_nation,
nr,
prestige,
position_type
)
var player: Player = _create_player(random_nation, nr, prestige, position_type)
nr += 1
player.team = p_team.name
player.statistics.team_name = p_team.name
player.team_id = p_team.id
player.league = p_league.name
p_team.players.append(player)

# RngUtil.rng.random lineup assignment
Expand Down Expand Up @@ -312,7 +331,7 @@ func _get_person_name(nation: Nation) -> String:
return mixed_names[RngUtil.rng.randi() % (size_female + size_male)]


func _get_person_surname(nation: Nation) -> String:
func _get_person_surname(world: World, nation: Nation) -> String:
# TODO bigger proability for neighbour nations (needs data)

# 10% change of having random nation's surname
Expand All @@ -326,21 +345,21 @@ func _get_person_surname(nation: Nation) -> String:
return names[nation_string]["last_names"][RngUtil.rng.randi() % size]


func _create_staff(team_prestige: int, team_nation: Nation, pyramid_level: int) -> Staff:
func _create_staff(world: World, team_prestige: int, team_nation: Nation, pyramid_level: int) -> Staff:
var staff: Staff = Staff.new()
staff.manager = _create_manager(team_prestige, team_nation, pyramid_level)
staff.president = _create_president(team_prestige, team_nation, pyramid_level)
staff.scout = _create_scout(team_prestige, team_nation, pyramid_level)
staff.manager = _create_manager(world, team_prestige, team_nation, pyramid_level)
staff.president = _create_president(world, team_prestige, team_nation, pyramid_level)
staff.scout = _create_scout(world, team_prestige, team_nation, pyramid_level)
return staff


func _create_manager(team_prestige: int, team_nation: Nation, pyramid_level: int) -> Manager:
func _create_manager(world: World, team_prestige: int, team_nation: Nation, pyramid_level: int) -> Manager:
var manager: Manager = Manager.new()
manager.prestige = _in_bounds_random(team_prestige)
var nation: Nation = _get_random_nationality(team_nation, team_prestige, pyramid_level)
var nation: Nation = _get_random_nationality(world, team_nation, team_prestige, pyramid_level)
manager.nation = nation.name
manager.name = _get_person_name(nation)
manager.surname = _get_person_surname(nation)
manager.surname = _get_person_surname(world, nation)

manager.contract = _get_contract(manager)

Expand All @@ -354,30 +373,34 @@ func _create_manager(team_prestige: int, team_nation: Nation, pyramid_level: int
return manager


func _create_president(team_prestige: int, team_nation: Nation, pyramid_level: int) -> President:
func _create_president(world: World, team_prestige: int, team_nation: Nation, pyramid_level: int) -> President:
var president: President = President.new()
president.prestige = _in_bounds_random(team_prestige)
var nation: Nation = _get_random_nationality(team_nation, team_prestige, pyramid_level)
var nation: Nation = _get_random_nationality(world, team_nation, team_prestige, pyramid_level)
president.nation = nation.name
president.name = _get_person_name(nation)
president.surname = _get_person_surname(nation)
president.surname = _get_person_surname(world, nation)
president.contract = _get_contract(president)
return president


func _create_scout(team_prestige: int, team_nation: Nation, pyramid_level: int) -> Scout:
func _create_scout(world: World, team_prestige: int, team_nation: Nation, pyramid_level: int) -> Scout:
var scout: Scout = Scout.new()
scout.prestige = _in_bounds_random(team_prestige)
var nation: Nation = _get_random_nationality(team_nation, team_prestige, pyramid_level)
var nation: Nation = _get_random_nationality(world, team_nation, team_prestige, pyramid_level)
scout.nation = nation.name
scout.name = _get_person_name(nation)
scout.surname = _get_person_surname(nation)
scout.surname = _get_person_surname(world, nation)
scout.contract = _get_contract(scout)
return scout


func _create_player(
nation: Nation, nr: int, p_prestige: int, p_position_type: Position.Type
world: World,
nation: Nation,
nr: int,
p_prestige: int,
p_position_type: Position.Type,
) -> Player:
var player: Player = Player.new()
_random_positions(player, p_position_type)
Expand All @@ -391,7 +414,7 @@ func _create_player(

player.price = _get_price(date.year - birth_date.year, prestige, player.position)
player.name = _get_person_name(nation)
player.surname = _get_person_surname(nation)
player.surname = _get_person_surname(world, nation)
player.birth_date = birth_date
player.nation = nation.name
player.foot = _get_random_foot()
Expand Down Expand Up @@ -458,7 +481,7 @@ func _get_team_prestige(pyramid_level: int) -> int:


func _get_random_nationality(
nation: Nation, prestige: int, pyramid_level: int
world: World, nation: Nation, prestige: int, pyramid_level: int
) -> Nation:
# (100 - prestige)% given nation, prestige% random nation
# with prestige, lower division teams have less players from other nations
Expand All @@ -485,7 +508,7 @@ func _in_bounds(value: int, max_bound: int = Const.MAX_PRESTIGE) -> int:
return maxi(mini(value, max_bound), 1)


func _load_person_names() -> void:
func _load_person_names(world: World) -> void:
for nation: Nation in world.get_all_nations():
var names_file: FileAccess = FileAccess.open(
NAMES_DIR + nation.name.to_lower() + ".json", FileAccess.READ
Expand Down Expand Up @@ -515,7 +538,7 @@ func _get_salary_budget(players: Array[Player], staff: Staff, prestige: int) ->
return salary_budget


func _generate_club_history() -> void:
func _generate_club_history(world: World) -> void:
# TODO world cup history (once national teams exist)
# TODO continental national teams cup

Expand All @@ -535,15 +558,16 @@ func _generate_club_history() -> void:
var home_goals: int = RngUtil.rng.randi_range(0, matchz.home.get_prestige() / 2)
var away_goals: int = RngUtil.rng.randi_range(0, matchz.away.get_prestige() / 2)
matchz.set_result(home_goals, away_goals, world)

world.promote_and_delegate_teams()


func _generate_player_history() -> void:
func _generate_player_history(_world: World) -> void:
pass


func _generate_world_from_csv() -> void:
world = World.new()
func _generate_world_from_csv() -> World:
var world: World = World.new()
world.initialize()
var file: FileAccess = FileAccess.open(WORLD_CSV_PATH, FileAccess.READ)

Expand All @@ -562,10 +586,12 @@ func _generate_world_from_csv() -> void:
var nation: String = line[1]
var league: String = line[2]
var city: String = line[3]
_initialize_city(continent, nation,league, city)
_initialize_city(world, continent, nation,league, city)

return world


func _initialize_city(continent_name: String, nation_name: String, league_name: String, team_name: String) -> void:
func _initialize_city(world: World, continent_name: String, nation_name: String, league_name: String, team_name: String) -> void:
# setup continent, if not done yet
var continent: Continent
var continent_filter: Array[Continent] = world.continents.filter(func(c: Continent) -> bool: return c.name == continent_name)
Expand Down
3 changes: 0 additions & 3 deletions game/src/resources/person/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum Form { Injured, Recover, Good, Excellent }
@export var injury_factor: int
@export var team: String # team name for easier filtering etc...
@export var team_id: int # team name for easier filtering etc...
@export var league: String # league name for easier filtering etc...
@export var form: Form
@export var morality: Morality
@export var statistics: Statistics
Expand All @@ -36,7 +35,6 @@ func _init(
p_name: String = "",
p_team: String = "",
p_team_id: int = 0,
p_league: String = "",
p_surname: String = "",
p_nation: String = "",
p_birth_date: Dictionary = Time.get_datetime_dict_from_system(),
Expand All @@ -60,7 +58,6 @@ func _init(
name = p_name
team = p_team
team_id = p_team_id
league = p_league
surname = p_surname
nation = p_nation
birth_date = p_birth_date
Expand Down
10 changes: 5 additions & 5 deletions game/src/resources/world/world.gd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func initialize() -> void:


func random_results() -> void:
for c: Continent in Global.world.continents:
for c: Continent in continents:
for n: Nation in c.nations:
n.random_results()

Expand All @@ -48,7 +48,7 @@ func get_active_league() -> League:


func get_active_nation() -> Nation:
for continent: Continent in Global.world.continents:
for continent: Continent in continents:
for nation: Nation in continent.nations:
for league: League in nation.leagues:
if league.id == Global.league.id:
Expand All @@ -58,7 +58,7 @@ func get_active_nation() -> Nation:


func get_active_continent() -> Continent:
for continent: Continent in Global.world.continents:
for continent: Continent in continents:
for nation: Nation in continent.nations:
for league: League in nation.leagues:
if league.id == Global.league.id:
Expand Down Expand Up @@ -102,15 +102,15 @@ func get_all_nations() -> Array[Nation]:

func get_all_leagues() -> Array[League]:
var leagues: Array[League] = []
for c: Continent in Global.world.continents:
for c: Continent in continents:
for n: Nation in c.nations:
leagues.append_array(n.leagues)
return leagues


func get_all_club_cups() -> Array[Competition]:
var cups: Array[Competition] = []
for c: Continent in Global.world.continents:
for c: Continent in continents:
cups.append(c.cup_clubs)
for n: Nation in c.nations:
cups.append(n.cup)
Expand Down
17 changes: 15 additions & 2 deletions game/src/tests/test_generator/test_generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ func test() -> void:
var reference_world: World = generator.generate_world()
assert(reference_world.continents.size() > 0)

# test generations 10x time
print("test: rqeuired properties...")

for continent: Continent in reference_world.continents:
for nation: Nation in continent.nations:
for league: League in nation.leagues:
for team: Team in league.teams:
assert(team.lineup_player_ids.size() == Const.LINEUP_PLAYERS_AMOUNT)
assert(team.players.size() > 10)
assert(team.get_goalkeeper() != null)
print("test: rqeuired properties done.")

print("test: deterministic...")
# test deterministic generations x time
for i: int in range(3):
print("test: run " + str(i + 1))
print("test: deterministic run " + str(i + 1))

RngUtil.reset_seed("TestSeed", 0)

Expand All @@ -41,5 +53,6 @@ func test() -> void:
reference_world.continents[0].nations[0].leagues[0].teams[0].players[1].name)
assert(test_world.continents[0].nations[0].leagues[0].teams[0].players[2].name == \
reference_world.continents[0].nations[0].leagues[0].teams[0].players[2].name)
print("test: deterministic done.")

print("test: generator done.")

0 comments on commit 4b23017

Please sign in to comment.