Ejemplo n.º 1
0
 def save(self):
     database.execute(
         "INSERT INTO pending_matches "
         "(message_id, player1, player2, time) "
         "VALUES (?, ?, ?, ?)",
         (self.message_id, self.player1, self.player2, self.time))
     database.commit()
Ejemplo n.º 2
0
 async def update_displayname(self, ctx, *name):
     name = " ".join(name).strip()
     if database.execute(
             "SELECT EXISTS (SELECT 1 FROM players WHERE id = ? AND platforms <> '')",
         (ctx.author.id, )).fetchone()[0] == 0:
         await ctx.send("You are not registered yet.")
         return
     if name == "":
         database.execute(
             "UPDATE players SET display_name = NULL WHERE id = ?",
             (ctx.author.id, ))
         database.commit()
         await ctx.send("Cleared display name.")
     else:
         if database.execute(
                 "SELECT EXISTS (SELECT 1 FROM players WHERE display_name = ?)",
             (name, )).fetchone()[0] == 1:
             await ctx.send(
                 f"The display name \"{utils.escape_markdown(name)}\" is already in use by another player."
             )
             return
         database.execute(
             "UPDATE players SET display_name = ? WHERE id = ?",
             (name, ctx.author.id))
         database.commit()
         await ctx.send(
             f"Display name set to \"{utils.escape_markdown(name)}\".")
Ejemplo n.º 3
0
    async def process_match_complete(self, match):
        player1 = match.player1_data
        old_rating1 = glicko2.Rating(player1["rating_mu"],
                                     player1["rating_phi"],
                                     player1["rating_sigma"])
        player2 = match.player2_data
        old_rating2 = glicko2.Rating(player2["rating_mu"],
                                     player2["rating_phi"],
                                     player2["rating_sigma"])

        if match.player1_score > match.player2_score:
            new_rating1, new_rating2 = (glicko2.Glicko2().rate_1vs1(
                old_rating1, old_rating2))
        else:
            new_rating2, new_rating1 = (glicko2.Glicko2().rate_1vs1(
                old_rating2, old_rating1))

        cursor = database.execute(
            """
			INSERT INTO matches (
				player1, player2, player1_score, player2_score,
				player1_old_mu, player1_old_phi, player1_old_sigma,
				player1_new_mu, player1_new_phi, player1_new_sigma,
				player2_old_mu, player2_old_phi, player2_old_sigma,
				player2_new_mu, player2_new_phi, player2_new_sigma
			) VALUES (
				?, ?, ?, ?,
				?, ?, ?,
				?, ?, ?,
				?, ?, ?,
				?, ?, ?
			)
		""", (player1["id"], player2["id"], match.player1_score, match.player2_score,
        old_rating1.mu, old_rating1.phi, old_rating1.sigma, new_rating1.mu,
        new_rating1.phi, new_rating1.sigma, old_rating2.mu, old_rating2.phi,
        old_rating2.sigma, new_rating2.mu, new_rating2.phi, new_rating2.sigma))
        database.commit()

        embed = discord.Embed(type="rich",
                              title="Match recorded",
                              color=0x00E323)
        await match_message_helper.add_report_field(
            embed, player1, match.player1_score, old_rating1.mu,
            old_rating1.phi, new_rating1.mu, new_rating1.phi)
        await match_message_helper.add_report_field(
            embed, player2, match.player2_score, old_rating2.mu,
            old_rating2.phi, new_rating2.mu, new_rating2.phi)
        embed.set_footer(text=f"Match ID: {cursor.lastrowid}")
        await self.output_channel.send(embed=embed)

        await utils.update_role(match.player1, old_rating1.mu, old_rating1.phi,
                                new_rating1.mu, new_rating1.phi)
        await utils.update_role(match.player2, old_rating2.mu, old_rating2.phi,
                                new_rating2.mu, new_rating2.phi)

        await self.cleanup_for_match(match)
Ejemplo n.º 4
0
 async def cleanup_for_match(self, match):
     database.execute("DELETE FROM pending_matches WHERE message_id = ?",
                      (match.message_id, ))
     database.commit()
     self.message_map.pop(match.message_id)
     self.player_map.pop(match.player1)
     self.player_map.pop(match.player2)
     await (await
            self.announcement_channel.fetch_message(match.message_id
                                                    )).clear_reactions()
Ejemplo n.º 5
0
 async def process_player(self, player, flag, embed):
     old_rating = glicko2.Rating(player["rating_mu"], player["rating_phi"],
                                 player["rating_sigma"])
     new_rating = old_rating if flag != 0 else \
      glicko2.Glicko2().rate_1vs1(old_rating, old_rating)[1]
     database.execute(
         "UPDATE players "
         "SET rating_mu = ?, rating_phi = ?, rating_sigma = ? "
         "WHERE id = ?",
         (new_rating.mu, new_rating.phi, new_rating.sigma, player["id"]))
     database.commit()
     await match_message_helper.add_report_field(embed, player, None,
                                                 old_rating.mu,
                                                 old_rating.phi,
                                                 new_rating.mu,
                                                 new_rating.phi)
