Ejemplo n.º 1
0
func (self *Game) start(c common.SkinnyContext) (err error) {
	if self.State != common.GameStateCreated {
		err = fmt.Errorf("%+v is already started", self)
		return
	}
	self.State = common.GameStateStarted
	self.Closed = true
	if err = c.DB().Set(self); err != nil {
		return
	}
	var startState *state.State
	if self.Variant == common.ClassicalString {
		if startState, err = classical.Start(); err != nil {
			return
		}
	} else {
		err = fmt.Errorf("Unknown variant %v", self.Variant)
		return
	}
	startPhase := startState.Phase()
	epoch, err := epoch.Get(c.DB())
	if err != nil {
		return
	}
	phase := &Phase{
		GameId:      self.Id,
		Ordinal:     0,
		Orders:      map[dip.Nation]map[dip.Province][]string{},
		Resolutions: map[dip.Province]string{},
		Season:      startPhase.Season(),
		Year:        startPhase.Year(),
		Type:        startPhase.Type(),
		Deadline:    epoch + (time.Minute * time.Duration(self.Deadlines[startPhase.Type()])),
	}
	phase.Units, phase.SupplyCenters, phase.Dislodgeds, phase.Dislodgers, phase.Bounces, _ = startState.Dump()
	if err = c.DB().Set(phase); err != nil {
		return
	}
	if err = self.allocate(c.DB(), phase); err != nil {
		return
	}
	if err = phase.Schedule(c); err != nil {
		return
	}
	phase.SendStartedEmails(c, self)
	return
}
Ejemplo n.º 2
0
		},
		ParseOrders: orders.ParseAll,
		ParseOrder:  orders.Parse,
		Graph:       start.Graph(),
		Phase:       classical.Phase,
		OrderTypes:  orders.OrderTypes(),
		Nations:     cla.Nations,
		PhaseTypes:  cla.PhaseTypes,
		Seasons:     cla.Seasons,
		UnitTypes:   cla.UnitTypes,
	},
	Variant{
		Name:  FleetRome,
		Graph: start.Graph(),
		Start: func() (result *state.State, err error) {
			if result, err = classical.Start(); err != nil {
				return
			}
			result.RemoveUnit(dip.Province("rom"))
			if err = result.SetUnit(dip.Province("rom"), dip.Unit{
				Type:   cla.Fleet,
				Nation: cla.Italy,
			}); err != nil {
				return
			}
			return
		},
		Blank:       classical.Blank,
		Phase:       classical.Phase,
		ParseOrders: orders.ParseAll,
		ParseOrder:  orders.Parse,
Ejemplo n.º 3
0
func assertGame(t *testing.T, name string) (phases, ords, positions, fails int, s *state.State) {
	file, err := os.Open(fmt.Sprintf("games/%v", name))
	if err != nil {
		t.Fatalf("%v", err)
	}
	if s, err = classical.Start(); err != nil {
		t.Fatalf("%v", err)
	}
	lines := bufio.NewReader(file)
	var match []string
	state := inNothing
	scCollector, unitCollector, dislodgedCollector := make(map[dip.Province]dip.Nation), make(map[dip.Province]dip.Unit), make(map[dip.Province]dip.Unit)
	for line, err := lines.ReadString('\n'); err == nil; line, err = lines.ReadString('\n') {
		line = strings.TrimSpace(line)
		switch state {
		case inNothing:
			if match = phaseReg.FindStringSubmatch(line); match != nil {
				phases += 1
				setPhase(t, &s, match)
			} else if line == positionsTag {
				state = inPositions
			} else {
				t.Fatalf("Unknown line for state inNothing: %v", line)
			}
		case inPositions:
			if match = posReg.FindStringSubmatch(line); match != nil {
				positions += 1
				verifyPosition(t, s, match, scCollector, unitCollector, dislodgedCollector, &fails)
			} else if line == ordersTag {
				verifyReversePositions(t, s, scCollector, unitCollector, dislodgedCollector, &fails)
				if fails > 0 {
					return
				}
				dip.ClearLog()
				scCollector, unitCollector, dislodgedCollector = make(map[dip.Province]dip.Nation), make(map[dip.Province]dip.Unit), make(map[dip.Province]dip.Unit)
				state = inOrders
			} else {
				t.Fatalf("Unknown line for state inPositions: %v", line)
			}
		case inOrders:
			ords += 1
			if match = moveReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Move(dip.Province(match[1]), dip.Province(match[2])))
			} else if match = moveViaConvoyReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Move(dip.Province(match[1]), dip.Province(match[2])).ViaConvoy())
			} else if match = supportMoveReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.SupportMove(dip.Province(match[1]), dip.Province(match[2]), dip.Province(match[3])))
			} else if match = supportHoldReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.SupportHold(dip.Province(match[1]), dip.Province(match[2])))
			} else if match = holdReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Hold(dip.Province(match[1])))
			} else if match = convoyReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Convoy(dip.Province(match[1]), dip.Province(match[2]), dip.Province(match[3])))
			} else if match = buildReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[2]), orders.Build(dip.Province(match[2]), dip.UnitType(match[1]), time.Now()))
			} else if match = removeReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Disband(dip.Province(match[1]), time.Now()))
			} else if match = disbandReg.FindStringSubmatch(line); match != nil {
				s.SetOrder(dip.Province(match[1]), orders.Disband(dip.Province(match[1]), time.Now()))
			} else if match = phaseReg.FindStringSubmatch(line); match != nil {
				ords -= 1
				phases += 1
				setPhase(t, &s, match)
				state = inNothing
			} else {
				t.Fatalf("Unknown line for state inOrders: %v", line)
			}
		default:
			t.Fatalf("Unknown state %v", state)
		}
	}
	return
}