Beispiel #1
0
func SetOrder(c common.WSContext) (result interface{}, err error) {
	var base64DecodedId []byte
	if base64DecodedId, err = base64.URLEncoding.DecodeString(c.Data().GetString("GameId")); err != nil {
		return
	}
	err = c.DB().Transact(func(d *kol.DB) (err error) {
		game := Game{Id: base64DecodedId}
		if err = d.Get(&game); err != nil {
			return
		}
		var member *Member
		if member, err = game.Member(d, c.Principal()); err != nil {
			return
		}
		var phase *Phase
		if _, phase, err = game.Phase(d, 0); err != nil {
			return
		}
		if phase == nil {
			err = fmt.Errorf("No phase for %+v found", game)
			return
		}
		nationOrders, found := phase.Orders[member.Nation]
		if !found {
			nationOrders = map[dip.Province][]string{}
			phase.Orders[member.Nation] = nationOrders
		}
		order := c.Data().GetStringSlice("Order")
		if len(order) == 1 {
			delete(nationOrders, dip.Province(order[0]))
		} else {
			var parsedOrder dip.Order
			parsedOrder, err = orders.Parse(order)
			if err != nil {
				return
			}
			var state *state.State
			state, err = phase.State()
			if err != nil {
				return
			}
			if err = parsedOrder.Validate(state); err != nil {
				return
			}
			nationOrders[dip.Province(order[0])] = order[1:]
		}
		if err = d.Set(phase); err != nil {
			return
		}
		return
	})
	return
}
Beispiel #2
0
func assertOrderValidity(t *testing.T, validator dip.Validator, order dip.Order, err error) {
	if e := order.Validate(validator); e != err {
		t.Errorf("%v should validate to %v, but got %v", order, err, e)
	}
}