Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain fee #38

Merged
merged 5 commits into from Aug 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions running.py
Expand Up @@ -31,7 +31,7 @@ def start_game(q):
msg_queue=q) msg_queue=q)


def start_http(): def start_http():
from srcs.web_server import main from srcs import web_server
sys.stdout = Logger('http.log') sys.stdout = Logger('http.log')
#application.settings['debug'] = False #application.settings['debug'] = False
#application.listen(9999) #application.listen(9999)
Expand All @@ -40,7 +40,7 @@ def start_http():
#except KeyboardInterrupt: #except KeyboardInterrupt:
#print "bye!" #print "bye!"


main() web_server.main()


def start_ai(name, roomid): def start_ai(name, roomid):
ai = __import__('examples.%s' % name, globals(), locals(), ['main']) ai = __import__('examples.%s' % name, globals(), locals(), ['main'])
Expand Down
36 changes: 33 additions & 3 deletions srcs/game.py
Expand Up @@ -11,6 +11,8 @@
WAITFORPLAYER='waitforplayer' WAITFORPLAYER='waitforplayer'
RUNNING='running' RUNNING='running'
FINISHED='finished' FINISHED='finished'
MAINTAIN_LEVEL_1 = 1.5
MAINTAIN_LEVEL_2 = 2


DEFAULT_MAP = 'srcs/map/fight_here.yml' DEFAULT_MAP = 'srcs/map/fight_here.yml'
# DEFAULT_MAP = 'srcs/map/test.yml' # DEFAULT_MAP = 'srcs/map/test.yml'
Expand Down Expand Up @@ -51,6 +53,8 @@ def __init__(self,


self.set_map(m) self.set_map(m)


self.map_max_units = m.max_sum
self.maintain_fee = m.meta.get('maintain_fee', False)
self.start() self.start()


def log(self, msg): def log(self, msg):
Expand Down Expand Up @@ -105,9 +109,16 @@ def add_player(self, name="unknown", side='python'):
player_id = len(self.players)-1 player_id = len(self.players)-1
planet_id = self.map.starts[player_id] planet_id = self.map.starts[player_id]
self.holds[planet_id] = [player_id, self.map.meta['start_unit']] self.holds[planet_id] = [player_id, self.map.meta['start_unit']]
# 用户加入时调整维护费用
self.adjust_mt_fee()
# 返回玩家的顺序, 以及玩家的id(用来验证控制权限) # 返回玩家的顺序, 以及玩家的id(用来验证控制权限)
return dict(seq=len(self.players) - 1, id=player.id) return dict(seq=len(self.players) - 1, id=player.id)


def adjust_mt_fee(self):
"""动态调整维修费用"""
active_playes = len([i if i.alive for i in self.players])
self.mt_base_line = int(self.map_max_units / float(2) / active_players

def get_seq(self, id): def get_seq(self, id):
"""根据玩家的id获取seq""" """根据玩家的id获取seq"""
for i, s in enumerate(self.players): for i, s in enumerate(self.players):
Expand Down Expand Up @@ -264,12 +275,26 @@ def battle_stage(self, arrives):
for move in arrives: for move in arrives:
self.battle(move) self.battle(move)


def mt_level(self, _side, base_line=2000):
"""
根据 玩家 units & base_line 返回增长系数, 最高为 1
"""
_units = self.get_info()['players'][_side]['units']
if _units <= base_line:
return float(1)
elif _units <= base_line * MAINTAIN_LEVEL_1:
return float(0.5)
elif _units <= base_line * MAINTAIN_LEVEL_2:
return float(0.25)
else:
return float(0)

def next_round(self): def next_round(self):
# 生产回合 # 生产回合
for i, data in enumerate(self.holds): for i, data in enumerate(self.holds):
side, count = data side, count = data
if side == None: continue if side == None: continue
next = self.count_growth(count, self.planets[i]) next = self.count_growth(count, self.planets[i], self.mt_level(side, self.mt_base_line))
if next <= 0: if next <= 0:
side = None side = None
next = 0 next = 0
Expand Down Expand Up @@ -374,11 +399,14 @@ def battle(self, move):
winner=planet_side)) winner=planet_side))
self.holds[to] = [planet_side, planet_count] self.holds[to] = [planet_side, planet_count]


def count_growth(self, planet_count, planet): def count_growth(self, planet_count, planet, mt_proc = 1):
max = planet['max'] max = planet['max']
res = planet['res'] res = planet['res']
cos = planet['cos'] cos = planet['cos']
new_count = int(planet_count * res + cos) # 兵力增量乘以维护费用水平(增长系数)
new_armies = (planet_count * (res - 1) + cos)
if self.maintain_fee: new_armies *= mt_proc
new_count = int(planet_count + new_armies)
if planet_count < max: if planet_count < max:
planet_count = min(new_count, max) planet_count = min(new_count, max)
elif new_count < planet_count: elif new_count < planet_count:
Expand Down Expand Up @@ -413,6 +441,8 @@ def no_response_player_die(self, player, round):
# 判断是否没有响应时间过长 # 判断是否没有响应时间过长
if player.no_resp_time >= MAX_LOST_TURN: if player.no_resp_time >= MAX_LOST_TURN:
player.alive = False player.alive = False
# 用户丢失后调整维护费用
self.adjust_mt_fee()
logging.debug('kill no response player: %d' % \ logging.debug('kill no response player: %d' % \
self.players.index(player)) self.players.index(player))
self.log('kill player for no response %s: , round is %s, time is %s' % (player.name, round, player.no_resp_time)) self.log('kill player for no response %s: , round is %s, time is %s' % (player.name, round, player.no_resp_time))
Expand Down
5 changes: 5 additions & 0 deletions srcs/map/map.py
Expand Up @@ -72,6 +72,11 @@ def load(self, data):
for name in self.meta['starts']] for name in self.meta['starts']]
random.shuffle(self.starts) random.shuffle(self.starts)


self.max_sum = 0
# 计算 map 上所有星球的最大产兵量总和
for i in self.planets:
self.max_sum += int(i['max'])

def test(): def test():
""" """
>>> map = Map.loadfile("srcs/map/test.yml") >>> map = Map.loadfile("srcs/map/test.yml")
Expand Down