telnetroulette/match.go

180 lines
3.3 KiB
Go
Raw Normal View History

2024-06-08 13:52:22 +02:00
package main
2024-06-08 23:23:20 +02:00
import (
"math/rand"
"strconv"
)
2024-06-08 13:52:22 +02:00
type Match struct {
2024-06-08 23:23:20 +02:00
host *Client
guest *Client
round int
itemCount int
gun Gun
Guestturn bool
}
func (m *Match) LoadGun() {
switch m.round {
case 1:
m.gun.magazine = m.gun.Reload(4)
break
case 2:
m.gun.magazine = m.gun.Reload(8)
break
case 3:
m.gun.magazine = m.gun.Reload(16)
break
default:
m.gun.magazine = m.gun.Reload(1)
break
}
}
func (m *Match) Start() {
m.NextRound()
}
func (m *Match) resetHP() {
switch m.round {
case 1:
m.host.HP = 5
m.guest.HP = 5
break
case 2:
m.host.HP = 2
m.guest.HP = 2
break
case 3:
m.host.HP = 4
m.guest.HP = 4
break
default:
m.host.HP = 1
m.guest.HP = 1
break
}
}
func (m *Match) announceRounds() {
live := 0
blank := 0
for _, round := range m.gun.magazine {
if round {
live++
} else {
blank++
}
}
doubled := "Shotgun is normal"
if m.gun.doubled {
doubled = "Shotgun is doubled"
}
m.Send("\r\nRounds are:\r\nLive - " + strconv.Itoa(live) + "\r\nBlank - " + strconv.Itoa(blank) + "\r\n" + doubled)
}
func (m *Match) announceHP() {
m.Send("\r\nHPs are:\r\n" +
m.host.Username + "(" + strconv.Itoa(m.host.ID) + ")" + " - " + strconv.Itoa(m.host.HP) + "\r\n" +
m.guest.Username + "(" + strconv.Itoa(m.guest.ID) + ")" + " - " + strconv.Itoa(m.guest.HP))
}
func (m *Match) giveItems() {
count := 0
switch m.round {
case 2:
count = 2
break
case 3:
count = 3
break
}
for i := 0; i < count; i++ {
m.host.items = append(m.host.items, Item{
kind: rand.Intn(m.itemCount), //currently only item 0 exists
match: m,
owner: m.host,
})
m.guest.items = append(m.host.items, Item{
kind: rand.Intn(m.itemCount), //currently only item 0 exists
match: m,
owner: m.guest,
})
}
}
func (m *Match) announceItems() {
out := ""
for i := 0; i < len(m.host.items); i++ {
name := m.host.items[i].name()
out += strconv.Itoa(i) + " - " + name + "\r\n"
}
m.SendMessage("My items:\r\n"+out, m.host)
out = ""
for i := 0; i < len(m.guest.items); i++ {
name := m.guest.items[i].name()
out += strconv.Itoa(i) + " - " + name + "\r\n"
}
m.SendMessage("My items:\r\n"+out, m.guest)
}
func (m *Match) NextRound() bool {
defer m.announceItems()
defer m.announceRounds()
defer m.announceHP()
defer m.LoadGun()
defer m.resetHP()
defer m.giveItems()
if m.round < 3 {
m.round++
} else if m.round == 3 {
return true //final
}
return false
}
func (m *Match) Send(message string) {
m.host.sendMessage(message, m.host)
m.guest.sendMessage(message, m.guest)
}
func (m *Match) SendMessage(message string, sender *Client) {
m.host.sendMessage(message, sender)
m.guest.sendMessage(message, sender)
}
func (m *Match) announceTurn() {
if m.Guestturn {
m.guest.sendMessage("It is your turn to shoot", m.host)
} else {
m.host.sendMessage("It is your turn to shoot", m.guest)
}
}
func (m *Match) CheckStatus() {
if m.host.HP < 1 {
m.SendMessage("I died", m.host)
m.SendMessage("I won", m.guest)
}
if m.guest.HP < 1 {
m.SendMessage("I died", m.guest)
m.SendMessage("I won", m.host)
}
if m.host.HP > 0 && m.guest.HP > 0 {
if m.round >= 3 {
m.SendMessage("I won", m.host)
m.SendMessage("I won", m.guest)
} else {
m.announceTurn()
}
}
if len(m.gun.magazine) == 0 {
m.NextRound()
}
2024-06-08 13:52:22 +02:00
}