telnetroulette/match.go

165 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-06-08 13:52:22 +02:00
package main
2024-06-08 23:23:20 +02:00
import (
2024-06-09 18:33:29 +02:00
"math/rand"
2024-06-08 23:23:20 +02:00
"strconv"
)
2024-06-09 11:02:18 +02:00
type match struct {
host *client
guest *client
round int
maxHP int
itemCount int
gun gun
guestsTurn bool
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
func (m *match) loadGun() {
2024-06-08 23:23:20 +02:00
switch m.round {
case 1:
2024-06-09 18:33:29 +02:00
m.gun.magazine = m.gun.reload(rand.Intn(4) + 2)
2024-06-08 23:23:20 +02:00
break
case 2:
2024-06-09 18:33:29 +02:00
m.gun.magazine = m.gun.reload(rand.Intn(6) + 4)
2024-06-08 23:23:20 +02:00
break
case 3:
2024-06-09 18:33:29 +02:00
m.gun.magazine = m.gun.reload(rand.Intn(10) + 6)
2024-06-08 23:23:20 +02:00
break
default:
2024-06-09 18:33:29 +02:00
m.gun.magazine = m.gun.reload(rand.Intn(16) + 1)
2024-06-08 23:23:20 +02:00
break
}
}
2024-06-09 11:02:18 +02:00
func (m *match) start() {
m.nextRound()
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
func (m *match) resetHP() {
HP := 1
2024-06-08 23:23:20 +02:00
switch m.round {
case 1:
2024-06-09 18:33:29 +02:00
HP = 2
2024-06-08 23:23:20 +02:00
break
case 2:
2024-06-09 18:33:29 +02:00
HP = 4
2024-06-08 23:23:20 +02:00
break
case 3:
2024-06-09 18:33:29 +02:00
HP = 5
2024-06-08 23:23:20 +02:00
break
}
2024-06-09 11:02:18 +02:00
m.host.HP = HP
m.guest.HP = HP
m.maxHP = HP
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
func (m *match) announceRounds() {
2024-06-08 23:23:20 +02:00
live := 0
blank := 0
for _, round := range m.gun.magazine {
if round {
live++
} else {
blank++
}
}
doubled := "Shotgun is normal"
if m.gun.doubled {
2024-06-09 11:02:18 +02:00
doubled = "Shotgun is sawed"
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
m.send("\r\nRounds are:\r\nLive - " + strconv.Itoa(live) + "\r\nBlank - " + strconv.Itoa(blank) + "\r\n" + doubled)
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
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())
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
func (m *match) giveItems() {
2024-06-08 23:23:20 +02:00
count := 0
switch m.round {
case 2:
count = 2
break
case 3:
count = 3
break
}
for i := 0; i < count; i++ {
2024-06-09 11:02:18 +02:00
m.host.giveItem()
m.guest.giveItem()
2024-06-08 23:23:20 +02:00
}
}
2024-06-09 11:02:18 +02:00
func (m *match) announceItems() {
m.sendMessage(m.host.renderItems(), m.host)
m.sendMessage(m.guest.renderItems(), m.guest)
2024-06-08 23:23:20 +02:00
}
2024-06-09 11:02:18 +02:00
func (m *match) nextRound() bool {
defer m.announceTurn()
2024-06-08 23:23:20 +02:00
defer m.announceItems()
defer m.announceRounds()
defer m.announceHP()
2024-06-09 11:02:18 +02:00
defer m.loadGun()
2024-06-08 23:23:20 +02:00
defer m.resetHP()
defer m.giveItems()
2024-06-09 11:02:18 +02:00
m.guestsTurn = false
2024-06-08 23:23:20 +02:00
if m.round < 3 {
m.round++
} else if m.round == 3 {
return true //final
}
return false
}
2024-06-09 11:02:18 +02:00
func (m *match) send(message string) {
2024-06-08 23:23:20 +02:00
m.host.sendMessage(message, m.host)
m.guest.sendMessage(message, m.guest)
}
2024-06-09 11:02:18 +02:00
func (m *match) sendMessage(message string, sender *client) {
2024-06-08 23:23:20 +02:00
m.host.sendMessage(message, sender)
m.guest.sendMessage(message, sender)
}
2024-06-09 11:02:18 +02:00
func (m *match) announceTurn() {
if m.guestsTurn {
m.guest.sendMessage("It is your turn\a", m.host)
2024-06-08 23:23:20 +02:00
} else {
2024-06-09 11:02:18 +02:00
m.host.sendMessage("It is your turn\a", m.guest)
2024-06-08 23:23:20 +02:00
}
}
2024-06-09 11:02:18 +02:00
func (m *match) checkStatus() {
2024-06-08 23:23:20 +02:00
if m.host.HP < 1 {
2024-06-09 11:02:18 +02:00
m.sendMessage("I died", m.host)
m.sendMessage("I won", m.guest)
2024-06-09 18:33:29 +02:00
m.nextRound()
2024-06-08 23:23:20 +02:00
}
if m.guest.HP < 1 {
2024-06-09 11:02:18 +02:00
m.sendMessage("I died", m.guest)
m.sendMessage("I won", m.host)
2024-06-09 18:33:29 +02:00
m.nextRound()
2024-06-08 23:23:20 +02:00
}
if m.host.HP > 0 && m.guest.HP > 0 {
if m.round >= 3 {
2024-06-09 11:02:18 +02:00
m.sendMessage("I won", m.host)
m.sendMessage("I won", m.guest)
2024-06-08 23:23:20 +02:00
} else {
m.announceTurn()
}
}
if len(m.gun.magazine) == 0 {
2024-06-09 18:33:29 +02:00
m.loadGun()
m.giveItems()
m.announceItems()
2024-06-08 23:23:20 +02:00
}
2024-06-08 13:52:22 +02:00
}