telnetroulette/match.go
2024-06-09 18:33:29 +02:00

165 lines
2.8 KiB
Go

package main
import (
"math/rand"
"strconv"
)
type match struct {
host *client
guest *client
round int
maxHP int
itemCount int
gun gun
guestsTurn bool
}
func (m *match) loadGun() {
switch m.round {
case 1:
m.gun.magazine = m.gun.reload(rand.Intn(4) + 2)
break
case 2:
m.gun.magazine = m.gun.reload(rand.Intn(6) + 4)
break
case 3:
m.gun.magazine = m.gun.reload(rand.Intn(10) + 6)
break
default:
m.gun.magazine = m.gun.reload(rand.Intn(16) + 1)
break
}
}
func (m *match) start() {
m.nextRound()
}
func (m *match) resetHP() {
HP := 1
switch m.round {
case 1:
HP = 2
break
case 2:
HP = 4
break
case 3:
HP = 5
break
}
m.host.HP = HP
m.guest.HP = HP
m.maxHP = HP
}
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 sawed"
}
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.getMentionName() + " - " + m.host.renderHP() + "\r\n" +
m.guest.getMentionName() + " - " + m.guest.renderHP())
}
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.giveItem()
m.guest.giveItem()
}
}
func (m *match) announceItems() {
m.sendMessage(m.host.renderItems(), m.host)
m.sendMessage(m.guest.renderItems(), m.guest)
}
func (m *match) nextRound() bool {
defer m.announceTurn()
defer m.announceItems()
defer m.announceRounds()
defer m.announceHP()
defer m.loadGun()
defer m.resetHP()
defer m.giveItems()
m.guestsTurn = false
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.guestsTurn {
m.guest.sendMessage("It is your turn\a", m.host)
} else {
m.host.sendMessage("It is your turn\a", m.guest)
}
}
func (m *match) checkStatus() {
if m.host.HP < 1 {
m.sendMessage("I died", m.host)
m.sendMessage("I won", m.guest)
m.nextRound()
}
if m.guest.HP < 1 {
m.sendMessage("I died", m.guest)
m.sendMessage("I won", m.host)
m.nextRound()
}
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.loadGun()
m.giveItems()
m.announceItems()
}
}