diff --git a/internal/handler/games/wavinghands/spells/cause-heavy-wounds.go b/internal/handler/games/wavinghands/spells/cause-heavy-wounds.go index bee75c0..11def61 100644 --- a/internal/handler/games/wavinghands/spells/cause-heavy-wounds.go +++ b/internal/handler/games/wavinghands/spells/cause-heavy-wounds.go @@ -13,24 +13,36 @@ type CauseHeavyWounds struct { Description string `json:"description"` Usage string `json:"usage"` Damage int `json:"damage"` - Resistences string `json:"resistences"` + Resistances string `json:"resistances"` Protections string `json:"protections"` } -func (cHW CauseHeavyWounds) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { - if (len(wizard.Right.Sequence) >= len(cHW.Sequence) && strings.HasSuffix(wizard.Right.Sequence, cHW.Sequence)) || - (len(wizard.Left.Sequence) >= len(cHW.Sequence) && strings.HasSuffix(wizard.Left.Sequence, cHW.ShSequence)) { +func (cHW *CauseHeavyWounds) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { + var returnString string = "" + if strings.HasSuffix(wizard.Right.Sequence, cHW.Sequence) { if strings.Contains(target.Wards, "cureHeavyWounds") { - target.HitPoints -= 1 - return fmt.Sprintf("%s caused heavy wounds on %s but they were protected and only sustained minimal damage", wizard.Name, target.Selector), nil + target.HitPoints -= cHW.Damage - 2 + returnString = fmt.Sprintf("%s caused heavy wounds on %s but they were protected and only sustained minimal damage", wizard.Name, target.Selector) } else { - target.HitPoints -= 3 - return fmt.Sprintf("%s caused heavy wounds on %s", wizard.Name, target.Selector), nil + target.HitPoints -= cHW.Damage + returnString = fmt.Sprintf("%s caused heavy wounds on %s", wizard.Name, target.Selector) + } + } + if strings.HasSuffix(wizard.Left.Sequence, cHW.Sequence) { + if returnString != "" { + returnString = returnString + "\n" + } + if strings.Contains(target.Wards, "cureHeavyWounds") { + target.HitPoints -= cHW.Damage - 2 + returnString += fmt.Sprintf("%s caused heavy wounds on %s but they were protected and only sustained minimal damage", wizard.Name, target.Selector) + } else { + target.HitPoints -= cHW.Damage + returnString += fmt.Sprintf("%s caused heavy wounds on %s", wizard.Name, target.Selector) } } - return "", nil + return returnString, nil } func GetCauseHeavyWoundsSpell(s *wavinghands.Spell, e error) (*CauseHeavyWounds, error) { if e != nil { @@ -44,7 +56,7 @@ func GetCauseHeavyWoundsSpell(s *wavinghands.Spell, e error) (*CauseHeavyWounds, Description: s.Description, Usage: s.Usage, Damage: s.Damage, - Resistences: s.Resistances, + Resistances: s.Resistances, Protections: s.Protections, }, nil } diff --git a/internal/handler/games/wavinghands/spells/cure-heavy-wounds.go b/internal/handler/games/wavinghands/spells/cure-heavy-wounds.go index bfa9822..7fa1de9 100644 --- a/internal/handler/games/wavinghands/spells/cure-heavy-wounds.go +++ b/internal/handler/games/wavinghands/spells/cure-heavy-wounds.go @@ -7,6 +7,8 @@ import ( "strings" ) +const HealingPoints = 2 + type CureHeavyWounds struct { Name string `json:"name"` Sequence string `json:"sequence"` @@ -14,21 +16,35 @@ type CureHeavyWounds struct { Description string `json:"description"` Usage string `json:"usage"` Damage int `json:"damage"` - Resistences string `json:"resistences"` + Resistances string `json:"resistances"` Protections string `json:"protections"` } -func (cHW CureHeavyWounds) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { - if (len(wizard.Right.Sequence) >= len(cHW.Sequence) && strings.HasSuffix(wizard.Right.Sequence, cHW.Sequence)) || - (len(wizard.Left.Sequence) >= len(cHW.Sequence) && strings.HasSuffix(wizard.Left.Sequence, cHW.ShSequence)) { +func (cHW *CureHeavyWounds) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { + var returnString string = "" + if strings.HasSuffix(wizard.Right.Sequence, cHW.Sequence) { wards := strings.Split(target.Wards, ",") wards = append(wards, "cureHeavyWounds") // Lasts one round target.Wards = strings.Join(wards, ",") - return fmt.Sprintf("%s has cast Cure Heavy Wounds on %s", wizard.Name, target.Selector), nil + returnString += fmt.Sprintf("%s has cast Cure Heavy Wounds on %s", wizard.Name, target.Selector) } + if strings.HasSuffix(wizard.Left.Sequence, cHW.Sequence) { + if returnString != "" { + returnString += "\n" + } + wards := strings.Split(target.Wards, ",") + wards = append(wards, "cureHeavyWounds") // Lasts one round + target.Wards = strings.Join(wards, ",") - return "", nil + target.HitPoints += HealingPoints + if target.HitPoints > wavinghands.MaxWhHp { + target.HitPoints = wavinghands.MaxWhHp + } + returnString += fmt.Sprintf("%s has cast Cure Heavy Wounds on %s", wizard.Name, target.Selector) + } + + return returnString, nil } func GetCureHeavyWoundsSpell(s *wavinghands.Spell, e error) (*CureHeavyWounds, error) { @@ -43,15 +59,21 @@ func GetCureHeavyWoundsSpell(s *wavinghands.Spell, e error) (*CureHeavyWounds, e Description: s.Description, Usage: s.Usage, Damage: s.Damage, - Resistences: s.Resistances, + Resistances: s.Resistances, Protections: s.Protections, }, nil } -func (cHW CureHeavyWounds) clear(target *wavinghands.Living) error { +func (cHW CureHeavyWounds) Clear(target *wavinghands.Living) error { wards := strings.Split(target.Wards, ",") idx := slices.Index(wards, "cureHeavyWounds") - wavinghands.Remove(wards, idx) - target.Wards = strings.Join(wards, ",") + if idx >= 0 { + wavinghands.Remove(wards, idx) + } + if len(wards) > 0 { + target.Wards = strings.Join(wards, ",") + } else { + target.Wards = "" + } return nil } diff --git a/internal/handler/games/wavinghands/spells/missile.go b/internal/handler/games/wavinghands/spells/missile.go new file mode 100644 index 0000000..8f88af0 --- /dev/null +++ b/internal/handler/games/wavinghands/spells/missile.go @@ -0,0 +1,61 @@ +package spells + +import ( + "fmt" + "github.com/pyrousnet/pyrous-gobot/internal/handler/games/wavinghands" + "strings" +) + +type Missile struct { + Name string `json:"name"` + Sequence string `json:"sequence"` + ShSequence string `json:"sh-sequence"` + Description string `json:"description"` + Usage string `json:"usage"` + Damage int `json:"damage"` + Resistances string `json:"resistances"` + Protections string `json:"protections"` +} + +func (m *Missile) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { + var returnString string = "" + if strings.HasSuffix(wizard.Right.Sequence, m.Sequence) { + + if strings.Contains(target.Wards, "shield") { + returnString += fmt.Sprintf("%s cast a missile at %s but they were protected by a shield and took no damage", wizard.Name, target.Selector) + } else { + target.HitPoints -= m.Damage + returnString += fmt.Sprintf("%s cast a missile at %s", wizard.Name, target.Selector) + } + } + if strings.HasSuffix(wizard.Left.Sequence, m.Sequence) { + if returnString != "" { + returnString += "\n" + } + if strings.Contains(target.Wards, "shield") { + returnString += fmt.Sprintf("%s cast a missile at %s but they were protected by a shield and took no damage", wizard.Name, target.Selector) + } else { + target.HitPoints -= m.Damage + returnString += fmt.Sprintf("%s cast a missile at %s", wizard.Name, target.Selector) + } + } + + return returnString, nil +} + +func GetMissileSpell(s *wavinghands.Spell, e error) (*Missile, error) { + if e != nil { + return &Missile{}, e + } + + return &Missile{ + Name: s.Name, + Sequence: s.Sequence, + ShSequence: s.ShSequence, + Description: s.Description, + Usage: s.Usage, + Damage: s.Damage, + Resistances: s.Resistances, + Protections: s.Protections, + }, nil +} diff --git a/internal/handler/games/wavinghands/spells/shield.go b/internal/handler/games/wavinghands/spells/shield.go new file mode 100644 index 0000000..26e2bab --- /dev/null +++ b/internal/handler/games/wavinghands/spells/shield.go @@ -0,0 +1,77 @@ +package spells + +import ( + "fmt" + "github.com/pyrousnet/pyrous-gobot/internal/handler/games/wavinghands" + "golang.org/x/exp/slices" + "strings" +) + +type Shield struct { + Name string `json:"name"` + Sequence string `json:"sequence"` + ShSequence string `json:"sh-sequence"` + Description string `json:"description"` + Usage string `json:"usage"` + Damage int `json:"damage"` + Resistances string `json:"resistances"` + Protections string `json:"protections"` +} + +func (s *Shield) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { + var returnString string + if strings.HasSuffix(wizard.Right.Sequence, s.Sequence) || strings.HasSuffix(wizard.Left.Sequence, s.Sequence) { + wards := strings.Split(target.Wards, ",") + wards = append(wards, "shield") + target.Wards = strings.Join(wards, ",") + + returnString = fmt.Sprintf("%s has cast Shield on %s", wizard.Name, target.Selector) + } + return returnString, nil +} + +func GetShieldSpell(s *wavinghands.Spell, e error) (*Shield, error) { + if e != nil { + return &Shield{}, e + } + + return &Shield{ + Name: s.Name, + Sequence: s.Sequence, + ShSequence: s.ShSequence, + Description: s.Description, + Usage: s.Usage, + Damage: s.Damage, + Resistances: s.Resistances, + Protections: s.Protections, + }, nil +} + +func (s *Shield) Clear(target *wavinghands.Living) error { + if target.Wards == "" { + return nil + } + wards := strings.Split(target.Wards, ",") + idx := slices.Index(wards, "shield") + if idx >= 0 { + wards = wavinghands.Remove(wards, idx) + } + wards = removeEmptyValues(wards) + if len(wards) > 0 { + target.Wards = strings.Join(wards, ",") + } else { + target.Wards = "" + } + return nil +} + +func removeEmptyValues(items []string) []string { + var result []string + for _, str := range items { + if str != "" { + result = append(result, str) + } + } + + return result +} diff --git a/internal/handler/games/wavinghands/spells/stab.go b/internal/handler/games/wavinghands/spells/stab.go new file mode 100644 index 0000000..1c83467 --- /dev/null +++ b/internal/handler/games/wavinghands/spells/stab.go @@ -0,0 +1,63 @@ +package spells + +import ( + "fmt" + "github.com/pyrousnet/pyrous-gobot/internal/handler/games/wavinghands" + "strings" +) + +type Stab struct { + Name string `json:"name"` + Sequence string `json:"sequence"` + ShSequence string `json:"sh-sequence"` + Description string `json:"description"` + Usage string `json:"usage"` + Damage int `json:"damage"` + Resistances string `json:"resistances"` + Protections string `json:"protections"` +} + +func (s *Stab) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { + var returnString string = "" + if strings.HasSuffix(wizard.Right.Sequence, "1") { + wizard.Right.Set(strings.TrimRight(wizard.Right.Sequence, "1")) + + if strings.Contains(target.Wards, "shield") { + returnString += fmt.Sprintf("%s tries to stab %s with right hand but they were protected by a shield and took no damage", wizard.Name, target.Selector) + } else { + target.HitPoints -= s.Damage + returnString += fmt.Sprintf("%s stabbed %s with right hand", wizard.Name, target.Selector) + } + } + if strings.HasSuffix(wizard.Left.Sequence, "1") { + wizard.Left.Set(strings.TrimRight(wizard.Left.Sequence, "1")) + if returnString != "" { + returnString += "\n" + } + if strings.Contains(target.Wards, "shield") { + returnString += fmt.Sprintf("%s tries to stab %s with left hand but they were protected by a shield and took no damage", wizard.Name, target.Selector) + } else { + target.HitPoints -= s.Damage + returnString += fmt.Sprintf("%s stabbed %s with left hand", wizard.Name, target.Selector) + } + } + + return returnString, nil +} + +func GetStabSpell(s *wavinghands.Spell, e error) (*Stab, error) { + if e != nil { + return &Stab{}, e + } + + return &Stab{ + Name: s.Name, + Sequence: s.Sequence, + ShSequence: s.ShSequence, + Description: s.Description, + Usage: s.Usage, + Damage: s.Damage, + Resistances: s.Resistances, + Protections: s.Protections, + }, nil +} diff --git a/internal/handler/games/wavinghands/spells/surrender.go b/internal/handler/games/wavinghands/spells/surrender.go index ffb0683..931e0f5 100644 --- a/internal/handler/games/wavinghands/spells/surrender.go +++ b/internal/handler/games/wavinghands/spells/surrender.go @@ -13,11 +13,11 @@ type Surrender struct { Description string `json:"description"` Usage string `json:"usage"` Damage int `json:"damage"` - Resistences string `json:"resistences"` + Resistances string `json:"resistances"` Protections string `json:"protections"` } -func (s Surrender) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { +func (s *Surrender) Cast(wizard *wavinghands.Wizard, target *wavinghands.Living) (string, error) { if strings.HasSuffix(wizard.Right.Sequence, s.Sequence) && strings.HasSuffix(wizard.Left.Sequence, s.ShSequence) { wizard.Living.HitPoints = 0 @@ -38,7 +38,7 @@ func GetSurrenderSpell(s *wavinghands.Spell, e error) (*Surrender, error) { Description: s.Description, Usage: s.Usage, Damage: s.Damage, - Resistences: s.Resistances, + Resistances: s.Resistances, Protections: s.Protections, }, nil } diff --git a/internal/handler/games/wavinghands/wavinghands.go b/internal/handler/games/wavinghands/wavinghands.go index 621de21..b005950 100644 --- a/internal/handler/games/wavinghands/wavinghands.go +++ b/internal/handler/games/wavinghands/wavinghands.go @@ -10,8 +10,9 @@ import ( const ( minTeams = 2 - maxTeams = 6 + maxTeams = 2 PREFIX = "wh-" + MaxWhHp = 15 ) type ( @@ -62,11 +63,14 @@ func (h *Hand) Set(s string) { h.Sequence = s } -func (h Hand) Get() []byte { +func (h *Hand) Get() []byte { return []byte(h.Sequence) } -func (h Hand) GetAt(index int) byte { +func (h *Hand) GetAt(index int) byte { + if index < 0 { + return ' ' + } return h.Sequence[index] } @@ -107,9 +111,9 @@ func GetHelpSpell(chSp string) string { var response string if spell.Name == "" { - response = fmt.Sprintf("/echo %s wasn't a spell.\n", chSp) + response = fmt.Sprintf("%s wasn't a spell.\n", chSp) } else { - response = fmt.Sprintf("/echo %s is defined as follows:\n```\n", spell.Name) + response = fmt.Sprintf("%s is defined as follows:\n```\n", spell.Name) response += fmt.Sprintf("Description: %s\n", spell.Description) response += fmt.Sprintf("Usage: %s\n", spell.Usage) response += "```\n" diff --git a/internal/handler/games/wh.go b/internal/handler/games/wh.go index 13fa80c..17957d1 100644 --- a/internal/handler/games/wh.go +++ b/internal/handler/games/wh.go @@ -12,6 +12,7 @@ import ( "golang.org/x/exp/slices" "reflect" "strings" + "time" ) type ( @@ -40,7 +41,7 @@ func NewWavingHands(event BotGame) (Game, error) { Left: wavinghands.Hand{}, Name: name, Living: wavinghands.Living{ - HitPoints: 15, + HitPoints: wavinghands.MaxWhHp, }, Curses: "", Protections: "", @@ -62,7 +63,7 @@ func NewWavingHands(event BotGame) (Game, error) { Left: wavinghands.Hand{}, Name: name, Living: wavinghands.Living{ - HitPoints: 15, + HitPoints: wavinghands.MaxWhHp, }, Curses: "", Protections: "", @@ -100,7 +101,7 @@ func StartWavingHands(event BotGame) (Game, error) { if !inGame { return Game{}, fmt.Errorf("player not active in game, cannot start") } - if len(g.gData.Players) > wavinghands.GetMinTeams() && len(g.gData.Players) <= wavinghands.GetMaxTeams() { + if len(g.gData.Players) >= wavinghands.GetMinTeams() && len(g.gData.Players) <= wavinghands.GetMaxTeams() { g.gData.State = "playing" } else if len(g.gData.Players) < wavinghands.GetMinTeams() { return Game{}, fmt.Errorf("not enough players to start the game") @@ -189,7 +190,11 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { directive := parts[0] switch directive { case "help": - response.Message = wavinghands.GetHelpSpell(parts[1]) + if len(parts) > 1 { + response.Message = wavinghands.GetHelpSpell(parts[1]) + } else { + response.Message = "What spell would you like help with?" + } response.Type = "dm" event.ResponseChannel <- response return nil, true @@ -235,6 +240,12 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { return err, true } fmt.Sscanf(event.body, "%s %s %s %s", &channelName, &rGesture, &lGesture, &target) + if target == "" { + response.Type = "dm" + response.Message = "missing target" + event.ResponseChannel <- response + return fmt.Errorf("missing target for waving hands"), true + } if channelName != "" { c, err := event.mm.GetChannelByName(channelName) if err != nil { @@ -248,11 +259,7 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { } g := Game{gData: wHGameData, Channel: channel} - p, err := GetCurrentPlayer(g, name) - - if target != "" { - t, err = FindTarget(g, target) - } + p, err := GetCurrentPlayer(&g, name) if err != nil { return err, true @@ -266,7 +273,7 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { if rGesture == "nothing" { rGesture = "0" } - if rGesture == "stab" { + if rGesture == "nothing" { lGesture = "0" } rightGestures := append(p.Right.Get(), rGesture[0]) @@ -281,56 +288,15 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { // Check All Players for gestures hasAllMoves := CheckAllPlayers(g) if hasAllMoves { - for i, p := range g.gData.Players { - rG := p.Right.GetAt(len(p.Right.Sequence) - 1) - lG := p.Left.GetAt(len(p.Left.Sequence) - 1) - announceGestures(&p, event.ResponseChannel, response, string(rG), string(lG), p.GetTarget()) - sr, err := spells.GetSurrenderSpell(wavinghands.GetSpell("Surrender")) - if err != nil { - return err, true - } - t = &p.Living - surrenderString, err := sr.Cast(&g.gData.Players[i], t) - if err == nil && surrenderString != "" { - response.Message = surrenderString - event.ResponseChannel <- response - } else if err != nil { - return err, true - } - // Run Protection Spells - - cHW, err := spells.GetCureHeavyWoundsSpell(wavinghands.GetSpell("Cure Heavy Wounds")) - if err != nil { - return err, true - } - chwResult, err := cHW.Cast(&g.gData.Players[i], t) - if err == nil && chwResult != "" { - response.Message = chwResult - event.ResponseChannel <- response - } else if err != nil { - return err, true - } - - // Run Damage Spells - CHW, err := spells.GetCauseHeavyWoundsSpell(wavinghands.GetSpell("Cause Heavy Wounds")) - if err != nil { - return err, true - } - chwResult, err = CHW.Cast(&g.gData.Players[i], t) - if err == nil && chwResult != "" { - response.Message = chwResult - event.ResponseChannel <- response - } else if err != nil { - return err, true - } + err, b, done := executeRound(event, &g, t, err, response) + if done { + return err, b } - - // Run Summon Spells - g.gData.Round += 1 } winner, err := getWHWinner(g) if err == nil { + time.Sleep(time.Second) response.Message = fmt.Sprintf("%s has won the game of waving hands.", winner.Name) event.ResponseChannel <- response @@ -342,6 +308,100 @@ func handleGameWithDirective(event BotGame, err error) (error, bool) { return nil, true } +func executeRound(event BotGame, g *Game, t *wavinghands.Living, err error, response comms.Response) (error, bool, bool) { + for i := range g.gData.Players { + p := g.gData.Players[i] + rG := p.Right.GetAt(len(p.Right.Sequence) - 1) + lG := p.Left.GetAt(len(p.Left.Sequence) - 1) + if p.GetTarget() != "" { + t, err = FindTarget(g, p.GetTarget()) + if err != nil { + return err, true, true + } + t.Selector = p.GetTarget() + } + announceGestures(&p, event.ResponseChannel, response, string(rG), string(lG), p.GetTarget()) + sr, err := spells.GetSurrenderSpell(wavinghands.GetSpell("Surrender")) + if err != nil { + return err, true, true + } + surrenderString, err := sr.Cast(&g.gData.Players[i], t) + if err == nil && surrenderString != "" { + response.Message = surrenderString + time.Sleep(time.Second) + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + // Run Protection Spells + shld, err := spells.GetShieldSpell(wavinghands.GetSpell("Shield")) + if err != nil { + return err, true, true + } + shldResult, err := shld.Cast(&g.gData.Players[i], t) + if err == nil && shldResult != "" { + response.Message = shldResult + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + + cHW, err := spells.GetCureHeavyWoundsSpell(wavinghands.GetSpell("Cure Heavy Wounds")) + if err != nil { + return err, true, true + } + chwResult, err := cHW.Cast(&g.gData.Players[i], t) + if err == nil && chwResult != "" { + response.Message = chwResult + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + + // Run Damage Spells + m, mErr := spells.GetMissileSpell(wavinghands.GetSpell("Missile")) + if mErr != nil { + return mErr, true, true + } + mResult, err := m.Cast(&g.gData.Players[i], t) + if err == nil && mResult != "" { + response.Message = mResult + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + s, sErr := spells.GetStabSpell(wavinghands.GetSpell("Stab")) + if sErr != nil { + return sErr, true, true + } + sResult, err := s.Cast(&g.gData.Players[i], t) + if err == nil && sResult != "" { + response.Message = sResult + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + CHW, err := spells.GetCauseHeavyWoundsSpell(wavinghands.GetSpell("Cause Heavy Wounds")) + if err != nil { + return err, true, true + } + chwResult, err = CHW.Cast(&g.gData.Players[i], t) + if err == nil && chwResult != "" { + response.Message = chwResult + event.ResponseChannel <- response + } else if err != nil { + return err, true, true + } + + cHW.Clear(&p.Living) + shld.Clear(t) + } + + // Run Summon Spells + g.gData.Round += 1 + return err, false, false +} + func announceGestures( p *wavinghands.Wizard, channel chan comms.Response, @@ -434,7 +494,7 @@ func getWHWinner(g Game) (wavinghands.Wizard, error) { } } -func FindTarget(g Game, selector string) (*wavinghands.Living, error) { +func FindTarget(g *Game, selector string) (*wavinghands.Living, error) { var name, monster string var wizard *wavinghands.Wizard parts := strings.Split(selector, ":") @@ -447,9 +507,11 @@ func FindTarget(g Game, selector string) (*wavinghands.Living, error) { for i, w := range g.gData.Players { if w.Name == name { wizard = &g.gData.Players[i] + } else { + continue } - if monster == "" { + if wizard != nil && monster == "" { wizard.Living.Selector = selector return &wizard.Living, nil } @@ -467,7 +529,7 @@ func FindTarget(g Game, selector string) (*wavinghands.Living, error) { return &wavinghands.Living{}, fmt.Errorf("not found") } -func GetCurrentPlayer(g Game, name string) (*wavinghands.Wizard, error) { +func GetCurrentPlayer(g *Game, name string) (*wavinghands.Wizard, error) { for i, w := range g.gData.Players { if w.Name == name { return &g.gData.Players[i], nil diff --git a/internal/handler/games/wh_test.go b/internal/handler/games/wh_test.go index b90d43b..b14ecb8 100644 --- a/internal/handler/games/wh_test.go +++ b/internal/handler/games/wh_test.go @@ -3,6 +3,7 @@ package games import ( "github.com/mattermost/mattermost-server/v6/model" "github.com/pyrousnet/pyrous-gobot/internal/cache" + "github.com/pyrousnet/pyrous-gobot/internal/comms" "reflect" "testing" ) @@ -12,64 +13,80 @@ func Test_handleEmptyBody(t *testing.T) { event BotGame response Response } + rs := make(chan comms.Response) tests := []struct { name string args args - want Response - want1 error - want2 bool + want error + want1 bool + want2 comms.Response }{ { name: "empty input", args: args{ event: BotGame{ - body: "", - sender: "", - target: "", - mm: nil, - settings: nil, - ReplyChannel: &model.Channel{Id: "test"}, - method: Method{}, - cache: &cache.MockCache{}, + body: "", + sender: "", + target: "", + mm: nil, + settings: nil, + ReplyChannel: &model.Channel{Id: "test"}, + ResponseChannel: rs, + method: Method{}, + Cache: &cache.MockCache{}, }, }, - want: Response{ - Message: "player is missing a name", - Type: "dm", + want: nil, + want1: true, + want2: comms.Response{ + ReplyChannelId: "test", + Message: "player is missing a name", + Type: "dm", + UserId: "", + Quit: nil, }, - want1: nil, - want2: true, }, { - name: "", + name: "test body", args: args{ event: BotGame{ - body: "", - sender: "test", - target: "", - mm: nil, - settings: nil, - ReplyChannel: &model.Channel{Id: "test"}, - method: Method{}, - cache: &cache.MockCache{}, + body: "", + sender: "test", + target: "", + mm: nil, + settings: nil, + ReplyChannel: &model.Channel{Id: "test"}, + ResponseChannel: rs, + method: Method{}, + Cache: &cache.MockCache{}, }, }, - want: Response{ - Message: "/echo test would like to play a game of Waving Hands.\n", + want: nil, + want1: false, + want2: comms.Response{ + ReplyChannelId: "test", + Message: "/echo test would like to play a game of Waving Hands.\n", + Type: "command", + UserId: "", + Quit: nil, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, got1, got2 := handleEmptyBody(tt.args.event, tt.args.response) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("handleEmptyBody() got = %v, want %v", got, tt.want) - } - if !reflect.DeepEqual(got1, tt.want1) { - t.Errorf("handleEmptyBody() got1 = %v, want %v", got1, tt.want1) - } - if got2 != tt.want2 { - t.Errorf("handleEmptyBody() got2 = %v, want %v", got2, tt.want2) + go func() { + got, got1 := handleEmptyBody(tt.args.event) + if got != tt.want { + t.Errorf("handleEmptyBody() got = %v, want %v", got, tt.want1) + } + + if got1 != tt.want1 { + t.Errorf("handleEmptyBody() got1 = %v, want1 %v", got1, tt.want1) + } + }() + got2 := <-rs + if !reflect.DeepEqual(got2, tt.want2) { + t.Errorf("handleEmptyBody() got2 = %v, want2 %v", got2, tt.want2) } }) } diff --git a/internal/users/users.go b/internal/users/users.go index aaa3ec0..ba30ccd 100644 --- a/internal/users/users.go +++ b/internal/users/users.go @@ -44,14 +44,16 @@ func SetupUsers(mm *mmclient.MMClient, c cache.Cache) error { func HandlePost(post *model.Post, mm *mmclient.MMClient, c cache.Cache) error { user, _, err := mm.Client.GetUser(post.UserId, "") - key := KeyPrefix + user.Username - persisted, ok, _ := GetUser(user.Username, c) + if err == nil { + key := KeyPrefix + user.Username + persisted, ok, _ := GetUser(user.Username, c) - if ok { - persisted.Message = post.Message + if ok { + persisted.Message = post.Message - ub, _ := json.Marshal(persisted) - c.Put(key, ub) + ub, _ := json.Marshal(persisted) + c.Put(key, ub) + } } return err diff --git a/spells.json b/spells.json index 19b668e..f549ba9 100644 --- a/spells.json +++ b/spells.json @@ -82,7 +82,7 @@ "sequence": "p", "sh-sequence": "", "description": "This spell protects the subject from monsters (that is, creatures created by a summoning spell), from missile spells and from stabs by wizards. The shield will block any number of such attacks but lasts for only one round. The shield protects the subject on the turn in which it was cast.", - "usage": "Counter-Spell can be cast with Gestures: \nProffered Palm (P)", + "usage": "Shield can be cast with Gestures: \nProffered Palm (P)", "damage": 0, "resistances": "", "protections": "shield"