Beispiel #1
0
func NewWorld() (*World, error) {
	var world World
	var region Region

	regionFiles, err := ioutil.ReadDir("./regions/")
	if err != nil {
		log.Printf("Could not read maps dir\n")
		return nil, err
	}

	world.Pcs = make(map[string]*Actor)
	world.Regions = make(map[string]*Region)

	// Load every region file in the regions folder
	for _, f := range regionFiles {
		if filepath.Ext(f.Name()) != ".json" {
			continue
		}

		log.Printf("Loading region %s\n", f.Name())

		file, err := ioutil.ReadFile("regions/" + f.Name())
		if err != nil {
			log.Printf("Could not read regions/%s\n", f.Name())
			return nil, err
		}

		err = json.Unmarshal(file, &region)
		if err != nil {
			log.Printf("Region %s exists, but config file is corrupt\n", f.Name())
			return nil, err
		}

		// Create a room handler goroutine and pipes to talk to and from each room
		for _, h := range region.Rows {
			for _, room := range h {
				//log.Printf(room);
				room.Pcs = make(map[string]*Pc)
				room.Npcs = make(map[string]*Npc)
				room.CmdChanWriteAsync, room.CmdChanReadAsync = network.NewPipe()
				room.CmdChanWriteSync = make(chan RoomHandlerCmd)
				room.CmdChanReadSync = make(chan bool)
				room.UpdateChans = make(map[string]chan<- network.GameMsg)
				for _, npc := range room.NpcEntities {
					room.Npcs[npc.Name] = npc
				}

				go roomHandler(room)
			}
		}

		world.Regions[region.Name] = &region
		world.RoomFetcherInChan = make(chan RoomFetcherMsg, 1)
		world.RoomFetcherOutChan = make(chan RoomFetcherMsg, 1)
		go world.roomFetcher()
	}

	return &world, nil
}
Beispiel #2
0
func NewClientConn(gameConn *network.GameConn) *ClientConn {
	var client ClientConn
	client.GameConn = gameConn
	client.SendChanWrite, client.SendChanRead = network.NewPipe()
	go client.clientReceiver()
	go client.clientSender()
	return &client
}
Beispiel #3
0
func main() {
	log.SetFlags(log.Lshortfile)

	// Prompt the user for their name and pass
	var name, pass, character string
	fmt.Printf("*** Stuff N' Things the RPG ***\n")
	fmt.Printf("Login:"******"Password:"******"Logging in as %s\n", name)

	req := network.GameMsg{LoginReq: &network.LoginReq{Version: 1, Username: name, Password: pass}}
	//log.Printf("main: %s\n", req);

	// Connect to the server
	conn, err := network.Connect("")
	if err != nil {
		log.Fatal(err)
	}

	// Send the request
	err = network.Send(conn, req)
	if err != nil {
		log.Fatal(err)
	}
	resp, msgType, err := network.Recv(conn)
	if err != nil {
		log.Fatal(err)
	}

	// Did we log in successfully?
	if msgType == network.TypeResp && resp.Resp.Success == true {
		fmt.Printf("Login Successful :)\n")
	} else {
		fmt.Printf("Login Failed :(\n")
		fmt.Printf("Server says: %s\n", resp.Resp.Message)
		os.Exit(1)
	}

	// Character select
	fmt.Printf("Available characters:\n")
	for index, each := range resp.Resp.Data {
		fmt.Printf("%d)  %s\n", index+1, each)
	}
	fmt.Printf("Which character would you like to play? ")
	fmt.Scanln(&character)

	req = network.GameMsg{AssumeActorReq: &network.AssumeActorReq{Actor: character}}

	// Send the request
	err = network.Send(conn, req)
	if err != nil {
		log.Fatal(err)
	}

	update := make(chan *network.RoomUpdate)
	activity := make(chan string)

	sendChanWrite, sendChanRead := network.NewPipe()

	go sendCmds(conn, sendChanRead)
	go recvUpdates(conn, update, activity)

	// Set up the UI
	// TODO: make this whole deal an object, add character and other stuff to it as a member
	startUI(update, activity, sendChanWrite, character)
}