Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions mh/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import math
import random
import time
from threading import RLock
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Comment on lines +308 to +311
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this approach fail when many users decide to solo play (and not fill up the city)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but even in that case there is not much that we can do, because there is a maximum amount of city per gate.


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):
Expand All @@ -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)
Expand Down