Esempio n. 1
0
func (sr Server) MoveGame(before int64, ftp game.FromToProm, who int64) (mkey int64, aftkey int64, err error) {
	log.Println("MoveGame", sr, before, ftp, who)
	var befga GameplayData
	err = sr.LoadGP(before, &befga)
	log.Println(befga, err)
	if err != nil {
		return
	}
	befga.Date = time.Now()
	var sta game.State
	err = sr.LoadState(befga.State, &sta)
	log.Println(sta, err)
	if err != nil {
		return
	}
	switch sta.MovesNext {
	case game.White:
		befga.White = &who
	case game.Gray:
		befga.Gray = &who
	case game.Black:
		befga.Black = &who
	}
	mov := ftp.Move(&sta)
	log.Println("now MoveIt", mov)
	afts, err := mov.EvalAfter()
	if err != nil {
		return
	}
	log.Println("MoveIT returned", afts)
	aftskey, err := sr.SaveState(afts)
	if err != nil {
		return
	}
	befga.State = aftskey
	aftkey, err = sr.SaveGP(&befga)
	if err != nil {
		return
	}
	mdfin := MoveData{
		FromTo:        [4]int8{ftp.FromTo.From()[0], ftp.FromTo.From()[1], ftp.FromTo.To()[0], ftp.FromTo.To()[1]},
		BeforeGame:    before,
		AfterGame:     aftkey,
		PawnPromotion: int8(ftp.PawnPromotion),
		Who:           who}
	mkey, err = sr.SaveMD(&mdfin)
	if err != nil {
		return
	}
	var loadedmd MoveData
	err = sr.LoadMD(mkey, &loadedmd)
	if err == nil {
		aftskey = loadedmd.AfterGame
	}
	return
}
Esempio n. 2
0
func (g *G) Turn() (breaking bool, err error) {
	bef := g.state
	var oftp game.FromToProm
	var do func() (game.FromToProm, error)
	if g.state.MovesNext == g.color {
		do = g.askplayer
	} else {
		do = g.movechk
	}
	if oftp, err = do(); err != nil {
		return
	}
	breaking = !g.state.PlayersAlive.Give(g.color)
	g.our.HeySituationChanges(oftp.Move(bef), g.state)
	return
}