Fixed bugs found on initial unveiling in beers and the handler code.

Beers now counts correctly and does not die when it can't find a user. The code
for nonexistant users was bogus and the code for setting beers was not counting
dates.

Handlers were not breaking after finding users (linear scan, ack). This should
probably be switched to a map[string]user type.
This commit is contained in:
Chris Sexton 2012-08-24 21:43:53 -04:00
parent 3953f0a831
commit a71e743e79
3 changed files with 9 additions and 10 deletions

View File

@ -61,7 +61,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
return &Bot{
Config: config,
Plugins: make(map[string]Handler),
Users: make([]User, 10),
Users: make([]User, 1),
Conn: c,
DbSession: session,
Db: db,

View File

@ -18,7 +18,9 @@ func (b *Bot) checkuser(nick string) *User {
var user *User = nil
for _, usr := range b.Users {
if usr.Name == nick {
fmt.Println("Matched ", nick)
user = &usr
break
}
}
if user == nil {
@ -106,10 +108,6 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
}
parts := strings.Fields(strings.ToLower(filteredMessage))
if iscmd {
fmt.Println("Hey, I got a command!")
}
user.MessageLog = append(user.MessageLog, message)
if strings.HasPrefix(filteredMessage, "help") && iscmd{
@ -125,7 +123,7 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
Command: iscmd,
Action: isaction,
}
fmt.Printf("%#v\n", msg)
fmt.Printf("%#v said %#v\n", user, msg)
for _, p := range b.Plugins {
if p.Message(msg) {
break

View File

@ -43,12 +43,12 @@ func (u *userBeers) Save(coll *mgo.Collection) {
func getUserBeers(coll *mgo.Collection, nick string) *userBeers {
ub := userBeers{New: true}
err := coll.Find(bson.M{"nick": nick}).One(&ub)
if err != nil {
panic(err)
}
coll.Find(bson.M{"nick": nick}).One(&ub)
if ub.New == true {
ub.New = false
ub.Nick = nick
ub.Beercount = 0
ub.Momentum = 0
ub.Save(coll)
}
return &ub
@ -146,6 +146,7 @@ func (p *BeersPlugin) Help(channel string, parts []string) {
func (p *BeersPlugin) setBeers(user *bot.User, amount int) {
ub := getUserBeers(p.Coll, user.Name)
ub.Beercount = amount
ub.Lastdrunk = time.Now()
ub.Save(p.Coll)
}