Skip to content

Commit

Permalink
Implemented various bad ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
horahoradev committed Jan 19, 2022
1 parent 093587f commit 647c60e
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 75 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ RUN cd /multi_server/public/play/games/default/ゆめ2っき/ && \

RUN /bin/bash -c 'mv /multi_server/public/play/games/default/ゆめ2っき/* /multi_server/public/play/games/default/'

COPY --from=0 /workdir/ynoclient/index.wasm /multi_server/public
COPY --from=0 /workdir/ynoclient/index.js /multi_server/public

COPY server/public /multi_server/public

RUN mkdir -p /multi_server/public/data/default && \
Expand Down
18 changes: 17 additions & 1 deletion server/internal/client/clientinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"fmt"
"net"

log "github.com/sirupsen/logrus"
Expand All @@ -17,6 +18,11 @@ func (cid *ClientID) GetAddr() net.Addr {
return cid.Conn.RemoteAddr()
}

// Dumb
func (cid *ClientID) IsClosed() bool {
return cid.Conn.RemoteAddr() == nil || cid.Conn.RemoteAddr().String() == ""
}

func (cid *ClientID) GetUsername() string {
return cid.Name
}
Expand All @@ -32,6 +38,7 @@ type Client interface {
GetAddr() net.Addr
GetUsername() string
SetUsername(name string)
IsClosed() bool
}

