import os import re from typing import List from flask import Flask, render_template, request from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, BooleanField from wtforms import DecimalField, RadioField, SelectField, TextAreaField, FileField, IntegerField from wtforms.validators import InputRequired,DataRequired,length app = Flask(__name__) app.config['SECRET_KEY'] = 'secretkey' class MyForm(FlaskForm): chatTekst = TextAreaField('Chat tekst', validators=[InputRequired()]) ac = IntegerField(label='Ac', validators=[InputRequired()]) advantage = BooleanField(label='Har advantage') class Action: def __init__(self) -> None: self.Dam=[] self.curActionValue=0 self.actionCounter=0 self.hit=[] self.targetAc=0 self.advantage=0 def addActionValue(self,actionValue): self.curActionValue=actionValue def setActionTypeToDam(self,DamType): self.Dam.append([self.actionCounter,self.curActionValue,DamType]) def setActionTypeToDam(self,value,DamType): self.Dam.append([self.actionCounter,value,DamType]) def setActionTypeToHit(self,rul): if(rul==1): self.actionCounter=self.actionCounter+1 self.hit.append([self.actionCounter,rul,self.curActionValue]) def setActionTypeToHit(self,value,rul): self.hit.append([self.actionCounter,rul,value]) def setTargetAc(self,ac): self.targetAc=ac def setWithAdvantage(self,advantage): self.advantage=advantage def getDamForHit(self): dam=dict() hits=False if(self.advantage==False): if(self.hit[0][2]>self.targetAc): #actioncounter styre hvilket rul er tale om hits=1 if(self.advantage==True): if(self.hit[0][2]>self.targetAc or self.hit[1][2]>self.targetAc): #actioncounter styre hvilket rul er tale om hits=1 if(hits==1): for d in self.Dam: if(dam.get(d[2])==None): dam[d[2]]=d[1] else: dam[d[2]]=dam[d[2]]+d[1] return dam damTypes=["Slashing","Radiant","Great Weapon Master","Divine","Holy Weapon","Great Weapon Master/Radiant"] con=0 def incContext(): res=0 if(con==0): con=1 res=1 con=con+1 if(con>2): con=1 res=1 def getContext(): return con def isItTheRightContext(c,line): res=0 return res def checkIfCounts(line): estCon=0 if(re.match("(^_?[0-9]+){1}( \+ [0-9]+)?( [0-9]+( \+ [0-9])*)?", line)): print("match : " + line) estCon=1 # attackroll eller dam return estCon def getAllDamage(runder): dam=dict() dam['Samlet']=0 for r in runder: print(r) damage = r.getDamForHit() for d in damage: print(d) if(dam.get(d)==None): dam[d]=damage[d] dam['Samlet']=dam['Samlet']+damage[d] else: dam[d]=dam[d]+damage[d] dam['Samlet']=dam['Samlet']+damage[d] return dam def parseText(runder,lines,ac,advantage): line=None for nextLine in lines: nextLine=nextLine.strip() if(line==None): line=nextLine continue print(line) c=checkIfCounts(line) if(c==1): if(nextLine in damTypes): c=2 # damage else: c=1 # to hit if(c==1): curAction=Action() curAction.setTargetAc(ac) curAction.setWithAdvantage(advantage) h=line.split(" ") if(h[0].rfind("+")>-1 or h[0].rfind("-")>-1): if(h[0].rfind("+")>-1): curAction.setActionTypeToHit(int(h[0].split("+")[0])+int(h[0].split("+")[1]),1) else: curAction.setActionTypeToHit(int(h[0].split("-")[0])-int(h[0].split("-")[1]),1) else: curAction.setActionTypeToHit(h[0]) if(h[1].rfind("+")>-1 or h[1].rfind("-")>-1): if(h[1].rfind("+")>-1): curAction.setActionTypeToHit(int(h[1].split("+")[0])+int(h[1].split("+")[1]),2) else: curAction.setActionTypeToHit(int(h[1].split("-")[0])-int(h[1].split("-")[1]),2) else: curAction.setActionTypeToHit(h[1],2) runder.append(curAction) if(c==2): if(len(runder)==0): continue curAction=runder[len(runder)-1] if(line.rfind("+")>-1 or line.rfind("-")>-1): if(line.rfind("+")>-1): curAction.setActionTypeToDam(int(line.split("+")[0])+int(line.split("+")[1]),nextLine) else: curAction.setActionTypeToDam(int(line.split("-")[0])+int(line.split("-")[1]),nextLine) else: curAction.setActionTypeToDam(int(line),nextLine) line=nextLine @app.route('/', methods=['GET', 'POST']) def main(): form = MyForm() runder: List[Action] = list() if form.validate_on_submit(): parseText(runder,form.chatTekst.data.splitlines(),form.ac.data, form.advantage.data) dam=getAllDamage(runder) return render_template('index.html', dam=dam, form=form) if __name__ == '__main__': app.run()