Verrouillage de cantons
This commit is contained in:
		
							
								
								
									
										55
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								bot.py
									
									
									
									
									
								
							@@ -68,18 +68,39 @@ def generer_carte():
 | 
			
		||||
        if data_canton['capture']:
 | 
			
		||||
            path = next(e for e in doc.getElementsByTagName('path') if e.getAttribute('id') == code_canton)
 | 
			
		||||
            couleur = data_canton['capture']
 | 
			
		||||
            path.setAttribute('class', f"capture-{couleur}")
 | 
			
		||||
            if data_canton['verrouille']:
 | 
			
		||||
                path.setAttribute('fill', f"url(#verrouille-{couleur})")
 | 
			
		||||
            else:
 | 
			
		||||
                path.setAttribute('class', f"capture-{couleur}")
 | 
			
		||||
    with open('map.svg', 'w') as f:
 | 
			
		||||
        doc.writexml(f)
 | 
			
		||||
    cairosvg.svg2png(url='map.svg', write_to='map.png')
 | 
			
		||||
 | 
			
		||||
@bot.command()
 | 
			
		||||
async def carte(ctx: commands.Context):
 | 
			
		||||
    with open('map.png', 'rb') as f:
 | 
			
		||||
        await ctx.send(file=discord.File(f, filename="battle4suisse.png"), ephemeral=True)
 | 
			
		||||
 | 
			
		||||
@bot.command()
 | 
			
		||||
async def capture(ctx: commands.Context, canton: CodeCanton, *, couleur: Couleur | None = None):
 | 
			
		||||
async def carte(ctx: commands.Context):
 | 
			
		||||
    rouges = list(canton_code for canton_code, canton in data['cantons'].items()
 | 
			
		||||
                  if canton['capture'] == "rouge")
 | 
			
		||||
    rouges_verrouilles = list(canton_code for canton_code, canton in data['cantons'].items()
 | 
			
		||||
                              if canton['capture'] == "rouge" and canton['verrouille'])
 | 
			
		||||
    noms_rouges = ", ".join(code_canton + (":lock:" if code_canton in rouges_verrouilles else "") for code_canton in rouges)
 | 
			
		||||
    verts = list(canton_code for canton_code, canton in data['cantons'].items()
 | 
			
		||||
                if canton['capture'] == "vert")
 | 
			
		||||
    verts_verrouilles = list(canton_code for canton_code, canton in data['cantons'].items()
 | 
			
		||||
                            if canton['capture'] == "vert" and canton['verrouille'])
 | 
			
		||||
    noms_verts = ", ".join(code_canton + (":lock:" if code_canton in verts_verrouilles else "") for code_canton in verts)
 | 
			
		||||
    libres = list(canton_code for canton_code, canton in data['cantons'].items()
 | 
			
		||||
                if canton['capture'] is None)
 | 
			
		||||
    message = f""":red_circle: Équipe rouge : **{len(rouges)} canton{"s" if len(rouges) > 1 else ""}** (dont **{len(rouges_verrouilles)} verrouillé{"s" if len(rouges_verrouilles) > 1 else ""}**) : {noms_rouges}
 | 
			
		||||
:green_circle: Équipe verte : **{len(verts)} canton{"s" if len(verts) > 1 else ""}** (dont **{len(verts_verrouilles)} verrouillé{"s" if len(verts_verrouilles) > 1 else ""}**) : {noms_verts}
 | 
			
		||||
:white_circle: **{len(libres)} canton{"s" if len(libres) > 1 else ""}** libre{"s" if len(libres) > 1 else ""} : {", ".join(libres)}"""
 | 
			
		||||
    generer_carte()
 | 
			
		||||
    with open('map.png', 'rb') as f:
 | 
			
		||||
        await ctx.send(message, file=discord.File(f, filename="battle4suisse.png"))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bot.command()
 | 
			
		||||
async def capturer(ctx: commands.Context, canton: CodeCanton, *, couleur: Couleur | None = None):
 | 
			
		||||
    if couleur is None:
 | 
			
		||||
        author_id = ctx.author.id
 | 
			
		||||
        for couleur, membres_equipe in data['equipes'].items():
 | 
			
		||||
@@ -90,11 +111,11 @@ async def capture(ctx: commands.Context, canton: CodeCanton, *, couleur: Couleur
 | 
			
		||||
    data['cantons'][canton]['capture'] = couleur
 | 
			
		||||
    with DATA_FILE.open('w') as data_file:
 | 
			
		||||
        json.dump(data, data_file, indent=4)
 | 
			
		||||
    generer_carte()
 | 
			
		||||
    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
			
		||||
    return await carte(ctx)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@capture.error
 | 
			
		||||
@capturer.error
 | 
			
		||||
async def capture_error(ctx, error):
 | 
			
		||||
    if isinstance(error, commands.BadLiteralArgument):
 | 
			
		||||
        await ctx.send(f"Canton inconnu : {error.argument}, valeurs possibles : {", ".join(error.literals)}")
 | 
			
		||||
@@ -102,6 +123,24 @@ async def capture_error(ctx, error):
 | 
			
		||||
        await ctx.send(str(error))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bot.command()
 | 
			
		||||
async def verrouiller(ctx: commands.Context, canton: CodeCanton, *, couleur: Couleur | None = None):
 | 
			
		||||
    if couleur is None:
 | 
			
		||||
        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]`.")
 | 
			
		||||
    data['cantons'][canton]['capture'] = couleur
 | 
			
		||||
    data['cantons'][canton]['verrouille'] = True
 | 
			
		||||
    with DATA_FILE.open('w') as data_file:
 | 
			
		||||
        json.dump(data, data_file, indent=4)
 | 
			
		||||
    generer_carte()
 | 
			
		||||
    await ctx.send(f"@everyone L'équipe {couleur} a capturé le canton de **{CANTONS[canton]}** !")
 | 
			
		||||
    return await carte(ctx)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bot.command()
 | 
			
		||||
async def reset(ctx: commands.Context, canton: CodeCanton):
 | 
			
		||||
    data['cantons'][canton]['capture'] = None
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
		 Before Width: | Height: | Size: 283 KiB After Width: | Height: | Size: 284 KiB  | 
		Reference in New Issue
	
	Block a user