-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplayerRPS.py
More file actions
140 lines (122 loc) · 5.05 KB
/
Copy pathMultiplayerRPS.py
File metadata and controls
140 lines (122 loc) · 5.05 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
import discord
from discord.ext import commands
import math
DEBUG = True
CHALLENGE_USAGE = "Usage: >>challenge [user] [choice]"
ACCEPT_USAGE = "Usage: >>accept [user] [choice]"
WIN = {"rock":"scissors", "paper":"rock", "scissors":"paper"}
OPTIONS = ["rock", "paper", "scissors"]
class Challenge:
challenger = None
challengee = None
def __init__(self, challenger, challengee, bot):
self.challenger = normalize(str(challenger), bot)
self.challengee = normalize(str(challengee), bot)
if DEBUG:
print("Constructing challenge:", \
self.challenger, "vs", self.challengee)
def __hash__(self):
return len(str(self.challenger))
def __eq__(self, other):
return self.challenger == other.challenger \
and self.challengee == other.challengee
def __ne__(self, other):
return not __eq__(other)
def normalize(user, bot):
if user[-1] == ">":
if DEBUG:
if DEBUG:
for member in bot.get_all_members():
print(member.id)
try:
for member in bot.get_all_members():
if int(user[2:-1]) == member.id:
user = str(member)
break
except Exception:
pass
return user
class MultiplayerRPSCog:
challenges = None
bot = None
def __init__(self, bot):
self.challenges = {}
self.bot = bot
@commands.command()
async def challenge(self, ctx):
challenger = str(ctx.message.author)
challengee = None
choice = None
try:
message_split = ctx.message.content.split(' ')
challengee = message_split[1]
choice = message_split[2]
except IndexError:
await ctx.send(CHALLENGE_USAGE)
if challengee != None:
if Challenge(challenger, challengee, self.bot) \
in self.challenges.keys():
await ctx.send("You have already challenged " + challengee)
elif Challenge(challenger, challengee, self.bot) \
in self.challenges.keys():
await ctx.send(challengee + " has already challenged you.")
else:
if choice in OPTIONS:
self.challenges[Challenge(challenger, challengee, self.bot)] \
= choice
await ctx.send("Awaiting response from " + challengee)
if DEBUG:
print(challenger, "has challenged", challengee)
else:
await ctx.send("You can't choose " + choice + ". Cheater.")
@commands.command()
async def accept(self, ctx):
challengee = str(ctx.message.author)
challenger = None
choice = None
try:
message_split = ctx.message.content.split(' ')
challenger = message_split[1]
choice = message_split[2]
except IndexError:
await ctx.send(ACCEPT_USAGE)
if challenger != None:
if Challenge(challenger, challengee, self.bot) \
in self.challenges.keys():
challenger_choice = self.challenges[ \
Challenge(challenger, challengee, self.bot)]
if choice in OPTIONS:
winner = challenger
if challenger_choice == WIN[choice]:
winner = challengee
elif challenger_choice == choice:
winner = None
winner_message = None
if winner == None:
winner_message = "It's a tie!"
else:
winner_message = winner + " wins!"
await ctx.send(challenger + " chose " + challenger_choice \
+ ". " + challengee + " chose " + choice \
+ ". " + winner_message)
del(self.challenges[Challenge(challenger, challengee, \
self.bot)])
else:
await ctx.send("You can't choose " + choice + ". Cheater.")
else:
await ctx.send(challenger + " has not challenged you.")
@commands.command()
async def challengers(self, ctx):
if DEBUG:
print(ctx.message.author.name + " is checking challengers.")
output = "You have been challenged by these users:"
for challenge in self.challenges.keys():
if challenge.challengee == str(ctx.message.author):
output += "\n" + challenge.challenger
await ctx.send(output)
@commands.command()
async def clear(self, ctx):
self.challenges.clear()
await ctx.send("Deleted all pending challenges.")
def setup(bot):
bot.add_cog(MultiplayerRPSCog(bot))