Ledger reimplemented with SQLite3
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import discord
|
import discord
|
||||||
|
import sqlite3
|
||||||
from discord import app_commands
|
from discord import app_commands
|
||||||
from BlackJack import BlackJack
|
from BlackJack import BlackJack
|
||||||
|
|
||||||
|
|
||||||
botIntents = discord.Intents.all()
|
botIntents = discord.Intents.all()
|
||||||
|
|
||||||
client = discord.Client(intents=botIntents)
|
client = discord.Client(intents=botIntents)
|
||||||
@@ -75,7 +77,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(interaction.user, 100)
|
await blackJack.play_game(interaction.user.id, 100)
|
||||||
|
|
||||||
async def discordInput(interaction: discord.Interaction, message:str):
|
async def discordInput(interaction: discord.Interaction, message:str):
|
||||||
response = HitOrStand()
|
response = HitOrStand()
|
||||||
@@ -86,6 +88,14 @@ async def discordInput(interaction: discord.Interaction, message:str):
|
|||||||
async def discordOutput(interaction: discord.Interaction, message):
|
async def discordOutput(interaction: discord.Interaction, message):
|
||||||
await interaction.followup.send(message)
|
await interaction.followup.send(message)
|
||||||
|
|
||||||
|
@tree.command(description="Find your Balance")
|
||||||
|
async def bal(interaction: discord.Interaction):
|
||||||
|
conn = sqlite3.connect("ledger.db")
|
||||||
|
cur = conn.cursor()
|
||||||
|
ID = interaction.user.id
|
||||||
|
data = cur.execute("SELECT * FROM ledger WHERE USERID = ?", (ID,))
|
||||||
|
await interaction.response.send_message(data)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
#### Fin ####
|
#### Fin ####
|
||||||
"""
|
"""
|
||||||
|
|||||||
42
BlackJack.py
42
BlackJack.py
@@ -48,7 +48,10 @@ def generateHand(hand, deck, handSize = 2):
|
|||||||
addCardToHand(hand, deck)
|
addCardToHand(hand, deck)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def handNumbersToCards(hand): return [convertNumberToCard(a) for a in hand]
|
def handNumbersToCards(hand):
|
||||||
|
cards = ""
|
||||||
|
for card in hand:
|
||||||
|
cards += convertNumberToCard(card)
|
||||||
|
|
||||||
def getHandTotal(hand):
|
def getHandTotal(hand):
|
||||||
cardValue = lambda c: min(c % 13 + 1, 10)
|
cardValue = lambda c: min(c % 13 + 1, 10)
|
||||||
@@ -60,23 +63,7 @@ def getHandTotal(hand):
|
|||||||
if (i + card) > 21:
|
if (i + card) > 21:
|
||||||
card = 1
|
card = 1
|
||||||
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):
|
||||||
@@ -91,7 +78,7 @@ class BlackJack:
|
|||||||
self.recv = recv
|
self.recv = recv
|
||||||
self.send = send
|
self.send = send
|
||||||
|
|
||||||
ledger = BlackJackLedger()
|
self.ledger = Ledger.Ledger()
|
||||||
|
|
||||||
def getPH(self):
|
def getPH(self):
|
||||||
return self.playerHand
|
return self.playerHand
|
||||||
@@ -152,9 +139,10 @@ class BlackJack:
|
|||||||
validInput = False
|
validInput = False
|
||||||
gameOver = False
|
gameOver = False
|
||||||
playerStood = False
|
playerStood = False
|
||||||
playerStats = self.readLedger(ID)
|
playerStats = self.ledger.readLedger(ID)
|
||||||
if playerStats is None:
|
if playerStats is None:
|
||||||
playerStats = (ID, 100, 0, 0)
|
self.ledger.writeLedger(ID)
|
||||||
|
playerStats = self.ledger.readLedger
|
||||||
|
|
||||||
while not gameOver:
|
while not gameOver:
|
||||||
playerWinState = self.checkHandState(self.playerHand)
|
playerWinState = self.checkHandState(self.playerHand)
|
||||||
@@ -199,13 +187,17 @@ class BlackJack:
|
|||||||
"\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand))
|
"\n" + "Dealers hand = " + str(getHandTotal(self.dealerHand)) + ": " + handNumbersToCards(self.dealerHand))
|
||||||
if playerWinState == "w":
|
if playerWinState == "w":
|
||||||
await self.send("You won!")
|
await self.send("You won!")
|
||||||
# self.writeLedger(ID, 2*bet, True)
|
win = (2*bet, 1, 0, ID)
|
||||||
|
self.ledger.updateLedger(playerStats, win)
|
||||||
elif playerWinState == "l":
|
elif playerWinState == "l":
|
||||||
await self.send("You busted!")
|
await self.send("You busted!")
|
||||||
# self.writeLedger(ID, -bet, False)
|
loss = (-bet, 0, 1, ID)
|
||||||
|
self.ledger.updateLedger(playerStats, loss)
|
||||||
elif dealerWinState == "w":
|
elif dealerWinState == "w":
|
||||||
await self.send("The Dealer reached 21 before you!")
|
await self.send("The Dealer reached 21 before you!")
|
||||||
# self.writeLedger(ID, -bet, False)
|
loss = (-bet, 0, 1, ID)
|
||||||
|
self.ledger.updateLedger(playerStats, loss)
|
||||||
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)
|
win = (2*bet, 1, 0, ID)
|
||||||
|
self.ledger.updateLedger(playerStats, win)
|
||||||
|
|||||||
59
Ledger.py
59
Ledger.py
@@ -2,39 +2,50 @@ import sqlite3
|
|||||||
|
|
||||||
class Ledger():
|
class Ledger():
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.data = []
|
self.db = sqlite3.connect("ledger.db")
|
||||||
self.indexes = {} # ID: Index
|
self.data = self.db.cursor()
|
||||||
|
self.data.execute(""" CREATE TABLE IF NOT EXISTS ledger (
|
||||||
|
ID integer PRIMARY KEY,
|
||||||
|
USERID integer,
|
||||||
|
MONEY integer DEFAULT 100,
|
||||||
|
WINS integer DEFAULT 0,
|
||||||
|
LOSSES integer DEFAULT 0
|
||||||
|
); """)
|
||||||
|
|
||||||
def readLedger(self, ID):
|
def readLedger(self, ID):
|
||||||
data = None
|
self.data.execute("""SELECT USERID, MONEY, WINS, LOSSES FROM ledger WHERE USERID = ?""", (ID,))
|
||||||
index = self.indexes.get(ID, None)
|
data = self.data.fetchone()
|
||||||
if index is not None:
|
|
||||||
data = self.data[index]
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def writeLedger(self, ID, data):
|
def writeLedger(self, ID):
|
||||||
oldData = self.readLedger(ID)
|
query = """ INSERT INTO ledger(USERID)
|
||||||
if oldData is not None:
|
VALUES(?) """
|
||||||
data = self.updateLedger(data, oldData)
|
self.data.execute(query, (ID,))
|
||||||
index = self.indexes[ID]
|
self.db.commit()
|
||||||
self.data[index] = data
|
|
||||||
else:
|
|
||||||
self.data.append(data)
|
|
||||||
index = len(self.data) - 1
|
|
||||||
self.indexes.update({ID: index})
|
|
||||||
|
|
||||||
self.saveLedger()
|
|
||||||
|
|
||||||
def updateLedger(self, data, newData):
|
def updateLedger(self, data, newData):
|
||||||
return data
|
query = """ UPDATE ledger
|
||||||
|
SET MONEY = ?,
|
||||||
|
WINS = ?,
|
||||||
|
LOSSES = ?,
|
||||||
|
WHERE USERID = ?"""
|
||||||
|
data = [data[i] + newData[i] for i in range(len(data))]
|
||||||
|
self.data.execute(query, data)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
conn = sqlite3.connect("ledger.db")
|
ledger = Ledger()
|
||||||
cur = conn.cursor()
|
ledger.writeLedger(10121)
|
||||||
table = """ CREATE TABLE IF NOT EXISTS
|
ID = 10121
|
||||||
|
data = ledger.readLedger(ID)
|
||||||
|
|
||||||
"""
|
ID = 2
|
||||||
cur.execute(table)
|
playerStats = ledger.readLedger(ID)
|
||||||
|
if playerStats is None:
|
||||||
|
print("Not real")
|
||||||
|
ledger.writeLedger(ID)
|
||||||
|
playerStats = ledger.readLedger(ID)
|
||||||
|
|
||||||
|
print(playerStats)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Binary file not shown.
BIN
__pycache__/Ledger.cpython-310.pyc
Normal file
BIN
__pycache__/Ledger.cpython-310.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user