Ledger Works

This commit is contained in:
2022-11-28 18:22:06 +10:00
parent ed65761e88
commit 33e9781914
3 changed files with 44 additions and 76 deletions

View File

@@ -75,7 +75,7 @@ async def bj(interaction: discord.Interaction):
discoutput = lambda m: discordOutput(interaction, m)
blackJack = BlackJack(discinput, discoutput)
await interaction.response.send_message("Let's play Black Jack!")
await blackJack.play_game(100)
await blackJack.play_game(interaction.user, 100)
async def discordInput(interaction: discord.Interaction, message:str):
response = HitOrStand()

View File

@@ -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))

View File

@@ -2,29 +2,39 @@ import pickle
class Ledger():
def __init__(self) -> None:
self.data = [] # [ID, wins, losses, money]
self.data = []
self.indexes = {} # ID: Index
def readLedger(self, ID):
data = None
index = self.indexes.get(ID, None)
if index is not None:
data = self.data[ID]
data = self.data[index]
return data
def writeLedger(self, ID, data):
newData = self.readLedger(ID)
if newData is not None:
_, wins, losses, money = newData
_, newWins, newLosses, newMoney = data
wins += newWins
losses += newLosses
money += newMoney
data = [ID, wins, losses, money]
oldData = self.readLedger(ID)
if oldData is not None:
data = self.updateLedger(data, oldData)
index = self.indexes[ID]
self.data[index] = data
else:
self.data.append(data)
index = len(self.data) - 1
self.indexes.update({ID: index})
def updateLedger(self, data, newData):
return data
def main():
ledger = Ledger()
ledger.writeLedger("Sus", [0,1])
ledger.writeLedger("Cum", [2,2])
print(ledger.readLedger("Cum"))
ledger.writeLedger("Cum", [2,3])
print(ledger.readLedger("Cum"))
if __name__ == "__main__":
main()