Commit d32bd3d3 authored by ceddral's avatar ceddral
Browse files

init gamelobby

parent 0c4ce822
package main
import (
tg "fahrradwurstmond2.0/telnet_gui"
"sync"
)
type GameLobbyPlayer struct {
name string
scr *tg.Screen
lobbyv *tg.View
lobbyKill chan struct{}
lobbyEvent chan struct{}
inputEvent chan []byte
}
type GameLobby struct {
lock *sync.Mutex
players map[string]*GameLobbyPlayer
}
func NewGameLobby() (gl *GameLobby) {
return &GameLobby{
lock: &sync.Mutex{},
players: make(map[string]*GameLobbyPlayer),
}
}
func (gl *GameLobby) Lock() {
gl.lock.Lock()
}
func (gl *GameLobby) Unlock() {
gl.lock.Unlock()
}
func (gl *GameLobby) Register(glp *Player) {
gl.Lock()
p := &GameLobbyPlayer{
name: glp.name,
scr: glp.scr,
lobbyv: tg.NewView(glp.scr, 0, 0, glp.scr.W, glp.scr.H),
lobbyKill: make(chan struct{}, 1),
lobbyEvent: make(chan struct{}, 50),
inputEvent: glp.inputEvent,
}
if pOld, ok := gl.players[glp.name]; ok {
pOld.lobbyKill <- struct{}{} // trigger loop termination
close(pOld.lobbyEvent)
pOld.lobbyKill <- struct{}{} // await loop termination
}
gl.players[glp.name] = p
gl.Unlock()
go gl.Loop(p)
gl.Update()
}
func (gl *GameLobby) Resize(w, h int, glp *Player) {
p := gl.players[glp.name]
p.lobbyv.Resize(w, h)
}
func (gl *GameLobby) Redraw(glp *Player) {
p := gl.players[glp.name]
p.lobbyEvent<-struct{}{}
}
func (gl *GameLobby) Update() {
for _, p := range gl.players {
select {
case p.lobbyEvent <- struct{}{}:
default:
panic("dos") //TODO only end game
}
}
}
func (gl *GameLobby) StartGame() {
ps := make([]string, 0)
gl.Lock()
for name, p := range gl.players {
p.lobbyKill <- struct{}{} // trigger termination
close(p.lobbyEvent)
ps = append(ps, name)
}
for _, p := range gl.players {
p.lobbyKill <- struct{}{} // await termination
}
gl.players = make(map[string]*GameLobbyPlayer)
gl.Unlock()
NewGame(ps, []string{"base", "ext1"})
}
func (gl *GameLobby) Loop(p *GameLobbyPlayer) {
for {
select {
case <-p.lobbyEvent:
case input := <-p.inputEvent:
key := toKey(input)
switch key {
case keyReturn:
// TODO only allow starting game for player #1
go gl.StartGame()
}
}
select { // check kill before doing anything that may lock
case <-p.lobbyKill:
return
default:
}
p.Redraw(gl)
p.scr.Update()
}
}
func (p *GameLobbyPlayer) Redraw(gl *GameLobby) {
gl.Lock()
defer gl.Unlock()
v := p.lobbyv
line := 0
fg := tg.ColorDefault
bg := tg.ColorDefault
for name := range gl.players {
v.DrawTextClearLine(fg, bg, []byte(name), 0, line)
line++
}
}
package main
import (
tg "fahrradwurstmond2.0/telnet_gui"
"fmt"
"log"
"net"
......@@ -25,6 +24,8 @@ func (gs *globalState) Unlock() {
var (
gs *globalState
glgl *GameLobby
)
type FakeConn struct{}
......@@ -76,35 +77,7 @@ func main() {
gs = &globalState{}
gs.lock = &sync.Mutex{}
gs.players = make(map[string]*Player)
gs.players["ceddral"] = &Player{
conn: &FakeConn{},
connClosed: make(chan struct{}, 1),
tnState: NewTelnetState(),
inputEvent: make(chan []byte, 50),
inputKill: make(chan struct{}, 0),
windowSizeLoopKill: make(chan struct{}, 0),
name: "ceddral",
context: nil,
scr: tg.NewScreen(&FakeConn{}, 80, 24, tg.ColorDefault, tg.ColorDefault),
}
gs.players["ceddral"].connClosed<-struct{}{}
go fakeInputLoop(gs.players["ceddral"].inputKill)
go gs.players["ceddral"].windowSizeLoop()
gs.players["since"] = &Player{
conn: &FakeConn{},
connClosed: make(chan struct{}, 1),
tnState: NewTelnetState(),
inputEvent: make(chan []byte, 50),
inputKill: make(chan struct{}, 0),
windowSizeLoopKill: make(chan struct{}, 0),
name: "since",
context: nil,
scr: tg.NewScreen(&FakeConn{}, 80, 24, tg.ColorDefault, tg.ColorDefault),
}
gs.players["since"].connClosed<-struct{}{}
go fakeInputLoop(gs.players["since"].inputKill)
go gs.players["since"].windowSizeLoop()
NewGame([]string{"since", "ceddral"}, []string{"base", "ext1"})
glgl = NewGameLobby()
if err != nil {
panic(err)
......
......@@ -82,10 +82,11 @@ func NewPlayer(conn net.Conn) (p *Player) {
go p.inputLoop()
} else {
p.windowSizeLoopKill = make(chan struct{})
// TODO new players need to be sent to the meta lobby context
p.context = glgl
p.scr = tg.NewScreen(conn, 80, 24, tg.ColorDefault, tg.ColorDefault)
go p.windowSizeLoop()
gs.players[p.name] = p
// TODO
// new players need to be sent to the meta lobby context
panic("TODO Connect for new players not implemented yet")
}
err = p.scr.Redraw()
if err != nil {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment