From 9fc29e4daa942805dd598761f11d9aef2ff0f6c7 Mon Sep 17 00:00:00 2001 From: Nahshon Unna-Tsameret Date: Mon, 26 Apr 2021 11:50:05 +0300 Subject: [PATCH] refactor to make more readable function Signed-off-by: Nahshon Unna-Tsameret --- controller/controller.go | 102 ++++++++++++++++++++++----------------- state/state.go | 37 ++++++++------ state/state_test.go | 37 +++++++++++++- webapp/index.gohtml | 25 +++++----- 4 files changed, 126 insertions(+), 75 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 0465272..8033953 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -64,53 +64,10 @@ func (c *Controller) do() { return case je := <-c.joystickEvents: - switch je { - case hat.MoveUp: - change = c.state.GoUp() - - case hat.MoveLeft: - change = c.state.GoLeft() - - case hat.MoveDown: - change = c.state.GoDown() - - case hat.MoveRight: - change = c.state.GoRight() - - case hat.Pressed: - change = c.state.Paint() - } + change = c.handleJoystickEvent(je) case e := <-c.clientEvents: - switch data := e.(type) { - case webapp.ClientEventRegistered: - id := uint64(data) - c.registered(id) - - case webapp.ClientEventReset: - if data { - change = c.state.Reset() - } - - case webapp.ClientEventSetColor: - color := common.Color(data) - change = c.state.SetColor(color) - - case webapp.ClientEventSetTool: - var err error - change, err = c.state.SetTool(string(data)) - if err != nil { - log.Printf(err.Error()) - change = nil - } - - case webapp.ClientEventDownload: - ch := chan [][]common.Color(data) - ch <- c.state.GetCanvasClone() - - case webapp.ClientEventUndo: - change = c.state.Undo() - } + change = c.handleWebClientEvent(e) } if change != nil { @@ -119,6 +76,61 @@ func (c *Controller) do() { } } +func (c *Controller) handleWebClientEvent(e webapp.ClientEvent) *state.Change { + switch data := e.(type) { + case webapp.ClientEventRegistered: + id := uint64(data) + c.registered(id) + + case webapp.ClientEventReset: + if data { + return c.state.Reset() + } + + case webapp.ClientEventSetColor: + color := common.Color(data) + return c.state.SetColor(color) + + case webapp.ClientEventSetTool: + var err error + change, err := c.state.SetTool(string(data)) + if err != nil { + log.Printf(err.Error()) + return nil + } + return change + + case webapp.ClientEventDownload: + ch := chan [][]common.Color(data) + ch <- c.state.GetCanvasClone() + + case webapp.ClientEventUndo: + return c.state.Undo() + } + + return nil +} + +func (c *Controller) handleJoystickEvent(je hat.Event) *state.Change { + switch je { + case hat.MoveUp: + return c.state.GoUp() + + case hat.MoveLeft: + return c.state.GoLeft() + + case hat.MoveDown: + return c.state.GoDown() + + case hat.MoveRight: + return c.state.GoRight() + + case hat.Pressed: + return c.state.Paint() + } + return nil +} + func (c *Controller) stop(signals chan os.Signal) { c.hat.Stop() <-c.joystickEvents // wait for the hat graceful shutdown diff --git a/state/state.go b/state/state.go index 67d88e5..7d3db66 100644 --- a/state/state.go +++ b/state/state.go @@ -14,6 +14,12 @@ const ( bucketName = "bucket" ) +const ( + wightColor = common.Color(0xFFFFFF) + blackColor = common.Color(0) + backgroundColor = blackColor +) + type Canvas [][]common.Color func (c Canvas) Clone() Canvas { @@ -49,8 +55,8 @@ type State struct { canvasWidth uint8 canvasHeight uint8 toolName string + tool tool color common.Color - tools map[string]tool } func NewState(canvasWidth, canvasHeight uint8) *State { @@ -59,12 +65,6 @@ func NewState(canvasWidth, canvasHeight uint8) *State { canvasHeight: canvasHeight, } - s.tools = map[string]tool{ - penName: s.pen, - eraserName: s.eraser, - bucketName: s.bucket, - } - _ = s.Reset() return s @@ -95,8 +95,8 @@ func (s *State) Reset() *Change { s.canvas = c s.cursor = cr s.window = win - s.color = 0xFFFFFF - s.toolName = penName + s.color = wightColor + _, _ = s.SetTool(penName) return s.GetFullChange() } @@ -148,7 +148,7 @@ func (s *State) GoRight() *Change { } func (s *State) Paint() *Change { - return s.tools[s.toolName]() + return s.tool() } func (s *State) setSinglePixelTool(color common.Color) *Change { @@ -174,7 +174,7 @@ func (s *State) pen() *Change { } func (s *State) eraser() *Change { - return s.setSinglePixelTool(0) + return s.setSinglePixelTool(backgroundColor) } func (s State) getNeighbors(center Pixel) []Pixel { @@ -289,14 +289,21 @@ func (s *State) SetColor(cl common.Color) *Change { } func (s *State) SetTool(toolName string) (*Change, error) { - if _, found := s.tools[toolName]; !found { - return nil, fmt.Errorf(`unknown tool "%s"`, toolName) - } - if toolName == s.toolName { return nil, nil } + switch toolName { + case penName: + s.tool = s.pen + case eraserName: + s.tool = s.eraser + case bucketName: + s.tool = s.bucket + default: + return nil, fmt.Errorf(`unknown tool "%s"`, toolName) + } + s.toolName = toolName return &Change{ ToolName: toolName, diff --git a/state/state_test.go b/state/state_test.go index f112172..91b53dc 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -368,7 +368,7 @@ func TestState_Undo(t *testing.T) { func TestBucket(t *testing.T) { s := NewState(8, 8) - s.canvas = [][]common.Color{ + s.canvas = Canvas{ {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 0}, @@ -381,7 +381,7 @@ func TestBucket(t *testing.T) { s.cursor.Y = 4 s.cursor.X = 4 - s.toolName = bucketName + _, _ = s.SetTool(bucketName) s.color = 2 s.Paint() expected := Canvas{ @@ -474,4 +474,37 @@ func TestBucket(t *testing.T) { if !reflect.DeepEqual(expected, s.canvas) { t.Errorf("should fill the square") } + + s.canvas = Canvas{ + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + } + + s.color = 6 + change := s.Paint() + + expected = Canvas{ + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + {6, 6, 6, 6, 6, 6, 6, 6}, + } + + if !reflect.DeepEqual(expected, s.canvas) { + t.Errorf("should fill the square") + } + + if len(change.Pixels) != int(s.canvasWidth*s.canvasWidth) { + t.Errorf("Change is too big") + } } diff --git a/webapp/index.gohtml b/webapp/index.gohtml index 69cf4b9..90cec02 100644 --- a/webapp/index.gohtml +++ b/webapp/index.gohtml @@ -185,19 +185,18 @@ const cursorElement = document.getElementById(cursorID) cursorElement.style.color = reverseColor(cursorElement.style.backgroundColor) - switch (tool) { - case "pen": - cursorElement.innerText = '+' - break - case "eraser": - cursorElement.innerText = 'x' - break - case "bucket": - cursorElement.innerText = 'o' - break - default: - cursorElement.innerText = '?' - } + cursorElement.innerText = function(tool) { + switch (tool) { + case "pen": + return '+' + case "eraser": + return 'x' + case "bucket": + return 'o' + default: + return '?' + } + }(tool) if (tool === "pen") { toolTypePen.checked = "checked"