Ledger Works
This commit is contained in:
@@ -75,7 +75,7 @@ async def bj(interaction: discord.Interaction):
|
|||||||
discoutput = lambda m: discordOutput(interaction, m)
|
discoutput = lambda m: discordOutput(interaction, m)
|
||||||
blackJack = BlackJack(discinput, discoutput)
|
blackJack = BlackJack(discinput, discoutput)
|
||||||
await interaction.response.send_message("Let's play Black Jack!")
|
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):
|
async def discordInput(interaction: discord.Interaction, message:str):
|
||||||
response = HitOrStand()
|
response = HitOrStand()
|
||||||
|
|||||||
86
BlackJack.py
86
BlackJack.py
@@ -1,5 +1,5 @@
|
|||||||
import random
|
import random
|
||||||
|
import Ledger
|
||||||
|
|
||||||
def convertNumberToCard(cardNumber):
|
def convertNumberToCard(cardNumber):
|
||||||
# Cards index from 0 i.e ace is 0, and the highest card value is 51
|
# 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
|
i += card
|
||||||
return i
|
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:
|
class BlackJack:
|
||||||
def __init__(self, recv, send):
|
def __init__(self, recv, send):
|
||||||
@@ -76,6 +91,8 @@ class BlackJack:
|
|||||||
self.recv = recv
|
self.recv = recv
|
||||||
self.send = send
|
self.send = send
|
||||||
|
|
||||||
|
ledger = BlackJackLedger()
|
||||||
|
|
||||||
def getPH(self):
|
def getPH(self):
|
||||||
return self.playerHand
|
return self.playerHand
|
||||||
|
|
||||||
@@ -131,18 +148,13 @@ class BlackJack:
|
|||||||
gameOver = True
|
gameOver = True
|
||||||
return gameOver
|
return gameOver
|
||||||
|
|
||||||
async def play_game(self, bet):
|
async def play_game(self, ID, bet):
|
||||||
validInput = False
|
validInput = False
|
||||||
gameOver = False
|
gameOver = False
|
||||||
playerStood = False
|
playerStood = False
|
||||||
# ID = self.recv("ID: (Unique)")
|
playerStats = self.readLedger(ID)
|
||||||
# IDFound = self.findID(ID)[0]
|
if playerStats is None:
|
||||||
# if IDFound:
|
playerStats = (ID, 100, 0, 0)
|
||||||
# playerStats = self.readLedger(ID)[1:]
|
|
||||||
# playerStats[0] = ID
|
|
||||||
# else:
|
|
||||||
# self.addToLedger(ID)
|
|
||||||
# playerStats = (ID, 100, 0, 0)
|
|
||||||
|
|
||||||
while not gameOver:
|
while not gameOver:
|
||||||
playerWinState = self.checkHandState(self.playerHand)
|
playerWinState = self.checkHandState(self.playerHand)
|
||||||
@@ -197,57 +209,3 @@ class BlackJack:
|
|||||||
elif dealerWinState == "l":
|
elif dealerWinState == "l":
|
||||||
await self.send("The Dealer busted before you!")
|
await self.send("The Dealer busted before you!")
|
||||||
# self.writeLedger(ID, 2*bet, True)
|
# 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))
|
|
||||||
|
|
||||||
|
|||||||
32
Ledger.py
32
Ledger.py
@@ -2,29 +2,39 @@ import pickle
|
|||||||
|
|
||||||
class Ledger():
|
class Ledger():
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.data = [] # [ID, wins, losses, money]
|
self.data = []
|
||||||
self.indexes = {} # ID: Index
|
self.indexes = {} # ID: Index
|
||||||
|
|
||||||
def readLedger(self, ID):
|
def readLedger(self, ID):
|
||||||
data = None
|
data = None
|
||||||
index = self.indexes.get(ID, None)
|
index = self.indexes.get(ID, None)
|
||||||
if index is not None:
|
if index is not None:
|
||||||
data = self.data[ID]
|
data = self.data[index]
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def writeLedger(self, ID, data):
|
def writeLedger(self, ID, data):
|
||||||
newData = self.readLedger(ID)
|
oldData = self.readLedger(ID)
|
||||||
|
if oldData is not None:
|
||||||
if newData is not None:
|
data = self.updateLedger(data, oldData)
|
||||||
_, wins, losses, money = newData
|
|
||||||
_, newWins, newLosses, newMoney = data
|
|
||||||
wins += newWins
|
|
||||||
losses += newLosses
|
|
||||||
money += newMoney
|
|
||||||
data = [ID, wins, losses, money]
|
|
||||||
index = self.indexes[ID]
|
index = self.indexes[ID]
|
||||||
self.data[index] = data
|
self.data[index] = data
|
||||||
else:
|
else:
|
||||||
self.data.append(data)
|
self.data.append(data)
|
||||||
index = len(self.data) - 1
|
index = len(self.data) - 1
|
||||||
self.indexes.update({ID: index})
|
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()
|
||||||
Reference in New Issue
Block a user