Advertisement
renix1

Highscore updated

Aug 11th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from getpass import getuser
  2. from os.path import exists
  3. import time
  4.  
  5.  
  6. FILENAME = 'pontuacao.huebr'
  7.  
  8.  
  9. class Highscore(object):
  10.     def __init__(self):
  11.         self.clock = time.strftime('%H:%M:%S')
  12.         self.date = time.strftime('%d/%m/%Y')
  13.         self.user = getuser()
  14.         self.__check()
  15.  
  16.     def __repr__(self, score):
  17.         return 'Usuário {} pontuou {} às {} em {}'.format(self.user, score, self.clock, self.date)
  18.  
  19.     @staticmethod
  20.     def __create():
  21.         """
  22.            Create the file
  23.        """
  24.         bits_init = '{}'.format(map(ord, 'RENI')).strip('[]').replace(',', '').replace(' ', '')
  25.         with open(FILENAME, 'wb') as f:
  26.             f.write(bits_init)
  27.         return True if exists('scores.huebr') else False
  28.  
  29.  
  30.     def __check(self):
  31.         """
  32.            Check if exists on the current path
  33.        """
  34.         if exists(FILENAME):
  35.             if not self.__check_header():
  36.                 quit(-1)
  37.         else:
  38.             self.__create()
  39.  
  40.  
  41.     def __check_header(self):
  42.         """
  43.            Check if header is valid, if wasn't exit with code error -1
  44.        """
  45.         buffer = open(FILENAME, 'rb').read(8)
  46.         buffer = ' '.join([buffer[i:i + 2] for i in range(0, len(buffer), 2)]).split(' ')
  47.         buffer = ''.join([chr(int(c)) for c in buffer])
  48.         return 1 if buffer == 'RENI' else 0
  49.  
  50.  
  51.     def check_score(self, score):
  52.         pass
  53.  
  54.  
  55.     def evaluate(self, score):
  56.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement