-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
118 lines (100 loc) · 2.65 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"math/rand"
"github.com/kjander0/ctf/conf"
"github.com/kjander0/ctf/entity"
"github.com/kjander0/ctf/logger"
"github.com/kjander0/ctf/net"
"github.com/kjander0/ctf/web"
)
type Game struct {
ClientC chan web.Client
World entity.World
}
func NewGame(clientC chan web.Client, gameMap *entity.Map) Game {
return Game{
ClientC: clientC,
World: entity.NewWorld(gameMap),
}
}
func (g *Game) Run() {
ticker := NewTicker(float64(conf.Shared.TickRate))
ticker.Start()
g.roundReset()
for {
// TODO: probs wanna accept more than 1 client per tick?
select {
case newClient := <-g.ClientC:
ok, id := g.World.NextPlayerId()
if !ok {
logger.Debug("server full, rejecting connection")
close(newClient.WriteC)
}
team := findNextTeam(&g.World)
g.World.PlayerList = append(g.World.PlayerList, entity.NewPlayer(id, team, newClient))
default:
}
net.ReceiveMessages(&g.World)
entity.UpdatePlayers(&g.World)
entity.UpdateProjectiles(&g.World)
entity.UpdateFlags(&g.World)
net.SendMessages(&g.World)
removeDisconnectedPlayers(&g.World)
if g.World.WinCooldownTicks > 0 {
g.World.WinCooldownTicks--
}
if g.World.WinningTeam != -1 && g.World.WinCooldownTicks == 0 {
g.roundReset()
}
g.World.Tick += 1
ticker.Sleep()
}
}
func (g *Game) roundReset() {
g.World.WinningTeam = -1
// Reset flags
g.World.FlagList = []entity.Flag{}
for _, pos := range g.World.Map.FlagSpawns {
g.World.FlagList = append(g.World.FlagList, entity.NewFlag(pos))
}
// Reset players
for i := range g.World.PlayerList {
player := &g.World.PlayerList[i]
entity.SendToJail(&g.World, player)
}
g.World.LaserList = []entity.Laser{}
}
func findNextTeam(world *entity.World) int {
greenCount := 0
redCount := 0
for i := range world.PlayerList {
switch world.PlayerList[i].Team {
case entity.TeamGreen:
greenCount += 1
case entity.TeamRed:
redCount += 1
}
}
if greenCount > redCount {
return entity.TeamGreen
} else if redCount > greenCount {
return entity.TeamGreen
} else {
if rand.Intn(2) == 0 {
return entity.TeamGreen
} else {
return entity.TeamRed
}
}
}
func removeDisconnectedPlayers(world *entity.World) {
for i := len(world.PlayerList) - 1; i >= 0; i -= 1 { // loop backwards for removing elements
if world.PlayerList[i].DoDisconnect {
logger.Infof("'%s' disconnected, remaining players: %d", world.PlayerList[i].Client.Username, len(world.PlayerList)-1)
close(world.PlayerList[i].Client.WriteC)
world.FreePlayerId(world.PlayerList[i].Id)
world.PlayerList[i] = world.PlayerList[len(world.PlayerList)-1]
world.PlayerList = world.PlayerList[0 : len(world.PlayerList)-1]
}
}
}