コード例 #1
0
ファイル: main.go プロジェクト: Janrain-Colin/langgame
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
}
コード例 #2
0
ファイル: game.go プロジェクト: JasonRivers/langgame
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()
}
コード例 #3
0
ファイル: main.go プロジェクト: Janrain-Colin/langgame
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
}
コード例 #4
0
ファイル: main.go プロジェクト: Janrain-Colin/langgame
func SetVariant(object *Object, option string) {
	if object.Current != "" {
		conn := redisPool.Get()
		defer conn.Close()

		if object.Current == option {
			object.Score += 100
			conn.Do("HINCRBY", "success", object.Current, 1)
		} else {
			object.Lives -= 1
			conn.Do("HINCRBY", "failure", object.Current, 1)
		}

		object.Completed = append(object.Completed, object.Current)
		object.Current = ""

		Set(object)
	}
}
コード例 #5
0
ファイル: stats.go プロジェクト: Janrain-Colin/langgame
func (this *StatsController) Get() {
	conn := redisPool.Get()
	defer conn.Close()

	var stats StatsObject
	stats.Variants = fmt.Sprintf("%v", len(variant.Objects))

	games, _ := redis.Int(conn.Do("HLEN", "games"))
	stats.Games = fmt.Sprintf("%v", games)

	died, _ := redis.Int(conn.Do("GET", "died"))
	survived, _ := redis.Int(conn.Do("GET", "survived"))
	stats.CompletionRate = fmt.Sprintf("%.1f", (float64(died+survived)/float64(games))*100)

	highest, _ := redis.Int(conn.Do("GET", "highest"))
	stats.Highest = fmt.Sprintf("%v", highest)

	stats.Easiest = FindMaxVariant(conn, "success", "failure")
	stats.Hardest = FindMaxVariant(conn, "failure", "success")

	this.Data["json"] = stats
	this.ServeJson()
}