-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChips.py
More file actions
276 lines (246 loc) · 8.64 KB
/
Copy pathChips.py
File metadata and controls
276 lines (246 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import discord.ext.commands as commands
import json, random
defaultChips = {"black": 0, "blue": 10, "red": 25, "white": 50}
class Chips(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.chips = defaultChips if self.load()[0] is None else self.load()[0]
self.playerChips = self.load()[1]
def save(self):
with open("chips.json", "w") as f:
json.dump(self.chips, f)
with open("players.json", "w") as f:
json.dump(self.playerChips, f)
def load(self):
try:
with open("chips.json", "r") as f:
loadedChips = json.load(f)
except:
loadedChips = None
try:
with open("players.json", "r") as f:
playerChips = json.load(f)
except:
playerChips = {}
return loadedChips, playerChips
def make_list(self):
chipList = []
for color, num in self.chips.items():
for i in range(num):
chipList.append(color)
return chipList
def pick3(self):
chipList = self.make_list()
return random.sample(chipList, 3)
def remove(self, color):
self.chips[color] -= 1
def add(self, color):
self.chips[color] += 1
def addToPlayer(self, player, color):
if self.playerChips.get(player, None) is None:
self.playerChips[player] = {}
self.playerChips[player][color] = self.playerChips[player].get(color, 0) + 1
def removeFromPlayer(self, player, color) -> bool:
if self.playerChips.get(player, None) is None:
return False
if self.playerChips[player].get(color, 0) == 0:
return False
self.playerChips[player][color] -= 1
return True
def list2str(self, chipList):
chipDict = {}
for color in chipList:
chipDict[color] = chipDict.get(color, 0) + 1
chipStr = ""
for color, num in chipDict.items():
chipStr += str(num) + " " + color + " chips, "
chipStr = chipStr[:-2]
return chipStr
def dict2str(self, chipDict):
msg = ""
for color in chipDict.keys():
msg += " " + str(chipDict.get(color, 0)) + " " + color + ","
msg = msg[:-1]
return msg
@commands.command(
name="getChips", brief="Get 3 random chips.", help="Get 3 random chips."
)
async def get_chips(self, ctx):
givenChips = self.pick3()
for chip in givenChips:
self.remove(chip)
self.addToPlayer(ctx.author.global_name, chip)
msg = (
ctx.author.global_name
+ " has been given "
+ self.list2str(givenChips)
+ "."
)
await ctx.send(msg)
@commands.command(
name="getColor",
brief="args: color, opt num, get num (def 1) chip of that color.",
)
async def get_color(self, ctx, color, num=1):
for i in range(int(num)):
self.remove(color.lower())
self.addToPlayer(ctx.author.global_name, color.lower())
msg = (
ctx.author.global_name
+ " has been given "
+ str(num)
+ " "
+ color
+ " chip"
)
if num != 1:
msg += "s"
msg += "."
await ctx.send(msg)
@commands.command(
name="getLegendary",
brief="get legendary chip. can't be used with normal command, won't be added back to pot",
)
async def get_legendary(self, ctx):
self.addToPlayer(ctx.author.global_name, "Legendary Black")
msg = "Legendary black chip given."
await ctx.send(msg)
@commands.command(
name="useLegendary", brief="use legendary chip, removes from player chips."
)
async def use_legendary(self, ctx):
wasUsed = self.removeFromPlayer(ctx.author.global_name, "Legendary Black")
if wasUsed:
msg = "Legendary black chip used."
else:
msg = "You don't have a legendary black chip."
await ctx.send(msg)
@commands.command(name="getRandom", brief="get a random chip.")
async def get_random(self, ctx):
givenChip = random.choice(self.make_list())
self.remove(givenChip)
self.addToPlayer(ctx.author.global_name, givenChip)
msg = (
ctx.author.global_name.title() + " has been given a " + givenChip + " chip."
)
await ctx.send(msg)
@commands.command(
name="returnChips",
brief="args: num1 color1, opt: num2 color2, up to 4, return chips to pot.",
aliases=["returnchips", "useChips", "use"],
)
async def return_chips(
self,
ctx,
num1,
color1,
num2=None,
color2=None,
num3=None,
color3=None,
num4=None,
color4=None,
):
num1 = int(num1)
isValid = True
for i in range(num1):
self.add(color1.lower())
isValid = isValid and self.removeFromPlayer(
ctx.author.global_name, color1.lower()
)
if num2:
num2 = int(num2)
for i in range(num2):
self.add(color2.lower())
isValid = isValid and self.removeFromPlayer(
ctx.author.global_name, color2.lower()
)
if num3:
num3 = int(num3)
for i in range(num3):
self.add(color3.lower())
isValid = isValid and self.removeFromPlayer(
ctx.author.global_name, color3.lower()
)
if num4:
num4 = int(num4)
for i in range(num4):
self.add(color4.lower())
isValid = isValid and self.removeFromPlayer(
ctx.author.global_name, color4.lower()
)
if isValid:
msg = "Chips returned."
else:
msg = "You don't have that many chips."
await ctx.send(msg)
@commands.command(name="addBlack", brief="add black chip to pot.")
async def add_black(self, ctx):
self.add("black")
msg = "Black chip added."
await ctx.send(msg)
@commands.command(name="removeBlack", brief="remove black chip from pot")
async def remove_black(self, ctx):
self.remove("black")
msg = "Black chip removed."
await ctx.send(msg)
@commands.command(name="showChips", brief="Show current pot.")
async def show_chips(self, ctx):
msg = "Current pot: " + self.list2str(self.make_list()) + "."
await ctx.send(msg)
@commands.command(name="showMine", brief="Show your chips.", aliases=["showPot"])
async def show_player_chips(self, ctx):
msg = (
ctx.author.global_name
+ "'s chips:"
+ self.dict2str(self.playerChips.get(ctx.author.global_name, {}))
+ "."
)
await ctx.send(msg)
@commands.command(
name="showAllPlayerChips", brief="Show everyone's chips.", aliases=["showAll"]
)
async def show_all_player_chips(self, ctx):
msg = ""
for player in self.playerChips.keys():
msg += (
player
+ "'s chips: "
+ self.dict2str(self.playerChips.get(player, {}))
+ ".\n"
)
await ctx.send(msg)
@commands.command(brief="save chips to file.")
async def saveChips(self, ctx):
self.save()
msg = "Chips saved."
await ctx.send(msg)
@commands.command(brief="reset chips to default.")
async def resetChips(self, ctx):
self.chips = defaultChips
self.playerChips = {}
msg = "Chips reset."
await ctx.send(msg)
# make a command to steal all chips from another player
@commands.command(
name="StealChips",
brief="args: <name>, Steal chips from another player\nONLY FOR DISCORD USERNAME CHANGE",
)
async def steal_chips(self, ctx, name):
for chips in self.playerChips[name]:
self.addToPlayer(ctx.author.global_name, chips)
self.removeFromPlayer(name, chips)
msg = "Chips stolen."
await ctx.send(msg)
@commands.command(name="deleteEmptyPlayers", brief="delete players with no chips")
async def delete_empty_players(self, ctx):
for player in self.playerChips.keys():
count = 0
for color in self.playerChips[player]:
count += self.playerChips[player][color]
if count == 0:
del self.playerChips[player]
msg = "Empty players deleted."
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(Chips(bot))