Пример #1
0
func main() {
	env_setup := misc.SetupEnvironment()
	setLogOutput()

	fmt.Println("WordPoker server is running... ")
	log.Debug("========================== starting server =================================")

	if env_setup == true {
		rand.Seed(time.Now().UTC().UnixNano())

		lobby := tywa.CreateLobby(3)

		http.Handle("/", lobby)
		http.Handle("/lobby/", lobby)
		http.Handle("/login/", lobby)
		http.Handle("/console/", lobby)
		http.Handle("/table/", lobby)

		http.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/tywa-client/styles"))))
		http.Handle("/scripts/", http.StripPrefix("/scripts/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/tywa-client/scripts"))))
		http.Handle("/closure/", http.StripPrefix("/closure/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/closure"))))
		http.Handle("/lime/", http.StripPrefix("/lime/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/lime"))))
		http.Handle("/tywa-client/", http.StripPrefix("/tywa-client/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/tywa-client"))))
		http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/tywa-client/assets"))))
		http.ListenAndServe(":8020", nil)
	} else {
		fmt.Println("Environment not setup correctly. Please check.")
	}
}
Пример #2
0
func (g *Game) gameInterface(w http.ResponseWriter, r *http.Request) {
	var gameTempl = template.Must(template.ParseFiles(misc.GetRootDir() + "gamble-server/templates/wrapper.html"))

	data := map[string]interface{}{}
	data["host"] = r.Host
	gameTempl.Execute(w, data)
}
Пример #3
0
func (l *Lobby) tableInterface(w http.ResponseWriter, r *http.Request) {
	var tableTempl = template.Must(template.ParseFiles(misc.GetRootDir() + "tywa-server/templates/table.html"))
	playerIdStr := misc.GetCookie(r, "playerId")
	playerName := misc.GetCookie(r, "playerName")
	playerId, _ := strconv.Atoi(playerIdStr)
	if playerIdStr != "" {
		urlTokens := strings.Split(r.URL.Path, "/") // /console/<table-ID>/
		tableIdStr := urlTokens[2]
		tableId, _ := strconv.Atoi(tableIdStr)

		if g := l.Games[tableId]; g != nil {
			// make sure that the table is active
			if g.State == GS_INACTIVE {
				g.init()
			}

			data := map[string]interface{}{}
			data["host"] = r.Host
			data["tableId"] = tableId
			data["playerId"] = playerId
			data["playerName"] = playerName
			tableTempl.Execute(w, data)
		} else {
			log.Debug("Table", tableId, "doesn't exist.")
			http.Redirect(w, r, "/lobby/", http.StatusFound)
		}
	} else {
		http.Redirect(w, r, "/login/", http.StatusFound)
	}
}
Пример #4
0
func (l *Lobby) loginInterface(w http.ResponseWriter, r *http.Request) {
	var loginTempl = template.Must(template.ParseFiles(misc.GetRootDir() + "tywa-server/templates/login.html"))

	playerIdStr := misc.GetCookie(r, "playerId")
	playerId, _ := strconv.Atoi(playerIdStr)
	if playerIdStr != "" {
		if _, ok := l.Players[playerId]; ok == false {
			playerName := misc.GetCookie(r, "playerName")
			p := CreatePlayer(playerId, playerName, PLAYER_INIT_CASH)
			l.Players[playerId] = p
		}
		http.Redirect(w, r, "/lobby/", http.StatusFound)
	} else {
		data := map[string]interface{}{}
		data["host"] = r.Host
		loginTempl.Execute(w, data)
	}
}
Пример #5
0
// distribution of letters
func InitHandSets(sets []*HandSet) []*HandSet {
	b, err := ioutil.ReadFile(misc.GetRootDir() + HANDSETS_FILE)
	if err != nil {
		log.Debug(err)
	}

	lines := strings.Split(string(b), "\n")
	handSets := make([]*HandSet, 0, len(lines))
	for i, l := range lines {
		if i >= NUM_HANDSETS {
			break
		}
		// Empty line occurs at the end of the file when we use Split.
		if len(l) == 0 {
			continue
		}

		letters := strings.Split(l, ",")
		numLettersRequired := NUM_CARDS_COMMUNITY + MAX_PLAYERS*NUM_CARDS_PLAYER
		if len(letters) < numLettersRequired {
			log.Debug("Insufficient letters passed. We need at least", numLettersRequired, "letters.")
		} else {
			hs := &HandSet{
				community: make([]string, 0, NUM_CARDS_COMMUNITY),
				players:   make([]([]string), 0, MAX_PLAYERS),
			}
			hs.community = letters[:MAX_PLAYERS+1]
			letters = letters[MAX_PLAYERS+1:]

			for i := 0; i < MAX_PLAYERS; i++ {
				p_letters := letters[:NUM_CARDS_PLAYER]
				letters = letters[NUM_CARDS_PLAYER:]
				hs.players = append(hs.players, p_letters)
			}
			handSets = append(handSets, hs)
		}
	}
	return handSets
}
Пример #6
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	game := gow.CreateGame()

	http.Handle("/", game)
	http.Handle("/console/", game)
	http.Handle("/game/", game)

	fmt.Println("Server started... listening on port 8020.")

	http.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/tywa-client/styles"))))
	http.Handle("/scripts/", http.StripPrefix("/scripts/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/gamble"))))
	http.Handle("/closure/", http.StripPrefix("/closure/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/closure"))))
	http.Handle("/lime/", http.StripPrefix("/lime/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/lime"))))
	http.Handle("/gamble/", http.StripPrefix("/gamble/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/gamble"))))
	http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(misc.GetRootDir()+"limejs/gamble/assets"))))
	http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir(misc.GetRootDir()+"gamble-server/templates"))))

	http.ListenAndServe(":8020", nil)

}
Пример #7
0
func (l *Lobby) lobbyInterface(w http.ResponseWriter, r *http.Request) {
	var lobbyTempl = template.Must(template.ParseFiles(misc.GetRootDir() + "tywa-server/templates/lobby.html"))

	playerIdStr := misc.GetCookie(r, "playerId")
	playerName := misc.GetCookie(r, "playerName")
	playerId, _ := strconv.Atoi(playerIdStr)
	if playerIdStr != "" {
		if _, ok := l.Players[playerId]; ok == true {
			l.Players[playerId].BetAction = BET_WAITING

			data := map[string]interface{}{}
			data["players"] = l.GetPlayers()
			data["games"] = l.GetGames()
			data["host"] = r.Host
			data["playerId"] = playerId
			data["PlayerName"] = playerName
			lobbyTempl.Execute(w, data)
		} else {
			http.Redirect(w, r, "/login/", http.StatusFound)
		}
	} else {
		http.Redirect(w, r, "/login/", http.StatusFound)
	}
}