# Path: turbulenz_local/tools.py
# def get_absolute_path(directory):
#     """
#     Return absolute path by working out path relative to games root
#     """
#     if isabs(directory):
#         return directory
# 
#     return realpath(normpath(join(config['games_root'], directory)))
# 
# def create_dir(directory):
#     """
#     Create the directory structure if necessary. return 'True' if it was created
#     successfully and can be written into, 'False' otherwise.
#     """
#     if directory.strip() == '':
#         return False
#     else:
#         absDir = get_absolute_path(directory)
#         try:
#             os.makedirs(absDir)
#         except OSError as e:
#             if e.errno != EEXIST:
#                 LOG.error('Failed creating meta dir: %s', str(e))
#                 return False
#         return os.access(absDir, os.W_OK)
# 
# Path: turbulenz_local/lib/validation.py
# class ValidationException(Exception):
#     def __init__(self, issues):
#         super(ValidationException, self).__init__()
#         self.issues = issues
# 
#     def __str__(self):
#         string = ''
#         issues = self.issues
#         first = True
# 
#         for issue in issues:
#             if not first:
#                 string += '\n'
# 
#             issue_id = issue[0]
#             errors = issue[1]['errors']
#             warnings = issue[1]['warnings']
# 
#             string += 'For identifier %s:\n' % issue_id
#             for e in errors:
#                 string += '    Error  : %s\n' % e
#             for w in warnings:
#                 string += '    Warning: %s' % w
# 
#             first = False
# 
#         return string
# 
# Path: turbulenz_local/models/userlist.py
# def get_user(self, username):
#     username_lower = username.lower()
#     with self.lock:
#         try:
#             return self.users[username_lower]
#         except KeyError:
#             LOG.info('No user with username "%s" adding user with defaults', username)
#             try:
#                 user = self._add_user(username_lower)
#                 self._write_users()
#                 return user
#             except ValueError as e:
#                 raise BadRequest(str(e))


import logging
import yaml
from re import compile as regex_compile
from time import time as time_now
from os.path import exists as path_exists, join as join_path, splitext
from math import floor, ceil, isinf, isnan
from threading import Lock
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir
from turbulenz_local.lib.validation import ValidationException
from turbulenz_local.models.userlist import get_user
            if (self.sort_by == 1 and old_score >= new_score) or (self.sort_by == -1 and old_score <= new_score):
                return {'bestScore': old_score}

            users_score.score = new_score
            users_score.score_time = score_time

            if self.aggregate:
                self.aggregate_score += new_score - old_score
            self._write_leaderboard()
            return {'newBest': True, 'prevBest': old_score}
        except KeyError:
            # User has no score on the leaderboard
            self._add_score(UserScore(user.username, new_score, score_time))
            self._write_leaderboard()
            return {'newBest': True}


    def remove(self):
        self._empty_leaderboard()


class GameLeaderboards(object):

    def __init__(self, game):
        self.leaderboards = {}
        self.ordered_leaderboards = []
        self.leaderboard_path = None

        self.issues = []


# Path: turbulenz_local/tools.py
# def get_absolute_path(directory):
#     """
#     Return absolute path by working out path relative to games root
#     """
#     if isabs(directory):
#         return directory
# 
#     return realpath(normpath(join(config['games_root'], directory)))
# 
# def create_dir(directory):
#     """
#     Create the directory structure if necessary. return 'True' if it was created
#     successfully and can be written into, 'False' otherwise.
#     """
#     if directory.strip() == '':
#         return False
#     else:
#         absDir = get_absolute_path(directory)
#         try:
#             os.makedirs(absDir)
#         except OSError as e:
#             if e.errno != EEXIST:
#                 LOG.error('Failed creating meta dir: %s', str(e))
#                 return False
#         return os.access(absDir, os.W_OK)
# 
# Path: turbulenz_local/lib/validation.py
# class ValidationException(Exception):
#     def __init__(self, issues):
#         super(ValidationException, self).__init__()
#         self.issues = issues
# 
#     def __str__(self):
#         string = ''
#         issues = self.issues
#         first = True
# 
#         for issue in issues:
#             if not first:
#                 string += '\n'
# 
#             issue_id = issue[0]
#             errors = issue[1]['errors']
#             warnings = issue[1]['warnings']
# 
#             string += 'For identifier %s:\n' % issue_id
#             for e in errors:
#                 string += '    Error  : %s\n' % e
#             for w in warnings:
#                 string += '    Warning: %s' % w
# 
#             first = False
# 
#         return string
# 
# Path: turbulenz_local/models/userlist.py
# def get_user(self, username):
#     username_lower = username.lower()
#     with self.lock:
#         try:
#             return self.users[username_lower]
#         except KeyError:
#             LOG.info('No user with username "%s" adding user with defaults', username)
#             try:
#                 user = self._add_user(username_lower)
#                 self._write_users()
#                 return user
#             except ValueError as e:
#                 raise BadRequest(str(e))


# Path: turbulenz_local/models/apiv1/leaderboards.py
import logging
import yaml
from re import compile as regex_compile
from time import time as time_now
from os.path import exists as path_exists, join as join_path, splitext
from math import floor, ceil, isinf, isnan
from threading import Lock
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir
from turbulenz_local.lib.validation import ValidationException
from turbulenz_local.models.userlist import get_user

            if (self.sort_by == 1 and old_score >= new_score) or (self.sort_by == -1 and old_score <= new_score):
                return {'bestScore': old_score}

            users_score.score = new_score
            users_score.score_time = score_time

            if self.aggregate:
                self.aggregate_score += new_score - old_score
            self._write_leaderboard()
            return {'newBest': True, 'prevBest': old_score}
        except KeyError:
            # User has no score on the leaderboard
            self._add_score(UserScore(user.username, new_score, score_time))
            self._write_leaderboard()
            return {'newBest': True}


    def remove(self):
        self._empty_leaderboard()


class GameLeaderboards(object):

    def __init__(self, game):
        self.leaderboards = {}
        self.ordered_leaderboards = []
        self.leaderboard_path = None

        self.issues = []


        yaml_path = unicode(get_absolute_path(join_path(game.path, 'leaderboards.yaml')))