protected void initialize() throws PerunException {

    // gets session for a system principal "perunRegistrar"
    final PerunPrincipal pp =
        new PerunPrincipal(
            "perunRegistrar",
            ExtSourcesManager.EXTSOURCE_NAME_INTERNAL,
            ExtSourcesManager.EXTSOURCE_INTERNAL);
    registrarSession = perun.getPerunSession(pp, new PerunClient());

    // cache expires after 5 minutes from creation
    requestCache =
        ExpiringMap.builder()
            .expiration(5, TimeUnit.MINUTES)
            .expirationPolicy(ExpiringMap.ExpirationPolicy.CREATED)
            .build();
  }
Exemple #2
0
public abstract class GameRoom implements Room, MatchmakingTarget {

  // Slot is reserved for 7 seconds.
  private static final int RESERVED_SLOT_EXPIRATION = 7;

  // State changing variables.
  private RoomState state = RoomState.DISABLED;

  private final Map<PlayerProfile, Object> reservations =
      ExpiringMap.builder().expiration(RESERVED_SLOT_EXPIRATION, TimeUnit.SECONDS).build();
  private final Set<Player> players = new HashSet<>();

  // Game room events.
  public final Event<Player> OnPlayerJoinEvent = new Event<>();
  public final Event<Player> OnPlayerLeaveEvent = new Event<>();

  // Basic variables.
  private final UUID uuid;
  private final String name;
  private final Game game;
  private final int maxPlayers;

  protected GameRoom(UUID uuid, String name, Game game, int maxPlayers) {
    this.uuid = uuid;
    this.name = name;
    this.game = game;
    this.maxPlayers = maxPlayers;
  }

  protected void push() {
    // TODO: Send state to master.
  }

  protected void setRoomState(RoomState state) {
    this.state = state;
    this.push();
  }

  public void create() {
    this.state = RoomState.WAITING;
    this.push();

    this.onCreate();
  }

  public void start() {
    this.state = RoomState.PLAYING;
    this.push();

    this.onStart();
  }

  public void reset() {
    this.state = RoomState.RESETING;
    this.push();

    this.onReset();
  }

  public void destroy() {
    this.state = RoomState.DISABLED;
    this.push();

    this.onDestroy();
  }

  protected abstract void onCreate();

  protected abstract void onStart();

  protected abstract void onReset();

  protected abstract void onDestroy();

  @Override
  public void addReservation(PlayerProfile profile) {
    this.reservations.put(profile, null);
  }

  @Override
  public boolean isReserved(PlayerProfile profile) {
    return this.reservations.containsKey(profile);
  }

  @Override
  public void addPlayer(Player player) {
    players.add(player);

    // Call event.
    OnPlayerJoinEvent.call(player);
  }

  @Override
  public void removePlayer(Player player) {
    players.remove(player);

    // Call event.
    OnPlayerLeaveEvent.call(player);
  }

  public void broadcast(String message) {
    for (Player player : this.players) {
      player.sendMessage(message);
    }
  }

  @Override
  public Set<PlayerProfile> getPlayerProfiles() {
    Set<PlayerProfile> profiles = new HashSet<>();
    for (Player item : this.players) {
      PlayerProfileImpl current = new PlayerProfileImpl(item.getUniqueId());
      current.setName(item.getName());
      profiles.add(current);
    }
    return profiles;
  }

  public Set<Player> getPlayers() {
    return new HashSet<>(players);
  }

  @Override
  public UUID getUUID() {
    return uuid;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public Game getGame() {
    return game;
  }

  @Override
  public int getMaxPlayers() {
    return maxPlayers;
  }

  @Override
  public RoomState getState() {
    return state;
  }
}