func init() { pt.AddHandler("after_init", func(args ...interface{}) { Conf = lib.NewTree() if e := Conf.LoadYaml(pt.ConfDir+"model.yml", true); e != nil { log.Fatal(e) } if v, has := Conf.Int("map.resource_max"); has { ResourceMax = v } if v, has := Conf.Int("map.resource_min"); has { ResourceMin = v } if v, has := Conf.Int("map.resource_rate"); has { ResourceRate = v } if v, has := Conf.Int("map.energe_rate"); has { EnergyRate = v } }) pt.AddHandler("after_init", func(args ...interface{}) { MapModel = newMapModel() UserModel = &userModel{orm.NewModel("user", &User{})} RoleModel = &roleModel{orm.NewModel("role", &Role{})} UserRoleModel = &userRoleModel{orm.NewModel("user_role", &UserRole{})} BaseModel = &baseModel{orm.NewModel("base", &Base{})} CenterModel = ¢erModel{orm.NewModel("main", &Center{})} LabModel = &labModel{orm.NewModel("lab", &Lab{})} ArmoryModel = &armoryModel{orm.NewModel("armory", &Armory{})} SupplyModel = &supplyModel{orm.NewModel("supply", &Supply{})} StopeModel = &stopeModel{orm.NewModel("stope", &Stope{})} StorageModel = &storageModel{orm.NewModel("storage", &Storage{})} }) }
func newMapModel() *mapModel { m := &mapModel{ Model: orm.NewModel("map", &Location{}), mlocker: &sync.Mutex{}, elocker: &sync.Mutex{}, RefreshState: RefreshStateOff, } m.Sum([]string{"metal", "energy"}, &m.metal, &m.energy) go m.refresh() return m }
panic("could not hash salt") } u.Salt = hex.EncodeToString(hash.Sum(nil)) u.Passwd = UserModel.HashPasswd(passwd, u.Salt) } func (u *User) CheckPasswd(passwd string) bool { return UserModel.HashPasswd(passwd, u.Salt) == u.Passwd } type userModel struct { *orm.Model } var UserModel = &userModel{orm.NewModel("user", new(User))} func (m *userModel) Find(id int64) *User { var u *User rows, e := orm.NewStmt().Select("u.*").From("User", "u"). Where("u.id = ?").Query(id) if e == nil { rows.ScanRow(&u) } return u } func (m *userModel) FindByEmail(email string) *User { var u *User
func (c *Comment) SetUser(u *User) { c.Uid = u.Id c.user = u } type CommentForm struct { Content string State int } func (f *CommentForm) LoadData(r *potato.Request) { f.Content, _ = r.String("content") f.State, _ = r.Int("state") } var CommentModel = &commentModel{orm.NewModel("comment", new(Comment))} type commentModel struct { *orm.Model } func (m *commentModel) FindBy(k string, v interface{}) []*Comment { stmt := orm.NewStmt().Select("c.*").From("Comment", "c"). Where(fmt.Sprintf("`c`.`%s` = ?", k)).Asc("id") rows, e := stmt.Query(v) if e != nil { return nil } comments := make([]*Comment, 0)
type TopicForm struct { Title, Content, Message string State int } func (f *TopicForm) LoadData(r *potato.Request) { f.Title, _ = r.String("title") f.Content, _ = r.String("content") f.State, _ = r.Int("state") } type topicModel struct { *orm.Model } var TopicModel = &topicModel{orm.NewModel("topic", new(Topic))} func (m *topicModel) Search(k, v string) []*Topic { stmt := orm.NewStmt().Select("t.*").From("Topic", "t"). Desc("id").Where(fmt.Sprintf( "`t`.`%s` LIKE ? AND `t`.`state` = ?", k)) rows, e := stmt.Query("%"+v+"%", TopicStatePublished) if e != nil { log.Println(e) return nil } topics := make([]*Topic, 0) for rows.Next() { var t *Topic