Ejemplo n.º 6
0
 async def update_username(self, ctx, platform, *name):
     platform = platform.casefold()
     if not platform in utils.platform_name_mapping:
         await ctx.send(
             "You need to provide a valid platform that is one of the following: "
             + ", ".join(utils.platform_names) + ".")
         return
     name = " ".join(name).strip()
     player = database.execute(
         "SELECT platforms FROM players WHERE id = ? AND platforms <> ''",
         (ctx.author.id, )).fetchone()
     if player is None:
         await ctx.send("You are not registered yet.")
         return
     if platform not in player[0].split():
         await ctx.send(
             f"You are not signed up for {utils.format_platform_name(platform)}."
         )
         return
     if name == "":
         database.execute(
             f"UPDATE players SET username_{platform} = NULL WHERE id = ?",
             (ctx.author.id, ))
         database.commit()
         await ctx.send(
             f"Cleared username for {utils.format_platform_name(platform)}."
         )
     else:
         if database.execute(
                 f"SELECT EXISTS (SELECT 1 FROM players WHERE username_{platform} = ?)",
             (name, )).fetchone()[0] == 1:
             await ctx.send(
                 f"The username \"{utils.escape_markdown(name)}\" on "
                 f"{utils.format_platform_name(platform)} is already in use by another player."
             )
             return
         database.execute(
             f"UPDATE players SET username_{platform} = ? WHERE id = ?",
             (name, ctx.author.id))
         database.commit()
         await ctx.send(
             f"Username on {utils.format_platform_name(platform)} set to "
             f"\"{utils.escape_markdown(name)}\".")
Ejemplo n.º 7
0
    async def register(self, ctx, *platforms_raw):
        platforms = self.sanitize_platforms(platforms_raw)
        if len(platforms) == 0:
            await ctx.send(
                "You need to provide a valid platform that is one of the following: "
                + ", ".join(utils.platform_names) + ".")
            return

        platform_names = ", ".join(
            utils.format_platform_name(platform) for platform in platforms)

        player = database.execute(
            "SELECT platforms, rating_mu, rating_phi FROM players WHERE id = ?",
            (ctx.author.id, )).fetchone()
        if player is None:
            # First time registering.
            database.execute(
                "INSERT INTO players (id, platforms) VALUES (?, ?)",
                (ctx.author.id, " ".join(platforms)))
            database.commit()
            await utils.update_role(ctx.author.id, None, None, 1500, 350)
            await ctx.send(f"Signed up for {platform_names}.")
        else:
            # Already registered.
            no_new_entries = True
            player_platforms = player["platforms"].split()
            for platform in platforms:
                if platform not in player_platforms:
                    player_platforms.append(platform)
                    no_new_entries = False
            if no_new_entries:
                await ctx.send(
                    f"You're already signed up for {platform_names}.")
                return
            else:
                database.execute(
                    "UPDATE players SET platforms = ? WHERE id = ?",
                    (" ".join(player_platforms), ctx.author.id))
                database.commit()
                await utils.update_role(ctx.author.id, None, None,
                                        player["rating_mu"],
                                        player["rating_phi"])
                await ctx.send(f"Signed up for {platform_names}.")
Ejemplo n.º 8
0
    async def unregister(self, ctx, *platforms_raw):
        if ctx.author.id in matchfinder.player_map:
            await ctx.send(
                "You are currently in the matckmaking queue. Leave the queue before unregistering."
            )
            return
        if ctx.author.id in match_manager.player_map:
            await ctx.send(
                "You have a match pending. Complete the match before unregistering."
            )
            return

        nuke = len(platforms_raw) != 0 and platforms_raw[0].casefold() == "all"
        platforms = self.sanitize_platforms(platforms_raw)
        if not nuke and len(platforms) == 0:
            await ctx.send(
                "You need to provide a valid platform that is one of the following: "
                + ", ".join(utils.platform_names) + ".")
            return
        platform_names = ", ".join(
            utils.format_platform_name(platform) for platform in platforms)

        player = database.execute(
            "SELECT platforms, rating_mu, rating_phi FROM players WHERE id = ? AND platforms <> ''",
            (ctx.author.id, )).fetchone()
        if player is None:
            await ctx.send(f"You are not signed up here, no worries.")
        else:
            old_player_platforms = player["platforms"].split()
            new_player_platforms = ([] if nuke else [
                platform for platform in old_player_platforms
                if platform not in platforms
            ])
            if len(new_player_platforms) != len(old_player_platforms):
                platform_nullify = ", ".join(f"username_{platform} = NULL"
                                             for platform in platforms)
                zenkeshita = nuke or len(new_player_platforms) == 0
                database.execute(
                    """
					UPDATE players SET
						platforms = "",
						display_name = NULL,
						username_pc = NULL,
						username_switch = NULL,
						username_ps4 = NULL
					WHERE id = :id
					""" if zenkeshita else
                    f"UPDATE players SET platforms = :platforms, {platform_nullify} WHERE id = :id",
                    {
                        "platforms": " ".join(new_player_platforms),
                        "id": ctx.author.id
                    })
                database.commit()
                await ctx.send(
                    "Unregistered from __all__ platforms. You are no longer in the system."
                    if nuke else f"Unregistered from {platform_names}." +
                    (" __You are no longer in the system.__"
                     if len(new_player_platforms) == 0 else ""))
                if zenkeshita:
                    await utils.update_role(ctx.author.id, player["rating_mu"],
                                            player["rating_phi"], None, None)
            else:
                await ctx.send(f"You are not signed up for {platform_names}.")