Première gestion du tirage des défis
This commit is contained in:
		
							
								
								
									
										96
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										96
									
								
								bot.py
									
									
									
									
									
								
							@@ -1,5 +1,7 @@
 | 
				
			|||||||
#!/usr/bin/env python3
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from collections import namedtuple
 | 
				
			||||||
 | 
					import copy
 | 
				
			||||||
from functools import partial
 | 
					from functools import partial
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
@@ -58,9 +60,22 @@ if DATA_FILE.exists():
 | 
				
			|||||||
    with DATA_FILE.open() as data_file:
 | 
					    with DATA_FILE.open() as data_file:
 | 
				
			||||||
        data = json.load(data_file)
 | 
					        data = json.load(data_file)
 | 
				
			||||||
else:
 | 
					else:
 | 
				
			||||||
    data = {'equipes': {'rouge': [], 'vert': []}, 'cantons': {code_canton: {'capture': None, 'verrouille': False} for code_canton in CANTONS.keys()}}
 | 
					    data = {
 | 
				
			||||||
 | 
					        'equipes': {'rouge': [], 'vert': []},
 | 
				
			||||||
 | 
					        'cantons': {code_canton: {'capture': None, 'verrouille': False} for code_canton in CANTONS.keys()},
 | 
				
			||||||
 | 
					        'defis': {
 | 
				
			||||||
 | 
					            'mains': {'rouge': [], 'vert': []},
 | 
				
			||||||
 | 
					            'tires_capture': [],
 | 
				
			||||||
 | 
					            'tires_competition': [],
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    with DATA_FILE.open('w') as data_file:
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
        json.dump(data, data_file, indent=4)
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					DEFIS_FILE = Path(__file__).parent / "defis.json"
 | 
				
			||||||
 | 
					with DEFIS_FILE.open() as defis_file:
 | 
				
			||||||
 | 
					    DEFIS = json.load(defis_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def generer_carte():
 | 
					def generer_carte():
 | 
				
			||||||
    doc = minidom.parse("map_blank.svg")
 | 
					    doc = minidom.parse("map_blank.svg")
 | 
				
			||||||
@@ -110,7 +125,7 @@ async def capturer(ctx: commands.Context, canton: CodeCanton, *, couleur: Couleu
 | 
				
			|||||||
            raise commands.BadArgument("Vous n'appartez à aucune équipe. Merci de faire `$equipe [rouge|vert]`.")
 | 
					            raise commands.BadArgument("Vous n'appartez à aucune équipe. Merci de faire `$equipe [rouge|vert]`.")
 | 
				
			||||||
    data['cantons'][canton]['capture'] = couleur
 | 
					    data['cantons'][canton]['capture'] = couleur
 | 
				
			||||||
    with DATA_FILE.open('w') as data_file:
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
        json.dump(data, data_file, indent=4)
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
					    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
				
			||||||
    return await carte(ctx)
 | 
					    return await carte(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -135,7 +150,7 @@ async def verrouiller(ctx: commands.Context, canton: CodeCanton, *, couleur: Cou
 | 
				
			|||||||
    data['cantons'][canton]['capture'] = couleur
 | 
					    data['cantons'][canton]['capture'] = couleur
 | 
				
			||||||
    data['cantons'][canton]['verrouille'] = True
 | 
					    data['cantons'][canton]['verrouille'] = True
 | 
				
			||||||
    with DATA_FILE.open('w') as data_file:
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
        json.dump(data, data_file, indent=4)
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
    generer_carte()
 | 
					    generer_carte()
 | 
				
			||||||
    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
					    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
				
			||||||
    return await carte(ctx)
 | 
					    return await carte(ctx)
 | 
				
			||||||
@@ -146,7 +161,7 @@ async def reset(ctx: commands.Context, canton: CodeCanton):
 | 
				
			|||||||
    data['cantons'][canton]['capture'] = None
 | 
					    data['cantons'][canton]['capture'] = None
 | 
				
			||||||
    data['cantons'][canton]['verrouille'] = False
 | 
					    data['cantons'][canton]['verrouille'] = False
 | 
				
			||||||
    with DATA_FILE.open('w') as data_file:
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
        json.dump(data, data_file, indent=4)
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
    generer_carte()
 | 
					    generer_carte()
 | 
				
			||||||
    return await carte(ctx)
 | 
					    return await carte(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -159,10 +174,79 @@ async def equipe(ctx: commands.Context, couleur: Couleur):
 | 
				
			|||||||
            membres_equipe.remove(author_id)
 | 
					            membres_equipe.remove(author_id)
 | 
				
			||||||
    data['equipes'][couleur].append(author_id)
 | 
					    data['equipes'][couleur].append(author_id)
 | 
				
			||||||
    with DATA_FILE.open('w') as data_file:
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
        json.dump(data, data_file, indent=4)
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
    await ctx.send(f"Équipe {couleur} rejointe")
 | 
					    await ctx.send(f"Équipe {couleur} rejointe")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.command()
 | 
				
			||||||
 | 
					async def defis(ctx: commands.Context, *, type_defi: Literal['capture', 'competition'] = "capture"):
 | 
				
			||||||
 | 
					    await ctx.send(f"Liste des défis de {type_defi} :\n" + "\n".join(f"* {defi['id']} : {defi['nom']}" for defi in DEFIS[type_defi]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.command()
 | 
				
			||||||
 | 
					async def description(ctx: commands.Context, type_defi: Literal['capture', 'competition'] = "capture"):
 | 
				
			||||||
 | 
					    defis = DEFIS[type_defi]
 | 
				
			||||||
 | 
					    embeds = []
 | 
				
			||||||
 | 
					    for page in range((len(defis) - 1) // 25 + 1):
 | 
				
			||||||
 | 
					        defis_page = defis[page * 25:(page + 1) * 25]
 | 
				
			||||||
 | 
					        embed = discord.Embed(title=f"Description des défis", colour=discord.Colour.gold())
 | 
				
			||||||
 | 
					        embed.set_footer(f"Page {page}/{(len(defis) - 1) // 25 + 1}")
 | 
				
			||||||
 | 
					        for defi in defis_page:
 | 
				
			||||||
 | 
					            embed.add_field(name=f"{defi['nom']} (n°{defi['id']})", value=defi['description'], inline=False)
 | 
				
			||||||
 | 
					        embeds.append(embed)
 | 
				
			||||||
 | 
					    await ctx.send(embeds=embeds)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.command()
 | 
				
			||||||
 | 
					async def tirage(ctx: commands.Context, nb_defis: int = 7):
 | 
				
			||||||
 | 
					    if data['defis']['mains']['rouge'] or data['defis']['mains']['vert']:
 | 
				
			||||||
 | 
					        raise commands.BadArgument("Les mains sont déjà initialisées")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    defis_libres = copy.deepcopy(DEFIS['capture'])
 | 
				
			||||||
 | 
					    for equipe in ('rouge', 'vert'):
 | 
				
			||||||
 | 
					        for _i in range(nb_defis):
 | 
				
			||||||
 | 
					            defi = random.choice(defis_libres)
 | 
				
			||||||
 | 
					            defis_libres.remove(defi)
 | 
				
			||||||
 | 
					            data['defis']['mains'][equipe].append(defi['id'])
 | 
				
			||||||
 | 
					            data['defis']['tires_capture'].append(defi['id'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        main = data['defis']['mains'][equipe]
 | 
				
			||||||
 | 
					        embeds = []
 | 
				
			||||||
 | 
					        colour = discord.Color.red() if equipe == "rouge" else discord.Color.green()
 | 
				
			||||||
 | 
					        for id_defi in main:
 | 
				
			||||||
 | 
					            defi = next(defi for defi in DEFIS['capture'] if defi['id'] == id_defi)
 | 
				
			||||||
 | 
					            embed = discord.Embed(title=defi['nom'], description=defi['description'], colour=colour)
 | 
				
			||||||
 | 
					            embed.set_footer(text=f"Défi n°{defi['id']}")
 | 
				
			||||||
 | 
					            embeds.append(embed)
 | 
				
			||||||
 | 
					        for member_id in data['equipes'][equipe]:
 | 
				
			||||||
 | 
					            channel_dm = await bot.create_dm(namedtuple("User", "id")(member_id))
 | 
				
			||||||
 | 
					            await channel_dm.send("Vos défis en main :", embeds=embeds)
 | 
				
			||||||
 | 
					    with DATA_FILE.open('w') as data_file:
 | 
				
			||||||
 | 
					        json.dump(data, data_file, indent=2)
 | 
				
			||||||
 | 
					    await ctx.send("Les mains de départ ont bien été tirées ! Le contenu vous a été envoyé en MP.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@bot.command()
 | 
				
			||||||
 | 
					async def main(ctx: commands.Context):
 | 
				
			||||||
 | 
					    author_id = ctx.author.id
 | 
				
			||||||
 | 
					    for couleur, membres_equipe in data['equipes'].items():
 | 
				
			||||||
 | 
					        if author_id in membres_equipe:
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        raise commands.BadArgument("Vous n'appartez à aucune équipe. Merci de faire `$equipe [rouge|vert]`.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    main = data['defis']['mains'][couleur]
 | 
				
			||||||
 | 
					    embeds = []
 | 
				
			||||||
 | 
					    colour = discord.Color.red() if couleur == "rouge" else discord.Color.green()
 | 
				
			||||||
 | 
					    for id_defi in main:
 | 
				
			||||||
 | 
					        defi = next(defi for defi in DEFIS['capture'] if defi['id'] == id_defi)
 | 
				
			||||||
 | 
					        embed = discord.Embed(title=defi['nom'], description=defi['description'], colour=colour)
 | 
				
			||||||
 | 
					        embed.set_footer(text=f"Défi n°{defi['id']}")
 | 
				
			||||||
 | 
					        embeds.append(embed)
 | 
				
			||||||
 | 
					    channel_dm = await bot.create_dm(ctx.author)
 | 
				
			||||||
 | 
					    await channel_dm.send("Vos défis en main :", embeds=embeds)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@equipe.error
 | 
					@equipe.error
 | 
				
			||||||
async def equipe_error(ctx, error):
 | 
					async def equipe_error(ctx, error):
 | 
				
			||||||
    await ctx.send(str(error))
 | 
					    await ctx.send(str(error))
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										101
									
								
								defis.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								defis.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,101 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "capture": [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 1,
 | 
				
			||||||
 | 
					      "nom": "Défi 1",
 | 
				
			||||||
 | 
					      "description": "Défi 1"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 2,
 | 
				
			||||||
 | 
					      "nom": "Défi 2",
 | 
				
			||||||
 | 
					      "description": "Défi 2"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 3,
 | 
				
			||||||
 | 
					      "nom": "Défi 3",
 | 
				
			||||||
 | 
					      "description": "Défi 3"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 4,
 | 
				
			||||||
 | 
					      "nom": "Défi 4",
 | 
				
			||||||
 | 
					      "description": "Défi 4"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 5,
 | 
				
			||||||
 | 
					      "nom": "Défi 5",
 | 
				
			||||||
 | 
					      "description": "Défi 5"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 6,
 | 
				
			||||||
 | 
					      "nom": "Défi 6",
 | 
				
			||||||
 | 
					      "description": "Défi 6"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 7,
 | 
				
			||||||
 | 
					      "nom": "Défi 7",
 | 
				
			||||||
 | 
					      "description": "Défi 7"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 8,
 | 
				
			||||||
 | 
					      "nom": "Défi 8",
 | 
				
			||||||
 | 
					      "description": "Défi 8"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 9,
 | 
				
			||||||
 | 
					      "nom": "Défi 9",
 | 
				
			||||||
 | 
					      "description": "Défi 9"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 10,
 | 
				
			||||||
 | 
					      "nom": "Défi 10",
 | 
				
			||||||
 | 
					      "description": "Défi 10"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 11,
 | 
				
			||||||
 | 
					      "nom": "Défi 11",
 | 
				
			||||||
 | 
					      "description": "Défi 11"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 12,
 | 
				
			||||||
 | 
					      "nom": "Défi 12",
 | 
				
			||||||
 | 
					      "description": "Défi 12"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 13,
 | 
				
			||||||
 | 
					      "nom": "Défi 13",
 | 
				
			||||||
 | 
					      "description": "Défi 13"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 14,
 | 
				
			||||||
 | 
					      "nom": "Défi 14",
 | 
				
			||||||
 | 
					      "description": "Défi 14"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 15,
 | 
				
			||||||
 | 
					      "nom": "Défi 15",
 | 
				
			||||||
 | 
					      "description": "Défi 15"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 16,
 | 
				
			||||||
 | 
					      "nom": "Défi 16",
 | 
				
			||||||
 | 
					      "description": "Défi 16"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
 | 
					  "competition": [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 1,
 | 
				
			||||||
 | 
					      "nom": "Compétition 1",
 | 
				
			||||||
 | 
					      "description": "Compétition 1"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 2,
 | 
				
			||||||
 | 
					      "nom": "Compétition 2",
 | 
				
			||||||
 | 
					      "description": "Compétition 2"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "id": 3,
 | 
				
			||||||
 | 
					      "nom": "Compétition 3",
 | 
				
			||||||
 | 
					      "description": "Compétition 3"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user