当前位置: 首页>>代码示例>>C++>>正文


C++ CTransaction::SetGameTx方法代码示例

本文整理汇总了C++中CTransaction::SetGameTx方法的典型用法代码示例。如果您正苦于以下问题:C++ CTransaction::SetGameTx方法的具体用法?C++ CTransaction::SetGameTx怎么用?C++ CTransaction::SetGameTx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CTransaction的用法示例。


在下文中一共展示了CTransaction::SetGameTx方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: error

bool
CreateGameTransactions (CNameDB& nameDb, const GameState& gameState,
                        const StepResult& stepResult,
                        std::vector<CTransaction>& outvgametx)
{
  if (fDebug)
    printf ("Constructing game tx @%d...\n", gameState.nHeight);

  // Create resulting game transactions
  // Transaction hashes must be unique
  outvgametx.clear ();

  CTransaction txNew;
  txNew.SetGameTx ();

  // Destroy name-coins of killed players
  const PlayerSet& killedPlayers = stepResult.GetKilledPlayers ();
  const KilledByMap& killedBy = stepResult.GetKilledBy ();
  txNew.vin.reserve (killedPlayers.size ());
  BOOST_FOREACH(const PlayerID &victim, killedPlayers)
    {
      const vchType vchName = vchFromString (victim);
      CTransaction tx;
      if (!GetTxOfNameAtHeight (nameDb, vchName, gameState.nHeight, tx))
        return error ("Game engine killed a non-existing player %s",
                      victim.c_str ());

      if (fDebug)
        printf ("  killed: %s\n", victim.c_str ());

      CTxIn txin(tx.GetHash (), IndexOfNameOutput (tx));

      /* List all killers, if player was simultaneously killed by several
         other players.  If the reason was not KILLED_DESTRUCT, handle
         it also.  If multiple reasons apply, the game tx is constructed
         for the first reason according to the ordering inside of KilledByMap.
         (Which in turn is determined by the enum values for KILLED_*.)  */

      typedef KilledByMap::const_iterator Iter;
      const std::pair<Iter, Iter> iters = killedBy.equal_range (victim);
      if (iters.first == iters.second)
        return error ("No reason for killed player %s", victim.c_str ());
      const KilledByInfo::Reason reason = iters.first->second.reason;

      /* Unless we have destruct, there should be exactly one entry with
         the "first" reason.  There may be multiple entries for different
         reasons, for instance, killed by poison and staying in spawn
         area at the same time.  */
      {
        Iter it = iters.first;
        ++it;
        if (reason != KilledByInfo::KILLED_DESTRUCT && it != iters.second
            && reason == it->second.reason)
          return error ("Multiple same-reason, non-destruct killed-by"
                        " entries for %s", victim.c_str ());
      }

      switch (reason)
        {
        case KilledByInfo::KILLED_DESTRUCT:
          txin.scriptSig << vchName << GAMEOP_KILLED_BY;
          for (Iter it = iters.first; it != iters.second; ++it)
            {
              if (it->second.reason != KilledByInfo::KILLED_DESTRUCT)
                {
                  assert (it != iters.first);
                  break;
                }
              txin.scriptSig << vchFromString (it->second.killer.ToString ());
            }
          break;

        case KilledByInfo::KILLED_SPAWN:
          txin.scriptSig << vchName << GAMEOP_KILLED_BY;
          break;

        case KilledByInfo::KILLED_POISON:
          txin.scriptSig << vchName << GAMEOP_KILLED_POISON;
          break;

        default:
          assert (false);
        }

      txNew.vin.push_back (txin);
    }
  if (!txNew.IsNull ())
    {
      outvgametx.push_back (txNew);
      if (fDebug)
        printf ("Game tx for killed players: %s\n", txNew.GetHashForLog ());
    }

  /* Pay bounties to the players who collected them.  The transaction
     inputs are just "dummy" containing informational messages.  */
  txNew.SetNull ();
  txNew.SetGameTx ();
  txNew.vin.reserve (stepResult.bounties.size ());
  txNew.vout.reserve (stepResult.bounties.size ());

//.........这里部分代码省略.........
开发者ID:BGBHUC,项目名称:huntercoin,代码行数:101,代码来源:gametx.cpp

示例2: CreateGameTransactions

bool CreateGameTransactions(CNameDB *pnameDb, const GameState &gameState, const StepResult &stepResult, std::vector<CTransaction> &outvgametx)
{
    // Create resulting game transactions
    // Transaction hashes must be unique
    outvgametx.clear();

    CTransaction txNew;
    txNew.SetGameTx();

    // Destroy name-coins of killed players
    txNew.vin.reserve(stepResult.killedPlayers.size());
    BOOST_FOREACH(const PlayerID &victim, stepResult.killedPlayers)
    {
        std::vector<unsigned char> vchName = vchFromString(victim);
        CTransaction tx;
        if (!pnameDb || !GetTxOfNameAtHeight(*pnameDb, vchName, gameState.nHeight, tx))
            return error("Game engine killed a non-existing player %s", victim.c_str());

        CTxIn txin(tx.GetHash(), IndexOfNameOutput(tx));

        txin.scriptSig << vchName << GAMEOP_KILLED_BY;

        // List all killers, if player was simultaneously killed by several other players
        typedef std::multimap<PlayerID, CharacterID>::const_iterator Iter;
        std::pair<Iter, Iter> iters = stepResult.killedBy.equal_range(victim);
        for (Iter it = iters.first; it != iters.second; ++it)
            txin.scriptSig << vchFromString(it->second.ToString());
        txNew.vin.push_back(txin);
    }
    if (!txNew.IsNull())
        outvgametx.push_back(txNew);

    // Pay bounties to the players who collected them
    txNew.SetNull();
    txNew.SetGameTx();
    txNew.vin.reserve(stepResult.bounties.size());      // Dummy inputs that contain informational messages only (one per each output)
    txNew.vout.reserve(stepResult.bounties.size());

    BOOST_FOREACH(const PAIRTYPE(CharacterID, CollectedLootInfo) &bounty, stepResult.bounties)
    {
        std::vector<unsigned char> vchName = vchFromString(bounty.first.player);
        CTransaction tx;
        if (!pnameDb || !GetTxOfNameAtHeight(*pnameDb, vchName, gameState.nHeight, tx))
            return error("Game engine created bounty for non-existing player");

        CTxOut txout;
        txout.nValue = bounty.second.nAmount;

        // Note: we only use the resulting game state to pay rewards. If we need to pay them to just-killed players,
        // this function should be modified to accept two game states, or the game state must be augmented with deadPlayers array.

        std::map<PlayerID, PlayerState>::const_iterator mi = gameState.players.find(bounty.first.player);
        if (mi == gameState.players.end())
            return error("Game engine created bounty for non-existing (dead?) player");

        if (!mi->second.address.empty())
        {
            // Player-provided addresses are validated before accepting them, so failing
            // here is ok
            if (!txout.scriptPubKey.SetBitcoinAddress(mi->second.address))
                return error("Failed to set player-provided address for bounty");
        }
        else
        {
            // TODO: Maybe pay to the script of the name-tx without extracting the address first
            // (see source of GetNameAddress - it obtains the script by calling RemoveNameScriptPrefix)
            uint160 addr;
            if (!GetNameAddress(tx, addr))
                return error("Cannot get name address for bounty");
            txout.scriptPubKey.SetBitcoinAddress(addr);
        }

        txNew.vout.push_back(txout);

        CTxIn txin;
        txin.scriptSig << vchName << GAMEOP_COLLECTED_BOUNTY
                << bounty.first.index
                << bounty.second.firstBlock
                << bounty.second.lastBlock
                << bounty.second.collectedFirstBlock
                << bounty.second.collectedLastBlock
            ;
        txNew.vin.push_back(txin);
    }
    if (!txNew.IsNull())
        outvgametx.push_back(txNew);

    return true;
}
开发者ID:HunterMinerCrafter,项目名称:huntercoin,代码行数:89,代码来源:gametx.cpp


注:本文中的CTransaction::SetGameTx方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。