コード例 #1
0
ファイル: handlers.go プロジェクト: marktai/T9-Server
func getUserGames(w http.ResponseWriter, r *http.Request) {

	vars := mux.Vars(r)
	userID, err := stringtoUint(vars["userID"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	if requireAuth {

		timeInt, path, messageHMACString, encoding, err := auth.ExtractAuthParamsNoUser(r)
		if err != nil {
			WriteError(w, err, 400)
			return
		}

		authed, err := auth.CheckAuthParams(userID, timeInt, path, messageHMACString, encoding)
		if err != nil || !authed {
			if err != nil {
				log.Println(err)
			}
			WriteErrorString(w, "Not Authorized Request", 401)
			return
		}
	}
	games, err := game.GetUserGames(userID)

	if err != nil {
		WriteError(w, err, 400)
		return
	}

	WriteJson(w, genMap("Games", games))
}
コード例 #2
0
ファイル: handlers.go プロジェクト: marktai/marktai.com
// upload logic
func upload(w http.ResponseWriter, r *http.Request) {
	userID, timeInt, requestPath, messageHMACString, encoding, err := auth.ExtractAuthParams(r)
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	authed, err := auth.CheckAuthParams(userID, timeInt, requestPath, messageHMACString, encoding)
	if !authed || err != nil {
		if err != nil {
			log.Println(err)
		}
		WriteErrorString(w, "Not authorized request", 400)
		return
	}

	r.ParseMultipartForm(32 << 20)
	file, handler, err := r.FormFile("file")
	if err != nil {
		WriteError(w, err, 500)
		return
	}
	defer file.Close()
	filename := handler.Filename

	// strip leading periods
	for len(filename) > 0 && filename[0] == '.' {
		filename = filename[1:]
	}
	filename = path.Clean(filename)
	if len(filename) == 0 {
		WriteErrorString(w, "Invalid filename", 400)
	}

	err = os.MkdirAll(fmt.Sprintf("./upload/%d", userID), 0774)
	if err != nil {
		WriteError(w, err, 500)
	}
	f, err := os.OpenFile(fmt.Sprintf("./upload/%d/%s", userID, filename), os.O_WRONLY|os.O_CREATE, 0664)
	if err != nil {
		WriteError(w, err, 500)
		return
	}
	defer f.Close()
	io.Copy(f, file)
	fmt.Fprintf(w, "%v", handler.Header)
}
コード例 #3
0
ファイル: handlers.go プロジェクト: marktai/T9-Server
func makeGameMove(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := stringtoUint(vars["GameID"])
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	player, err := stringtoUint(r.FormValue("Player"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Player form value"), 400)
		return
	}

	box, err := stringtoUint(r.FormValue("Box"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Box form value"), 400)
		return
	}

	square, err := stringtoUint(r.FormValue("Square"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Square form value"), 400)
		return
	}

	if requireAuth {

		timeInt, path, messageHMACString, encoding, err := auth.ExtractAuthParamsNoUser(r)
		if err != nil {
			WriteError(w, err, 400)
			return
		}

		authed, err := auth.CheckAuthParams(player, timeInt, path, messageHMACString, encoding)
		if err != nil || !authed {
			if err != nil {
				log.Println(err)
			}
			WriteErrorString(w, "Not Authorized Request", 401)
			return
		}
	}

	game, err := game.GetGame(id)
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	err = game.MakeMove(player, box, square)
	if err != nil {
		WriteError(w, err, 400)
		return
	}

	_, err = game.Update()
	WriteOutputError(w, genMap("Output", "Successful"), err)

	if err == nil {
		err = ws.BroadcastEvent(id, "Change", fmt.Sprintf("Changed %d, %d", box, square))
		if err != nil {
			log.Println(err)
		}

	}
}
コード例 #4
0
ファイル: handlers.go プロジェクト: marktai/T9-Server
func makeGame(w http.ResponseWriter, r *http.Request) {
	player1, err := stringtoUint(r.FormValue("Player1"))
	if err != nil {
		WriteError(w, errors.New("Error parsing Player1 form value"), 400)
		return
	}

	player2, err := stringtoUint(r.FormValue("Player2"))
	if err != nil {
		player2Username := r.FormValue("Player2")
		if player2Username != "" {
			player2, err = auth.GetUserID(player2Username)
		}
		if err != nil {
			WriteError(w, errors.New("Error parsing Player2 form value"), 400)
			return
		}
	}

	starter, err := stringtoUint(r.FormValue("Starter"))
	// 0 means random
	// 1 means player1 starts
	// 2 means player2 starts
	if err != nil {
		starter = 0
	}

	if starter > 2 {
		WriteError(w, errors.New("Starter must be 0-2"), 400)
		return
	}

	if requireAuth {

		timeInt, path, messageHMACString, encoding, err := auth.ExtractAuthParamsNoUser(r)
		if err != nil {
			WriteError(w, err, 400)
			return
		}

		authed, err := auth.CheckAuthParams(player1, timeInt, path, messageHMACString, encoding)
		if err != nil || !authed {
			if err != nil {
				log.Println(err)
			}
			WriteErrorString(w, "Not Authorized Request", 401)
			return
		}
	}

	// 0 means that 50% chance of switching
	switch starter {
	case 0:
		if rand.Intn(2) == 1 {
			// log.Println("Same order")
			break
		}
		// log.Println("Reversed order")
		fallthrough
	case 2:
		player1, player2 = player2, player1
	}

	game, err := game.MakeGame(player1, player2)

	if err != nil {
		WriteError(w, err, 400)
		return
	}

	WriteJson(w, genMap("GameID", game.GameID))
}