Example #1
0
File: main.go Project: shazow/mog
func TokenGet(c context.Context, w http.ResponseWriter, r *http.Request, u *User, g *goon.Goon) (interface{}, error) {
	if !validCSRF(r.FormValue("csrf"), u) {
		return nil, errCSRF
	}
	redir, err := url.Parse(r.FormValue("redirect"))
	if err != nil {
		return nil, err
	}
	uk, err := g.KeyError(u)
	if err != nil {
		return nil, err
	}
	b := make([]byte, 64)
	if _, err := rand.Read(b); err != nil {
		return nil, err
	}
	token := &Token{
		ID:     base64.URLEncoding.EncodeToString(b),
		User:   uk,
		Name:   r.FormValue("hostname"),
		Issued: time.Now(),
	}
	if _, err := g.Put(token); err != nil {
		return nil, err
	}
	values := redir.Query()
	values.Add("token", token.ID)
	redir.RawQuery = values.Encode()
	http.Redirect(w, r, redir.String(), 302)
	return nil, nil
}
Example #2
0
File: main.go Project: shazow/mog
func DeleteSource(c context.Context, w http.ResponseWriter, r *http.Request, u *User, g *goon.Goon) (interface{}, error) {
	var d models.Delete
	if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
		return nil, err
	}
	p := &Protocol{
		ID:   d.Protocol,
		User: g.Key(u),
	}
	s := &Source{
		ID:       d.Name,
		Protocol: g.Key(p),
	}
	err := g.Delete(g.Key(s))
	return nil, err
}
Example #3
0
File: main.go Project: shazow/mog
func GetSource(c context.Context, w http.ResponseWriter, r *http.Request, u *User, g *goon.Goon) (interface{}, error) {
	var ss []*Source
	k := g.Kind(&Source{})
	q := datastore.NewQuery(k).Ancestor(g.Key(u))
	if _, err := g.GetAll(q, &ss); err != nil {
		return nil, err
	}
	ret := make([]*models.Source, len(ss))
	for i, s := range ss {
		ret[i] = &models.Source{
			Protocol: s.Protocol.StringID(),
			Name:     s.ID,
			Blob:     s.Blob,
		}
	}
	return ret, nil
}
Example #4
0
File: main.go Project: shazow/mog
func SetSource(c context.Context, w http.ResponseWriter, r *http.Request, u *User, g *goon.Goon) (interface{}, error) {
	var ss []*models.Source
	if err := json.NewDecoder(r.Body).Decode(&ss); err != nil {
		return nil, err
	}
	for _, s := range ss {
		p := &Protocol{
			ID:   s.Protocol,
			User: g.Key(u),
		}
		src := Source{
			ID:       s.Name,
			Protocol: g.Key(p),
			Blob:     s.Blob,
		}
		log.Println(s.Name, s.Protocol, len(s.Blob))
		_, err := g.Put(&src)
		if err != nil {
			return nil, err
		}
	}
	return nil, nil
}
Example #5
0
func updateVillage(us UpdateSchedule, g *goon.Goon) error {
	village := Village{No: us.VillageNo}
	if err := g.Get(&village); err != nil || village.Day <= 0 {
		return nil
	}
	village.Day++
	err := g.RunInTransaction(func(g *goon.Goon) error {
		vKey := g.Key(village)
		q3 := datastore.NewQuery("Person").Ancestor(vKey).Order("CreatedTime")
		people := make([]Person, 0, 10)
		_, err := g.GetAll(q3, &people)
		if err != nil {
			return err
		}
		// 3日目以降のみ投票処理
		var posts []Post
		if village.Day >= 3 {
			posts = Execute(people)
		} else if village.Day == 2 {
			t := setting.NpcName + "は無残な死体で見つかった。"
			p := Post{Author: "System", AuthorID: "0", Text: t, Time: time.Now(), Type: SystemMessage}
			posts = append(posts, p)
			p2 := Post{Author: "System", AuthorID: "0", Time: time.Now(), Type: SystemMessage}
			p2.Text = setting.SecondDaySystemPost
			posts = append(posts, p2)
		}
		before := make([]Person, len(people))
		copy(before, people)
		posts = append(posts, Fortune(people)...)
		Raid(people)
		// Check Dead Person
		peaceful := true
		for i := range people {
			if !before[i].Dead && people[i].Dead {
				peaceful = false
				t := people[i].Name + "は無残な死体で見つかった。"
				p := Post{Author: "System", AuthorID: "0", Text: t, Time: time.Now(), Type: SystemMessage}
				posts = append(posts, p)
			}
			people[i].VoteTarget = ""
			people[i].AbilityTarget = ""
		}
		if peaceful && village.Day >= 3 {
			t := "今日は誰も犠牲者がいないようだ。"
			p := Post{Author: "System", AuthorID: "0", Text: t, Time: time.Now(), Type: SystemMessage}
			posts = append(posts, p)
		}
		j := Judge(people)
		if j > 0 {
			village.Day *= -1
			var t string
			switch j {
			case 1:
				t = setting.VillagerWin
			case 2:
				t = setting.WerewolfWin
			case 3:
				t = setting.FoxWin
			}
			p := Post{Author: "System", AuthorID: "0", Text: t, Time: time.Now(), Type: SystemMessage}
			posts = append(posts, p)
		}
		_, err = g.Put(&village)
		if err != nil {
			return err
		}
		for i := range posts {
			posts[i].Day = village.Day
			if posts[i].Day <= -1 {
				posts[i].Day = -1
			}
			posts[i].ParentKey = vKey
			_, err = g.Put(&posts[i])
			if err != nil {
				return err
			}
		}
		for i := 0; i < len(people); i++ {
			people[i].ParentKey = vKey
			_, err = g.Put(&people[i])
			if err != nil {
				return err
			}
		}

		us.SetNextUpdateTime(time.Now())
		us.ParentKey = vKey
		_, err = g.Put(&us)
		if err != nil {
			return err
		}
		return nil
	}, nil)
	if err != nil {
		return err
	}
	return nil
}