func (this *GameController) Put() { object, err := Game(this) if err == nil { conn := redisPool.Get() defer conn.Close() option := this.GetString("option") correct := &variant.DumpObject{} if object.Current != "" { correct = variant.ConvertToDumpObject(variant.Objects[object.Current]) if object.Current == option { correct = nil } } else { correct = nil } game.SetVariant(object, option) highest, _ := redis.Int(conn.Do("GET", "highest")) if object.Score > highest { if _, err := conn.Do("SET", "highest", object.Score); err != nil { beego.Critical(err) } } if object.Lives <= 0 { this.Data["json"] = &GameResponse{object, nil, correct, "died"} if _, err := conn.Do("INCR", "died"); err != nil { beego.Critical(err) } } else { variant := game.GetVariant(object) if variant != nil { this.Data["json"] = &GameResponse{object, game.GetVariant(object), correct, "ready"} } else { this.Data["json"] = &GameResponse{object, nil, correct, "survived"} if _, err := conn.Do("INCR", "survived"); err != nil { beego.Critical(err) } } } } else { this.Data["json"] = map[string]string{"error": fmt.Sprint(err)} } this.ServeJson() }
func Set(object *Object) (err error) { conn := redisPool.Get() defer conn.Close() data := Dump(object) _, err = conn.Do("HSET", "games", object.ObjectId, data) if err != nil { beego.Critical(err) return errors.New("Can't store object") } return nil }
func Get(objectId string) (response *Object, err error) { conn := redisPool.Get() defer conn.Close() data, err := redis.Bytes(conn.Do("HGET", "games", objectId)) if err != nil { beego.Critical(err) return nil, errors.New("Can't get object") } object := &Object{} err = Restore(data, object) if err != nil { return nil, errors.New("Can't restore object") } return object, nil }