gamelobby.go 2.81 KB
Newer Older
ceddral's avatar
ceddral committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
ceddral's avatar
ceddral committed
20
	playerOrder []string
ceddral's avatar
ceddral committed
21
22
23
24
25
26
}

func NewGameLobby() (gl *GameLobby) {
	return &GameLobby{
		lock:    &sync.Mutex{},
		players: make(map[string]*GameLobbyPlayer),
ceddral's avatar
ceddral committed
27
		playerOrder: make([]string, 0),
ceddral's avatar
ceddral committed
28
29
30
31
32
33
34
35
36
37
38
39
	}
}

func (gl *GameLobby) Lock() {
	gl.lock.Lock()
}

func (gl *GameLobby) Unlock() {
	gl.lock.Unlock()
}

func (gl *GameLobby) Register(glp *Player) {
40
	glp.scr.Clear()
ceddral's avatar
ceddral committed
41
	gl.Lock()
ceddral's avatar
ceddral committed
42
	gl.unregisterUnlocked(glp.name)
ceddral's avatar
ceddral committed
43
44
45
46
47
48
49
50
51
	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,
	}
	gl.players[glp.name] = p
ceddral's avatar
ceddral committed
52
	gl.playerOrder = append(gl.playerOrder, p.name)
ceddral's avatar
ceddral committed
53
54
	go gl.Loop(p)
	gl.Update()
55
	gl.Unlock()
ceddral's avatar
ceddral committed
56
57
}

ceddral's avatar
ceddral committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
func (gl *GameLobby) Unregister(name string) {
	gl.Lock()
	gl.unregisterUnlocked(name)
	gl.Unlock()
}

func (gl *GameLobby) unregisterUnlocked(name string) {
	p, ok := gl.players[name]
	if !ok {
		return
	}
	p.lobbyKill <- struct{}{} // trigger loop termination
	close(p.lobbyEvent)
	p.lobbyKill <- struct{}{} // await loop termination
	delete(gl.players, name)
	for i, namei := range gl.playerOrder {
		if namei != name {
			continue
		}
		copy(gl.playerOrder[i:], gl.playerOrder[i+1:])
		gl.playerOrder = gl.playerOrder[:len(gl.playerOrder) - 1]
	}
}

ceddral's avatar
ceddral committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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() {
	gl.Lock()
ceddral's avatar
ceddral committed
104
105
106
107
	ps := make([]string, len(gl.playerOrder))
	copy(ps, gl.playerOrder)
	for name := range gl.players {
		gl.unregisterUnlocked(name)
ceddral's avatar
ceddral committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
	}
	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
ceddral's avatar
ceddral committed
142
	for _, name := range gl.playerOrder {
ceddral's avatar
ceddral committed
143
144
145
146
		v.DrawTextClearLine(fg, bg, []byte(name), 0, line)
		line++
	}
}