Compare commits
8 Commits
a8571a8536
...
main
Author | SHA1 | Date | |
---|---|---|---|
0f78a63fab | |||
2b34c71573 | |||
bf471b4934 | |||
4e195c6ba3 | |||
111564b666 | |||
46f6c2cfec | |||
d170a5e209 | |||
d911248c53 |
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="GoContextTodo" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||||
|
<inspection_tool class="GoUnnecessarilyExportedIdentifiers" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/telnetroulette.iml" filepath="$PROJECT_DIR$/.idea/telnetroulette.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
9
.idea/telnetroulette.iml
generated
Normal file
9
.idea/telnetroulette.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@@ -1,2 +1,3 @@
|
|||||||
# telnetroulette
|
# Telnet roulette
|
||||||
|
# Just like Buckshot Roulette, but in telnet
|
||||||
|
|
||||||
|
122
client.go
Normal file
122
client.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/BRNSystems/go-telnet"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type client struct {
|
||||||
|
Client *telnet.Client
|
||||||
|
Username string
|
||||||
|
ID int
|
||||||
|
History []string
|
||||||
|
HistoryIndex int
|
||||||
|
context *telnet.Context
|
||||||
|
writer *telnet.Writer
|
||||||
|
reader *telnet.Reader
|
||||||
|
match *match
|
||||||
|
isHost bool
|
||||||
|
HP int
|
||||||
|
items []item
|
||||||
|
// Add more fields as needed
|
||||||
|
}
|
||||||
|
|
||||||
|
func newClient(context *telnet.Context, writer *telnet.Writer, reader *telnet.Reader) int {
|
||||||
|
client := client{}
|
||||||
|
|
||||||
|
clientsMutex.Lock()
|
||||||
|
clients = append(clients, &client)
|
||||||
|
clientID := len(clients) - 1
|
||||||
|
defer clientsMutex.Unlock()
|
||||||
|
|
||||||
|
clients[clientID].ID = clientID
|
||||||
|
clients[clientID].Username = "Client #" + strconv.Itoa(clientID)
|
||||||
|
clients[clientID].context = context
|
||||||
|
clients[clientID].writer = writer
|
||||||
|
clients[clientID].reader = reader
|
||||||
|
clients[clientID].isHost = false
|
||||||
|
clients[clientID].HP = -1
|
||||||
|
return clientID
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeClient(clientID int) {
|
||||||
|
clientsMutex.Lock()
|
||||||
|
defer clientsMutex.Unlock()
|
||||||
|
clients = append(clients[:clientID], clients[clientID+1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) send(message string) {
|
||||||
|
writer := *(client.writer)
|
||||||
|
_, err := writer.Write([]byte(message))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) sendMessage(message string, sender *client) {
|
||||||
|
client.send("\a\r\n<" + sender.getMentionName() + "> " + message + "\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIDByName(name string) int {
|
||||||
|
clientsMutex.Lock()
|
||||||
|
defer clientsMutex.Unlock()
|
||||||
|
for _, client := range clients {
|
||||||
|
if client.Username == name {
|
||||||
|
return client.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func getByIDOrName(by string) int {
|
||||||
|
//if integer assume clientID
|
||||||
|
var toClientID int
|
||||||
|
toClientID, err := strconv.Atoi(by)
|
||||||
|
if err != nil {
|
||||||
|
toClientID = getIDByName(by)
|
||||||
|
}
|
||||||
|
if toClientID < len(clients) && toClientID >= 0 {
|
||||||
|
return toClientID
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) renderHP() string {
|
||||||
|
var graphic strings.Builder
|
||||||
|
graphic.WriteString(strconv.Itoa(client.HP) + " / " + strconv.Itoa(client.match.maxHP) + " ")
|
||||||
|
for i := 1; i <= client.match.maxHP; i++ {
|
||||||
|
if client.HP >= i {
|
||||||
|
graphic.WriteString("█")
|
||||||
|
} else {
|
||||||
|
graphic.WriteString("░")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return graphic.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) renderItems() string {
|
||||||
|
if len(client.items) > 0 {
|
||||||
|
var out strings.Builder
|
||||||
|
for i := 0; i < len(client.items); i++ {
|
||||||
|
name := client.items[i].name()
|
||||||
|
out.WriteString(strconv.Itoa(i) + " - " + name + "\r\n")
|
||||||
|
}
|
||||||
|
return "My items are:\r\n" + out.String()
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) getMentionName() string {
|
||||||
|
return client.Username + "(" + strconv.Itoa(client.ID) + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *client) giveItem() {
|
||||||
|
client.items = append(client.items, item{
|
||||||
|
kind: rand.Intn(client.match.itemCount),
|
||||||
|
match: client.match,
|
||||||
|
owner: client,
|
||||||
|
})
|
||||||
|
}
|
251
commands.go
Normal file
251
commands.go
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type command struct {
|
||||||
|
commandHandlerFunc commandHandlerFunc
|
||||||
|
description string
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandHandlerFunc func(args []string, clientID int) string
|
||||||
|
|
||||||
|
type commandRegistry struct {
|
||||||
|
commands map[string]command
|
||||||
|
mutex sync.Mutex // Mutex to safely access commands map
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCommandRegistry() *commandRegistry {
|
||||||
|
return &commandRegistry{
|
||||||
|
commands: make(map[string]command),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cr *commandRegistry) registerCommand(commandName string, handler commandHandlerFunc, desc string) {
|
||||||
|
cr.mutex.Lock()
|
||||||
|
defer cr.mutex.Unlock()
|
||||||
|
cr.commands[commandName] = command{
|
||||||
|
commandHandlerFunc: handler,
|
||||||
|
description: desc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cr *commandRegistry) getCommandHandler(command string, clientID int) (commandHandlerFunc, bool, int) {
|
||||||
|
cr.mutex.Lock()
|
||||||
|
defer cr.mutex.Unlock()
|
||||||
|
commandEntry, ok := cr.commands[command]
|
||||||
|
handler := commandEntry.commandHandlerFunc
|
||||||
|
return handler, ok, clientID
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerCommands(registry *commandRegistry) {
|
||||||
|
registry.registerCommand("clear", clearCommand, `Clears your screen (clear)`)
|
||||||
|
registry.registerCommand("setname", setNameCommand, `Sets your name (setname yourname[string])`)
|
||||||
|
registry.registerCommand("list", listClientsCommand, `Lists clients (list)`)
|
||||||
|
registry.registerCommand("rng", randomCommand, `Generates a random integer (rng from[int] to[int{bigger than from}], both need to be above zero)`)
|
||||||
|
registry.registerCommand("tell", tellClientCommand, `Tell a client (tell client[id or string{username}])`)
|
||||||
|
registry.registerCommand("challenge", startCommand, `Challenge a client (challenge client[id or string{username}])`)
|
||||||
|
registry.registerCommand("accept", acceptCommand, `Accepts a challenge (accept)`)
|
||||||
|
registry.registerCommand("shoot", shootCommand, `Shoots at a client (shoot target[int or string], accepts self as a parameter) {Shooting self with a blank keeps your turn}`)
|
||||||
|
registry.registerCommand("useitem", useItemCommand, `Uses an item in a match (useitem index[number])`)
|
||||||
|
registry.registerCommand("status", statusCommand, `Prints the game status (status)`)
|
||||||
|
// Add more commands here...
|
||||||
|
}
|
||||||
|
|
||||||
|
func statusCommand(_ []string, clientID int) string {
|
||||||
|
if clients[clientID].match != nil && clients[clientID].match.round > 0 {
|
||||||
|
clients[clientID].match.announceHP()
|
||||||
|
clients[clientID].match.announceItems()
|
||||||
|
clients[clientID].match.announceRounds()
|
||||||
|
clients[clientID].match.announceTurn()
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return "Not in match"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func useItemCommand(args []string, clientID int) string {
|
||||||
|
param := -1
|
||||||
|
itemID := -1
|
||||||
|
if len(args) >= 2 {
|
||||||
|
var err error
|
||||||
|
param, err = strconv.Atoi(args[1])
|
||||||
|
if err != nil {
|
||||||
|
param = -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(args) >= 1 {
|
||||||
|
var err error
|
||||||
|
itemID, err = strconv.Atoi(args[0])
|
||||||
|
if err != nil {
|
||||||
|
itemID = -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if itemID >= 0 && itemID < clients[clientID].match.itemCount {
|
||||||
|
clients[clientID].match.sendMessage("\r\nUsing item "+clients[clientID].items[itemID].name(), clients[clientID])
|
||||||
|
clients[clientID].items[itemID].useItem(param)
|
||||||
|
return "Used"
|
||||||
|
} else {
|
||||||
|
return "ERROR using item"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func shootCommand(args []string, clientID int) string {
|
||||||
|
if clients[clientID].match != nil && clients[clientID].match.round > 0 {
|
||||||
|
if clients[clientID].match.guestsTurn != clients[clientID].isHost {
|
||||||
|
defer clients[clientID].match.checkStatus()
|
||||||
|
defer clients[clientID].match.announceHP()
|
||||||
|
defer clients[clientID].match.announceRounds()
|
||||||
|
self := false
|
||||||
|
if len(args) == 1 {
|
||||||
|
if strings.ToLower(args[0]) == "self" {
|
||||||
|
self = true
|
||||||
|
} else if getByIDOrName(args[0]) == clientID {
|
||||||
|
self = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var fired int
|
||||||
|
var target *client
|
||||||
|
if self {
|
||||||
|
target = clients[clientID]
|
||||||
|
fired = clients[clientID].match.gun.shoot(target)
|
||||||
|
if fired == 1 {
|
||||||
|
clients[clientID].match.guestsTurn = clients[clientID].isHost
|
||||||
|
clients[clientID].match.sendMessage("Shot myself with a live", clients[clientID])
|
||||||
|
} else {
|
||||||
|
clients[clientID].match.sendMessage("Shot myself with a blank", clients[clientID])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if clients[clientID].isHost {
|
||||||
|
target = clients[clientID].match.guest
|
||||||
|
} else {
|
||||||
|
target = clients[clientID].match.host
|
||||||
|
}
|
||||||
|
fired = clients[clientID].match.gun.shoot(target)
|
||||||
|
if fired == 1 {
|
||||||
|
clients[clientID].match.guestsTurn = clients[clientID].isHost
|
||||||
|
clients[clientID].match.sendMessage("Shot "+target.Username+"("+strconv.Itoa(target.ID)+")"+"with a live", clients[clientID])
|
||||||
|
} else {
|
||||||
|
clients[clientID].match.guestsTurn = clients[clientID].isHost
|
||||||
|
clients[clientID].match.sendMessage("Shot "+target.Username+"("+strconv.Itoa(target.ID)+")"+"with a blank", clients[clientID])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "SHOT"
|
||||||
|
} else {
|
||||||
|
return "It is not your turn"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "Your are not in a match"
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func acceptCommand(_ []string, clientID int) string {
|
||||||
|
clientsMutex.Lock()
|
||||||
|
defer clientsMutex.Unlock()
|
||||||
|
matchesMutex.Lock()
|
||||||
|
defer matchesMutex.Unlock()
|
||||||
|
if clients[clientID].match != nil && clients[clientID].match.guest == clients[clientID] {
|
||||||
|
clients[clientID].match.host.sendMessage("Accepted", clients[clientID])
|
||||||
|
clients[clientID].isHost = false
|
||||||
|
clients[clientID].match.guestsTurn = false
|
||||||
|
clients[clientID].match.start()
|
||||||
|
return "Accepted"
|
||||||
|
}
|
||||||
|
return "No match to accept"
|
||||||
|
}
|
||||||
|
|
||||||
|
func startCommand(args []string, clientID int) string {
|
||||||
|
if len(args) == 1 {
|
||||||
|
targetID := getByIDOrName(args[0])
|
||||||
|
if targetID >= 0 && targetID != clientID && (clients[clientID].match == nil || clients[clientID].match.round <= 0) {
|
||||||
|
match := match{
|
||||||
|
host: clients[clientID],
|
||||||
|
guest: clients[targetID],
|
||||||
|
round: 0,
|
||||||
|
gun: gun{
|
||||||
|
doubled: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
clientsMutex.Lock()
|
||||||
|
clients[clientID].match = &match
|
||||||
|
clients[targetID].match = &match
|
||||||
|
match.itemCount = 8
|
||||||
|
clients[clientID].isHost = true
|
||||||
|
clients[targetID].sendMessage("Type \"accept\" to accept a match.", clients[clientID])
|
||||||
|
clientsMutex.Unlock()
|
||||||
|
matchesMutex.Lock()
|
||||||
|
matches = append(matches, &match)
|
||||||
|
matchesMutex.Unlock()
|
||||||
|
return "Successfully challenged"
|
||||||
|
} else {
|
||||||
|
return "Unknown user"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "Invalid argument"
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomCommand(args []string, _ int) string {
|
||||||
|
// Handle help command
|
||||||
|
if len(args) == 2 {
|
||||||
|
from, err1 := strconv.Atoi(args[0])
|
||||||
|
to, err2 := strconv.Atoi(args[1])
|
||||||
|
if err1 != nil || err2 != nil {
|
||||||
|
return "Invalid argument"
|
||||||
|
}
|
||||||
|
if to-from <= 0 {
|
||||||
|
return "Invalid range"
|
||||||
|
}
|
||||||
|
return "The number is " + strconv.Itoa(rand.Intn(to-from)+from)
|
||||||
|
}
|
||||||
|
return "No range provided"
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearCommand(_ []string, _ int) string {
|
||||||
|
// Handle help command
|
||||||
|
return "\033[H\033[2J\033[K"
|
||||||
|
}
|
||||||
|
|
||||||
|
func setNameCommand(args []string, clientID int) string {
|
||||||
|
// Handle help command
|
||||||
|
if len(args) >= 1 {
|
||||||
|
newName := args[0]
|
||||||
|
clients[clientID].Username = newName
|
||||||
|
return "Name set to " + newName
|
||||||
|
}
|
||||||
|
return "No name provided"
|
||||||
|
}
|
||||||
|
|
||||||
|
func listClientsCommand(_ []string, _ int) string {
|
||||||
|
var message strings.Builder
|
||||||
|
for i, client := range clients {
|
||||||
|
message.WriteString(strconv.Itoa(i) + ": " + client.Username + "\n")
|
||||||
|
}
|
||||||
|
return message.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func tellClientCommand(args []string, clientID int) string {
|
||||||
|
message := "Message not delivered"
|
||||||
|
if len(args) >= 2 {
|
||||||
|
toClientID := getByIDOrName(args[0])
|
||||||
|
if toClientID >= 0 {
|
||||||
|
toClientName := clients[toClientID].Username
|
||||||
|
var newMessage strings.Builder
|
||||||
|
//all the remaining arguments are a part of the message, join them with space
|
||||||
|
for i, arg := range args {
|
||||||
|
if i > 0 {
|
||||||
|
newMessage.WriteString(arg + " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clients[toClientID].sendMessage(newMessage.String(), clients[clientID])
|
||||||
|
return "Message sent to " + toClientName
|
||||||
|
} else {
|
||||||
|
message = "Invalid recipient"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message
|
||||||
|
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module telnetroulette
|
||||||
|
|
||||||
|
go 1.22
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/BRNSystems/go-telnet v0.0.0-20240607215108-c7f613a868ed
|
||||||
|
github.com/reiver/go-oi v1.0.0
|
||||||
|
)
|
6
go.sum
Normal file
6
go.sum
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
github.com/BRNSystems/go-telnet v0.0.0-20240607213850-6f6b77773107 h1:mch1qDcG/Cj3c+RKlQiTSgtd1ijhFHG986HtEuvJCgA=
|
||||||
|
github.com/BRNSystems/go-telnet v0.0.0-20240607213850-6f6b77773107/go.mod h1:xZD4mQdIL/DseYfMKq66/DDBwxmX48EmqjlMY0opBDg=
|
||||||
|
github.com/BRNSystems/go-telnet v0.0.0-20240607215108-c7f613a868ed h1:ogTX00ebS+daoXPZJ869USmnn1WIrYkZg+v944meYio=
|
||||||
|
github.com/BRNSystems/go-telnet v0.0.0-20240607215108-c7f613a868ed/go.mod h1:xZD4mQdIL/DseYfMKq66/DDBwxmX48EmqjlMY0opBDg=
|
||||||
|
github.com/reiver/go-oi v1.0.0 h1:nvECWD7LF+vOs8leNGV/ww+F2iZKf3EYjYZ527turzM=
|
||||||
|
github.com/reiver/go-oi v1.0.0/go.mod h1:RrDBct90BAhoDTxB1fenZwfykqeGvhI6LsNfStJoEkI=
|
43
gun.go
Normal file
43
gun.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
|
type gun struct {
|
||||||
|
doubled bool
|
||||||
|
magazine []bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gun) shoot(target *client) int {
|
||||||
|
shot := 2
|
||||||
|
if len(g.magazine) == 0 {
|
||||||
|
return shot
|
||||||
|
}
|
||||||
|
shot = 0
|
||||||
|
if g.magazine[len(g.magazine)-1] {
|
||||||
|
shot = 1
|
||||||
|
if g.doubled {
|
||||||
|
target.HP -= 2
|
||||||
|
} else {
|
||||||
|
target.HP -= 1
|
||||||
|
}
|
||||||
|
target.send("\a\a")
|
||||||
|
target.match.send("")
|
||||||
|
}
|
||||||
|
|
||||||
|
g.rack()
|
||||||
|
|
||||||
|
g.doubled = false
|
||||||
|
return shot
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gun) rack() {
|
||||||
|
g.magazine = g.magazine[:len(g.magazine)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *gun) reload(count int) []bool {
|
||||||
|
g.magazine = make([]bool, count)
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
g.magazine[i] = rand.Int63()&(1<<62) == 0
|
||||||
|
}
|
||||||
|
return g.magazine
|
||||||
|
}
|
144
item.go
Normal file
144
item.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
kind int
|
||||||
|
match *match
|
||||||
|
owner *client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *item) name() string {
|
||||||
|
switch i.kind {
|
||||||
|
case 0:
|
||||||
|
return "Saw"
|
||||||
|
case 1:
|
||||||
|
return "Beer"
|
||||||
|
case 2:
|
||||||
|
return "Phone"
|
||||||
|
case 3:
|
||||||
|
return "Magnifying glass"
|
||||||
|
case 4:
|
||||||
|
return "Expired medicine"
|
||||||
|
case 5:
|
||||||
|
return "Cigarette pack"
|
||||||
|
case 6:
|
||||||
|
return "Inverter"
|
||||||
|
case 7:
|
||||||
|
return "Adrenaline"
|
||||||
|
default:
|
||||||
|
return "None"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *item) useItem(param1 int) {
|
||||||
|
successfully := false
|
||||||
|
if i.owner.isHost != i.owner.match.guestsTurn {
|
||||||
|
|
||||||
|
successfully = true
|
||||||
|
switch i.kind {
|
||||||
|
case 0:
|
||||||
|
i.match.gun.doubled = true
|
||||||
|
i.match.sendMessage("I doubled the damage for 1 round by sawing the shotgun", i.owner)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
i.match.gun.rack()
|
||||||
|
i.match.sendMessage("I rack the magazine", i.owner)
|
||||||
|
i.match.announceRounds()
|
||||||
|
break
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
roundIndex := rand.Intn(len(i.match.gun.magazine) - 2)
|
||||||
|
round := i.match.gun.magazine[roundIndex]
|
||||||
|
roundString := ""
|
||||||
|
if round {
|
||||||
|
roundString = "live"
|
||||||
|
} else {
|
||||||
|
roundString = "blank"
|
||||||
|
}
|
||||||
|
roundForPlayer := int(math.Abs(float64(len(i.match.gun.magazine) - 2 - roundIndex)))
|
||||||
|
i.match.sendMessage("I used a phone", i.owner)
|
||||||
|
i.owner.send("The " + strconv.Itoa(roundForPlayer) + " round from now is " + roundString)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
round := i.match.gun.magazine[len(i.match.gun.magazine)-1]
|
||||||
|
roundString := ""
|
||||||
|
if round {
|
||||||
|
roundString = "live"
|
||||||
|
} else {
|
||||||
|
roundString = "blank"
|
||||||
|
}
|
||||||
|
i.match.sendMessage("I used a magnifying glass", i.owner)
|
||||||
|
i.owner.send("Next round is " + roundString)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
if rand.Int63()&(1<<62) == 0 {
|
||||||
|
for cnt := 0; cnt < 2; cnt++ {
|
||||||
|
if i.owner.HP < i.match.maxHP {
|
||||||
|
i.owner.HP++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
i.owner.HP--
|
||||||
|
i.match.announceHP()
|
||||||
|
i.match.checkStatus()
|
||||||
|
}
|
||||||
|
i.match.sendMessage("I used an expired medicine", i.owner)
|
||||||
|
i.match.announceHP()
|
||||||
|
break
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
if i.owner.HP < i.match.maxHP {
|
||||||
|
i.owner.HP++
|
||||||
|
}
|
||||||
|
i.match.sendMessage("I used a pack of cigarettes", i.owner)
|
||||||
|
i.match.announceHP()
|
||||||
|
break
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
i.match.gun.magazine[len(i.match.gun.magazine)-1] = !i.match.gun.magazine[len(i.match.gun.magazine)-1]
|
||||||
|
i.match.sendMessage("I used an inverter", i.owner)
|
||||||
|
break
|
||||||
|
case 7:
|
||||||
|
var otherPlayer *client
|
||||||
|
if i.owner.isHost {
|
||||||
|
otherPlayer = i.match.guest
|
||||||
|
} else {
|
||||||
|
otherPlayer = i.match.host
|
||||||
|
}
|
||||||
|
if param1 < len(otherPlayer.items) {
|
||||||
|
i.owner.items = append(i.owner.items, otherPlayer.items[param1])
|
||||||
|
otherPlayer.items = append(otherPlayer.items[:param1], otherPlayer.items[param1:]...)
|
||||||
|
itemName := otherPlayer.items[param1].name()
|
||||||
|
if itemName[0] == 'a' || itemName[0] == 'e' || itemName[0] == 'i' || itemName[0] == 'o' || itemName[0] == 'u' || itemName[0] == 'y' {
|
||||||
|
itemName = " an " + itemName
|
||||||
|
} else {
|
||||||
|
itemName = " a " + itemName
|
||||||
|
}
|
||||||
|
i.match.sendMessage("I stole from "+otherPlayer.Username+"("+strconv.Itoa(otherPlayer.ID)+")"+itemName, i.owner)
|
||||||
|
} else {
|
||||||
|
i.owner.send("This requires another parameter")
|
||||||
|
successfully = false
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
i.owner.send("It is not your turn")
|
||||||
|
}
|
||||||
|
if successfully {
|
||||||
|
for cnt, item := range i.owner.items {
|
||||||
|
if item.kind == i.kind {
|
||||||
|
i.owner.items = append(i.owner.items[:cnt], i.owner.items[cnt+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i.match.announceItems()
|
||||||
|
}
|
||||||
|
}
|
31
main.go
Normal file
31
main.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/BRNSystems/go-telnet"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var clients []*client
|
||||||
|
var clientsMutex sync.Mutex
|
||||||
|
var matches []*match
|
||||||
|
var matchesMutex sync.Mutex
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addr := ":6969"
|
||||||
|
commandRegistry := newCommandRegistry()
|
||||||
|
termHandler := newInternalTerminalHandler("# ",
|
||||||
|
` ____ _ _ ____ _ _ _
|
||||||
|
/ ___|| |__ ___ | |_ __ _ _ _ _ __ | _ \ ___ _ _| | ___| |_| |_ ___
|
||||||
|
\___ \| '_ \ / _ \| __/ _`+"`"+` | | | | '_ \ | |_) / _ \| | | | |/ _ \ __| __/ _ \
|
||||||
|
___) | | | | (_) | || (_| | |_| | | | | | _ < (_) | |_| | | __/ |_| || __/
|
||||||
|
|____/|_| |_|\___/ \__\__, |\__,_|_| |_| |_| \_\___/ \__,_|_|\___|\__|\__\___|
|
||||||
|
|___/`,
|
||||||
|
commandRegistry)
|
||||||
|
|
||||||
|
// Register commands
|
||||||
|
registerCommands(commandRegistry)
|
||||||
|
|
||||||
|
if err := telnet.ListenAndServe(addr, termHandler); nil != err {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
164
match.go
Normal file
164
match.go
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
163
terminal_handler.go
Normal file
163
terminal_handler.go
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/BRNSystems/go-telnet"
|
||||||
|
"github.com/reiver/go-oi"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type internalTerminalHandler struct {
|
||||||
|
ShellCharacter string
|
||||||
|
WelcomeMessage string
|
||||||
|
CommandRegistry *commandRegistry
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInternalTerminalHandler(shellCharacter, welcomeMessage string, commandRegistry *commandRegistry) *internalTerminalHandler {
|
||||||
|
return &internalTerminalHandler{
|
||||||
|
ShellCharacter: shellCharacter,
|
||||||
|
WelcomeMessage: welcomeMessage,
|
||||||
|
CommandRegistry: commandRegistry,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearLine(w telnet.Writer) error {
|
||||||
|
_, err := w.RawWrite([]byte{0x1B, 0x5B, 0x4B})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *internalTerminalHandler) ServeTELNET(ctx telnet.Context, w telnet.Writer, r telnet.Reader) {
|
||||||
|
clientID := newClient(&ctx, &w, &r)
|
||||||
|
|
||||||
|
defer removeClient(clientID)
|
||||||
|
_, err := w.RawWrite([]byte{0xff, 0xfb, 0x03})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
} //send char by char
|
||||||
|
_, err = w.RawWrite([]byte{0xff, 0xfb, 0x01})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
} //echo from server
|
||||||
|
_, err = w.Write([]byte(strings.ReplaceAll(handler.WelcomeMessage, "\n", "\r\n") + "\r\n"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//replace every \n with \r\n in WelcomeMessage
|
||||||
|
_, err = w.Write([]byte(handler.ShellCharacter))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var lineBuffer []byte
|
||||||
|
|
||||||
|
CSISequence := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
var bt [1]byte
|
||||||
|
n, err := r.Read(bt[:])
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if n > 0 {
|
||||||
|
b := bt[0]
|
||||||
|
|
||||||
|
if b == 0x03 || b == 0x04 {
|
||||||
|
return
|
||||||
|
} else if b == 0x1B { //escape
|
||||||
|
CSISequence = 1
|
||||||
|
} else if CSISequence == 1 && b == 0x5B {
|
||||||
|
CSISequence = 2
|
||||||
|
} else if CSISequence == 2 && b == 'A' {
|
||||||
|
if len(lineBuffer) == 0 && len(clients[clientID].History) > 0 {
|
||||||
|
if clients[clientID].HistoryIndex < len(clients[clientID].History)-1 {
|
||||||
|
clients[clientID].HistoryIndex++
|
||||||
|
}
|
||||||
|
lineBuffer = []byte(clients[clientID].History[clients[clientID].HistoryIndex])
|
||||||
|
if clearLine(w) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err := w.Write(lineBuffer)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if CSISequence == 2 && b == 'B' {
|
||||||
|
if len(lineBuffer) == 0 && len(clients[clientID].History) > 0 {
|
||||||
|
if clients[clientID].HistoryIndex > 0 {
|
||||||
|
clients[clientID].HistoryIndex--
|
||||||
|
}
|
||||||
|
lineBuffer = []byte(clients[clientID].History[clients[clientID].HistoryIndex])
|
||||||
|
if clearLine(w) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err := w.Write(lineBuffer)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if b == 0x7F {
|
||||||
|
if len(lineBuffer) > 0 {
|
||||||
|
_, err := w.Write([]byte{0x08, 0x20, 0x08})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lineBuffer = lineBuffer[:len(lineBuffer)-1]
|
||||||
|
}
|
||||||
|
} else if b != '\r' && b != '\n' && b != 0 {
|
||||||
|
CSISequence = 0
|
||||||
|
lineBuffer = append(lineBuffer, b)
|
||||||
|
_, err := w.RawWrite([]byte{b})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if b == '\r' {
|
||||||
|
_, err := w.Write([]byte("\r\n"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
command := string(lineBuffer)
|
||||||
|
args := strings.Fields(command)
|
||||||
|
var message strings.Builder
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
cmd := args[0]
|
||||||
|
internalCommand := false
|
||||||
|
if cmd == "exit" {
|
||||||
|
internalCommand = true
|
||||||
|
return
|
||||||
|
} else if cmd == "help" {
|
||||||
|
internalCommand = true
|
||||||
|
var commands strings.Builder
|
||||||
|
for command, commandEntry := range handler.CommandRegistry.commands {
|
||||||
|
commands.WriteString(command + " - " + commandEntry.description + "\r\n")
|
||||||
|
}
|
||||||
|
// Handle help command
|
||||||
|
if len(clients) > 0 {
|
||||||
|
clients[clientID].send("Just help yourself, " + clients[clientID].Username + "\r\nCommands:\r\n" + commands.String())
|
||||||
|
} else {
|
||||||
|
clients[clientID].send("No clients available" + "\r\nCommands:\r\n" + commands.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
handlerFunc, ok, clientID := handler.CommandRegistry.getCommandHandler(cmd, clientID)
|
||||||
|
clients[clientID].HistoryIndex = 0
|
||||||
|
if ok || internalCommand {
|
||||||
|
if ok {
|
||||||
|
message.WriteString(handlerFunc(args[1:], clientID))
|
||||||
|
}
|
||||||
|
clients[clientID].History = append(clients[clientID].History, command)
|
||||||
|
} else {
|
||||||
|
message.WriteString("Command not found")
|
||||||
|
}
|
||||||
|
message.WriteString("\n" + handler.ShellCharacter)
|
||||||
|
} else {
|
||||||
|
message.WriteString(handler.ShellCharacter)
|
||||||
|
}
|
||||||
|
_, err = oi.LongWrite(w, []byte(strings.ReplaceAll(message.String(), "\n", "\r\n")))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lineBuffer = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user