public void updateHitScore(String playerToken, int level, int score) {

        PersistenceManager pm = pmfInstance.getPersistenceManager();
        try {
            Player player = pm.getObjectById(Player.class, playerToken);

            if (player != null) {
                Game game = pm.getObjectById(Game.class, player.getGameId());

                Key key = KeyFactory.createKey(PlayerScores.class.getSimpleName(), game.getId() + "@" + playerToken + ":" + level);

                PlayerScores hit;
                try {
                    hit = pm.getObjectById(PlayerScores.class, key);
                } catch (JDOObjectNotFoundException ex) {
                    hit = new PlayerScores();
                    hit.setId(key);
                    hit.setLevel(level);
                    hit.setLevelScore(score);
                    pm.makePersistent(hit);
                }

                if (hit.getLevelScore() < score) {
                    hit.setLevel(score);
                }

                Key gameHitKey = KeyFactory.createKey(GameHitScore.class.getSimpleName(), game.getId() + ":" + level);
                GameHitScore gameHit;
                try {
                    gameHit = pm.getObjectById(GameHitScore.class, gameHitKey);
                    if (gameHit.getLevelScore() < score) {
                        gameHit.setLevelScore(score);
                        gameHit.setPlayerName(player.getPlayerName());
                    }
                } catch (JDOObjectNotFoundException ex) {
                    gameHit = new GameHitScore();
                    gameHit.setId(gameHitKey);
                    gameHit.setGameToken(game.getToken());
                    gameHit.setLevel(level);
                    gameHit.setLevelScore(score);
                    gameHit.setPlayerName(player.getPlayerName());
                    pm.makePersistent(gameHit);
                }


                if (gameHit.getLevelHits() == null) {
                    gameHit.setLevelHits(0);
                }
                gameHit.setLevelHits(gameHit.getLevelHits() + 1);

                Long levelHits = game.getLevelHits();
                if (levelHits == null) {
                    levelHits = 0L;
                }
                levelHits++;
                game.setLevelHits(levelHits);

                Calendar midnight = Calendar.getInstance();
                midnight.set(Calendar.HOUR, 0);
                midnight.set(Calendar.MINUTE, 0);
                midnight.set(Calendar.SECOND, 0);
                midnight.set(Calendar.MILLISECOND, 0);
                long time = midnight.getTimeInMillis();
                Key gameActivityKey = KeyFactory.createKey(GameActivity.class.getSimpleName(), game.getId() + time);
                GameActivity activity;
                try {
                    activity = pm.getObjectById(GameActivity.class, gameActivityKey);
                    activity.setHits(activity.getHits() + 1);
                } catch (JDOObjectNotFoundException ex) {
                    activity = new GameActivity();
                    activity.setId(gameActivityKey);
                    activity.setHits(1);
                    activity.setGameToken(game.getId());
                    activity.setDate(time);
                    pm.makePersistent(activity);
                }
            }

        } finally {
            pm.close();
        }

    }