From 12cd3777dc17484d50a245da070ee30f0245a75d Mon Sep 17 00:00:00 2001 From: InusualZ Date: Sat, 18 Mar 2023 19:16:35 -0400 Subject: [PATCH] Calculate the amount of gate and city based on the server's capacity --- mh/database.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/mh/database.py b/mh/database.py index e93b698..e3b366b 100644 --- a/mh/database.py +++ b/mh/database.py @@ -19,6 +19,7 @@ along with this program. If not, see . """ +import math import random import time from threading import RLock @@ -224,11 +225,11 @@ def reset(self): class City(Lockable): LAYER_DEPTH = 3 - def __init__(self, name, parent): + def __init__(self, name, parent, is_jap=False): self.name = name self.parent = parent self.state = LayerState.EMPTY - self.players = Players(4) + self.players = Players(City.get_max_capacity(is_jap)) self.optional_fields = [] self.leader = None self.reserved = None @@ -247,6 +248,10 @@ def in_quest_players(self): def get_capacity(self): return self.players.get_capacity() + @staticmethod + def get_max_capacity(is_jap): + return 8 if is_jap else 4 + def get_state(self): size = self.get_population() if size == 0: @@ -293,16 +298,23 @@ def reserve(self, reserve): class Gate(object): LAYER_DEPTH = 2 + MAX_CITY_PER_GATE = 40 - def __init__(self, name, parent, city_count=40, player_capacity=100): + def __init__(self, name, parent, capacity=100, is_jap=False): self.name = name self.parent = parent self.state = LayerState.EMPTY + + city_max_capacity = City.get_max_capacity(is_jap) + city_count = int(min( + math.ceil(capacity / city_max_capacity), Gate.MAX_CITY_PER_GATE)) + capacity = city_count * city_max_capacity + self.cities = [ - City("City{}".format(i), self) + City("City{}".format(i), self, is_jap) for i in range(1, city_count+1) ] - self.players = Players(player_capacity) + self.players = Players(capacity) self.optional_fields = [] def get_population(self): @@ -326,16 +338,27 @@ def get_state(self): class Server(object): LAYER_DEPTH = 1 + MAX_GATE_PER_SERVER = 40 - def __init__(self, name, server_type, gate_count=40, capacity=2000, + def __init__(self, name, server_type, capacity=6400, is_jap=False, addr=None, port=None): self.name = name self.parent = None self.server_type = server_type self.addr = addr self.port = port + + city_max_capacity = City.get_max_capacity(is_jap) + city_count = int(math.ceil(capacity / city_max_capacity)) + gate_count = int(min( + math.ceil(float(city_count) / Server.MAX_GATE_PER_SERVER), + Server.MAX_GATE_PER_SERVER)) + max_gate_player_capacity = Gate.MAX_CITY_PER_GATE * city_max_capacity + capacity = city_count * city_max_capacity + self.gates = [ - Gate("City Gate{}".format(i), self) + Gate("City Gate{}".format(i), self, + min(max_gate_player_capacity, capacity), is_jap) for i in range(1, gate_count+1) ] self.players = Players(capacity)