Пример #1
0
func (gs *gameServer) clientListenRead(ws *websocket.Conn) {
	defer func() {
		ws.Close()
	}()

	for {
		select {
		default:
			var move util.ClientMove
			err := websocket.JSON.Receive(ws, &move)
			if err == io.EOF {
				return
				// EOF!
			} else if err != nil {
				LOGE.Println(err)
			} else {
				var dir lib2048.Direction
				switch move.Direction {
				case 0:
					dir = lib2048.Up
				case 1:
					dir = lib2048.Right
				case 2:
					dir = lib2048.Down
				case 3:
					dir = lib2048.Left
				}
				LOGV.Println("Received", dir, "from web client")
				move := lib2048.NewMove(dir)
				gs.clientMoveCh <- move
			}
		}
	}
}
Пример #2
0
func RandomMove() *lib2048.Move {
	num := r.Int() % 4
	var dir lib2048.Direction
	switch num {
	case 0:
		dir = lib2048.Up
	case 1:
		dir = lib2048.Left
	case 2:
		dir = lib2048.Down
	case 3:
		dir = lib2048.Right
	}
	return lib2048.NewMove(dir)
}
Пример #3
0
func testOneCentralOneClientOneGameserv() {
	// Step 1: Boot Testing Client
	cservAddr := "http://" + util.CENTRALHOSTPOST
	cli, err := cmdlineclient.NewCClient(cservAddr, "", util.DEFAULTINTERVAL)
	processError(err, util.CFAIL)

	// Step 2: Initialize moves + obtain correct answer
	movelist := []*lib2048.Move{
		lib2048.NewMove(lib2048.Left),
		lib2048.NewMove(lib2048.Right),
		lib2048.NewMove(lib2048.Left),
		lib2048.NewMove(lib2048.Up),
		lib2048.NewMove(lib2048.Up),
		lib2048.NewMove(lib2048.Down),
		lib2048.NewMove(lib2048.Right),
		lib2048.NewMove(lib2048.Left),
	}
	desiredGame := util.CalculateGameState(cli.GetGameState(), movelist)

	// Step 3: Test
	for _, m := range movelist {
		cli.InputMove(m.Direction)
		time.Sleep(1000 * time.Millisecond) // has to be large enough so one move per paxos round
	}

	LOGV.Println("CLI's game state")
	LOGV.Println(cli.GetGameState())
	LOGV.Println("Desired game state")
	LOGV.Println(desiredGame)

	if !cli.GetGameState().Equals(desiredGame) {
		fmt.Println("PHAIL: STATES ARE NOT CORRECTZ")
		failCount++
		return
	}
	fmt.Println("PASS")
	passCount++
}