Beispiel #1
0
func SavePlayer(p *Player) error {
	hash := fnv.New64()
	hash.Write([]byte(p.Name))
	name := fmt.Sprintf("%x.player", hash.Sum64())
	f, err := os.Create(filepath.Join(base.GetDataDir(), "players", name))
	if err != nil {
		return err
	}
	defer f.Close()
	base.SetStoreVal("last player", name)
	return EncodePlayer(f, p)
}
Beispiel #2
0
func editMode() {
	draggingAndZooming(editor.GetViewer())
	if ui.FocusWidget() == nil {
		for name := range editors {
			if key_map[fmt.Sprintf("%s editor", name)].FramePressCount() > 0 && ui.FocusWidget() == nil {
				ui.RemoveChild(editor)
				editor_name = name
				editor = editors[editor_name]
				loadAllRegistries()
				editor.Reload()
				ui.AddChild(editor)
			}
		}

		if key_map["save"].FramePressCount() > 0 && chooser == nil {
			path, err := editor.Save()
			if err != nil {
				base.Warn().Printf("Failed to save: %v", err.Error())
			}
			if path != "" && err == nil {
				base.SetStoreVal(fmt.Sprintf("last %s path", editor_name), base.TryRelative(datadir, path))
			}
		}

		if key_map["load"].FramePressCount() > 0 && chooser == nil {
			callback := func(path string, err error) {
				ui.DropFocus()
				ui.RemoveChild(anchor)
				chooser = nil
				anchor = nil
				err = editor.Load(path)
				if err != nil {
					base.Warn().Printf("Failed to load: %v", err.Error())
				} else {
					base.SetStoreVal(fmt.Sprintf("last %s path", editor_name), base.TryRelative(datadir, path))
				}
			}
			chooser = gui.MakeFileChooser(filepath.Join(datadir, fmt.Sprintf("%ss", editor_name)), callback, gui.MakeFileFilter(fmt.Sprintf(".%s", editor_name)))
			anchor = gui.MakeAnchorBox(gui.Dims{wdx, wdy})
			anchor.AddChild(chooser, gui.Anchor{0.5, 0.5, 0.5, 0.5})
			ui.AddChild(anchor)
			ui.TakeFocus(chooser)
		}

		// Don't select tabs in an editor if we're doing some other sort of command
		ok_to_select := true
		for _, v := range key_map {
			if v.FramePressCount() > 0 {
				ok_to_select = false
				break
			}
		}
		if ok_to_select {
			for i := 1; i <= 9; i++ {
				if gin.In().GetKey(gin.KeyId('0'+i)).FramePressCount() > 0 {
					editor.SelectTab(i - 1)
				}
			}
		}
	}
}
Beispiel #3
0
func InsertOnlineMenu(ui gui.WidgetParent) error {
	var sm OnlineMenu
	datadir := base.GetDataDir()
	err := base.LoadAndProcessObject(filepath.Join(datadir, "ui", "start", "online", "layout.json"), "json", &sm.layout)
	if err != nil {
		return err
	}
	sm.buttons = []ButtonLike{
		&sm.layout.Back,
		&sm.layout.Unstarted.Up,
		&sm.layout.Unstarted.Down,
		&sm.layout.Active.Up,
		&sm.layout.Active.Down,
		&sm.layout.User,
		&sm.layout.NewGame,
	}
	sm.control.in = make(chan struct{})
	sm.control.out = make(chan struct{})
	sm.layout.Back.f = func(interface{}) {
		ui.RemoveChild(&sm)
		InsertStartMenu(ui)
	}
	sm.ui = ui

	fmt.Sscanf(base.GetStoreVal("netid"), "%d", &net_id)
	if net_id == 0 {
		net_id = mrgnet.NetId(mrgnet.RandomId())
		base.SetStoreVal("netid", fmt.Sprintf("%d", net_id))
	}

	in_newgame := false
	sm.layout.NewGame.f = func(interface{}) {
		if in_newgame {
			return
		}
		in_newgame = true
		go func() {
			var req mrgnet.NewGameRequest
			req.Id = net_id
			var resp mrgnet.NewGameResponse
			done := make(chan bool, 1)
			go func() {
				mrgnet.DoAction("new", req, &resp)
				done <- true
			}()
			select {
			case <-done:
			case <-time.After(5 * time.Second):
				resp.Err = "Couldn't connect to server."
			}
			<-sm.control.in
			defer func() {
				in_newgame = false
				sm.control.out <- struct{}{}
			}()
			if resp.Err != "" {
				sm.layout.Error.err = resp.Err
				base.Error().Printf("Couldn't make new game: %v", resp.Err)
				return
			}
			ui.RemoveChild(&sm)
			err := InsertMapChooser(
				ui,
				func(name string) {
					ui.AddChild(MakeGamePanel(name, nil, nil, resp.Game_key))
				},
				InsertOnlineMenu,
			)
			if err != nil {
				base.Error().Printf("Error making Map Chooser: %v", err)
			}
		}()
	}

	for _, _glb := range []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted} {
		glb := _glb
		glb.Up.f = func(interface{}) {
			glb.Scroll.Up()
		}
		glb.Up.valid_func = func() bool {
			return glb.Scroll.Height > glb.Scroll.Dy
		}
		glb.Down.f = func(interface{}) {
			glb.Scroll.Down()
		}
		glb.Down.valid_func = func() bool {
			return glb.Scroll.Height > glb.Scroll.Dy
		}

		glb.update = make(chan mrgnet.ListGamesResponse)
	}
	go func() {
		var resp mrgnet.ListGamesResponse
		mrgnet.DoAction("list", mrgnet.ListGamesRequest{Id: net_id, Unstarted: true}, &resp)
		sm.layout.Unstarted.update <- resp
	}()
	go func() {
		var resp mrgnet.ListGamesResponse
		mrgnet.DoAction("list", mrgnet.ListGamesRequest{Id: net_id, Unstarted: false}, &resp)
		sm.layout.Active.update <- resp
	}()

	sm.layout.User.Button.f = func(interface{}) {
		var req mrgnet.UpdateUserRequest
		req.Name = sm.layout.User.Entry.text
		req.Id = net_id
		var resp mrgnet.UpdateUserResponse
		go func() {
			mrgnet.DoAction("user", req, &resp)
			<-sm.control.in
			sm.layout.User.SetText(resp.Name)
			sm.update_alpha = 1.0
			sm.update_time = time.Now()
			sm.control.out <- struct{}{}
		}()
	}
	go func() {
		var resp mrgnet.UpdateUserResponse
		mrgnet.DoAction("user", mrgnet.UpdateUserRequest{Id: net_id}, &resp)
		<-sm.control.in
		sm.layout.User.SetText(resp.Name)
		sm.update_alpha = 1.0
		sm.update_time = time.Now()
		sm.control.out <- struct{}{}
	}()

	ui.AddChild(&sm)
	return nil
}