void HypotheticBattle::nextTurn(uint32_t unitId) { activeUnitId = unitId; auto unit = getForUpdate(unitId); unit->removeUnitBonus(Bonus::UntilGetsTurn); unit->afterGetsTurn(); }
void HypotheticBattle::setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) { std::shared_ptr<StackWithBonuses> changed = getForUpdate(id); changed->load(data); if(healthDelta < 0) { changed->removeUnitBonus(Bonus::UntilBeingAttacked); } }
void HypotheticBattle::nextRound(int32_t roundNr) { //TODO:HypotheticBattle::nextRound for(auto unit : battleAliveUnits()) { auto forUpdate = getForUpdate(unit->unitId()); //TODO: update Bonus::NTurns effects forUpdate->afterNewRound(); } }
void HypotheticBattle::removeUnit(uint32_t id) { std::set<uint32_t> ids; ids.insert(id); while(!ids.empty()) { auto toRemoveId = *ids.begin(); auto toRemove = getForUpdate(toRemoveId); if(!toRemove->ghost) { toRemove->onRemoved(); //TODO: emulate detachFromAll() somehow //stack may be removed instantly (not being killed first) //handle clone remove also here if(toRemove->cloneID >= 0) { ids.insert(toRemove->cloneID); toRemove->cloneID = -1; } //TODO: cleanup remaining clone links if any // for(auto s : stacks) // { // if(s->cloneID == toRemoveId) // s->cloneID = -1; // } } ids.erase(toRemoveId); } }
std::string DomElement::asJavaScript(EscapeOStream& out, Priority priority) const { switch(priority) { case Delete: if (deleted_ || removeAllChildren_) { out << declare(); if (deleted_) { out << var_ << ".parentNode.removeChild(" << var_ << ");"; } else if (removeAllChildren_) { out << "while (" << var_ << ".firstChild) {" << var_ << ".removeChild(" << var_ << ".firstChild);" << "}"; } } return var_; break; case Create: if (mode_ == ModeCreate) { out << declare(); if (id_.length() != 0) out << var_ << ".setAttribute('id', '" << id_ << "');"; } return var_; break; case Update: { /* * short-cut for frequent short manipulations */ if (mode_ == ModeUpdate && numManipulations_ == 1) { if (properties_.find(Wt::PropertyStyleDisplay) != properties_.end()) { std::string style = properties_.find(Wt::PropertyStyleDisplay)->second; if (style == "none") { out << "hide('" << id_ << "');"; return var_; } else if (style == "inline") { out << "inline('" + id_ + "');"; return var_; } else if (style == "block") { out << "block('" + id_ + "');"; return var_; } } } processEvents(); if (deleted_) break; if (replaced_) { declare(out); std::string varr = replaced_->asJavaScript(out, Create); replaced_->asJavaScript(out, Update); out << varr << ".style.display = " << var_ << ".style.display;"; out << var_ << ".parentNode.replaceChild(" << varr << "," << var_ << ");"; return var_; } else if (insertBefore_) { declare(out); std::string varr = insertBefore_->asJavaScript(out, Create); insertBefore_->asJavaScript(out, Update); out << var_ << ".parentNode.insertBefore(" << varr << "," << var_ + ");"; return var_; } const AttributeMap& attributesToSet = attributes_; for (AttributeMap::const_iterator i = attributesToSet.begin(); i != attributesToSet.end(); ++i) { declare(out); if (i->first == "class") { out << var_ << ".className = "; jsStringLiteral(out, i->second, '\''); out << ";"; } else { out << var_ << ".setAttribute('" << i->first << "',"; jsStringLiteral(out, i->second, '\''); out << ");"; } } for (EventHandlerMap::const_iterator i = eventHandlers_.begin(); i != eventHandlers_.end(); ++i) { if ((mode_ == ModeUpdate) || (i->second.first.length() > 0)) { declare(out); std::string fName = "f" + boost::lexical_cast<std::string>(nextId_++); out << "function " << fName << "(event) { if (!event) event = window.event; " << i->second.first << "}"; out << var_ << ".on" << i->first << "=" << fName << ";"; } } if ((mode_ == ModeCreate || wasEmpty_) && canWriteInnerHTML()) { if (!childrenToAdd_.empty() || !childrenHtml_.empty()) { declare(out); out << var_ << ".innerHTML=\'"; EscapeOStreamScope scope = jsStringLiteral(out, '\''); out << childrenHtml_; TimeoutList timeouts; for (unsigned i = 0; i < childrenToAdd_.size(); ++i) childrenToAdd_[i].second->asHTML(out, timeouts); scope.pop(); out << "\';"; createTimeoutJs(out, timeouts); } } else { for (unsigned i = 0; i < childrenToAdd_.size(); ++i) { declare(out); DomElement *child = childrenToAdd_[i].second; std::string cvar = child->asJavaScript(out, Create); child->asJavaScript(out, Update); if (childrenToAdd_[i].first.length() > 0) { DomElement *beforeElement = getForUpdate(childrenToAdd_[i].first, DIV); out << var_ << ".insertBefore(" << cvar << "," << beforeElement->createReference() << ");"; delete beforeElement; } else { out << var_ << ".appendChild(" << cvar << ");"; } } } for (PropertyMap::const_iterator i = properties_.begin(); i != properties_.end(); ++i) { declare(out); std::string p; std::string value; switch(i->first) { case Wt::PropertyInnerHTML: out << var_ << ".innerHTML="; jsStringLiteral(out, i->second, '\''); out << ';'; break; case Wt::PropertyValue: out << var_ << ".value="; jsStringLiteral(out, i->second, '\''); out << ';'; break; case Wt::PropertyDisabled: out << var_ << ".disabled=" << i->second << ';'; break; case Wt::PropertyChecked: out << var_ << ".checked=" << i->second << ';'; break; case Wt::PropertySelected: out << var_ << ".selected=" << i->second << ';'; break; case Wt::PropertySelectedIndex: out << var_ << ".selectedIndex=" << i->second << ';'; break; case Wt::PropertySrc: out << var_ << ".src='" << i->second << "\';"; break; case Wt::PropertyText: out << var_ << ".text="; jsStringLiteral(out, i->second, '\''); out << ';'; break; default: if ((i->first >= Wt::PropertyStylePosition) && (i->first <= Wt::PropertyStyleDisplay)) { static std::string cssNames[] = { "position", "zIndex", "float", "clear", "width", "height", "minWidth", "minHeight", "maxWidth", "maxHeight", "left", "right", "top", "bottom", "verticalAlign", "textAlign", "padding", "marginTop", "marginRight", "marginBottom", "marginLeft", "cursor", "borderTop", "borderRight", "borderBottom", "borderLeft", "color", "overflow", "overflow", "fontFamily", "fontStyle", "fontVariant", "fontWeight", "fontSize", "backgroundColor", "backgroundImage", "backgroundRepeat", "backgroundPosition", "textDecoration", "tableLayout", "borderSpacing", "display" }; out << var_ << ".style." << cssNames[i->first - Wt::PropertyStylePosition] << "=\'" << i->second << "\';"; } } } for (unsigned i = 0; i < methodCalls_.size(); ++i) { declare(out); out << var_ << "." << methodCalls_[i] << ';'; } if (timeOut_ != -1) out << "addTimerEvent('" << id_ << "', " << boost::lexical_cast<std::string>(timeOut_) + ");"; out << javaScript_; return var_; } } return var_; }
DomElement *DomElement::getForUpdate(const WObject *object, ElementType type) { return getForUpdate(object->formName(), type); }
void HypotheticBattle::removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) { getForUpdate(id)->removeUnitBonus(bonus); bonusTreeVersion++; }
void HypotheticBattle::moveUnit(uint32_t id, BattleHex destination) { std::shared_ptr<StackWithBonuses> changed = getForUpdate(id); changed->position = destination; }