QueryData generate()

in misc/osquery_game/challenge.cpp [481:585]


  QueryData generate(QueryContext& ctx) {
    QueryData results;

    kDay++;

    if (kDay == 1) {
      bool weeds = false;
      for (auto& c : kCells) {
        if (c.i == PLANT || c.i == SEEDLING) {
          weeds = true;
          c.i = BLANK;
        }
      }
      if (weeds) {
        LOG(WARNING) << "Some plants or seedlings were overtaken by weeds.";
      }
    }

    if (kDay > 5) {
      LOG(ERROR) << "The farming season is over.";
      return {};
    }

    Row r;

    LOG(WARNING) << "Good morning! It is day " << (int)kDay << "/256 " << kEmoji[SUNNYFACE];

    auto actions = ctx.constraints["action"].getAll(EQUALS);
    if (actions.size() > 1) {
      LOG(ERROR) << "You can only perform 1 action a day.";
    } else if (actions.size() == 0) {
      // Just show
      r["action"] = A_SHOW;
    } else {
      r["action"] = *actions.begin();
    }

    // A lot of things try to get destinations.
    auto dsts = getDstPositions(ctx.constraints["dst"]);
    auto srcs = ctx.constraints["src"].getAll<long long>(EQUALS);

    // Move from one tile to another.
    if (r["action"] == A_MOVE) {
      if (srcs.size() != 1) {
        LOG(ERROR) << "Invalid move. The src column must contain one value.";
      } else {
        auto src = *srcs.begin();
        r["src"] = std::to_string(src);
        if (dsts.size() != 1) {
          LOG(ERROR) << "Invalid move. The dst column must contain one value.";
        } else {
          r["dst"] = std::to_string(*dsts.begin());
          if (!tryValidMove(src, *dsts.begin())) {
            LOG(ERROR) << "Invalid move. You are trying something that is not allowed.";
          }
        }
      }
    } else if (r["action"] == A_PLANT) {
      if (srcs.size() != 0) {
        LOG(ERROR) << "Invalid plant. The src column is not used.";
      } else {
        auto single_dst = tryPlant(dsts);
        if (single_dst == 0) {
          LOG(ERROR) << "Invalid plant. You are trying something that is not allowed.";
        } else {
          r["dst"] = std::to_string(single_dst);
        }
      }
    } else if (r["action"] == A_WATER) {
      if (srcs.size() != 0) {
        LOG(ERROR) << "Invalid water. The src column is not used.";
      } else {
        auto single_dst = tryWater(dsts);
        if (single_dst == 0) {
          LOG(ERROR) << "Invalid water. You are trying something that is not allowed.";
        } else {
          r["dst"] = std::to_string(single_dst);
        }
      }
    } else if (r["action"] == A_PICKUP) {
      if (dsts.size() != 0) {
        LOG(ERROR) << "Invalid pickup. The dst column is not used.";
      } else if (srcs.size() != 1) {
        LOG(ERROR) << "Invalid pickup. The src column must contain one value.";
      } else {
        auto single_src = *srcs.begin();
        if (!tryPickup(single_src)) {
          LOG(ERROR) << "Invalid pickup. You are trying something that is not allowed.";
        } else {
          r["src"] = std::to_string(single_src);
        }
      }
    }

    if (kSheepGood && kHavePlant) {
      LOG(ERROR) << "You completed all quests. Congrats! Your prize is " << getenv("KEY");
    }

    r["farm"] = showFarm();
    if (!kSheepGood && kThreads.size() == 0) {
      kThreads.emplace_back(std::async(std::launch::async, sheepRunsAway));
    }

    return {r};
  }