Ledger Works
This commit is contained in:
86
BlackJack.py
86
BlackJack.py
@@ -1,5 +1,5 @@
|
||||
import random
|
||||
|
||||
import Ledger
|
||||
|
||||
def convertNumberToCard(cardNumber):
|
||||
# Cards index from 0 i.e ace is 0, and the highest card value is 51
|
||||
@@ -62,6 +62,21 @@ def getHandTotal(hand):
|
||||
i += card
|
||||
return i
|
||||
|
||||
class BlackJackLedger(Ledger):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
def updateLedger(self, data, oldData):
|
||||
ID, money, wins, losses = oldData
|
||||
_, newMoney, newWins, newLosses = data
|
||||
|
||||
wins += newWins
|
||||
losses += newLosses
|
||||
money += newMoney
|
||||
data = [ID, money, wins, losses]
|
||||
return data
|
||||
|
||||
|
||||
|
||||
class BlackJack:
|
||||
def __init__(self, recv, send):
|
||||
@@ -76,6 +91,8 @@ class BlackJack:
|
||||
self.recv = recv
|
||||
self.send = send
|
||||
|
||||
ledger = BlackJackLedger()
|
||||
|
||||
def getPH(self):
|
||||
return self.playerHand
|
||||
|
||||
@@ -131,18 +148,13 @@ class BlackJack:
|
||||
gameOver = True
|
||||
return gameOver
|
||||
|
||||
async def play_game(self, bet):
|
||||
async def play_game(self, ID, bet):
|
||||
validInput = False
|
||||
gameOver = False
|
||||
playerStood = False
|
||||
# ID = self.recv("ID: (Unique)")
|
||||
# IDFound = self.findID(ID)[0]
|
||||
# if IDFound:
|
||||
# playerStats = self.readLedger(ID)[1:]
|
||||
# playerStats[0] = ID
|
||||
# else:
|
||||
# self.addToLedger(ID)
|
||||
# playerStats = (ID, 100, 0, 0)
|
||||
playerStats = self.readLedger(ID)
|
||||
if playerStats is None:
|
||||
playerStats = (ID, 100, 0, 0)
|
||||
|
||||
while not gameOver:
|
||||
playerWinState = self.checkHandState(self.playerHand)
|
||||
@@ -197,57 +209,3 @@ class BlackJack:
|
||||
elif dealerWinState == "l":
|
||||
await self.send("The Dealer busted before you!")
|
||||
# self.writeLedger(ID, 2*bet, True)
|
||||
|
||||
def findID(self, ID):
|
||||
with open("ledger.txt", "r") as ledger:
|
||||
IDFound = False
|
||||
lineNo = 0
|
||||
print(ID)
|
||||
for line in ledger:
|
||||
print(lineNo)
|
||||
splitline = line.split(":")
|
||||
if splitline[0] == ID:
|
||||
IDFound = True
|
||||
break
|
||||
lineNo += 1
|
||||
return IDFound, lineNo
|
||||
|
||||
def readLedger(self, ID):
|
||||
lineNo = self.findID(ID)[1]
|
||||
with open("ledger.txt", "r") as ledger:
|
||||
lines = ledger.readlines()
|
||||
|
||||
line = lines[lineNo]
|
||||
splitLine = line.split(":")
|
||||
money, wins, losses = int(splitLine[1]), int(
|
||||
splitLine[2]), int(splitLine[3])
|
||||
return [lines, lineNo, money, wins, losses]
|
||||
|
||||
def writeLedger(self, ID, moneyWon, wonAGame):
|
||||
lines, lineNo, currentMoney, currentWins, currentLosses = self.readLedger(
|
||||
ID)
|
||||
updatedMoney = currentMoney + moneyWon
|
||||
if wonAGame:
|
||||
updatedWins = currentWins + 1
|
||||
updatedLosses = currentLosses
|
||||
else:
|
||||
updatedLosses = currentLosses + 1
|
||||
updatedWins = currentWins
|
||||
|
||||
with open("ledger.txt", "w") as ledger:
|
||||
i = 0
|
||||
for line in lines:
|
||||
if i == lineNo:
|
||||
ledger.write("{ID}:{Money}:{Wins}:{Losses}\n".format(
|
||||
ID=ID,
|
||||
Money=updatedMoney,
|
||||
Wins=updatedWins,
|
||||
Losses=updatedLosses))
|
||||
else:
|
||||
ledger.write(line)
|
||||
i += 1
|
||||
|
||||
def addToLedger(self, ID, starterCash=100):
|
||||
with open("ledger.txt", "a") as ledger:
|
||||
ledger.write("{ID}:{Money}:0:0\n".format(ID=ID, Money=starterCash))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user