Battle structure, and cleanup

This commit is contained in:
skiesel 2019-07-07 21:35:31 -04:00
parent 46ac787c06
commit 918c8d46c8
5 changed files with 189 additions and 11 deletions

View File

@ -23,3 +23,11 @@ type AbilityRef struct {
func (a *Ability) string() string {
return fmt.Sprintf("%s : %d DM, %d HL, %d SH, %d WK, %d CR\n", a.Name, a.Damage, a.Heal, a.Shield, a.Weaken, a.Critical)
}
func (a *Ability) applyCreature(creature, targetCreature *Creature) string {
return ""
}
func (a *Ability) applyPlayer(creature *Creature, targetPlayer *Player) string {
return ""
}

View File

@ -87,3 +87,14 @@ func (vp *VelouremonPlugin) handleAddAbility(c bot.Connector, tokens []string) b
vp.bot.Send(c, bot.Message, vp.channel, "!add_ability [name] [damage 0-255] [heal 0-255] [shield 0-255] [weaken 0-255] [critical 0-255]")
return true
}
func (vp *VelouremonPlugin) handleBattle(c bot.Connector, player *Player) bool {
id, _ := vp.bot.Send(c, bot.Message, vp.channel, "Let's get ready to rumble!")
vp.threads[id] = &Interaction {
id: id,
players: []*Player{player},
creatures: []*Creature{},
}
vp.bot.Send(c, bot.Reply, vp.channel, "Let's get ready to rumble!", id)
return true
}

View File

