Package com.xmage.core.entity.model

Examples of com.xmage.core.entity.model.ServerStats


        return StaticHolder.instance;
    }

    public JSONObject buildFrom(Resource<ServerStats> resource) {
       
        ServerStats serverStats = resource.getDefault();

        JSONObject statsJson = new JSONObject();

        statsJson.put("numberOfGamesPlayed", serverStats.getNumberOfGamesPlayed());
        statsJson.put("numberOfUniquePlayers", serverStats.getNumberOfUniquePlayers());
        statsJson.put("numberOfPlayersPlayedOnlyOnce", serverStats.getNumberOfPlayersPlayedOnce());
        statsJson.put("top3Players", serverStats.getTop3Players());

        return statsJson;
    }
View Full Code Here


        defaultResource = event;
    }

    public Resource getAll() {
        try {
            ServerStats serverStats = xmageStatsRepository.getServerStats();
            if (serverStats != null) {
                defaultResource = serverStats;
            } else {
                error = DomainErrors.Errors.STATUS_NOT_FOUND;
            }
View Full Code Here

    private static final Logger logger = LoggerFactory.getLogger(XMageStatsRepositoryImpl.class);

    @Override
    public ServerStats getServerStats() {
        ServerStats serverStats = new ServerStats();

        List<Log> logs = EntityManager.instance.getAllLogs();
        logger.info("logs found count: " + logs.size());

        int numberOfGamesPlayed = 0;
        Set<String> playerNames = new HashSet<String>();

        // Get nicknames and games started count
        Map<String, Integer> nicknames = new HashMap<String, Integer>();
        for (Log log : logs) {
            if (log.getKey().equals(LogKeys.KEY_GAME_STARTED)) {
                if (log.getArguments() != null) {
                    int index = 0;
                    for (String argument : log.getArguments()) {
                        if (index > 0) {
                            inc(nicknames, argument);
                        }
                        index++;
                    }
                }
                numberOfGamesPlayed++;
            }
        }

        // Sort games
        Collection<Integer> values = nicknames.values();
        List<Integer> games = new ArrayList<Integer>();
        games.addAll(values);
        Collections.sort(games, new Comparator<Integer>() {
            @Override
            public int compare(Integer i1, Integer i2) {
                return i2.compareTo(i1);
            }
        });

        // Top-3
        List<Integer> numbersToFind = new ArrayList<Integer>();
        for (Integer numberOfGames : games) {
            numbersToFind.add(numberOfGames);
            if (numbersToFind.size() == 3) {
                break;
            }
        }

        Map<Integer, String> players = new LinkedHashMap<Integer, String>();
        for (Integer number : numbersToFind) {
            for (Map.Entry<String, Integer> entry : nicknames.entrySet()) {
                if (entry.getValue().equals(number)) {
                    players.put(entry.getValue(), entry.getKey());
                    break;
                }
            }
            if (players.size() == 3) {
                break;
            }
        }

        // Build top-3 string
        StringBuilder top3 = new StringBuilder();
        for (Map.Entry<Integer, String> entry : players.entrySet()) {
            top3.append("[").append(entry.getValue()).append(":").append(entry.getKey()).append("]");
        }

        // Played only once
        Integer oneGamePlayers = 0;
        for (Integer numberOfGames : games) {
            if (numberOfGames == 1) {
                oneGamePlayers++;
            }
        }

        serverStats.setNumberOfGamesPlayed(numberOfGamesPlayed);
        serverStats.setNumberOfUniquePlayers(nicknames.size());
        serverStats.setTop3Players(top3.toString());
        serverStats.setNumberOfPlayersPlayedOnce(oneGamePlayers);

        return serverStats;
    }
View Full Code Here

TOP

Related Classes of com.xmage.core.entity.model.ServerStats

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.