Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. import getpass, string, time, win32con, win32api
  2. from threading import Thread
  3. from uuid import getnode as getMacAddress
  4. from random import randrange
  5. gameEnded = False
  6. passWordDict,currMoneyDict,hasPrevGameDict, currRecordDict, highScoreDict,userNameList,timeDict = {},{},{},{},{},[],{}
  7. inGame = False
  8. alphapuncList = [x for x in string.ascii_lowercase] + [x for x in string.punctuation] + [' ']
  9. file = 'UserAccounts.csv'
  10. def isFloat(num):
  11. if num == '':
  12. return False
  13. newList = [x for x in string.ascii_lowercase] + [x for x in string.punctuation if x != '.'] + [' ']
  14. num = str(num)
  15. for char in num:
  16. if char in newList:
  17. return False
  18. if num.count('.') > 1:
  19. return False
  20. return True
  21.  
  22. def dicts():
  23. resetFile()
  24. with open(file,'r+') as f:
  25. readOnly()
  26. for line in f:
  27. myList = line.strip().split(',')
  28. userName = myList[0]
  29. userNameList.append(userName)
  30. passWordDict[userName],currMoneyDict[userName],hasPrevGameDict[userName],currRecordDict[userName],highScoreDict[userName],timeDict[userName] = myList[1],float(myList[2]),int(myList[3]),myList[4],float(myList[5]),myList[6]
  31.  
  32. def startGame():
  33. dicts()
  34. #print(passWordDict,currMoneyDict,hasPrevGameDict, currRecordDict, highScoreDict,userNameList)
  35. userStatus = input("Login to existing profile or Create new profile? (Enter 'Login' or 'NewUser') ").lower().strip()
  36. while userStatus != 'newuser' and userStatus != 'login':
  37. userStatus = input("Enter 'Login' or 'NewUser' ").lower().strip()
  38. if userStatus == 'newuser':
  39. newUser()
  40. elif userStatus == 'login':
  41. login()
  42.  
  43.  
  44. def newUser():
  45. username = input("Enter username: ").strip()
  46. while username in userNameList:
  47. username = input("Username is taken. Enter a new one: ").strip()
  48. password = getpass.getpass("Enter your password")
  49. password = hasher(username,password)
  50. mac = getMacAddress()
  51. macAddress = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
  52. resetFile()
  53. with open(file,'a+') as f:
  54. f.write("{},{},1000,0,0,0,0\n".format(username,password))
  55. readOnly()
  56. passWordDict[username],currMoneyDict[username],hasPrevGameDict[username],currRecordDict[username],highScoreDict[username],timeDict[username] = password,1000,0,'0/0',0,0
  57. userNameList.append(username)
  58. print("User Created")
  59. diceGame(username)
  60.  
  61. def secToTime(sec):
  62. sec = float(sec)
  63. hour = int(sec/3600)
  64. sec -= hour*3600
  65. minute = int(sec/60)
  66. sec -= minute*60
  67. return ("{} hour(s), {} minute(s), {} second(s)".format(hour, minute, round(sec,1)))
  68.  
  69. def login():
  70. username = input("Enter your username: ")
  71. while username not in passWordDict:
  72. print("Unknown username")
  73. username = input("Enter your username: ")
  74. password = getpass.getpass("Enter your password:")
  75. if int(passWordDict[username]) == hasher(username,password):
  76. if time.time() - float(timeDict[username]) <= 86400:
  77. print("It hasn't been 1 day! Please wait another {}".format(secToTime(86400-(time.time() - float(timeDict[username])))))
  78. else:
  79. print('Welcome back {}!'.format(username))
  80. diceGame(username)
  81. else:
  82. print("Wrong Password. Try Again")
  83. login()
  84.  
  85.  
  86.  
  87. def hasher(username,password): #need to hash with a seed that doesnt change.
  88. seed = sum([ord(x) for x in username])
  89. hashed = int("".join([str(ord(x)) for x in password]))
  90. div = int("{}{}".format(ord(username[0]),ord(password[0])))
  91. hashed = round((hashed/div)*seed)
  92. if hashed == 0:
  93. hashed = round(((seed/div)*seed**2)*4)
  94. return hashed
  95.  
  96. def validRoll(num):
  97. if not num.isnumeric():
  98. return False
  99. elif int(num) < 1 or int(num) > 6:
  100. return False
  101. return True
  102.  
  103. def validMoney(num,money):
  104. if not isFloat(num):
  105. return False
  106. elif float(num) > money:
  107. return False
  108. return True
  109.  
  110. def resetFile():
  111. win32api.SetFileAttributes(file, win32con.FILE_ATTRIBUTE_NORMAL)
  112. def readOnly():
  113. win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_HIDDEN)
  114. win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_READONLY)
  115.  
  116. def logout(username,money,totalMoneyWon):
  117. currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username] = money,totalMoneyWon,1
  118. resetFile()
  119. with open(file,'w') as f:
  120. for name in userNameList:
  121. f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
  122. readOnly()
  123.  
  124. def logoutEnd(username,totalMoneyWon,gameEndTime):
  125. currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username],timeDict[username] = 1000,totalMoneyWon,0,gameEndTime
  126. if totalMoneyWon > int(highScoreDict[username]):
  127. highScoreDict[username] == totalMoneyWon
  128. resetFile()
  129. with open(file,'w') as f:
  130. for name in userNameList:
  131. f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
  132. readOnly()
  133.  
  134.  
  135. def displayHighScore():
  136. pass
  137.  
  138. def autosave():
  139. while True:
  140. if validSave:
  141. time.sleep(1)
  142. currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username] = currUserMoney,currScore,1
  143. resetFile()
  144. with open(file,'w') as f:
  145. for name in userNameList:
  146. f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
  147. print("\nAutoSaved\n\n")
  148. readOnly()
  149.  
  150. def diceGame(username):
  151. backGroundAutoSave.start()
  152. global userName
  153. global currUserMoney
  154. global currScore
  155. global validSave
  156. validSave = False
  157. userName = username
  158. inGame = True
  159. totalMoneyWon = 0
  160. print("To log out type 'logout' when asking for your bet on the number.\nTo display highscores, type 'highscores' instead.\n")
  161. if hasPrevGameDict[username] == 1:
  162. print("From last game your score is {}".format(currRecordDict[username]))
  163. totalMoneyWon = float(currRecordDict[username])
  164. money = currMoneyDict[username]
  165. while money > 0:
  166.  
  167. print("You have ${}\n".format(money))
  168. correct = randrange(1,7)
  169.  
  170. betRoll = input("What number would you like to bet on? ").strip()
  171. if betRoll.lower() == 'logout':
  172. logout(username,money,totalMoneyWon)
  173. break
  174. if betRoll.lower() == 'highscores':
  175. displayHighScore()
  176. betRoll = input("\nWhat number would you like to bet on? ").strip()
  177.  
  178. while not validRoll(betRoll):
  179. print("Enter a number between 1 and 6 inclusive")
  180. betRoll = input("What number would you like to bet on? ").strip()
  181.  
  182. betMoney = input("How much would you like to bet ").strip()
  183. while not validMoney(betMoney,money):
  184. print("Enter a number less than ${}".format(money))
  185. betMoney = input("How much would you like to bet ").strip()
  186.  
  187. betRoll = int(betRoll)
  188. betMoney = float(betMoney)
  189.  
  190. if betRoll == correct:
  191. print('\nYou got it!')
  192. money += betMoney * 3
  193. currMoneyDict[username] = money
  194. totalMoneyWon += betMoney * 3
  195.  
  196. else:
  197. print("\nThat wasn't it!\nCorrect number was {}".format(correct))
  198. money -= betMoney
  199. currMoneyDict[username] = money
  200. currUserMoney = money
  201. currScore = totalMoneyWon
  202. validSave = True
  203.  
  204.  
  205. print('You are out of money. Your final score is {}'.format(totalMoneyWon))
  206. print("See you in 1 day!")
  207. logoutEnd(username,totalMoneyWon,time.time())
  208. if totalMoneyWon > highScoreDict[username]:
  209. highScoreDict[username] = totalMoneyWon
  210.  
  211.  
  212. def main():
  213. global backGroundAutoSave
  214. backGroundAutoSave = Thread(target=autosave)
  215. try:
  216. startGame()
  217. except KeyboardInterrupt:
  218. if inGame:
  219. logout(userName,currUserMoney,currScore)
  220. raise KeyboardInterrupt
  221. else:
  222. raise KeyboardInterrupt
  223. resetFile()
  224. input("Press Enter to Exit")
  225.  
  226.  
  227.  
  228. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement