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 var net_id mrgnet.NetId 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(10 * 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.Down.f = func(interface{}) { glb.Scroll.Down() } 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 }
func (sm *OnlineMenu) Think(g *gui.Gui, t int64) { if sm.last_t == 0 { sm.last_t = t return } dt := t - sm.last_t sm.last_t = t if sm.mx == 0 && sm.my == 0 { sm.mx, sm.my = gin.In().GetCursor("Mouse").Point() } done := false for !done { select { case sm.control.in <- struct{}{}: <-sm.control.out default: done = true } } var net_id mrgnet.NetId fmt.Sscanf(base.GetStoreVal("netid"), "%d", &net_id) for _, glb := range []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted} { select { case list := <-glb.update: glb.games = glb.games[0:0] for i := range list.Games { var b Button base.Log().Printf("Adding button: %s", list.Games[i].Name) b.Text.Justification = sm.layout.Text.Justification b.Text.Size = sm.layout.Text.Size if net_id == list.Games[i].Denizens_id { b.Text.String = fmt.Sprintf("%s vs %s as the Intruders", list.Games[i].Name, list.Games[i].Intruders_name) } else { b.Text.String = fmt.Sprintf("%s vs %s as the Denizens", list.Games[i].Name, list.Games[i].Denizens_name) } game_key := list.Game_keys[i] active := (glb == &sm.layout.Active) in_joingame := false b.f = func(interface{}) { if in_joingame { return } in_joingame = true if active { go func() { var req mrgnet.StatusRequest req.Id = net_id req.Game_key = game_key var resp mrgnet.StatusResponse done := make(chan bool, 1) go func() { mrgnet.DoAction("status", req, &resp) done <- true }() select { case <-done: case <-time.After(10 * time.Second): resp.Err = "Couldn't connect to server." } <-sm.control.in defer func() { in_joingame = false sm.control.out <- struct{}{} }() if resp.Err != "" || resp.Game == nil { sm.layout.Error.err = resp.Err base.Error().Printf("Couldn't join game: %v", resp.Err) return } sm.ui.RemoveChild(sm) sm.ui.AddChild(MakeGamePanel("", nil, nil, game_key)) }() } else { go func() { var req mrgnet.JoinGameRequest req.Id = net_id req.Game_key = game_key var resp mrgnet.JoinGameResponse done := make(chan bool, 1) go func() { mrgnet.DoAction("join", req, &resp) done <- true }() select { case <-done: case <-time.After(10 * time.Second): resp.Err = "Couldn't connect to server." } <-sm.control.in defer func() { in_joingame = false sm.control.out <- struct{}{} }() if resp.Err != "" || !resp.Successful { sm.layout.Error.err = resp.Err base.Error().Printf("Couldn't join game: %v", resp.Err) return } sm.ui.RemoveChild(sm) sm.ui.AddChild(MakeGamePanel("", nil, nil, game_key)) }() } } glb.games = append(glb.games, &b) } sort.Sort(onlineButtonSlice(glb.games)) glb.Scroll.Height = int(base.GetDictionary(sm.layout.Text.Size).MaxHeight() * float64(len(list.Games))) default: } if (gui.Point{sm.mx, sm.my}.Inside(glb.Scroll.Region())) { for _, button := range glb.games { button.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } } else { for _, button := range glb.games { button.Think(sm.region.X, sm.region.Y, 0, 0, dt) } } glb.Scroll.Think(dt) } if sm.update_alpha > 0.0 && time.Now().Sub(sm.update_time).Seconds() >= 2 { sm.update_alpha = doApproach(sm.update_alpha, 0.0, dt) } for _, button := range sm.buttons { button.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } }
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.Back.f = func(interface{}) { ui.RemoveChild(&sm) InsertStartMenu(ui) } var net_id mrgnet.NetId 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)) } for _, _glb := range []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted} { glb := _glb glb.Up.f = func(interface{}) { glb.Scroll.Up() } glb.Down.f = func(interface{}) { glb.Scroll.Down() } 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.update_user = make(chan mrgnet.UpdateUserResponse) 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.update_user <- resp }() } go func() { var resp mrgnet.UpdateUserResponse mrgnet.DoAction("user", mrgnet.UpdateUserRequest{Id: net_id}, &resp) sm.update_user <- resp }() ui.AddChild(&sm) return nil }
func (sm *OnlineMenu) Think(g *gui.Gui, t int64) { if sm.last_t == 0 { sm.last_t = t return } dt := t - sm.last_t sm.last_t = t if sm.mx == 0 && sm.my == 0 { sm.mx, sm.my = gin.In().GetCursor("Mouse").Point() } done := false for !done { select { case sm.control.in <- struct{}{}: <-sm.control.out default: done = true } } var net_id mrgnet.NetId fmt.Sscanf(base.GetStoreVal("netid"), "%d", &net_id) for i := range []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted} { glb := []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted}[i] select { case list := <-glb.update: glb.games = glb.games[0:0] for j := range list.Games { var b Button var name string base.Log().Printf("Adding button: %s", list.Games[j].Name) b.Text.Justification = sm.layout.Text.Justification b.Text.Size = sm.layout.Text.Size if net_id == list.Games[j].Denizens_id { name = list.Games[j].Name } else { name = list.Games[j].Name } b.Text.String = "Join!" game_key := list.Game_keys[j] active := (glb == &sm.layout.Active) in_joingame := false b.f = func(interface{}) { if in_joingame { return } in_joingame = true if active { go func() { var req mrgnet.StatusRequest req.Id = net_id req.Game_key = game_key var resp mrgnet.StatusResponse done := make(chan bool, 1) go func() { mrgnet.DoAction("status", 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_joingame = false sm.control.out <- struct{}{} }() if resp.Err != "" || resp.Game == nil { sm.layout.Error.err = resp.Err base.Error().Printf("Couldn't join game: %v", resp.Err) return } sm.ui.RemoveChild(sm) sm.ui.AddChild(MakeGamePanel("", nil, nil, game_key)) }() } else { go func() { var req mrgnet.JoinGameRequest req.Id = net_id req.Game_key = game_key var resp mrgnet.JoinGameResponse done := make(chan bool, 1) go func() { mrgnet.DoAction("join", 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_joingame = false sm.control.out <- struct{}{} }() if resp.Err != "" || !resp.Successful { sm.layout.Error.err = resp.Err base.Error().Printf("Couldn't join game: %v", resp.Err) return } sm.ui.RemoveChild(sm) sm.ui.AddChild(MakeGamePanel("", nil, nil, game_key)) }() } } if active { d := Button{} d.Text.String = "Delete!" d.Text.Justification = "right" d.Text.Size = sm.layout.Text.Size d.f = func(interface{}) { go func() { var req mrgnet.KillRequest req.Id = net_id req.Game_key = game_key var resp mrgnet.KillResponse done := make(chan bool, 1) go func() { mrgnet.DoAction("kill", req, &resp) done <- true }() select { case <-done: case <-time.After(5 * time.Second): resp.Err = "Couldn't connect to server." } <-sm.control.in if resp.Err != "" { sm.layout.Error.err = resp.Err base.Error().Printf("Couldn't kill game: %v", resp.Err) } else { algorithm.Choose2(&glb.games, func(gf gameField) bool { return gf.key != req.Game_key }) } sm.control.out <- struct{}{} }() } glb.games = append(glb.games, gameField{&b, &d, name, list.Game_keys[j], list.Games[j]}) } else { glb.games = append(glb.games, gameField{&b, nil, name, list.Game_keys[j], list.Games[j]}) } } glb.Scroll.Height = int(base.GetDictionary(sm.layout.Text.Size).MaxHeight() * float64(len(list.Games))) default: } sm.hover_game = nil if (gui.Point{sm.mx, sm.my}.Inside(glb.Scroll.Region())) { for i := range glb.games { game := &glb.games[i] var region gui.Region region.X = game.join.(*Button).bounds.x region.Y = game.join.(*Button).bounds.y region.Dx = glb.Scroll.Dx region.Dy = int(base.GetDictionary(sm.layout.Text.Size).MaxHeight()) if (gui.Point{sm.mx, sm.my}.Inside(region)) { sm.hover_game = game } game.join.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) if game.delete != nil { game.delete.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } } } else { for _, game := range glb.games { game.join.Think(sm.region.X, sm.region.Y, 0, 0, dt) if game.delete != nil { game.delete.Think(sm.region.X, sm.region.Y, 0, 0, dt) } } } glb.Scroll.Think(dt) } if sm.update_alpha > 0.0 && time.Now().Sub(sm.update_time).Seconds() >= 2 { sm.update_alpha = doApproach(sm.update_alpha, 0.0, dt) } for _, button := range sm.buttons { button.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } }
func (sm *OnlineMenu) Think(g *gui.Gui, t int64) { if sm.last_t == 0 { sm.last_t = t return } dt := t - sm.last_t sm.last_t = t sm.layout.Unstarted.Scroll.Think(dt) if sm.mx == 0 && sm.my == 0 { sm.mx, sm.my = gin.In().GetCursor("Mouse").Point() } select { case resp := <-sm.update_user: sm.layout.User.Entry.text = resp.Name sm.update_alpha = 1.0 sm.update_time = time.Now() default: } var net_id mrgnet.NetId fmt.Sscanf(base.GetStoreVal("netid"), "%d", &net_id) for _, glb := range []*gameListBox{&sm.layout.Active, &sm.layout.Unstarted} { select { case list := <-glb.update: glb.games = glb.games[0:0] for i := range list.Games { var b Button base.Log().Printf("Adding button: %s", list.Games[i].Name) b.Text.Justification = sm.layout.Text.Justification b.Text.Size = sm.layout.Text.Size b.Text.String = list.Games[i].Name b.f = func(interface{}) { // var req mrgnet.JoinGameRequest // req.Id = net_id // req.Game_key = list.Ids[i] // var resp mrgnet.JoinGameResponse // mrgnet.DoAction("join", req, &resp) } glb.games = append(glb.games, &b) } sort.Sort(onlineButtonSlice(glb.games)) glb.Scroll.Height = int(base.GetDictionary(sm.layout.Text.Size).MaxHeight() * float64(len(list.Games))) default: } base.Log().Printf("Num: %d, region: %v", len(glb.games), glb.Scroll.Region()) if (gui.Point{sm.mx, sm.my}.Inside(glb.Scroll.Region())) { for _, button := range glb.games { button.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } } else { for _, button := range glb.games { button.Think(sm.region.X, sm.region.Y, 0, 0, dt) } } } if sm.update_alpha > 0.0 && time.Now().Sub(sm.update_time).Seconds() >= 2 { sm.update_alpha = doApproach(sm.update_alpha, 0.0, dt) } for _, button := range sm.buttons { button.Think(sm.region.X, sm.region.Y, sm.mx, sm.my, dt) } }