Exemplo n.º 1
0
func (g *Game) wsHandler(ws *websocket.Conn) {
	id, err := strconv.Atoi(misc.GetCookie(ws.Request(), "playerId"))
	name := misc.GetCookie(ws.Request(), "playerName")

	if err == nil {
		p := g.lobby.Players[id]
		if p != nil {
			p.Init(id, name, ws)

			g.register <- p
			defer func() {
				recover()
				g.unregister <- p
				p.cleanup()
			}()

			go p.writer()
			p.reader(g)
		} else {
			log.Debug("Player not in players list. Send him to lobby.")
		}
	} else {
		log.Debug("Player ID not found in cookie.")
	}
}
Exemplo n.º 2
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)
	}
}
Exemplo n.º 3
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)
	}
}
Exemplo n.º 4
0
func (l *Lobby) handleLogout(w http.ResponseWriter, r *http.Request) {
	playerIdStr := misc.GetCookie(r, "playerId")
	if playerIdStr != "" {
		playerId, _ := strconv.Atoi(playerIdStr)
		if _, ok := l.Players[playerId]; ok != false {
			log.Debug("%s (%d) left the lobby.", l.Players[playerId].Name, playerId)
			delete(l.Players, playerId)
		}
		misc.UnsetCookie(w, r, "playerId")
		misc.UnsetCookie(w, r, "playerName")
	}
	http.Redirect(w, r, "/login/", http.StatusFound)
}
Exemplo n.º 5
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)
	}
}
Exemplo n.º 6
0
func (l *Lobby) handleLogin(w http.ResponseWriter, r *http.Request) {
	playerName := r.FormValue("player-name")
	playerIdStr := misc.GetCookie(r, "playerId")
	playerId, _ := strconv.Atoi(playerIdStr)
	if playerIdStr != "" {
		if _, ok := l.Players[playerId]; ok == false {
			p := CreatePlayer(playerId, playerName, PLAYER_INIT_CASH)
			l.Players[playerId] = p
		}
	} else {
		playerId = time.Now().Hour()*1000000 + time.Now().Minute()*10000 + time.Now().Second()*100 + rand.Intn(99) + rand.Intn(99)
		p := CreatePlayer(playerId, playerName, PLAYER_INIT_CASH)
		l.Players[playerId] = p

		misc.SetCookie(w, "playerId", strconv.Itoa(playerId))
		misc.SetCookie(w, "playerName", playerName)
	}
	log.Debug("%s (%d) entered the lobby.", playerName, playerId)
	http.Redirect(w, r, "/lobby/", http.StatusFound)
}