// Register creates a new user, with the given nickname and password. It returns // the user created, or an error if the nickname already exists. The nickname // is canonicalized, and the password is salted and hashed. func (s *Users) Register(nickname string, password string) (*User, error) { if s.passwordSalt == "" { return nil, errors.New("No password salt found.") } id, err := uuid.GenUUID() if err != nil { return nil, err } u := &User{ Id: id, Nickname: nickname, Pwhash: s.hashPassword(canonicalizePassword(password))} u.Token, err = uuid.GenUUID() if err != nil { return nil, err } key := canonicalizeNickname(u.Nickname) if s.usersByNickname[key] != nil { return nil, errors.New("Duplicate nickname.") } s.users = append(s.users, u) s.usersByNickname[key] = u s.usersById[u.Id] = u s.usersByToken[u.Token] = u return u, nil }
// initPlayers sets up the Players array with initial values. func (g *Game) initPlayers(names []string) { g.addMessage("Initializing players...") g.Players = make([]*Player, len(names)) for i, name := range names { id, err := uuid.GenUUID() if err != nil { panic(err) } p := &Player{ ID: id, Name: name, Factories: make([]Factory, 1), Money: 12, ChatMessages: Queue{Capacity: 500}, TurnOrder: i} p.Factories[0] = Factory{Key: "p1", Capacity: 1} g.Players[i] = p } p := g.Players[0] p.IsCurrent = true }
func Instance() { g = Golem.new() g.cluster_time = Time.now() g.me = uuid.GenUUID() g.wake_up = make(chan int) g.known_nodes = make(map[string]*Node) g.request_queue = RequestQueue.new() g.zcontext, _ = zmq.NewContext() }
// makeNewGame creates a new game with the provided player names. func makeNewGame(name string, playerNames []string) *Game { var g = new(Game) id, err := uuid.GenUUID() if err != nil { panic(err) } g.ID = id g.Name = name g.Messages.Capacity = 500 g.Phase = Development Games[g.ID] = g g.addMessage(fmt.Sprintf("Created game %s...", g.Name)) g.loadLocos() g.prepareLocos() g.initPlayers(playerNames) g.determineTurnOrder() return g }
// Login validates a user login and returns the User. If a user is // logged in, he gets assigned a Token. func (s *Users) Login(nickname, password string) (*User, error) { u := s.LookupByNickname(nickname) if u == nil { return nil, InvalidNicknameError } if u.Pwhash != s.hashPassword(canonicalizePassword(password)) { return nil, InvalidPasswordError } // login succeeded, so assign token if it hasn't already been assigned. if u.Token == "" { var err error u.Token, err = uuid.GenUUID() if err != nil { return nil, err } s.usersByToken[u.Token] = u } return u, nil }
func handleHello(client *Client, f map[string]interface{}) { status := 200 if f["uaid"] == nil { uaid, err := uuid.GenUUID() if err != nil { status = 400 log.Println("GenUUID error %s", err) } client.UAID = uaid } else { client.UAID = f["uaid"].(string) resetClient := false if f["channelIDs"] != nil { for _, foo := range f["channelIDs"].([]interface{}) { channelID := foo.(string) if gServerState.UAIDToChannelIDs[client.UAID] == nil { gServerState.UAIDToChannelIDs[client.UAID] = make(ChannelIDSet) // since we don't have any channelIDs, don't bother looping any more resetClient = true break } if _, ok := gServerState.UAIDToChannelIDs[client.UAID][channelID]; !ok { resetClient = true break } } } if resetClient { // delete the older connection delete(gServerState.ConnectedClients, client.UAID) delete(gServerState.UAIDToChannelIDs, client.UAID) // TODO(nsm) clear up ChannelIDToChannels which now has extra // channelIDs not associated with any client uaid, err := uuid.GenUUID() if err != nil { status = 400 log.Println("GenUUID error %s", err) } client.UAID = uaid } } gServerState.ConnectedClients[client.UAID] = client if f["wakeup_hostport"] != nil { m := f["wakeup_hostport"].(map[string]interface{}) client.Ip = m["ip"].(string) client.Port = m["port"].(float64) log.Println("Got hostport pair ", client.Ip, client.Port) } else { log.Println("No hostport ", f) } type HelloResponse struct { Name string `json:"messageType"` Status int `json:"status"` UAID string `json:"uaid"` } hello := HelloResponse{"hello", status, client.UAID} j, err := json.Marshal(hello) if err != nil { log.Println("Could not convert hello response to json %s", err) return } if err = websocket.Message.Send(client.Websocket, string(j)); err != nil { log.Println("Could not send message to ", client.Websocket, err.Error()) } }
func handleHello(client *Client, f map[string]interface{}) { status := 200 var candidateUAID string var err error if _, ok := f["uaid"]; !ok { f["uaid"] = "" } candidateUAID = f["uaid"].(string) if filter.Find([]byte(candidateUAID)) != nil { handleError(client, f, 503, "Service Unavailable") verbose("Only use characters from \"A-Za-z0-9._-\" for UAIDs") return } if len(candidateUAID) > 0 && len(client.UAID) > 0 && (candidateUAID != client.UAID) { handleError(client, f, 503, "Service Unavaliable") verbose("If you've already sent a \"hello\", the UAID must either be", "blank, or the same value passed previously.") return } if f["channelIDs"] == nil { handleError(client, f, 401, "Invalid Command") verbose("ChannelIDs must be specified, even if there is no content.", "e.g. channelIDs:[]") return } if len(candidateUAID) == 0 && len(client.UAID) > 0 { candidateUAID = client.UAID } log.Printf("=== uaid: ", f["uaid"]) if candidateUAID == "" { verbose("No \"uaid\" found. Creating one...") candidateUAID, err = uuid.GenUUID() if err != nil { status = 400 handleError(client, f, 500, "UUID Generation error") log.Println("GenUUID error %s", err) } } else { if filter.Find([]byte(candidateUAID)) != nil { handleError(client, f, 401, "Invalid Command") verbose("Only use characters from \"A-Za-z0-9._-\" for UAIDs") return } if len(candidateUAID) > 100 { handleError(client, f, 401, "Invalid Command") verbose( "UAIDs should be less than 100 characters long.", "Try using a UUID4 value.") return } } prevUAID := client.UAID client.UAID = candidateUAID resetClient := false for _, chid := range f["channelIDs"].([]interface{}) { channelID := chid.(string) if filter.Find([]byte(channelID)) != nil { verbose(fmt.Sprintf("Skipping invalid channel %s", chid)) continue } if gServerState.UAIDToChannelIDs[client.UAID] == nil { //gServerState.UAIDToChannelIDs[client.UAID] = make(ChannelIDSet) // since we don't have any channelIDs, don't bother looping //any more verbose("No channels found for UAID, resetting...") resetClient = true break } if _, ok := gServerState.UAIDToChannelIDs[client.UAID][channelID]; !ok { verbose("Channel not found for UAID, resetting...") resetClient = true break } } if resetClient { // delete the older connection for _, channel := range gServerState.UAIDToChannelIDs[prevUAID] { delete(gServerState.UAIDToChannelIDs[prevUAID], channel.ChannelID) delete(gServerState.ChannelIDToChannel, channel.ChannelID) } delete(gServerState.ConnectedClients, client.UAID) delete(gServerState.UAIDToChannelIDs, client.UAID) // TODO(nsm) clear up ChannelIDToChannels which now has extra // channelIDs not associated with any client uaid, err := uuid.GenUUID() if err != nil { status = 400 log.Println("GenUUID error %s", err) } client.UAID = uaid } gServerState.ConnectedClients[client.UAID] = client if f["wakeup_hostport"] != nil { m := f["wakeup_hostport"].(map[string]interface{}) client.Ip = m["ip"].(string) client.Port = m["port"].(float64) log.Println("Got hostport pair ", client.Ip, client.Port) } else { log.Println("No hostport ", f) } type HelloResponse struct { Name string `json:"messageType"` Status int `json:"status"` UAID string `json:"uaid"` } hello := HelloResponse{"hello", status, client.UAID} j, err := json.Marshal(hello) if err != nil { log.Println("Could not convert hello response to json %s", err) return } if err = websocket.Message.Send(client.Websocket, string(j)); err != nil { log.Println("Could not send message to ", client.Websocket, err.Error()) } }