/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.l2jmobius.gameserver.instancemanager;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import com.l2jmobius.gameserver.enums.MatchingRoomType;
import com.l2jmobius.gameserver.model.actor.instance.L2PcInstance;
import com.l2jmobius.gameserver.model.base.ClassId;
import com.l2jmobius.gameserver.model.matching.MatchingRoom;
/**
* @author Sdw
*/
public class MatchingRoomManager
{
private volatile Set _waitingList;
private static final Map> _rooms = new ConcurrentHashMap<>(2);
private final AtomicInteger _id = new AtomicInteger(0);
public void addToWaitingList(L2PcInstance player)
{
if (_waitingList == null)
{
synchronized (this)
{
if (_waitingList == null)
{
_waitingList = ConcurrentHashMap.newKeySet(1);
}
}
}
_waitingList.add(player);
}
public void removeFromWaitingList(L2PcInstance player)
{
getPlayerInWaitingList().remove(player);
}
public Set getPlayerInWaitingList()
{
return _waitingList == null ? Collections.emptySet() : _waitingList;
}
public List getPlayerInWaitingList(int minLevel, int maxLevel, List classIds, String query)
{
return _waitingList == null ? Collections.emptyList() : _waitingList.stream().filter(p -> (p.getLevel() >= minLevel) && (p.getLevel() <= maxLevel)).filter(p -> (classIds == null) || classIds.contains(p.getClassId())).filter(p -> query.isEmpty() || p.getName().toLowerCase().contains(query)).collect(Collectors.toList());
}
public int addMatchingRoom(MatchingRoom room)
{
final int roomId = _id.incrementAndGet();
_rooms.computeIfAbsent(room.getRoomType(), k -> new ConcurrentHashMap<>()).put(roomId, room);
return roomId;
}
public void removeMatchingRoom(MatchingRoom room)
{
_rooms.getOrDefault(room.getRoomType(), Collections.emptyMap()).remove(room.getId());
}
public Map getPartyMathchingRooms()
{
return _rooms.get(MatchingRoomType.PARTY);
}
public List getPartyMathchingRooms(int location, int level)
{
//@formatter:off
return _rooms.getOrDefault(MatchingRoomType.PARTY, Collections.emptyMap()).values().stream()
.filter(r -> (location < 0) || (r.getLocation() == location))
.filter(r -> (r.getMinLvl() <= level) && (r.getMaxLvl() >= level))
.collect(Collectors.toList());
//@formatter:on
}
public Map getCCMathchingRooms()
{
return _rooms.get(MatchingRoomType.COMMAND_CHANNEL);
}
public List getCCMathchingRooms(int location, int level)
{
//@formatter:off
return _rooms.getOrDefault(MatchingRoomType.COMMAND_CHANNEL, Collections.emptyMap()).values().stream()
.filter(r -> r.getLocation() == location)
.filter(r -> (r.getMinLvl() <= level) && (r.getMaxLvl() >= level))
.collect(Collectors.toList());
//@formatter:on
}
public MatchingRoom getCCMatchingRoom(int roomId)
{
return _rooms.getOrDefault(MatchingRoomType.COMMAND_CHANNEL, Collections.emptyMap()).get(roomId);
}
public MatchingRoom getPartyMathchingRoom(int location, int level)
{
//@formatter:off
return _rooms.getOrDefault(MatchingRoomType.PARTY, Collections.emptyMap()).values().stream()
.filter(r -> r.getLocation() == location)
.filter(r -> (r.getMinLvl() <= level) && (r.getMaxLvl() >= level))
.findFirst()
.orElse(null);
//@formatter:on
}
public MatchingRoom getPartyMathchingRoom(int roomId)
{
return _rooms.getOrDefault(MatchingRoomType.PARTY, Collections.emptyMap()).get(roomId);
}
public static MatchingRoomManager getInstance()
{
return SingletonHolder._instance;
}
private static class SingletonHolder
{
protected static final MatchingRoomManager _instance = new MatchingRoomManager();
}
}