예제 #1
0
 private void initPlayground() {
   okBtn.setEnabled(false);
   isMouseEnabled = false;
   removeActors(Match.class);
   for (int i = 0; i < nbMatches; i++) {
     Match match = new Match();
     addActor(match, new Location(2 + 3 * i, 4));
     match.addMouseTouchListener(this, GGMouse.lClick);
   }
 }
 /**
  * Updates all league tables with a match. Expects to be called with matches in chronological
  * order (earliest first) and only updates before the games on the next day.
  */
 public void updateWithMatch(Match match) {
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(match.getDate());
   int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
   if (dayOfYear != lastSeenDay) {
     lastSeenDay = dayOfYear;
     for (LeagueTable lTable : tables.values()) {
       lTable.updateLeague(matchBuffer);
     }
     matchBuffer.clear();
   }
   matchBuffer.add(match);
 }
예제 #3
0
 public void repeat(SikuliEvent.Type type, Object pattern, Match match, long secs) {
   if (type == SikuliEvent.Type.CHANGE) {
     Debug.error("EventMgr: repeat: CHANGE repeats automatically");
   } else if (type == SikuliEvent.Type.VANISH) {
     Debug.error("EventMgr: repeat: not supported for VANISH");
   } else if (type == SikuliEvent.Type.APPEAR) {
     _state.put(pattern, State.REPEAT);
     if (secs <= 0) {
       secs = (long) _region.getWaitForVanish();
     }
     _wait.put(pattern, (new Date()).getTime() + 1000 * secs);
     Debug.log(
         2,
         "EventMgr: repeat: requested for APPEAR: "
             + pattern.toString()
             + " at "
             + match.toStringShort()
             + " after "
             + secs
             + " seconds");
     sthgLeft = true;
   }
 }
예제 #4
0
  /**
   * Runs the server. The server will wait for some match info (which specifies the teams and set of
   * maps to run) and then begin running matches.
   */
  public void run() {
    int aWins = 0, bWins = 0;

    while (!matches.isEmpty()) {
      Match match = matches.peek();
      if (!finished.isEmpty())
        match.setInitialTeamMemory(finished.getLast().getComputedTeamMemory());

      try {
        debug("running match " + match);
        match.initialize();
        runMatch(match, proxyWriter);
        finished.add(match);
        matches.remove(match);

        if (match.getWinner() == Team.A) aWins++;
        else if (match.getWinner() == Team.B) bWins++;

        match.finish();

        // Allow best of three scrimmages -- single game scrims should still work fine
        // TODO:This "win mode" should probably be something from the database
        if (mode == Mode.TOURNAMENT || mode == Mode.SCRIMMAGE) {
          if (aWins == 2 || bWins == 2) break;
        }

      } catch (Exception e) {
        ErrorReporter.report(e);
        error("couldn't run match: ");

        this.state = State.ERROR;
      }
    }

    proxyWriter.terminate();
  }
