mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 04:50:56 +00:00
53 lines
996 B
Go
53 lines
996 B
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
Objects map[string]*Object
|
||
|
)
|
||
|
|
||
|
type Object struct {
|
||
|
ObjectId string
|
||
|
Score int64
|
||
|
PlayerName string
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
Objects = make(map[string]*Object)
|
||
|
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
||
|
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
||
|
}
|
||
|
|
||
|
func AddOne(object Object) (ObjectId string) {
|
||
|
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||
|
Objects[object.ObjectId] = &object
|
||
|
return object.ObjectId
|
||
|
}
|
||
|
|
||
|
func GetOne(ObjectId string) (object *Object, err error) {
|
||
|
if v, ok := Objects[ObjectId]; ok {
|
||
|
return v, nil
|
||
|
}
|
||
|
return nil, errors.New("ObjectId Not Exist")
|
||
|
}
|
||
|
|
||
|
func GetAll() map[string]*Object {
|
||
|
return Objects
|
||
|
}
|
||
|
|
||
|
func Update(ObjectId string, Score int64) (err error) {
|
||
|
if v, ok := Objects[ObjectId]; ok {
|
||
|
v.Score = Score
|
||
|
return nil
|
||
|
}
|
||
|
return errors.New("ObjectId Not Exist")
|
||
|
}
|
||
|
|
||
|
func Delete(ObjectId string) {
|
||
|
delete(Objects, ObjectId)
|
||
|
}
|