Example #1
0
File: suabm.go Project: pciet/suabm
func SetForPiles(s []Pile) store.Set {
	assert.Require(len(s) != 0, "suabm: zero length slice argument")
	//for _, p := range s {

	//}
	set := store.EmptyTypedSet(Pile{}, uint(len(s)))
	for _, p := range s {
		set = set.Add(p)
	}
	assert.Require(uint(len(s)) == set.Count(), "suabm: mismatch between set count and length of input piles")
	panic("unfinished code path")
	return store.Set{}
}
Example #2
0
File: match.go Project: pciet/sand
// Wait blocks until a match is available or the specified timeout occurs.
// Returns game ID or error.
func (m *Maker) Wait(player string, timeout time.Duration) (string, error) {
	rdy := m.add(player)
	assert.Require(rdy != nil, "Add(%v) returns a nil channel", player)
	select {
	case <-time.After(timeout):
		m.remove(player)
		return "", ErrTimeout
	case gid := <-rdy:
		return gid, nil
	}
	assert.Require(false, "invalid execution path")
	return "", nil
}
Example #3
0
File: suabm.go Project: pciet/suabm
func (a HomogenizedPileSet) Decay() HomogenizedPileSet {
	// 8U either converts to 4A or S+M
	// 4A converts to 2B
	// 2B converts to S+M
	assert.Require(false, "suabm: not implemented")
	return a
}
Example #4
0
File: suabm.go Project: pciet/suabm
func (p Pile) Weight() uint {
	switch p.Type {
	case S:
		return Sw * p.Count
	case U:
		return Uw * p.Count
	case A:
		return Aw * p.Count
	case B:
		return Bw * p.Count
	case M:
		return Mw * p.Count
	default:
		assert.Require(false, "unknown suabm pile type")
	}
	assert.Require(false, "invalid execution path")
	return 0
}
Example #5
0
// Messages from the source Messager with the corresponding type will be routed to the target Messager.
// Calling multiple times for the same type will have any messages fan-out from the source to all added targets.
func (r *Router) Simplex(source Messager, target Messager, forType string) error {
	err := r.register(target)
	if err == errMessagerAlreadyTracked {
		// then it's all good
	} else if err != nil {
		return err
	}
	err = r.register(source)
	if err == errMessagerAlreadyTracked {
		// then it's all good
		// TODO: is it?
	} else if err != nil {
		return err
	}

	r.RLock()
	s, exists := r.messagers[source.Key()]
	r.RUnlock()
	assert.Require(exists == true, "source messager registered but does not exist in messagers map")

	r.RLock()
	t, exists := r.messagers[target.Key()]
	r.RUnlock()
	assert.Require(exists == true, "source messager registered but does not exist in messagers map")

	s.Lock()
	defer s.Unlock() // WATCH THIS if making changes after

	routes, exists := s.routes[forType]
	if exists == false {
		s.routes[forType] = make(map[string]chan<- Message)
		routes = s.routes[forType]
	}
	_, exists = routes[t.Key()]
	if exists == true {
		return ErrRouteAlreadySet
	}
	routes[t.Key()] = t.Writer()

	return nil // s.Unlock() was deferred
}
Example #6
0
// TODO: DO NOT use this function in production code.
func DefaultRelational() (Relational, error) {
	// TODO: DO NOT USE IN PRODUCTION CODE
	assert.Require(false, "do not use in production code")
	database, err := sql.Open("postgres", "user=sandtest dbname=sandtest sslmode=disable")
	if err != nil {
		return Relational{}, err
	}
	err = database.Ping()
	if err != nil {
		return Relational{}, err
	}
	return Relational{DB: database}, nil
}
Example #7
0
// RemoveSimplex deletes a single direction route.
func (r *Router) RemoveSimplex(source Messager, target Messager, forType string) error {

	r.RLock()
	src, exists := r.messagers[source.Key()]
	r.RUnlock()

	assert.Require(exists == true, "r.messagers[%+v] does not exist", source.Key())

	src.RLock()
	_, ok := src.routes[forType]
	src.RUnlock()

	assert.Require(ok == true, "source.routes[%+v] does not exist", forType)

	src.Lock()
	defer src.Unlock() // WATCH THIS if making any changes after

	_, here := src.routes[forType][target.Key()]
	assert.Require(here == true, "source.routes[%+v][%+v] does not exist", forType, target.Key())

	delete(src.routes[forType], target.Key())

	if len(src.routes[forType]) == 0 {
		delete(src.routes, forType)
	}

	// if no routes are set then the Router doesn't need to care about this Messager
	if len(src.routes) == 0 {
		err := r.unregister(src)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #8
0
func NewMessageTester() msgTester {
	m := msgTester{
		reader: make(chan Message),
		writer: make(chan Message),
	}
	id, err := random.UniqueId(3)
	assert.Require(err == nil, "random.Unique(64) returned error: %v", err)
	m.key = id
	go func(wr chan Message) {
		for {
			msg := <-wr
			m.reader <- msg
		}
	}(m.writer)
	return m
}
Example #9
0
File: suabm.go Project: pciet/suabm
func (a Pile) TypeString() string {
	switch a.Type {
	case S:
		return Sstr
	case U:
		return Ustr
	case A:
		return Astr
	case B:
		return Bstr
	case M:
		return Mstr
	default:
		assert.Require(false, "suabm: invalid pile type")
	}
	return ""
}
Example #10
0
File: suabm.go Project: pciet/suabm
func RandomPile(count uint) Pile {
	p := Pile{
		Count: count,
	}
	switch random.RollD5() {
	case 0:
		p.Type = S
	case 1:
		p.Type = U
	case 2:
		p.Type = A
	case 3:
		p.Type = B
	case 4:
		p.Type = M
	default:
		assert.Require(false, "suabm: random.RollD5() returned an index outside of 0-4")
	}
	return p
}