예제 #5
0
 private void checkPatterns(ScreenImage simg) {
   Finder finder = null;
   if (Settings.UseImageFinder) {
     finder = new ImageFinder(_region);
     ((ImageFinder) finder).setIsMultiFinder();
   } else {
     finder = new Finder(simg, _region);
   }
   String imgOK;
   Debug.log(
       3,
       "observe: checkPatterns entry: sthgLeft: %s isObserving: %s",
       sthgLeft,
       _region.isObserving());
   for (Object ptn : _state.keySet()) {
     if (_state.get(ptn) != State.FIRST
         && _state.get(ptn) != State.UNKNOWN
         && _state.get(ptn) != State.REPEAT) {
       continue;
     }
     imgOK = null;
     if (ptn instanceof String) {
       imgOK = finder.find((String) ptn);
       Image img = Image.create((String) ptn);
       if (img.isValid()) {
         imgOK = finder.find(img);
       } else if (img.isText()) {
         imgOK = finder.findText((String) ptn);
       }
     } else if (ptn instanceof Pattern) {
       imgOK = finder.find((Pattern) ptn);
     } else if (ptn instanceof Image) {
       imgOK = finder.find((Image) ptn);
     }
     if (null == imgOK) {
       Debug.error("EventMgr: checkPatterns: Image not valid", ptn);
       _state.put(ptn, State.MISSING);
       continue;
     }
     if (_state.get(ptn) == State.REPEAT) {
       Debug.log(3, "repeat: checking");
       if (_lastMatch.get(ptn).exists(ptn) != null) {
         if ((new Date()).getTime() > _wait.get(ptn)) {
           _state.put(ptn, State.APPEARED);
           Debug.log(3, "repeat: vanish timeout");
           // time out
         } else {
           sthgLeft = true;
         }
         continue; // not vanished within given time or still there
       } else {
         _state.put(ptn, State.UNKNOWN);
         sthgLeft = true;
         Debug.log(3, "repeat: has vanished");
         continue; // has vanished, repeat
       }
     }
     Match m = null;
     boolean hasMatch = false;
     if (finder.hasNext()) {
       m = finder.next();
       if (m.getScore() >= getSimiliarity(ptn)) {
         hasMatch = true;
         _lastMatch.put(ptn, m);
       }
     }
     if (hasMatch) {
       Debug.log(
           2,
           "EventMgr: checkPatterns: "
               + ptn.toString()
               + " match: "
               + m.toStringShort()
               + " in "
               + _region.toStringShort());
     } else if (_state.get(ptn) == State.FIRST) {
       Debug.log(
           2,
           "EventMgr: checkPatterns: "
               + ptn.toString()
               + " match: "
               + "NO"
               + " in "
               + _region.toStringShort());
       _state.put(ptn, State.UNKNOWN);
     }
     if (_appearOb.containsKey(ptn)) {
       if (_state.get(ptn) != State.APPEARED) {
         if (hasMatch) {
           _state.put(ptn, State.APPEARED);
           _count.put(ptn, _count.get(ptn) + 1);
           callAppearObserver(ptn, m);
         } else {
           sthgLeft = true;
         }
       }
     } else if (_vanishOb.containsKey(ptn)) {
       if (_state.get(ptn) != State.VANISHED) {
         if (!hasMatch) {
           _state.put(ptn, State.VANISHED);
           _count.put(ptn, _count.get(ptn) + 1);
           callVanishObserver(ptn, _lastMatch.get(ptn));
         } else {
           sthgLeft = true;
         }
       }
     }
     if (!_region.isObserving()) {
       break;
     }
   }
   Debug.log(
       3,
       "observe: checkPatterns exit: sthgLeft: %s isObserving: %s",
       sthgLeft,
       _region.isObserving());
 }
예제 #6
0
  /**
   * Runs a match; configures the controller and list of proxies, and starts running the game in a
   * separate thread.
   */
  private void runMatch(final Match match, final ProxyWriter proxyWriter) throws Exception {
    if (Mode.HEADLESS.equals(mode) || Mode.SCRIMMAGE.equals(mode) || Mode.TOURNAMENT.equals(mode)) {
      this.state = State.RUNNING;
      this.runUntil = Integer.MAX_VALUE;
    }

    // Poll for RUNNING, if mode == Mode.LOCAL
    while (!State.RUNNING.equals(state)) {
      try {
        Thread.sleep(250);
      } catch (InterruptedException e) {
      }
    }

    long startTime = System.currentTimeMillis();

    say("-------------------- Match Starting --------------------");
    say(match.toString());

    // Compute the header and send it to all listeners.
    MatchHeader header = match.getHeader();
    proxyWriter.enqueue(header);
    ExtensibleMetadata exHeader = match.getHeaderMetadata();
    proxyWriter.enqueue(exHeader);

    this.state = State.RUNNING;

    int count = 0;

    final String throttle = options.get("bc.server.throttle");
    final int throttleCount = options.getInt("bc.server.throttle-count");
    final boolean doYield = "yield".equals(throttle);
    final boolean doSleep = "sleep".equals(throttle);

    // If there are more rounds to be run, run them and
    // and send the round (and optionally stats) bytes to
    // recipients.
    while (match.hasMoreRounds()) {

      // If not paused/stopped:
      switch (this.state) {
        case RUNNING:
          if (match.getRoundNumber() == runUntil) {
            Thread.sleep(25);
            break;
          }

          final RoundDelta round = match.getRound();
          if (round == null) break;

          if (GameState.BREAKPOINT.equals(match.getGameState())) {
            this.state = State.PAUSED;
            proxyWriter.enqueue(new PauseEvent());
          } else if (GameState.DONE.equals(match.getGameState())) {
            this.state = State.FINISHED;
          }

          proxyWriter.enqueue(round);

          if (count++ == throttleCount) {
            if (doYield) Thread.yield();
            else if (doSleep) Thread.sleep(1);
            count = 0;
          }

          break;

        case PAUSED:
          Thread.sleep(250);
          break;
      }
    }

    // Compute footer data.
    GameStats gameStats = match.getGameStats();
    MatchFooter footer = match.getFooter();

    proxyWriter.enqueue(gameStats);
    proxyWriter.enqueue(footer);

    say(match.getWinnerString());
    say("-------------------- Match Finished --------------------");

    double timeDiff = (System.currentTimeMillis() - startTime) / 1000.0;
    debug(String.format("match completed in %.4g seconds", timeDiff));

    this.state = State.FINISHED;
  }