statbot/processor_levelup.go

158 lines
4.2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
2015-11-02 19:48:29 +00:00
type levelUpStatProcessor struct{}
func (p *levelUpStatProcessor) GetName() string {
return "LevelUp Statistics"
}
func (p *levelUpStatProcessor) GetStatKeys() []string {
return []string{
"levelup-*",
}
}
2015-10-29 19:16:45 +00:00
func (p *levelUpStatProcessor) ProcessMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessAdminMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessBotMessage(m *Message) {}
2015-10-29 19:16:45 +00:00
func (p *levelUpStatProcessor) ProcessUserMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessAdminUserMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessBotUserMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessChannelMessage(m *Message) {
// levelup XP, for now, is awarded like this:
// Message in #random: 1 xp
// Message in #general: 2 xp
// Message in any other channel: 3 xp + 1 xp for that channel
chnl, err := getChannelInfo(m.Channel)
if err == nil {
if chnl.Name == "random" {
addUserStat(m.User, "levelup-xp", 1)
} else if chnl.Name == "general" {
addUserStat(m.User, "levelup-xp", 2)
} else {
addUserStat(m.User, "levelup-xp", 3)
addUserStat(m.User, "levelup-xp-"+chnl.Name, 1)
}
}
}
2015-10-29 19:16:45 +00:00
func (p *levelUpStatProcessor) ProcessAdminChannelMessage(m *Message) {}
func (p *levelUpStatProcessor) ProcessBotChannelMessage(m *Message) {}
2015-11-02 19:48:29 +00:00
/*
* Web Site Module
*/
type levelUpWebModule struct{}
func (wm *levelUpWebModule) GetName() string {
return "LevelUp Web Module"
}
func (wm *levelUpWebModule) GetRoutes() map[string]func(http.ResponseWriter, *http.Request) {
ret := make(map[string]func(http.ResponseWriter, *http.Request))
ret["/levelup/"] = wm.handleLevelUpGeneral
ret["/levelup/profile/{username}"] = wm.handleLevelUpProfile
2015-11-02 19:48:29 +00:00
return ret
}
func (wm *levelUpWebModule) Register() {
for k, v := range wm.GetRoutes() {
r.HandleFunc(k, v)
}
}
func (wm *levelUpWebModule) GetMenuEntries() []menuItem {
var ret []menuItem
ret = append(ret, menuItem{Text: "LevelUp!", Link: "/levelup/"})
return ret
}
func (wm *levelUpWebModule) GetBottomMenuEntries() []menuItem {
var ret []menuItem
return ret
}
func (wm *levelUpWebModule) handleLevelUpGeneral(w http.ResponseWriter, req *http.Request) {
type UserLevelUpStats struct {
Name string
Xp int
ChannelStats map[string]int
}
type StatData struct {
UserStats []UserLevelUpStats
}
openDatabase()
userLst := getUserList()
var userStats []UserLevelUpStats
for _, k := range userLst {
usrName := getUserName(k)
if usrName == "stat_bot" || usrName == "bot" {
continue
}
xpVal, _ := getUserStat(k, "levelup-xp")
userStats = append(userStats, UserLevelUpStats{
Name: usrName,
Xp: xpVal,
ChannelStats: getAllLevelUpChannelXp(k),
})
}
closeDatabase()
site.TemplateData = StatData{UserStats: userStats}
2015-11-02 19:48:29 +00:00
initRequest(w, req)
setMenuItemActive("LevelUp!")
sc := "var levelUpStats = {"
sc = sc + "users:["
if len(userStats) > 0 {
for _, k := range userStats {
sc = sc + "{"
sc = sc + "name:\"" + k.Name + "\","
sc = fmt.Sprintf("%sxp:%d,", sc, k.Xp)
sc = sc + "channels:["
if len(k.ChannelStats) > 0 {
for chK, chV := range k.ChannelStats {
sc = fmt.Sprintf("%s{name:\"%s\",xp:%d},", sc, chK, chV)
}
sc = sc[:len(sc)-1]
}
sc = sc + "]},"
}
sc = sc[:len(sc)-1]
}
sc = sc + "]};"
addToInlineScript(sc)
site.Scripts = append(site.Scripts, "/assets/js/levelup_main.js")
2015-11-02 19:48:29 +00:00
showPage("levelup-main.html", site, w)
2015-11-02 19:48:29 +00:00
}
func (wm *levelUpWebModule) handleLevelUpProfile(w http.ResponseWriter, req *http.Request) {
type UserLevelUpStats struct {
Name string
Xp int
ChannelStats map[string]int
OtherStats map[string]int
}
var u *User
var err error
vars := mux.Vars(req)
if u, err = getUserInfoFromName(vars["username"]); err != nil {
pageNotFound(fmt.Errorf(fmt.Sprintf("Couldn't find a profile for user "+vars["username"])+"\n%s\n", err), w)
return
}
p := UserLevelUpStats{Name: vars["username"]}
p.Xp, _ = getUserStat(u.ID, "levelup-xp")
p.ChannelStats = getAllLevelUpChannelXp(u.ID)
p.OtherStats = getAllNonLevelUpStats(u.ID)
site.TemplateData = p
initRequest(w, req)
showPage("levelup-profile.html", site, w)
}