mainstub.go 2.66 KB
Newer Older
1
2
3
4
5
package main

import (
	tg "fahrradwurstmond2.0/telnet_gui"
	"fmt"
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
6
7
	"log"
	"net"
8
	"sync"
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
9
	"time"
Michael Gebhard's avatar
Michael Gebhard committed
10
	"math/rand"
11
12
)

13
type globalState struct {
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
14
	lock    *sync.Mutex
15
	players map[string]*Player
16
17
18
19
20
21
22
23
24
25
26
27
}

func (gs *globalState) Lock() {
	gs.lock.Lock()
}

func (gs *globalState) Unlock() {
	gs.lock.Unlock()
}

var (
	gs *globalState
28
29
)

Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
30
type FakeConn struct{}
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

func (conn *FakeConn) Write(p []byte) (int, error) {
	return len(p), nil
}

func (conn *FakeConn) Read(p []byte) (int, error) {
	return 0, nil
}
func (conn *FakeConn) Close() error {
	return nil
}

func (conn *FakeConn) LocalAddr() net.Addr {
	return nil
}

func (conn *FakeConn) RemoteAddr() net.Addr {
	return nil
}

func (conn *FakeConn) SetDeadline(t time.Time) error {
	return nil
}

func (conn *FakeConn) SetReadDeadline(t time.Time) error {
	return nil
}

func (conn *FakeConn) SetWriteDeadline(t time.Time) error {
	return nil
}

63
64
65
66
func fakeInputLoop(kill chan struct{}) {
	<-kill
}

67
68
69
70
71
func fakeWindowSizeLoop(kill chan struct{}) {
	<-kill
	<-kill
}

72
73
func main() {
	var err error
Michael Gebhard's avatar
Michael Gebhard committed
74
75
	rand.Seed(time.Now().UnixNano())
	InitBoard()
76
77
78
79
	gs = &globalState{}
	gs.lock = &sync.Mutex{}
	gs.players = make(map[string]*Player)
	gs.players["ceddral"] = &Player{
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
80
		conn:       &FakeConn{},
81
		connClosed: make(chan struct{}, 1),
82
		tnState:    NewTelnetState(),
83
		inputEvent: make(chan []byte, 50),
84
		inputKill:  make(chan struct{}, 0),
85
		windowSizeLoopKill:  make(chan struct{}, 0),
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
86
87
		name:       "ceddral",
		context:    nil,
88
		scr:        tg.NewScreen(&FakeConn{}, 80, 24, tg.ColorDefault, tg.ColorDefault),
89
	}
90
	gs.players["ceddral"].connClosed<-struct{}{}
91
	go fakeInputLoop(gs.players["ceddral"].inputKill)
92
	go gs.players["ceddral"].windowSizeLoop()
Jonny Schäfer's avatar
Jonny Schäfer committed
93
	gs.players["since"] = &Player{
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
94
		conn:       &FakeConn{},
95
		connClosed: make(chan struct{}, 1),
96
		tnState:    NewTelnetState(),
Jonny Schäfer's avatar
Jonny Schäfer committed
97
		inputEvent: make(chan []byte, 50),
98
		inputKill:  make(chan struct{}, 0),
99
		windowSizeLoopKill:  make(chan struct{}, 0),
Michael Gebhard's avatar
go fmt    
Michael Gebhard committed
100
101
		name:       "since",
		context:    nil,
102
		scr:        tg.NewScreen(&FakeConn{}, 80, 24, tg.ColorDefault, tg.ColorDefault),
Jonny Schäfer's avatar
Jonny Schäfer committed
103
	}
104
	gs.players["since"].connClosed<-struct{}{}
105
	go fakeInputLoop(gs.players["since"].inputKill)
106
	go gs.players["since"].windowSizeLoop()
ceddral's avatar
ceddral committed
107
	NewGame([]string{"since", "ceddral"}, []string{"base", "ext1"})
Jonny Schäfer's avatar
Jonny Schäfer committed
108

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	if err != nil {
		panic(err)
	}
	stubPlayerConnectLoop(2323)
}

func stubPlayerConnectLoop(port int) {
	ln, err := net.Listen("tcp", ":"+fmt.Sprintf("%d", port))
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Server started")
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println(err)
			continue
		}
		go func() {
128
			gs.Lock()
129
			defer gs.Unlock()
130
			p := NewPlayer(conn)
131
132
133
			if p == nil {
				return
			}
134
135
136
137
			p.context.Register(p)
		}()
	}
}