func (c *Games) ReadBasicStats(game_id int) revel.Result { game, err := models.GameFromId(game_id) if err != nil { return c.RenderError(err) } player_count := ` SELECT count(1) FROM player WHERE player.game_id = $1 ` nPlayer, err := Dbm.SelectInt(player_count, game_id) if err != nil { return c.RenderError(err) } zombies := ` SELECT count(1) FROM player WHERE player.game_id = $1 AND player.last_fed < $2 ` nZombies, err := Dbm.SelectInt(zombies, game_id, time.Now().Add(-game.TimeToStarve())) if err != nil { return c.RenderError(err) } tag_list := ` SELECT claimed "value" FROM tag INNER JOIN player ON tag.tagger_id = player.id WHERE player.game_id = $1 ` var wrapped_tag_timestamps []*date_wrapper _, err = Dbm.Select(&wrapped_tag_timestamps, tag_list, game_id) if err != nil { return c.RenderError(err) } tag_timestamps := make([]string, len(wrapped_tag_timestamps)) for i, timestamp := range wrapped_tag_timestamps { tag_timestamps[i] = timestamp.Value.String() } out := make(map[string]interface{}) out["num_humans"] = nPlayer - nZombies out["num_zombies"] = nZombies out["tag_list"] = tag_timestamps return c.RenderJson(out) }
func TagByRandomTaggedZombie() { oz := SelectRandomTaggedZombie() oz_player, _ := models.PlayerFromId(oz.Id) human := SelectTestHuman("AND player.game_id = $1", oz_player.Game_id) if human == nil { return } now := time.Now() game, _ := models.GameFromId(human.Game_id) _, _, err := models.NewTag(game, oz_player, human, &now) if err != nil { revel.WARN.Print(err) } }
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) }