示例#1
0
文件: gameroom.go 项目: krc56/catan
/************************************
 * Return codes:
 * SUCESS if insert worked
 * DBERROR if issue during insert
 * NOTFOUND if user not in DB
 ***********************************/
func (g *GameRoom) Create(db mgo.Database, u user.User) (returnCode int) {

	// first make sure the user is in the DB
	if u.Query(db) != error.SUCCESS {
		return error.NOTFOUND
	}

	// if so, go right ahead and insert into game_room
	g.Creator = u.Email
	g.Players = []string{u.Email}
	g.Invitations = []string{}

	err := db.C("game_room").Insert(g)

	if err != nil {
		// There was an error
		return error.DBERROR
	}

	return error.SUCCESS
}
示例#2
0
文件: router.go 项目: krc56/catan
func main() {

	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	db := session.DB("catan")

	var newUser = map[string]string{
		"email":    "*****@*****.**",
		"password": "******",
		"fname":    "Sean",
		"lname":    "Myers",
	}

	user1 := user.User{newUser["email"], newUser["password"], newUser["fname"],
		newUser["lname"], 0, 0, token.Token{}}

	fmt.Printf("Catan!\n")

	fmt.Printf("Adding user to DB: ")
	if user1.New(*db) == error.ALREADY {
		fmt.Printf("%s\n", error.GetDescription(error.ALREADY))
	}

	user1.Email = "*****@*****.**"

	var newUser2 = map[string]string{
		"email":    "*****@*****.**",
		"password": "******",
		"fname":    "Zach",
		"lname":    "Sadler",
	}

	user2 := user.User{newUser2["email"], newUser2["password"], newUser2["fname"],
		newUser2["lname"], 0, 0, token.Token{}}

	// Test New
	user2Err := user2.New(*db)
	fmt.Printf("Adding second user to DB: ")
	if user2Err == error.ALREADY {
		fmt.Printf("%s\n", error.GetDescription(error.ALREADY))
	} else if user2Err == error.SUCCESS {
		fmt.Printf("%s\n", error.GetDescription(error.SUCCESS))
	}

	// Test AddFriend
	friendshipErr := user1.AddFriend(*db, user2.Email)
	if friendshipErr == error.SUCCESS {
		fmt.Printf("Friendship added: ")
	} else if friendshipErr == error.NOTFOUND {
		fmt.Printf("One of the users is not in DB: ")
	} else if friendshipErr == error.ALREADY {
		fmt.Printf("Friendship already exits: ")
	} else if friendshipErr == error.DBERROR {
		fmt.Printf("Insert failed: ")
	}
	fmt.Printf("%s\n", error.GetDescription(friendshipErr))

	// Test RemoveFriend
	if friendshipErr == error.ALREADY {
		removeErr := user1.RemoveFriend(*db, user2.Email)
		fmt.Printf("Testing removal of friendship: %s\n", error.GetDescription(removeErr))
	}

	// Test GetToken
	errCode, userToken := token.GetToken(*db, newUser["email"], newUser["password"])
	if errCode == error.SUCCESS {
		user1.Token = userToken
	}
	fmt.Printf("Testing GetToken: %s\n", error.GetDescription(errCode))

	// Test Query
	queryErr := user1.Query(*db)
	fmt.Printf("Querying user: %s\n", error.GetDescription(queryErr))

	// Test AddWin/AddLoss
	fmt.Printf("The user before addding a win and 2 losses:\n%s\n", user1)
	user1.AddWin(*db)
	user1.AddLoss(*db)
	user1.AddLoss(*db)

	user1.Query(*db)
	fmt.Printf("The user after adding the win and losses:\n%s\n", user1)

	/*****************************
	 * Added to test functionality
	 * of AddWin and AddLoss
	 *****************************/

	testRoom := gameroom.GameRoom{}
	roomErr := testRoom.Create(*db, user1)
	fmt.Printf("Testing gameroom.Create: %v\n", error.GetDescription(roomErr))
	fmt.Printf("Here's the room: %v\n", testRoom)

	/*****************************
	 * END Added to test functionality
	 * of AddWin and AddLoss
	 *****************************/

}