Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import getpass, string, time, win32con, win32api
- from threading import Thread
- from uuid import getnode as getMacAddress
- from random import randrange
- gameEnded = False
- passWordDict,currMoneyDict,hasPrevGameDict, currRecordDict, highScoreDict,userNameList,timeDict = {},{},{},{},{},[],{}
- inGame = False
- alphapuncList = [x for x in string.ascii_lowercase] + [x for x in string.punctuation] + [' ']
- file = 'UserAccounts.csv'
- def isFloat(num):
- if num == '':
- return False
- newList = [x for x in string.ascii_lowercase] + [x for x in string.punctuation if x != '.'] + [' ']
- num = str(num)
- for char in num:
- if char in newList:
- return False
- if num.count('.') > 1:
- return False
- return True
- def dicts():
- resetFile()
- with open(file,'r+') as f:
- readOnly()
- for line in f:
- myList = line.strip().split(',')
- userName = myList[0]
- userNameList.append(userName)
- 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]
- def startGame():
- dicts()
- #print(passWordDict,currMoneyDict,hasPrevGameDict, currRecordDict, highScoreDict,userNameList)
- userStatus = input("Login to existing profile or Create new profile? (Enter 'Login' or 'NewUser') ").lower().strip()
- while userStatus != 'newuser' and userStatus != 'login':
- userStatus = input("Enter 'Login' or 'NewUser' ").lower().strip()
- if userStatus == 'newuser':
- newUser()
- elif userStatus == 'login':
- login()
- def newUser():
- username = input("Enter username: ").strip()
- while username in userNameList:
- username = input("Username is taken. Enter a new one: ").strip()
- password = getpass.getpass("Enter your password")
- password = hasher(username,password)
- mac = getMacAddress()
- macAddress = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
- resetFile()
- with open(file,'a+') as f:
- f.write("{},{},1000,0,0,0,0\n".format(username,password))
- readOnly()
- passWordDict[username],currMoneyDict[username],hasPrevGameDict[username],currRecordDict[username],highScoreDict[username],timeDict[username] = password,1000,0,'0/0',0,0
- userNameList.append(username)
- print("User Created")
- diceGame(username)
- def secToTime(sec):
- sec = float(sec)
- hour = int(sec/3600)
- sec -= hour*3600
- minute = int(sec/60)
- sec -= minute*60
- return ("{} hour(s), {} minute(s), {} second(s)".format(hour, minute, round(sec,1)))
- def login():
- username = input("Enter your username: ")
- while username not in passWordDict:
- print("Unknown username")
- username = input("Enter your username: ")
- password = getpass.getpass("Enter your password:")
- if int(passWordDict[username]) == hasher(username,password):
- if time.time() - float(timeDict[username]) <= 86400:
- print("It hasn't been 1 day! Please wait another {}".format(secToTime(86400-(time.time() - float(timeDict[username])))))
- else:
- print('Welcome back {}!'.format(username))
- diceGame(username)
- else:
- print("Wrong Password. Try Again")
- login()
- def hasher(username,password): #need to hash with a seed that doesnt change.
- seed = sum([ord(x) for x in username])
- hashed = int("".join([str(ord(x)) for x in password]))
- div = int("{}{}".format(ord(username[0]),ord(password[0])))
- hashed = round((hashed/div)*seed)
- if hashed == 0:
- hashed = round(((seed/div)*seed**2)*4)
- return hashed
- def validRoll(num):
- if not num.isnumeric():
- return False
- elif int(num) < 1 or int(num) > 6:
- return False
- return True
- def validMoney(num,money):
- if not isFloat(num):
- return False
- elif float(num) > money:
- return False
- return True
- def resetFile():
- win32api.SetFileAttributes(file, win32con.FILE_ATTRIBUTE_NORMAL)
- def readOnly():
- win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_HIDDEN)
- win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_READONLY)
- def logout(username,money,totalMoneyWon):
- currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username] = money,totalMoneyWon,1
- resetFile()
- with open(file,'w') as f:
- for name in userNameList:
- f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
- readOnly()
- def logoutEnd(username,totalMoneyWon,gameEndTime):
- currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username],timeDict[username] = 1000,totalMoneyWon,0,gameEndTime
- if totalMoneyWon > int(highScoreDict[username]):
- highScoreDict[username] == totalMoneyWon
- resetFile()
- with open(file,'w') as f:
- for name in userNameList:
- f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
- readOnly()
- def displayHighScore():
- pass
- def autosave():
- while True:
- if validSave:
- time.sleep(1)
- currMoneyDict[username],currRecordDict[username],hasPrevGameDict[username] = currUserMoney,currScore,1
- resetFile()
- with open(file,'w') as f:
- for name in userNameList:
- f.write("{},{},{},{},{},{},{}\n".format(name,passWordDict[name],currMoneyDict[name],hasPrevGameDict[name],currRecordDict[name],highScoreDict[name],timeDict[name]))
- print("\nAutoSaved\n\n")
- readOnly()
- def diceGame(username):
- backGroundAutoSave.start()
- global userName
- global currUserMoney
- global currScore
- global validSave
- validSave = False
- userName = username
- inGame = True
- totalMoneyWon = 0
- print("To log out type 'logout' when asking for your bet on the number.\nTo display highscores, type 'highscores' instead.\n")
- if hasPrevGameDict[username] == 1:
- print("From last game your score is {}".format(currRecordDict[username]))
- totalMoneyWon = float(currRecordDict[username])
- money = currMoneyDict[username]
- while money > 0:
- print("You have ${}\n".format(money))
- correct = randrange(1,7)
- betRoll = input("What number would you like to bet on? ").strip()
- if betRoll.lower() == 'logout':
- logout(username,money,totalMoneyWon)
- break
- if betRoll.lower() == 'highscores':
- displayHighScore()
- betRoll = input("\nWhat number would you like to bet on? ").strip()
- while not validRoll(betRoll):
- print("Enter a number between 1 and 6 inclusive")
- betRoll = input("What number would you like to bet on? ").strip()
- betMoney = input("How much would you like to bet ").strip()
- while not validMoney(betMoney,money):
- print("Enter a number less than ${}".format(money))
- betMoney = input("How much would you like to bet ").strip()
- betRoll = int(betRoll)
- betMoney = float(betMoney)
- if betRoll == correct:
- print('\nYou got it!')
- money += betMoney * 3
- currMoneyDict[username] = money
- totalMoneyWon += betMoney * 3
- else:
- print("\nThat wasn't it!\nCorrect number was {}".format(correct))
- money -= betMoney
- currMoneyDict[username] = money
- currUserMoney = money
- currScore = totalMoneyWon
- validSave = True
- print('You are out of money. Your final score is {}'.format(totalMoneyWon))
- print("See you in 1 day!")
- logoutEnd(username,totalMoneyWon,time.time())
- if totalMoneyWon > highScoreDict[username]:
- highScoreDict[username] = totalMoneyWon
- def main():
- global backGroundAutoSave
- backGroundAutoSave = Thread(target=autosave)
- try:
- startGame()
- except KeyboardInterrupt:
- if inGame:
- logout(userName,currUserMoney,currScore)
- raise KeyboardInterrupt
- else:
- raise KeyboardInterrupt
- resetFile()
- input("Press Enter to Exit")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement