示例#1
0
func (h *Human) Tell(action *sdz.Action) {
	jsonData, _ := json.Marshal(action)
	Log("--> %s", jsonData)
	err := websocket.JSON.Send(h.conn, action)
	if err != nil {
		sdz.Log("Error in Human.Go Send - %v", err)
		h.Close()
		return
	}
}
示例#2
0
func (h *Human) Listen() (action *sdz.Action, open bool) {
	action = new(sdz.Action)
	err := websocket.JSON.Receive(h.conn, action)
	if err != nil {
		sdz.Log("Error receiving action from human - %v", err)
		return nil, false
	}
	action.Playerid = h.Id
	jsonData, _ := json.Marshal(action)
	Log("<-- %s", jsonData)
	return action, true
}
示例#3
0
func main() {
	go listenForFlash()
	cp = &ConnectionPool{connections: make(chan *Human, 100)}
	http.Handle("/connect", websocket.Handler(wshandler))
	http.Handle("/cards/", http.StripPrefix("/cards/", http.FileServer(http.Dir("cards"))))
	http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js"))))
	http.Handle("/web-socket-js/", http.StripPrefix("/web-socket-js/", http.FileServer(http.Dir("web-socket-js"))))
	http.HandleFunc("/", serveGame)
	err := http.ListenAndServe(":80", nil)
	if err != nil {
		sdz.Log("Cannot listen on port 80, check permissions or run as root - " + err.Error())
	}
	err = http.ListenAndServe(":1080", nil)
	if err != nil {
		panic("Cannot listen for http requests - " + err.Error())
	}
}
示例#4
0
func listenForFlash() {
	response := []byte("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>")
	ln, err := net.Listen("tcp", ":843")
	if err != nil {
		sdz.Log("Cannot listen on port 843 for flash policy file, will not serve non WebSocket clients, check permissions or run as root - " + err.Error())
		return
	}
	for {
		conn, err := ln.Accept()
		if err != nil {
			// handle error
			continue
		}
		conn.Write(response)
		conn.Close()
	}
}
示例#5
0
func main() {
	autoClient := false
	for _, s := range os.Args {
		fmt.Println(s)
		if s == "auto" {
			autoClient = true
		}
	}
	conn, err := websocket.Dial("ws://localhost:10080/connect", "", "http://localhost:10080/")
	var playerid int
	var hand *sdz.Hand
	var bidAmount int
	var trump sdz.Suit
	if err != nil {
		sdz.Log("Error - %v", err)
		return
	}
	defer conn.Close()
	var previousPlay *sdz.Action
	var previousCard sdz.Card
	for {
		var action sdz.Action
		err := websocket.JSON.Receive(conn, &action)
		if err != nil {
			sdz.Log("Error decoding - %v", err)
			return
		}
		switch action.Type {
		case "Bid":
			if action.Playerid == playerid {
				sdz.Log("How much would you like to bid?:")
				fmt.Scan(&bidAmount)
				send(conn, sdz.CreateBid(bidAmount, playerid))
			} else {
				// received someone else's bid value'
				sdz.Log("Player #%d bid %d", action.Playerid, action.Bid)
			}
		case "Play":
			if action.Playerid == playerid {
				if previousPlay == nil || action.Lead == "" {
					previousPlay = &action
				} else {
					sdz.Log("Server rejected the play of %s, invalid play", previousCard)
					*hand = append(*hand, previousCard)
					sort.Sort(hand)
				}
				var card sdz.Card
				if action.Lead == "" {
					card = (*hand)[0]
				} else {
					for _, c := range *hand {
						if sdz.ValidPlay(c, action.WinningCard, action.Lead, hand, trump) {
							card = c
						}
					}
				}
				sdz.Log("Your turn, in your hand is %s - what would you like to play? Trump is %s - valid play is %s:", hand, trump, card)
				for {
					if !autoClient {
						fmt.Scan(&card)
					}
					//sdz.Log("Received input %s", card)
					if hand.Remove(card) {
						send(conn, sdz.CreatePlay(card, playerid))
						previousCard = card
						break
					} else {
						sdz.Log("Invalid play, card does not exist in your hand")
					}
				}
			} else {
				sdz.Log("Player %d played card %s", action.Playerid, action.PlayedCard)
				previousPlay = nil // not going to ask us to replay since the next response was another player's play
				// received someone else's play'
			}
		case "Trump":
			if action.Playerid == playerid {
				sdz.Log("What would you like to make trump?")
				fmt.Scan(&trump)
				send(conn, sdz.CreateTrump(trump, playerid))
			} else {
				sdz.Log("Player %d says trump is %s", action.Playerid, action.Trump)
				trump = action.Trump
			}
		case "Throwin":
			sdz.Log("Player %d threw in", action.Playerid)
		case "Deal":
			playerid = action.Playerid
			hand = &action.Hand
			sdz.Log("Your hand is - %s", hand)
		case "Meld":
			sdz.Log("Player %d is melding %s for %d points", action.Playerid, action.Hand, action.Amount)
		case "Score": // this client does not have to implement this type as it's already told through Message actions
		case "Message":
			sdz.Log(action.Message)
		case "Hello":
			var response string
			fmt.Scan(&response)
			send(conn, sdz.CreateHello(response))
		case "Game":
			var option int
			fmt.Scan(&option)
			send(conn, sdz.CreateGame(option))
		default:
			sdz.Log("Received an action I didn't understand - %v", action)
		}

	}
}
示例#6
0
func send(conn *websocket.Conn, action *sdz.Action) {
	err := websocket.JSON.Send(conn, action)
	if err != nil {
		sdz.Log("Error sending - %v", err)
	}
}