:spakles: Database and OOP

This commit is contained in:
NikolajDanger
2020-08-13 16:31:28 +02:00
parent f431c079d1
commit 4127e537a1
31 changed files with 3674 additions and 3731 deletions

View File

@ -1,141 +1,138 @@
import json, random
import random
from funcs import getName, logThis
from . import monopolyDraw
from funcs import logThis
from .monopolyDraw import DrawMonopoly
rulesAndTerms = {
"pass go money" : 200,
"money term" : "GP"
}
def monopolyStart(channel):
logThis("Starting a monopoly game")
with open("resources/games/monopolyGames.json", "r") as f:
data = json.load(f)
class Monopoly():
def __init__(self, bot):
self.bot = bot
self.draw = DrawMonopoly(bot)
if channel not in data:
buildings = [0] * 40
def monopolyStart(self, channel):
logThis("Starting a monopoly game")
game = self.bot.database["monopoly games"].find_one({"_id":channel})
data[channel] = {"players" : {}, "player list" : [],"turn" : 0, "buildings" : buildings, "last roll" : [0,0], "started" : False}
if game == None:
buildings = [0] * 40
with open("resources/games/monopolyGames.json", "w") as f:
json.dump(data,f,indent=4)
try:
monopolyDraw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
return "Started a monopoly game. Use \"!monopoly join\" to join within the next minute.", True, False, True, True
else:
return "There's already a monopoly game going on.", False, False, False, False
newGame = {"_id":channel,"players" : {}, "player list" : [],"turn" : 0, "buildings" : buildings, "last roll" : [0,0], "started" : False}
def monopolyJoin(channel,user):
with open("resources/games/monopolyGames.json", "r") as f:
data = json.load(f)
self.bot.database["monopoly games"].insert_one(newGame)
if channel in data:
if not data[channel]["started"]:
if user not in data[channel]["players"]:
if len(data[channel]["players"]) < 6:
try:
self.draw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
data[channel]["players"][user] = {"position" : 0, "properties" : [], "money" : 1500, "doubles" : 0}
return "Started a monopoly game. Use \"!monopoly join\" to join within the next minute.", True, False, True, True
else:
return "There's already a monopoly game going on.", False, False, False, False
with open("resources/games/monopolyGames.json", "w") as f:
json.dump(data,f,indent=4)
def monopolyJoin(self,channel,user):
game = self.bot.database["monopoly games"].find_one({"_id":channel})
return getName(user)+" has joined the game.", False, False, False, False
if game != None:
if not game["started"]:
if user not in game["players"]:
if len(game["players"]) < 6:
newPlayer = {"position" : 0, "properties" : [], "money" : 1500, "doubles" : 0}
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"players."+user:newPlayer}})
return self.bot.funcs.getName(user)+" has joined the game.", False, False, False, False
else:
return "There are already 6 players in the game.", False, False, False, False
else:
return "There are already 6 players in the game.", False, False, False, False
return "You're already in the game!", False, False, False, False
else:
return "You're already in the game!", False, False, False, False
return "It's too late to join.", False, False, False, False
else:
return "It's too late to join.", False, False, False, False
else:
return "There's no game going on.", False, False, False, False
return "There's no game going on.", False, False, False, False
def parseMonopoly(command, channel, user):
logThis("Parsing "+command)
commands = command.split()
if command in [" ", ""] or commands[0] == "start":
try:
return monopolyStart(channel)
except:
logThis("Error starting game (error code 1620)")
elif commands[0] == "join":
try:
return monopolyJoin(channel,user)
except:
logThis("Error joining game (error code 1630)")
elif commands[0] == "roll":
try:
return monopolyRoll(channel,user)
except:
logThis("Error rolling (error code 1650)")
else:
return "I didn't understand that (error code 1602)", False, False, False, False
def monopolyContinue(channel):
with open("resources/games/monopolyGames.json", "r") as f:
data = json.load(f)
if channel in data:
if data[channel]["started"] == False:
data[channel]["started"] = True
playerList = list(data[channel]["players"].keys())
random.shuffle(playerList)
data[channel]["player list"] = playerList
turn = 0
def parseMonopoly(self, command, channel, user):
logThis("Parsing "+command)
commands = command.split()
if command in [" ", ""] or commands[0] == "start":
try:
return self.monopolyStart(channel)
except:
logThis("Error starting game (error code 1620)")
elif commands[0] == "join":
try:
return self.monopolyJoin(channel,user)
except:
logThis("Error joining game (error code 1630)")
elif commands[0] == "roll":
try:
return self.monopolyRoll(channel,user)
except:
logThis("Error rolling (error code 1650)")
else:
if data[channel]["last roll"][0] == data[channel]["last roll"][1]:
turn = data[channel]["turn"]
return "I didn't understand that (error code 1602)", False, False, False, False
def monopolyContinue(self, channel):
game = self.bot.database["monopoly games"].find_one({"_id":channel})
if game != None:
if game["started"] == False:
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"started":True}})
playerList = list(game["players"].keys())
random.shuffle(playerList)
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"player list":playerList}})
turn = 0
else:
turn = (data[channel]["turn"] + 1)%len(data[channel]["player list"])
data[channel]["turn"] = turn
playerList = list(data[channel]["players"].keys())
with open("resources/games/monopolyGames.json", "w") as f:
json.dump(data,f,indent=4)
try:
monopolyDraw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
if game["last roll"][0] == game["last roll"][1]:
turn = game["turn"]
else:
turn = (game["turn"] + 1)%len(game["player list"])
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"turn":turn}})
playerList = list(game["players"].keys())
message = "It's "+getName(playerList[turn])+"'s turn. Use the 🎲 reaction to roll. You can also use \"!monopoly trade\" at any time."
return message, True, True, False
try:
self.draw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
def monopolyRoll(channel,user):
with open("resources/games/monopolyGames.json", "r") as f:
data = json.load(f)
turn = data[channel]["turn"]
currentPlayer = data[channel]["player list"][turn]
if playerList == []:
return "No one joined. Ending game.", False, True, True
else:
message = "It's "+self.bot.funcs.getName(playerList[turn])+"'s turn. Use the 🎲 reaction to roll. You can also use \"!monopoly trade\" at any time."
return message, True, True, False
if user == currentPlayer:
rolls = [random.randint(1,6),random.randint(1,6)]
message = getName(user)+" rolled a "+str(rolls[0])+" and a "+str(rolls[1])+"."
if rolls[0] == rolls[1]:
message += " Doubbles!"
def monopolyRoll(self, channel,user):
game = self.bot.database["monopoly games"].find_one({"_id":channel})
roll = rolls[0] + rolls[1]
oldPosition = data[channel]["players"][user]["position"]
newPosition = (oldPosition + roll)%40
if newPosition < oldPosition:
data[channel]["players"][user]["money"] += rulesAndTerms["pass go money"]
message += "\nYou passed go and got "+str(rulesAndTerms["pass go money"])+" "+rulesAndTerms["money term"]+"."
turn = game["turn"]
currentPlayer = game["player list"][turn]
data[channel]["players"][user]["position"] = newPosition
data[channel]["last roll"] = rolls
if user == currentPlayer:
rolls = [random.randint(1,6),random.randint(1,6)]
message = self.bot.funcs.getName(user)+" rolled a "+str(rolls[0])+" and a "+str(rolls[1])+"."
if rolls[0] == rolls[1]:
message += " Doubbles!"
with open("resources/games/monopolyGames.json", "w") as f:
json.dump(data,f,indent=4)
try:
monopolyDraw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
return message, True, True, False, True
else: return "", False, False, False, False
roll = rolls[0] + rolls[1]
oldPosition = game["players"][user]["position"]
newPosition = (oldPosition + roll)%40
if newPosition < oldPosition:
self.bot.database["monopoly games"].update_one({"_id":channel},
{"$inc":{"players."+user+".money":rulesAndTerms["pass go money"]}})
message += "\nYou passed go and got "+str(rulesAndTerms["pass go money"])+" "+rulesAndTerms["money term"]+"."
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"players."+user+".position":newPosition}})
self.bot.database["monopoly games"].update_one({"_id":channel},{"$set":{"last roll":rolls}})
try:
self.draw.drawImage(channel)
except:
logThis("Error drawing board (error code 1640)")
return message, True, True, False, True
else: return "", False, False, False, False