type GameClient struct {
Expand All @@ -54,8 +61,17 @@ func (gc *GameClient) Unignore(ipv4 net.Addr) {
}

func (gc *GameClient) Send(payload []byte, sender net.Addr) error {
// if sender == nil {
// return errors.New("Sender has disconnected")
// }

if gc.ClientID.Conn.RemoteAddr() == nil {
fmt.Errorf("client's remote addr is nil, has likely already disconnected. Dropping message.")
return nil
}

// If the sender is the current user, just return
if gc.ClientID.Conn.RemoteAddr().String() == sender.String() {
if sender != nil && gc.ClientID.Conn.RemoteAddr().String() == sender.String() {
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion server/internal/client/clientmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (cm *ClientPubsubManager) Broadcast(payload interface{}, sockinfo *ClientSo

log.Debugf("Broadcasting for room %s", sockinfo.RoomName)
for _, client := range clients {
err = client.ClientInfo.Send(payloadBytes, sockinfo.ClientInfo.GetAddr())
c := sockinfo.ClientInfo
if c == nil {
// Skip? Disconnect? TODO
}
err = client.ClientInfo.Send(payloadBytes, c.GetAddr())
if err != nil {
log.Errorf("Failed to send to client. Err: %s. Continuing...", err)
}
Expand Down
65 changes: 38 additions & 27 deletions server/internal/client/syncobject.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package client

import guuid "github.com/google/uuid"
import (
"log"
"strconv"
)

var serial int = 0

type Position struct {
X uint16
Y uint16
X uint16 `json:"x"`
Y uint16 `json:"y"`
}

// return {id: data.readUInt32LE(2), value: data.readUInt32LE(6)};
Expand All @@ -22,7 +27,7 @@ type Sound struct {

type Sprite struct {
ID uint16 `json:"id"`
Sheet uint32 `json:"sheet"`
Sheet string `json:"sheet"`
}

// return {frame: data.readUInt16LE(2)};
Expand All @@ -43,30 +48,31 @@ type Weather struct {
}

type SyncObject struct {
UID string `json:"uid"` // Actually a UUID
Type string `json:"type"`
UID string `json:"uid"` // Actually a UUID

Pos Position `json:"pos,omitempty"`
Pos *Position `json:"pos,omitempty"`
posChanged bool

Sprite Sprite `json:"sprite,omitempty"`
Sprite *Sprite `json:"sprite,omitempty"`
spriteChanged bool

Weather Weather `json:"weather,omitempty"`
Weather *Weather `json:"weather,omitempty"`
weatherChanged bool

Variable Variable `json:'variable,omitempty"`
Variable *Variable `json:"variable,omitempty"`
variableChanged bool

Sound Sound `json:"sound,omitempty"`
Sound *Sound `json:"sound,omitempty"`
soundChanged bool

Name string `json:"name,omitempty"`
nameChanged bool

AnimFrame AnimFrame `json:"animframe,omitempty"`
AnimFrame uint16 `json:"animframe,omitempty"`
animframeChanged bool

Switch Switch `json:"switch,omitempty"`
Switch *Switch `json:"switch,omitempty"`
switchChanged bool

MovementAnimationSpeed uint16 `json:"movementAnimationSpeed,omitempty"`
Expand All @@ -80,39 +86,44 @@ type SyncObject struct {
}

func NewSyncObject() *SyncObject {
uuid := guuid.New()
return &SyncObject{UID: uuid.String()}
serial += 1
return &SyncObject{UID: strconv.Itoa(serial), Type: "objectSync", MovementAnimationSpeed: 4}
}

func (so *SyncObject) SetPos(x, y uint16) {
so.posChanged = true
so.Pos = Position{X: x, Y: y}
so.Pos = &Position{X: x, Y: y}
}

func (so *SyncObject) SetSprite(id uint16, sheet uint32) {
func (so *SyncObject) SetSprite(id uint16, sheet string) {
// "why do I have to do this?"
if id == 0 && sheet == "" {
return
}
so.Sprite = &Sprite{ID: id, Sheet: sheet}
so.spriteChanged = true
// TODO: sprite validation goes here
so.Sprite = Sprite{ID: id, Sheet: sheet}
log.Printf("SPRITE CHANGED TO ID %s and SHEET %s", id, sheet)
}

func (so *SyncObject) SetSound(volume uint16, tempo uint16, balance uint16, name string) {
so.soundChanged = true
so.Sound = Sound{Volume: volume, Tempo: tempo, Balance: balance, Name: name}
so.Sound = &Sound{Volume: volume, Tempo: tempo, Balance: balance, Name: name}
}

func (so *SyncObject) SetWeather(t, strength uint16) {
so.weatherChanged = true
so.Weather = Weather{Type: t, Strength: strength}
so.Weather = &Weather{Type: t, Strength: strength}
}

func (so *SyncObject) SetSwitch(id, value uint32) {
so.switchChanged = true
so.Switch = Switch{ID: id, Value: value}
so.Switch = &Switch{ID: id, Value: value}
}

func (so *SyncObject) SetAnimFrame(frame uint16) {
so.animframeChanged = true
so.AnimFrame = AnimFrame{Frame: frame}
so.AnimFrame = frame
}

func (so *SyncObject) SetName(name string) {
Expand All @@ -137,7 +148,7 @@ func (so *SyncObject) SetTypingStatus(typingStatus uint16) {

func (so *SyncObject) SetVariable(id, value uint32) {
so.variableChanged = true
so.Variable = Variable{ID: id, Value: value}
so.Variable = &Variable{ID: id, Value: value}
}

func (so *SyncObject) GetAllChanges() interface{} {
Expand All @@ -160,7 +171,7 @@ func (so *SyncObject) clearChanges() {

// So this is horrific BUT idk what to do about it lol
func (so *SyncObject) GetFlushedChanges() interface{} {
s := SyncObject{UID: so.UID}
s := SyncObject{UID: so.UID, Type: so.Type}

if so.posChanged {
s.Pos = so.Pos
Expand Down Expand Up @@ -190,19 +201,19 @@ func (so *SyncObject) GetFlushedChanges() interface{} {
s.Name = so.Name
}

if s.movementAnimationSpeedChanged {
if so.movementAnimationSpeedChanged {
s.MovementAnimationSpeed = so.MovementAnimationSpeed
}

if s.facingChanged {
if so.facingChanged {
s.Facing = so.Facing
}

if s.typingStatusChanged {
if so.typingStatusChanged {
s.TypingStatus = so.TypingStatus
}

if s.animframeChanged {
if so.animframeChanged {
s.AnimFrame = so.AnimFrame
}

Expand Down
16 changes: 8 additions & 8 deletions server/internal/clientmessages/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Movement struct {
type Sprite struct {
MatchPrefix string `ynoproto:"02"`
SpriteID uint16
Spritesheet uint32
Spritesheet string
}

//uint16 packet type, uint16 volume, uint16 tempo, uint16 balance, string sound file
Expand All @@ -99,42 +99,42 @@ type Name struct {

//uint16 packet type, uint16 movement speed
type MovementAnimationSpeed struct {
MatchPrefix string `ynoproto:"4"`
MatchPrefix string `ynoproto:"06"`
MovementSpeed uint16
}

//uint16 packet type, uint32 var id, int32 value
type Variable struct {
MatchPrefix string `ynoproto:"5"`
MatchPrefix string `ynoproto:"07"`
ID uint32
Value uint32
}

//uint16 packet type, uint32 switch id, int32 value
type SwitchSync struct {
MatchPrefix string `ynoproto:"6"`
MatchPrefix string `ynoproto:"08"`
ID uint32
Value uint32
}

//uint16 packet type, uint16 type
type AnimType struct {
MatchPrefix string `ynoproto:"7"`
MatchPrefix string `ynoproto:"09"`
Type uint16
}

//uint16 packet type, uint16 frame
type AnimFrame struct {
MatchPrefix string `ynoproto:"8"`
MatchPrefix string `ynoproto:"0A"`
Frame uint16
}

type Facing struct {
MatchPrefix string `ynoproto:"9"`
MatchPrefix string `ynoproto:"0B"`
Facing uint16
}

type TypingStatus struct {
MatchPrefix string `ynoproto:"10"`
MatchPrefix string `ynoproto:"0C"`
TypingStatus uint16
}
8 changes: 4 additions & 4 deletions server/internal/msghandler/chathandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (ch *ChatHandler) muxMessage(payload []byte, c gnet.Conn, s *client.ClientS

func (ch *ChatHandler) pardonChat(payload []byte, client *client.ClientSockInfo) error {
t := clientmessages.UnignoreChatEvents{}
matched, err := protocol.Marshal(payload, &t)
matched, err := protocol.Marshal(payload, &t, false)
switch {
case !matched:
return errors.New("Failed to match")
Expand Down Expand Up @@ -98,7 +98,7 @@ func (ch *ChatHandler) pardonChat(payload []byte, client *client.ClientSockInfo)

func (ch *ChatHandler) ignoreChat(payload []byte, client *client.ClientSockInfo) error {
t := clientmessages.IgnoreChatEvents{}
matched, err := protocol.Marshal(payload, &t)
matched, err := protocol.Marshal(payload, &t, false)
switch {
case !matched:
return errors.New("Failed to match")
Expand Down Expand Up @@ -136,7 +136,7 @@ func (ch *ChatHandler) ignoreChat(payload []byte, client *client.ClientSockInfo)

func (ch *ChatHandler) setUsername(payload []byte, client *client.ClientSockInfo) error {
t := clientmessages.SetUsername{}
matched, err := protocol.Marshal(payload, &t)
matched, err := protocol.Marshal(payload, &t, false)
switch {
case err != nil:
return err
Expand All @@ -160,7 +160,7 @@ func (ch *ChatHandler) setUsername(payload []byte, client *client.ClientSockInfo

func (ch *ChatHandler) sendUserMessage(payload []byte, client *client.ClientSockInfo) error {
t := clientmessages.SendMessage{}
matched, err := protocol.Marshal(payload, &t)
matched, err := protocol.Marshal(payload, &t, false)
switch {
case err != nil:
return err
Expand Down
Loading

0 comments on commit 647c60e

Please sign in to comment.