Permalink
Browse files

gogo

  • Loading branch information...
1 parent fd69c42 commit e564a96d6c386b9545d419e4d7f1ae407e44e204 @alexbft committed May 13, 2011
View
Binary file not shown.
View
@@ -85,14 +85,17 @@ def move(self):
fight = fighter.fight
if not fighter.should_move():
self._error("Should not move")
- card = fighter.card_in_hand_by_id(self.q["card"])
- if card == None:
- self._error("Wrong card")
- cast_target = fight.fighter_by_id(self.q["cast_target"])
- if cast_target == None or cast_target not in card.spell.targets(fighter):
- self._error("Wrong cast target")
atk_target = fight.fighter_by_id(self.q["atk_target"])
if atk_target == None or not atk_target.alive or atk_target not in fighter.enemies():
self._error("Wrong atk target")
- fighter.move = {"card": card.id, "cast_target": cast_target.id, "atk_target": atk_target.id}
+ if self.q["card"] != None:
+ card = fighter.card_in_hand_by_id(self.q["card"])
+ if card == None:
+ self._error("Wrong card")
+ cast_target = fight.fighter_by_id(self.q["cast_target"])
+ if cast_target == None or cast_target not in card.spell.targets(fighter):
+ self._error("Wrong cast target")
+ fighter.move = {"card": card.id, "cast_target": cast_target.id, "atk_target": atk_target.id}
+ else:
+ fighter.move = {"card": None, "atk_target": atk_target.id}
fighter.fight.check_end_turn()
View
@@ -6,23 +6,20 @@ class Card:
def __init__(self, src=None):
if src:
self.__dict__.update(src)
- try:
- self.spell = spell.get(self.spell)
- except:
- # DEBUG!
- self.spell = spell.get("Attack")
+ self.spell = spell.get(self.spell)
else:
self.id = unique_id()
def js_save(self):
res = dict(self.__dict__)
+ del res["spell"]
res["spell"] = self.spell.id
return res
@staticmethod
def random():
# STUB
- return Card.create(random.choice(["Attack", "Heal"]))
+ return Card.create(random.choice(["Attack", "Heal", "UpMagic"]))
@staticmethod
def create(spell_id):
View
@@ -9,10 +9,10 @@ def js(username, from_time = 0):
return list(db().chat.find({"time": {"$gt": from_time}}).sort("time", direction=DESCENDING).limit(20))
@staticmethod
- def add(username, message, channel = None):
+ def add(username, message, channel = None, params = None):
db().chat.ensure_index("time", ttl = INDEX_TTL)
- db().chat.insert({"name": username, "message": message, "time": now(), "channel": channel})
+ db().chat.insert({"name": username, "message": message, "time": now(), "channel": channel, "params": params})
@staticmethod
- def addsys(message, channel = None):
- Chat.add("$system", message, channel)
+ def addsys(message, channel = None, params = None):
+ Chat.add("$system", message, channel, params)
View
@@ -0,0 +1,44 @@
+# coding: utf-8
+
+def get(id, *args):
+ res = globals()[id](*args)
+ res.id = id
+ res.args = args
+ return res
+
+class Effect:
+ name = "Unknown"
+ icon = "+"
+ priority = 0
+ group = None
+
+ def recount(self):
+ pass
+
+ def onturn(self):
+ pass
+
+ def js_save(self):
+ return {"id": self.id, "args": self.args, "turns_total": self.turns_total, "turns_left": self.turns_left, "source": self.source}
+
+ @staticmethod
+ def js_load(src, host):
+ res = get(src["id"], *src["args"])
+ res.turns_total = src["turns_total"]
+ res.turns_left = src["turns_left"]
+ res.source = src["source"]
+ res.host = host
+ return res
+
+ def js(self):
+ return {"name": self.name, "icon": self.icon, "turns_left": self.turns_left}
+
+class UpMagic(Effect):
+ name = "Mighty Magic"
+ priority = -10
+
+ def __init__(self, q):
+ self.q = q
+
+ def recount(self):
+ self.host.stats.cur["matk"] *= 2
View
@@ -7,6 +7,7 @@
import user
from chat import Chat
import random
+from itertools import chain
def User(u):
return user.User(u)
@@ -82,6 +83,8 @@ def begin_turn(self):
f.move = None
if f.alive:
f.draw(1)
+ for eff in self.effects():
+ eff.onturn()
self.check_fight_over()
if not self.over():
self.check_end_turn()
@@ -91,8 +94,8 @@ def update(self):
f.need_update(True)
self.save()
- def log(self, message):
- Chat.addsys(message, self.channel())
+ def log(self, message, params = None):
+ Chat.addsys(message, self.channel(), params)
def channel(self):
return "$fight" + str(self.id)
@@ -128,16 +131,26 @@ def run_ai(self):
pass
def process_casts(self):
- to_cast = [(f.card_in_hand_by_id(f.move["card"]), f, self.fighter_by_id(f.move["cast_target"])) for f in self.fighters() if f.move]
+ to_cast = [(f.card_in_hand_by_id(f.move["card"]), f, self.fighter_by_id(f.move["cast_target"])) for f in self.fighters() if f.move and f.move["card"]]
to_cast.sort(key=lambda item: item[0].spell.priority)
for (card, caster, target) in to_cast:
card.spell._cast(caster, target)
caster.hand.remove(card)
+ def effects(self):
+ ff = (f for f in self.fighters() if f.alive)
+ eff = list(chain.from_iterable(f.effects for f in ff))
+ eff.sort(key=lambda e: e.priority)
+ return eff
+
def recount_stats(self):
- for f in self.fighters():
- if f.alive:
- f.recount_stats()
+ ff = (f for f in self.fighters() if f.alive)
+ for f in ff:
+ f.stats.reset()
+ for eff in self.effects():
+ eff.recount()
+ for f in ff:
+ f.check_stats()
self.check_fight_over()
def process_attacks(self):
@@ -152,8 +165,10 @@ def drop_excess_cards(self):
f.hand = f.hand[:f.stats["max_hand"]]
def drop_old_effects(self):
- # TODO
- pass
+ for f in self.fighters():
+ for eff in f.effects:
+ eff.turns_left -= 1
+ f.effects = [eff for eff in f.effects if eff.turns_left > 0]
def check_fight_over(self):
p0 = sum(1 for f in self.p[0] if f.alive)
View
@@ -5,6 +5,7 @@
from random import shuffle
import user
from stats import Stats
+from effect import Effect
class Fighter:
@@ -14,6 +15,7 @@ def __init__(self, fight, src = None):
self.deck = from_js_list(Card, self.deck)
self.hand = from_js_list(Card, self.hand)
self.stats = Stats(self.stats)
+ self.effects = [Effect.js_load(eff, self) for eff in self.effects]
else:
self.id = unique_id()
self.stats = Stats()
@@ -32,6 +34,7 @@ def from_user(fight, user, team_id):
for key in ["atk", "def", "matk", "mdef", "hp_max"]:
f.stats[key] = user[key]
f.hp = f.stats["hp_max"]
+ f.effects = []
f.stats["max_hand"] = 6
shuffle(f.deck)
f.hand = []
@@ -44,12 +47,14 @@ def js_save(self):
res["deck"] = to_js_list(Card, self.deck)
res["hand"] = to_js_list(Card, self.hand)
res["stats"] = self.stats.js_save()
+ res["effects"] = to_js_list(Effect, self.effects)
return res
def js(self):
res = self.js_save()
res["deck"] = len(res["deck"])
res["should_move"] = self.should_move()
+ res["effects"] = [eff.js() for eff in self.effects]
return res
def js_enemy(self):
@@ -60,6 +65,9 @@ def js_enemy(self):
def js_ally(self):
return self.js_enemy()
+
+ def js_link(self):
+ return {"name": self.name, "level": self.level, "id": self.id}
def need_update(self, need=None):
if self._type == "user":
@@ -91,26 +99,21 @@ def heal(self, amount):
self.check_stats()
return self.hp - old
- def recount_stats(self):
- self.stats.reset()
- # TODO: применить эффекты
- self.check_stats()
-
def check_stats(self):
if self.hp > self.stats["hp_max"]:
self.hp = self.stats["hp_max"]
if self.hp <= 0:
self.die()
- self.fight.log(self.name + u" умирает!")
+ self.fight.log(u"{:f} умирает!", self.js_link())
def die(self):
self.alive = False
- # TODO: убрать эффекты
+ self.effects = []
def attack(self, other):
raw = 1.25 ** (self.level - 1) * self.stats["atk"]
dmg = other.damage(raw, source=self, magic=False)
- self.fight.log(u"%s наносит %s %s урона." % (self.name, other.name, dmg))
+ self.fight.log(u"{attacker:f} наносит {target:f} %s урона." % dmg, {"attacker": self.js_link(), "target": other.js_link()})
def damage(self, amount, source=None, magic=False, spec=None):
resist = self.stats["mdef"] if magic else self.stats["def"]
@@ -123,12 +126,24 @@ def damage(self, amount, source=None, magic=False, spec=None):
def draw(self, num):
if len(self.deck) < num:
self.die()
- self.fight.log(self.name + u" проигрывает бой из-за нехватки карт!")
+ self.fight.log(u"{:f} проигрывает бой из-за нехватки карт!", self.js_link())
else:
cards = self.deck[:num]
self.deck = self.deck[num:]
self.hand += cards
+ def add_effect(self, eff, turns, source):
+ if eff.group != None:
+ if source in self.allies():
+ self.effects = [e for e in self.effects if e.group != eff.group]
+ else:
+ self.effects = [e for e in self.effects if not (e.group == eff.group and e.source == source.id)]
+ eff.turns_total = turns
+ eff.turns_left = turns
+ eff.host = self
+ eff.source = source.id
+ self.effects.append(eff)
+
def __getattr__(self, key):
if key.startswith("_"):
raise AttributeError()
View
@@ -2,6 +2,7 @@
import utils
from chat import Chat
+import effect
def get(id):
res = globals()[id]()
@@ -10,9 +11,9 @@ def get(id):
def heal_msg(f, amount):
if amount > 0:
- return f.name + u" восстанавливает " + str(amount) + u" единиц здоровья."
+ return (u"{healed:f} восстанавливает " + str(amount) + u" единиц здоровья.", {"healed": f.js_link()})
else:
- return ""
+ return ("", None)
class Spell:
name = "Unknown"
@@ -42,15 +43,15 @@ def targets(self, fighter):
res = [f for f in res if f.alive]
return res
- def format_msg(self, message):
- return message.replace("{caster}", self._caster.name).replace("{spell}", self.name).replace("{target}", self._target.name)
-
- def msg(self, message):
- self._caster.fight.log(self.format_msg(message))
+ def msg(self, message, params = None):
+ if params == None:
+ params = {}
+ params.update({"caster": self._caster.js_link(), "target": self._target.js_link(), "spell": self.js_link()})
+ self._caster.fight.log(message, params)
self._need_msg = False
def cast_message(self):
- self.msg(u"{caster} применяет способность {spell} на %s." % (u"cебя" if self.target_self() else "{target}"))
+ self.msg(u"{caster:f} применяет способность {spell:s} на %s." % (u"cебя" if self.target_self() else "{target:f}"))
def _cast(self, caster, target):
self._caster = caster
@@ -65,6 +66,12 @@ def cast(self):
def target_self(self):
return self._caster == self._target
+
+ def js_link(self):
+ return {"name": self.name}
+
+ def add_effect(self, target, effect, turns):
+ target.add_effect(effect, turns=turns, source=self._caster)
class Attack(Spell):
name = "Attack! +20%"
@@ -78,8 +85,15 @@ class Heal(Spell):
name = "Heal 10 HP"
def cast(self):
- healmsg = heal_msg(self._target, self._target.heal(10))
+ hmsg, hparams = heal_msg(self._target, self._target.heal(10))
if self.target_self():
- self.msg(u"{caster} лечится. " + healmsg)
+ self.msg(u"{caster:f} лечится. " + hmsg, hparams)
else:
- self.msg(u"{caster} лечит {target}. " + healmsg)
+ self.msg(u"{caster:f} лечит {target:f}. " + hmsg, hparams)
+
+class UpMagic(Spell):
+ name = "Mighty Magic"
+ target = "self"
+
+ def cast(self):
+ self.add_effect(self._target, effect.get("UpMagic", 2), 3)
Oops, something went wrong.

0 comments on commit e564a96

Please sign in to comment.