@ -13,12 +13,26 @@ type Interaction struct {
id string
players []*Player
creatures []*Creature
started bool
lastUpdate time.Time
}
func randomInteraction(c bot.Connector, vp *VelouremonPlugin) {
for {
<-vp.timer.C
fifteenMinutesAgo := time.Now().Add(-15 * time.Minute)
remove := []string{}
for _, i := range vp.threads {
if i.lastUpdate.Before(fifteenMinutesAgo) {
vp.bot.Send(c, bot.Reply, vp.channel, "Cleaning up the bodies.", i.id)
remove = append(remove, i.id)
}
}
for _, id := range remove {
delete(vp.threads, id)
}
if vp.channel != "" {
creature := vp.creatures[rand.Intn(len(vp.creatures))]
message := fmt.Sprintf("A wild %s appeared.", creature.Name)
@ -30,23 +44,43 @@ func randomInteraction(c bot.Connector, vp *VelouremonPlugin) {
creatures: []*Creature{
vp.buildOutCreature(creature),
},
started: false,
lastUpdate: time.Now(),
}
vp.bot.Send(c, bot.Reply, vp.channel, "A wild %s appeared.", id)
vp.bot.Send(c, bot.Reply, vp.channel, message, id)
}
vp.timer.Reset(1 * time.Hour)
vp.timer.Reset(time.Duration(15 + rand.Intn(30)) * time.Minute)
}
}
func (i *Interaction) handleMessage(vp *VelouremonPlugin, c bot.Connector, player *Player, tokens []string) bool {
i.lastUpdate = time.Now()
if len(tokens) > 0 {
command := strings.ToLower(tokens[0])
if command == "join" {
return i.handleJoin(vp, c, player)
} else if command == "run" {
return i.handleRun(vp, c, player)
if len(tokens) == 1 {
command := strings.ToLower(tokens[0])
if command == "status" {
vp.bot.Send(c, bot.Reply, vp.channel, player.string(), i.id)
} else if command == "join" {
return i.handleJoin(vp, c, player)
} else if command == "run" {
return i.handleRun(vp, c, player)
} else if command == "flail" {
return i.handleFlail(vp, c, player)
}
} else if len(tokens) == 3 {
src := strings.ToLower(tokens[0])
ability := strings.ToLower(tokens[1])
target := strings.ToLower(tokens[2])
return i.handleAbilityUseCreature(vp, c, player, src, ability, target)
} else if len(tokens) == 4 {
src := strings.ToLower(tokens[0])
ability := strings.ToLower(tokens[1])
targetPlayer := strings.ToLower(tokens[2])
target := strings.ToLower(tokens[3])
return i.handleAbilityUsePlayer(vp, c, player, src, ability, targetPlayer, target)
}
}
return false
@ -75,3 +109,122 @@ func (i *Interaction) handleRun(vp *VelouremonPlugin, c bot.Connector, player *P
vp.bot.Send(c, bot.Reply, vp.channel, player.Name + " is not currently in the party.", i.id)
return true
}
func (i *Interaction) handleFlail(vp *VelouremonPlugin, c bot.Connector, player *Player) bool {
return true
}
func (i *Interaction) handleAbilityUseCreature(vp *VelouremonPlugin, c bot.Connector, player *Player, src, ab, target string) bool {
var creature *Creature
for _, c := range player.Creatures {
if strings.ToLower(c.Name) == src {
creature = c
break
}
}
if creature == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature source: " + src, i.id)
return true
}
var ability *Ability
for _, a := range creature.Abilities {
if strings.ToLower(a.Name) == ab {
ability = a
break
}
}
if ability == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature ability: " + ab, i.id)
return true
}
var targetCreature *Creature
for _, c := range i.creatures {
if strings.ToLower(c.Name) == target {
targetCreature = c
break
}
}
var targetPlayer *Player
for _, p := range i.players {
if strings.ToLower(p.Name) == target {
targetPlayer = p
break
}
}
if targetCreature == nil && targetPlayer == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature/player target: " + target, i.id)
return true
}
if targetCreature != nil {
vp.bot.Send(c, bot.Reply, vp.channel, fmt.Sprintf("%s's %s used %s on %s", player.Name, creature.Name, ability.Name, targetCreature.Name), i.id)
status := ability.applyCreature(creature, targetCreature)
vp.bot.Send(c, bot.Reply, vp.channel, status, i.id)
} else {
vp.bot.Send(c, bot.Reply, vp.channel, fmt.Sprintf("%s's %s used %s on %s", player.Name, creature.Name, ability.Name, targetPlayer.Name), i.id)
status := ability.applyPlayer(creature, targetPlayer)
vp.bot.Send(c, bot.Reply, vp.channel, status, i.id)
}
return true
}
func (i *Interaction) handleAbilityUsePlayer(vp *VelouremonPlugin, c bot.Connector, player *Player, src, ab, tPlayer, target string) bool {
var creature *Creature
for _, c := range player.Creatures {
if strings.ToLower(c.Name) == src {
creature = c
break
}
}
if creature == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature source: " + src, i.id)
return true
}
var ability *Ability
for _, a := range creature.Abilities {
if strings.ToLower(a.Name) == ab {
ability = a
break
}
}
if ability == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature ability: " + ab, i.id)
return true
}
var targetPlayer *Player
for _, p := range i.players {
if strings.ToLower(p.Name) == tPlayer {
targetPlayer = p
break
}
}
if targetPlayer == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized player target: " + tPlayer, i.id)
return true
}
var targetCreature *Creature
for _, c := range targetPlayer.Creatures {
if strings.ToLower(c.Name) == target {
targetCreature = c
break
}
}
if targetCreature == nil {
vp.bot.Send(c, bot.Reply, vp.channel, "Unrecognized creature target: " + target, i.id)
return true
}
vp.bot.Send(c, bot.Reply, vp.channel, fmt.Sprintf("%s's %s used %s on %s's %s", player.Name, creature.Name, ability.Name, targetPlayer.Name, targetCreature.Name), i.id)
status := ability.applyCreature(creature, targetCreature)
vp.bot.Send(c, bot.Reply, vp.channel, status, i.id)
return true
}

View File

@ -9,9 +9,9 @@ func (vp *VelouremonPlugin) populateDBWithBaseData() {
"Charpov", 10, 10)
vp.db.Exec(`insert into velouremon_abilities (name, damage, heal, shield, weaken, critical) values (?, ?, ?, ?, ?, ?);`,
"Procrastinate", 0, 0, 10, 0, 0)
"Heal", 0, 10, 0, 0, 0)
vp.db.Exec(`insert into velouremon_abilities (name, damage, heal, shield, weaken, critical) values (?, ?, ?, ?, ?, ?);`,
"Defend", 0, 0, 5, 0, 0)
"Attack", 10, 0, 0, 0, 0)
vp.db.Exec(`insert into velouremon_abilities (name, damage, heal, shield, weaken, critical) values (?, ?, ?, ?, ?, ?);`,
"Graduate", 0, 255, 0, 0, 0)
}

View File

@ -63,6 +63,12 @@ func (vp *VelouremonPlugin) message(c bot.Connector, kind bot.Kind, message msg.
return false
}
return vp.handleStatus(c, player)
} else if command == "battle" {
player, err := vp.getOrAddPlayer(message.User)
if err != nil {
return false
}
return vp.handleBattle(c, player)
} else if len(tokens) > 1 {
if command == "add_creature" {
return vp.handleAddCreature(c, tokens[1:])