package facade;

import DTO.BadgesDTO;
import DTO.MapBoardDTO;
import DTO.MapBoardDTO2;
import DTO.PlayerMapBoardDTO;
import DTO.PlayerDTO;
import DTO.PlayerDTOMini;
import DataMapper.DataMapperCalls;
import com.google.common.collect.MapMaker;
import entity.MapBoard;
import entity.MapValues;
import entity.Player;
import entity.UrlBanners;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import utils.PuSelector;

public class Facade {

    private static Map<String, Player> playerCache = new LinkedHashMap();
    private static ConcurrentMap<String, MapBoard> mapBoardCache = new MapMaker().concurrencyLevel(4).makeMap();
    private static ConcurrentMap<String, ConcurrentMap<String, MapValues>> playerRelatedMapValuesCache = new MapMaker().concurrencyLevel(6).makeMap();
    private static final long EXPIRE_TIME_IN_SECONDS = TimeUnit.SECONDS.convert(30, TimeUnit.MINUTES);
    private static Instant elapsedTime = Instant.now();
    private static final ForkJoinPool executor = instantiateExecutor();

    private static ForkJoinPool instantiateExecutor() {
        return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
                ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                null, false);
    }

    private boolean getMap(String map, String Identifier) {
        boolean identified = Pattern.compile(Pattern.quote(Identifier), Pattern.CASE_INSENSITIVE).matcher(map).find();
        return identified;
    }

    private boolean getPlayer(Player player, String Identifier) {
        boolean identified = Pattern.compile(Pattern.quote(Identifier), Pattern.CASE_INSENSITIVE).matcher(player.getName()).find();
        if (!identified) {
            identified = Pattern.compile(Pattern.quote(Identifier), Pattern.CASE_INSENSITIVE).matcher(player.getSteamID()).find();
        }
        return identified;
    }

    public List<MapBoardDTO2> getSearchMapsResults(String identifier) {
        List<String> arrnames = new ArrayList(mapBoardCache.keySet());
        Collections.sort(arrnames, String.CASE_INSENSITIVE_ORDER);
        List<MapBoardDTO2> arrMaps = new ArrayList();
        for (String str : arrnames) {
            if (getMap(str, identifier)) {
                Map.Entry<String, Integer> getMapnameAndStage = GetMapnameAndStage(str);
                String mapName = getMapnameAndStage.getKey();
                arrMaps.add(new MapBoardDTO2(mapName, getMapnameAndStage.getValue()));
            }
        }
        return arrMaps;
    }

    public List<PlayerDTO> getSearchPlayerResults(String Identifier) {
        List<Player> players = new ArrayList(playerCache.values());
        List<PlayerDTO> playersDTO = new ArrayList();
        Collection<Player> playerAvatarsCaching = new ArrayList();
        for (Player player : players) {
            if (getPlayer(player, Identifier)) {
                playerAvatarsCaching.add(player);
            }
        }
        updateAvatarsToCache(playerAvatarsCaching);
        players = new ArrayList(playerCache.values());
        for (Player player : players) {
            if (getPlayer(player, Identifier)) {
                playersDTO.add(new PlayerDTO(player));
            }
        }
        return playersDTO;
    }

    public int getSpecificFetchMap(String mapname, int stage) {
        MapBoard specificMapBoardCache = mapBoardCache.getOrDefault(mapname + "S" + stage, null);
        if (specificMapBoardCache == null) {
            specificMapBoardCache = mapBoardCache.get(mapname);
        }
        return specificMapBoardCache.getMapvalues().size();
    }

    public List<MapBoardDTO2> getAllMaps() {
        List<String> arrnames = new ArrayList(mapBoardCache.keySet());
        Collections.sort(arrnames, String.CASE_INSENSITIVE_ORDER);
        List<MapBoardDTO2> arrMaps = new ArrayList();
        // System.out.println("arrnames size: " + arrnames.size() + "\n");
        for (String str : arrnames) {
            //System.out.println("getAllMaps str: " + str + "\n");
            Map.Entry<String, Integer> getMapnameAndStage = GetMapnameAndStage(str);
            String mapName = getMapnameAndStage.getKey();
            arrMaps.add(new MapBoardDTO2(mapName, getMapnameAndStage.getValue()));
        }
        return arrMaps;
    }

    public Entry<String, Integer> GetMapnameAndStage(String mapName) {
        Pattern pattern = Pattern.compile("S(\\d+)");
        Matcher matcher = pattern.matcher(mapName);
        String map = "";
        int stage = 1; //0
        if (matcher.find()) {
            String group = matcher.group();
            map = mapName.substring(0, mapName.indexOf(group));
            stage = Integer.valueOf(group.substring(1));
        } else {
            map = mapName;
        }
        return new AbstractMap.SimpleEntry(map, stage);
    }

    public List<MapBoardDTO> getSpecificMap(String mapname, int stage, int iterator) {
        int mapPlayerAddiditionCap = 75;
        List<MapBoardDTO> returnlist = new ArrayList();
        String mapRetrieved = mapname + "S" + stage;
        MapBoard mapBoard = mapBoardCache.getOrDefault(mapRetrieved, null);
        if (mapBoard == null) {
            mapRetrieved = mapname;
            mapBoard = mapBoardCache.get(mapRetrieved);
        }
        List<MapValues> mapvalues = mapBoard.getMapvalues();
        int counter = 0;
        Collection<Player> playerAvatarsCaching = new ArrayList();
        for (MapValues mapvalue : mapvalues) {
            if (counter >= iterator && counter <= iterator + mapPlayerAddiditionCap) {
                Player getPlayer = playerCache.get(mapvalue.getPlayerSteamID());
                String avatar = getPlayer.getAvatar();
                if (avatar == null) {
                    playerAvatarsCaching.add(getPlayer);
                }
            }
            if (counter > iterator + mapPlayerAddiditionCap) {
                break;
            }
            counter++;
        }
        counter = 0;
        updateAvatarsToCache(playerAvatarsCaching);
        for (MapValues mapvalue : mapvalues) {
            if (counter >= iterator && counter <= iterator + mapPlayerAddiditionCap) {
                String avatar = playerCache.get(mapvalue.getPlayerSteamID()).getAvatar();
                MapBoardDTO mapBoardDTO = new MapBoardDTO(mapvalue, playerCache.get(mapvalue.getPlayerSteamID()).getName(), avatar);
                mapBoardDTO.setBadgesUrls(playerCache.get(mapvalue.getPlayerSteamID()));
                returnlist.add(mapBoardDTO);
            }
            if (counter > iterator + mapPlayerAddiditionCap) {
                break;
            }
            counter++;
        }
        return returnlist;
    }

    public List<PlayerMapBoardDTO> getPlayerMaps(String steamid, int iterator) {
        int playerMapsCapAddition = 35;
        try {
            ConcurrentMap<String, MapValues> playerMaps = playerRelatedMapValuesCache.get(steamid);
            List<PlayerMapBoardDTO> playerMapBoardDtos = new ArrayList();
            int counter = 0;
            List<String> arrnames = new ArrayList(playerMaps.keySet());
            Collections.sort(arrnames, String.CASE_INSENSITIVE_ORDER);
            for (String mapnames : arrnames) {
                if (counter >= iterator && counter <= iterator + playerMapsCapAddition) {
                    PlayerMapBoardDTO mapBoardDto = new PlayerMapBoardDTO(steamid);
                    Map.Entry<String, Integer> getMapnameAndStage = GetMapnameAndStage(mapnames);
                    String mapName = getMapnameAndStage.getKey();
                    int stage = (getMapnameAndStage.getValue());
                    MapValues mapvalues = playerMaps.get(mapnames);
                    mapBoardDto.addMapValues(mapvalues, mapName, stage);
                    playerMapBoardDtos.add(mapBoardDto);
                }
                if (counter > iterator + playerMapsCapAddition) {
                    break;
                }
                counter++;
            }
            return playerMapBoardDtos;
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
            return null;
        }
    }

    public BadgesDTO getPlayerBadges(String steamID) {
        return new BadgesDTO(playerCache.get(steamID));
    }

    public PlayerDTO getPlayer(String steamid) {
        if (playerCache.get(steamid).getAvatar() == null) {
            Collection<Player> playerAvatarsCaching = new ArrayList();
            playerAvatarsCaching.add(playerCache.get(steamid));
            updateAvatarsToCache(playerAvatarsCaching);
        }
        return new PlayerDTO(playerCache.get(steamid));
    }

    public Collection<PlayerDTO> getleaderBoard(int iterator) {
        int playerAddiditionCap = 99;
        Collection<Player> playerAvatarsCaching = new ArrayList();
        Collection<PlayerDTO> playersDTO = new ArrayList();
        int counter = 0;
        for (Player player : playerCache.values()) {
            if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
                if (player.getAvatar() == null || player.getAvatar().isEmpty()) {
                    playerAvatarsCaching.add(player);
                }
            }
            if (counter > iterator + playerAddiditionCap) {
                break;
            }
            counter++;
        }
        if (!playerAvatarsCaching.isEmpty()) {
            updateAvatarsToCache(playerAvatarsCaching);
        }
        counter = 0;
        for (Player player : playerCache.values()) {
            if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
                playersDTO.add(new PlayerDTO(player));
            }
            if (counter > iterator + playerAddiditionCap) {
                break;
            }
            counter++;
        }
        return playersDTO;
    }

    public Collection<PlayerDTOMini> getleaderBoardMinified(int iterator) {
        int playerAddiditionCap = 99;
        Collection<PlayerDTOMini> playersDTO = new ArrayList();
        int counter = 0;
        for (Player player : playerCache.values()) {
            if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
                playersDTO.add(new PlayerDTOMini(player.getName(), player.getPlayerPoints()));
            }
            if (counter > iterator + playerAddiditionCap) {
                break;
            }
            counter++;
        }
        return playersDTO;
    }

    private ConcurrentMap<String, Player> addNewPlayerValuesToCache(Collection<Player> keyCollection, ConcurrentMap<String, Collection<Integer>> playerUrlBanners) {
        //2 = user, 6 = mapper,  7 = admin, 8 = technical staff, 10 = leader,  12 = vip, 13 = trial admin, 15 = event winner, 19 = discord manager
        // 20 NSFW, 21 = Retired admin, 25 = event manager, 11 = GA
        ConcurrentMap<String, Player> playersConcurrentMap = new MapMaker().concurrencyLevel(6).makeMap();
        for (Player player : keyCollection) {
            Collection<Integer> banners = playerUrlBanners.getOrDefault(player.getSteamID(), null);
            if (banners != null) {
                List<UrlBanners> urlBanners = new ArrayList();
                //switch statement did not want to respond
                for (int bannerID : banners) {
                    String bannerName = "";
                    String bannerURL = "";
                    if (bannerID == 2) {
                        bannerName = "User";
                        bannerURL = "https://unloze.com/images/badges/Member_Badge.png";
                    } else if (bannerID == 6) {
                        bannerName = "Mapper";
                        bannerURL = "https://unloze.com/images/badges/Mapper_Badge.png";
                    } else if (bannerID == 7) {
                        bannerName = "Admin";
                        bannerURL = "https://unloze.com/images/badges/Admin_Badge.png";
                    } else if (bannerID == 8) {
                        bannerName = "Technical Staff";
                        bannerURL = "https://unloze.com/images/badges/Senior-Developer_Badge.png";
                    } else if (bannerID == 10) {
                        bannerName = "Leader";
                        bannerURL = "https://unloze.com/images/badges/Leader_Badge.png";
                    } else if (bannerID == 11) {
                        bannerName = "Global Admin";
                        bannerURL = "https://unloze.com/images/badges/Senior-Admin_Badge.png";
                    } else if (bannerID == 12) {
                        bannerName = "VIP";
                        bannerURL = "https://unloze.com/images/badges/VIP_Badge.png";
                    } else if (bannerID == 13) {
                        bannerName = "Trial Admin";
                        bannerURL = "https://unloze.com/images/badges/Junior-Admin_Badge.png";
                    } else if (bannerID == 19) {
                        bannerName = "Discord Manager";
                        bannerURL = "https://unloze.com/images/badges/Discord-Manager.png";
                    } else if (bannerID == 21) {
                        bannerName = "Retired Admin";
                        bannerURL = "https://unloze.com/images/badges/Retired-Admin_Badge.png";
                    } else if (bannerID == 25) {
                        bannerName = "Event Manager";
                        bannerURL = "https://unloze.com/images/badges/Event-Manager.png";
                    } else if (bannerID == 29) {
                        bannerName = "Veteran Admin";
                        bannerURL = "https://unloze.com/images/badges/Veteran_Badge.png";
                    }
                    if (!bannerName.isEmpty() && !bannerURL.isEmpty()) {
                        urlBanners.add(new UrlBanners(bannerName, bannerURL));
                    }
                }
                player.setUrlBanners(urlBanners);
            }
            playersConcurrentMap.put(player.getSteamID(), player);
        }
        return playersConcurrentMap;
    }

    private void retrieveAvatarFull(String avatarurl) {
        try {
            Callable<Entry<String, String>> worker;
            final ConcurrentMap<Integer, Future<Entry<String, String>>> futures = new MapMaker().concurrencyLevel(6).makeMap();
            URL url = new URL(avatarurl);
            JSONTokener tokener = new JSONTokener(url.openStream());
            JSONObject root = new JSONObject(tokener);
            JSONObject jsonObject = root.getJSONObject("response");
            JSONArray jsonArray = jsonObject.getJSONArray("players");
            for (int incrementer = 0; incrementer < jsonArray.length(); incrementer++) {
                JSONObject jsonObjectPlayer = jsonArray.getJSONObject(incrementer);
                try {
                    worker = new CallableJsonObject(jsonObjectPlayer);
                    futures.put(incrementer, executor.submit(worker));
                } catch (Exception ex) {
                    //some are simply nosteamers with no steam data
                }
            }
            futures.values().forEach(future -> {
                try {
                    Entry<String, String> getEntry = future.get(5, TimeUnit.SECONDS);
                    playerCache.get(getEntry.getKey()).setAvatar(getEntry.getValue());
                } catch (InterruptedException | ExecutionException | TimeoutException ex) {
                    Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
                }
            });
        } catch (MalformedURLException ex) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateAvatarsToCache(Collection<Player> players) {
        //http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=262F3F0C6B83E0F263C272C3762002F1&steamids=76561198029832363,76561198003907342
        StringBuilder avatarURL = new StringBuilder();
        avatarURL.append("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=262F3F0C6B83E0F263C272C3762002F1&steamids=");
        boolean init = true;
        for (Player player : players) {
            if (!init) {
                avatarURL.append(",");
            }
            avatarURL.append(Long.toString(convertSteamIdToCommunityId(player.getSteamID())));
            init = false;
        }
        retrieveAvatarFull(avatarURL.toString());
        setNonExistingAvatarOnPlayers(players);
    }

    private void setNonExistingAvatarOnPlayers(Collection<Player> players) {
        for (Player player : players) {
            String avatar = player.getAvatar();
            if (avatar == null) {
                playerCache.get(player.getSteamID()).setAvatar("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg");
            }
        }
    }

    public static long convertSteamIdToCommunityId(String steamId) {
        if (steamId.matches("^STEAM_[0-1]:[0-1]:[0-9]+$")) {
            String[] tmpId = steamId.substring(8).split(":");
            return Long.valueOf(tmpId[0]) + Long.valueOf(tmpId[1]) * 2 + 76561197960265728L; //76561197960265728L
        } else if (steamId.matches("^\\[U:[0-1]:[0-9]+\\]+$")) {
            String[] tmpId = steamId.substring(3, steamId.length() - 1).split(":");
            return Long.valueOf(tmpId[0]) + Long.valueOf(tmpId[1]) + 76561197960265728L; //76561197960265727L
        }
        return 0;
    }

    private void addNewMapBoardValuesToCache(Collection<MapBoard> value) {
        for (MapBoard mapboard : value) {
            mapBoardCache.put(mapboard.getMapName(), mapboard);
        }
    }

    private void orderingMapBoardValuesByTime() {
        Collection<MapBoard> mapboards = mapBoardCache.values();
        for (MapBoard mapboard : mapboards) {
            //System.out.println("mapName: " + mapboard.getMapName());
            List<MapValues> mapvalues = new ArrayList();
            for (MapValues mv : mapboard.getMapvalues()) {
                mapvalues.add(mv);
            }
            mapvalues.sort(Comparator.comparing(MapValues::getTime));
            mapBoardCache.get(mapboard.getMapName()).setMapvalues(mapvalues);
        }
    }

    private void setPositionAndMapPoints() {
        for (MapBoard mapboard : mapBoardCache.values()) {
            List<MapValues> mapvalues = mapboard.getMapvalues();
            int playerCount = mapvalues.size();
            //2024 edit, its been a damn long time ago this all was made
            //editing so top 5% and top 1% of finisher each get an extra boost.
            int FivePercent = 0;
            int OnePercent = 0;
            if (playerCount >= 100) {
                FivePercent = (int) Math.floor(playerCount * 0.05);
                OnePercent = (int) Math.floor(playerCount * 0.01);
            }
            int position = 1;

            //suggested by roko. cause in rare cases a guy in top 1% or 5% has the same time as a guy right behind him not getting the percentage boost.
            //if rank 10 and rank 11 both have a time of 10.546 maybe only rank 10 gets the 1% boost but not rank 11. this is meant to give rank 11 the boost too.
            float previousTimeOnePercent = 0.0f;
            float previousTimeFivePercent = 0.0f;
            for (MapValues mapvalue : mapvalues) {
                mapvalue.setPosition(position);
                int Points = playerCount - position;
                if (playerCount <= 250) {
                    //if less than 250 people finished a time i also want to give them each an additional 2500 points. 
                    //that is to make up hard timers that not many people actually finish so they are considered worth more.
                    //for example race mode in mako and race timers in jump king should be rewarded a lot more due to their difficulties.
                    Points += 2500;
                }
                if (position <= OnePercent) {
                    Points *= 4;
                    previousTimeOnePercent = mapvalue.getTime(); //eventually is set for the last guy getting 1% boost.
                } else if (position <= FivePercent) {
                    previousTimeFivePercent = mapvalue.getTime(); //eventually is set for the last guy getting 5% boost.
                    if (previousTimeFivePercent == previousTimeOnePercent) {
                        //if a guy is listed as part of 5% boost but has the exact same time as the last guy getting 1% boost give him the 1% boost benefit too.
                        Points *= 4;
                    } else {
                        Points *= 2;
                    }
                }
                else if (previousTimeFivePercent == mapvalue.getTime()) {
                    //if the last guy getting 5% boost has a time of 10.654 and the next guy also has a time of 10.654 give him the boost too.
                    Points *= 2;
                }
                mapvalue.setMapPoints(Points);
                position++;
            }
        }
    }

    private List<Player> setPlayerPoints(ConcurrentMap<String, Player> playersMap) {
        for (MapBoard mapboard : mapBoardCache.values()) {
            List<MapValues> mapvalues = mapboard.getMapvalues();
            for (MapValues mapvalue : mapvalues) {
                String steamID = mapvalue.getPlayerSteamID();
                int mapPoints = mapvalue.getMapPoints();
                playersMap.get(steamID).addPlayerPoints(mapPoints);
            }
        }
        return new ArrayList(playersMap.values());
    }

    private void updatePlayerRanks(List<Player> playersConcurrentmap) {
        int rank = 1;
        playersConcurrentmap.sort(Comparator.comparing(Player::getPlayerPoints).reversed());
        for (Player player : playersConcurrentmap) {
            player.setRank(rank);
            playerCache.put(player.getSteamID(), player);
            rank++;
        }
    }

    public void setTimesAsociatedToPeople() {
        Collection<MapBoard> mapboards = mapBoardCache.values();
        for (MapBoard mapboard : mapboards) {
            List<MapValues> mapvalues = mapboard.getMapvalues();
            for (MapValues mapvalue : mapvalues) {
                ConcurrentMap<String, MapValues> playerMapValueCache = playerRelatedMapValuesCache.getOrDefault(mapvalue.getPlayerSteamID(), null);
                if (playerMapValueCache == null) {
                    ConcurrentMap<String, MapValues> localMap = new MapMaker().concurrencyLevel(4).makeMap();
                    localMap.put(mapboard.getMapName(), mapvalue);
                    playerRelatedMapValuesCache.put(mapvalue.getPlayerSteamID(), localMap);
                } else {
                    playerMapValueCache.put(mapboard.getMapName(), mapvalue);
                    playerRelatedMapValuesCache.put(mapvalue.getPlayerSteamID(), playerMapValueCache);
                }
            }
        }
    }

    public String checkTimeElapse() {
        Instant current = Instant.now();
        long timeElapsed = Duration.between(elapsedTime, current).toMillis();
        int cachesize = playerCache.size();
        if (timeElapsed >= EXPIRE_TIME_IN_SECONDS * 1000 || playerCache.isEmpty()) {
            String elapsed = String.valueOf(timeElapsed);
            elapsedTime = Instant.now();
            Entry<Collection<Player>, Collection<MapBoard>> playersMapBoards = DataMapperCalls.getAllValues();
            Collection<MapBoard> mapBoardCollection = playersMapBoards.getValue();
            ConcurrentMap<String, Collection<Integer>> playerRelatedUrlBanners = DataMapperCalls.getURLBanners();
            playerCache.clear();
            mapBoardCache.clear();
            playerRelatedMapValuesCache.clear();
            addNewMapBoardValuesToCache(mapBoardCollection);
            ConcurrentMap<String, Player> playersConcurrentmap = addNewPlayerValuesToCache(playersMapBoards.getKey(), playerRelatedUrlBanners);
            orderingMapBoardValuesByTime();
            setPositionAndMapPoints();
            updatePlayerRanks(setPlayerPoints(playersConcurrentmap));
            setTimesAsociatedToPeople();
            //updateEntitiesPersistence();
            //System.out.println("timeElapsed: " + elapsed + "\nEXPIRE_TIME_IN_SECONDS: " + EXPIRE_TIME_IN_SECONDS * 1000 + "cachesize: " + cachesize);
            //return "timeElapsed: " + elapsed + "\nEXPIRE_TIME_IN_SECONDS: " + EXPIRE_TIME_IN_SECONDS * 1000 + "cachesize: " + cachesize;
        }
        return "";
    }

    public void updateEntitiesPersistence() {
        EntityManager em = PuSelector.getEntityManagerFactory("pu").createEntityManager();
        em.getTransaction().begin();
        em.createQuery("delete from MapBoard").executeUpdate();
        em.createQuery("delete from MapValues").executeUpdate();
        em.createQuery("delete from Player").executeUpdate();
        em.createQuery("delete from UrlBanners").executeUpdate();
        Collection<Player> players = playerCache.values();
        Collection<MapBoard> mapBoards = mapBoardCache.values();
        for (Player player : players) {
            em.persist(player);
            List<UrlBanners> urlBanners = player.getUrlBanners();
            for (UrlBanners urlB : urlBanners) {
                em.persist(urlB);
            }
        }
        for (MapBoard mapboard : mapBoards) {
            em.persist(mapboard);
        }
        em.getTransaction().commit();
    }

    public List<Player> getAllPlayersFromCache() {
        checkTimeElapse();
        return new ArrayList(playerCache.values());
    }

    public Collection<MapBoard> getAllMapBoardsFromCache() {
        checkTimeElapse();
        return mapBoardCache.values();
    }

}
