Esempio n. 1
0
func InsertTestPlayer() *models.Player {
	user := SelectTestUser()
	game := SelectTestGame()
	player := &models.Player{0, user.Id, game.Id, nil, models.TimeTrackedModel{}}
	err := controllers.Dbm.Insert(player)
	if err != nil {
		revel.WARN.Print(err)
	}
	human_code := models.HumanCode{player.Id, "", models.TimeTrackedModel{}}
	human_code.GenerateCode()
	err = controllers.Dbm.Insert(&human_code)
	if err != nil {
		revel.WARN.Print(err)
	}
	err = models.CreateJoinedGameEvent(player)
	if err != nil {
		revel.WARN.Print(err)
	}
	return player
}
Esempio n. 2
0
func (c *Players) Create() revel.Result {
	data, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		return c.RenderError(err)
	}

	const keyName string = "players"
	var typedJson map[string][]models.Player

	err = json.Unmarshal(data, &typedJson)
	if err != nil {
		return c.RenderError(err)
	}

	modelObjects := typedJson[keyName]

	// Prepare for bulk insert (only way to do it, promise)
	interfaces := make([]interface{}, len(modelObjects))
	for i, player := range modelObjects {
		interfaces[i] = interface{}(&player)

		user_id := player.User_id
		game_id := player.Game_id

		result, err := MemberExists(user_id, game_id)
		if err != nil {
			return c.RenderError(err)
		}
		revel.WARN.Print("Member exists: ", result)
		// if this user is not a member of an org, add them
		game, err := models.GameFromId(game_id)
		if err != nil {
			return c.RenderError(err)
		}
		member := models.Member{0, user_id, game.Organization_id, models.TimeTrackedModel{}}
		err = Dbm.Insert(&member)
		if err != nil {
			return c.RenderError(err)
		}
	}

	// do the bulk insert
	err = Dbm.Insert(interfaces...)
	if err != nil {
		return c.RenderError(err)
	}

	// Return a copy of the data with id's set
	for _, playerInterface := range interfaces {
		player := playerInterface.(*models.Player)
		// add a human code for the player
		human_code := models.HumanCode{player.Id, "", models.TimeTrackedModel{}}
		human_code.GenerateCode()
		err = Dbm.Insert(&human_code)
		if err != nil {
			return c.RenderError(err)
		}
		err = models.CreateJoinedGameEvent(playerInterface.(*models.Player))
		if err != nil {
			return c.RenderError(err)
		}
	}

	out := make(map[string]interface{})
	out[keyName] = interfaces
	return c.RenderJson(out)
}