diff --git a/internal/senders/history.go b/internal/senders/history.go new file mode 100644 index 0000000..90ca6b0 --- /dev/null +++ b/internal/senders/history.go @@ -0,0 +1,28 @@ +package senders + +import ( + "bytes" + "log" + templatedata "rcp/elite/internal/types/template-data" + "rcp/elite/internal/utils" + + "github.com/gorilla/websocket" +) + +func AppendHistory(conn *websocket.Conn, playerMove, opponentMove, winner string) error { + historyData := templatedata.History{ + PlayerMove: playerMove, + OpponentMove: opponentMove, + Winner: winner, + } + + var tplBufferOpponent bytes.Buffer + err := utils.Templates.ExecuteTemplate(&tplBufferOpponent, "history-row", historyData) + if err != nil { + log.Printf("Error executing template history : %v\n", err) + return err + } + + return conn.WriteMessage(websocket.TextMessage, tplBufferOpponent.Bytes()) + +} diff --git a/internal/services/game.go b/internal/services/game.go index 39e86a0..10cb454 100644 --- a/internal/services/game.go +++ b/internal/services/game.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "errors" "fmt" - "log" "rcp/elite/internal/senders" "rcp/elite/internal/types" "sync" @@ -176,8 +175,6 @@ func SetPlayerMove(playerName string, move string) error { return errors.New("player not in game") } - log.Printf("Move allowed %v", doesGameAllowMove(v)) - if !doesGameAllowMove(v) { return errors.New("game currently does not allow player to make move") } @@ -235,6 +232,8 @@ func mainGameLoop(gameId string) { sendEndRound(gameId, winner, tie) + updatePlayersHistory(gameId, winner) + time.Sleep(3 * time.Second) } @@ -259,6 +258,18 @@ func mainGameLoop(gameId string) { } +func updatePlayersHistory(gameId, winner string) { + g, ok := getGameFromId(gameId) + + if !ok { + return + } + + for _, p := range g.Players { + senders.AppendHistory(p.Conn, p.Move, getOpponentMove(g.Players, p), winner) + } +} + func hasReachMaxRound(gameId string) bool { g, ok := getGameFromId(gameId) @@ -278,9 +289,10 @@ func roundStart(gameId string) { return } - for _, p := range g.Players { + for k, p := range g.Players { senders.SendMessage(p.Conn, "Round start! you have 5s", "empty-5s") p.Move = "" + g.Players[k] = p } diff --git a/internal/types/template-data/history.go b/internal/types/template-data/history.go new file mode 100644 index 0000000..5d78375 --- /dev/null +++ b/internal/types/template-data/history.go @@ -0,0 +1,7 @@ +package templatedata + +type History struct { + PlayerMove string + OpponentMove string + Winner string +} diff --git a/package-lock.json b/package-lock.json index 0a4fe93..59f3852 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "RPS-elite", + "name": "RCP", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/static/styles/output.css b/static/styles/output.css index 248a991..c6da076 100644 --- a/static/styles/output.css +++ b/static/styles/output.css @@ -1 +1 @@ -/*! tailwindcss v3.4.3 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}:host,html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.mt-2{margin-top:.5rem}.mt-\[20rem\]{margin-top:20rem}.flex{display:flex}.h-12{height:3rem}.h-16{height:4rem}.h-24{height:6rem}.h-36{height:9rem}.h-44{height:11rem}.h-5{height:1.25rem}.h-56{height:14rem}.h-\[1rem\]{height:1rem}.h-\[64px\]{height:64px}.h-fit{height:-moz-fit-content;height:fit-content}.h-full{height:100%}.min-h-\[20rem\]{min-height:20rem}.w-1{width:.25rem}.w-1\/2{width:50%}.w-36{width:9rem}.w-5{width:1.25rem}.w-\[20px\]{width:20px}.w-\[28rem\]{width:28rem}.w-\[64px\]{width:64px}.w-auto{width:auto}.w-fit{width:-moz-fit-content;width:fit-content}.w-full{width:100%}.flex-col{flex-direction:column}.items-center{align-items:center}.justify-center{justify-content:center}.justify-between{justify-content:space-between}.gap-2{gap:.5rem}.gap-5{gap:1.25rem}.rounded-full{border-radius:9999px}.rounded-lg{border-radius:.5rem}.rounded-xl{border-radius:.75rem}.border-2{border-width:2px}.bg-blue-500{--tw-bg-opacity:1;background-color:rgb(59 130 246/var(--tw-bg-opacity))}.bg-orange-400{--tw-bg-opacity:1;background-color:rgb(251 146 60/var(--tw-bg-opacity))}.bg-red-500{--tw-bg-opacity:1;background-color:rgb(239 68 68/var(--tw-bg-opacity))}.bg-slate-400{--tw-bg-opacity:1;background-color:rgb(148 163 184/var(--tw-bg-opacity))}.bg-white{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity))}.p-2{padding:.5rem}.px-2{padding-left:.5rem;padding-right:.5rem}.px-5{padding-left:1.25rem;padding-right:1.25rem}.py-1{padding-top:.25rem;padding-bottom:.25rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.font-bold{font-weight:700}.font-semibold{font-weight:600}.opacity-30{opacity:.3}.transition-all{transition-property:all;transition-timing-function:cubic-bezier(.4,0,.2,1);transition-duration:.15s}:root{--primary-color:#fec856;--secondary-color:#fe9256;--light-bg-color:#fef2da}.bg-primary-light{background-color:var(--light-bg-color)}.bg-primary{background-color:var(--primary-color)}.bg-secondary,.hover-bg-secondary:hover{background-color:var(--secondary-color)}.border-primary{border-color:var(--primary-color)}.empty-3s{animation:empty 3s ease-out 0s;animation-fill-mode:forwards}.empty-5s{animation:empty 5s ease-out 0s;animation-fill-mode:forwards}@keyframes empty{0%{width:100%}to{width:0}} \ No newline at end of file +/*! tailwindcss v3.4.3 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}:host,html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.mt-2{margin-top:.5rem}.mt-\[20rem\]{margin-top:20rem}.flex{display:flex}.h-12{height:3rem}.h-16{height:4rem}.h-24{height:6rem}.h-36{height:9rem}.h-44{height:11rem}.h-5{height:1.25rem}.h-56{height:14rem}.h-8{height:2rem}.h-\[1rem\]{height:1rem}.h-\[64px\]{height:64px}.h-fit{height:-moz-fit-content;height:fit-content}.h-full{height:100%}.min-h-\[20rem\]{min-height:20rem}.w-1{width:.25rem}.w-1\/2{width:50%}.w-36{width:9rem}.w-5{width:1.25rem}.w-\[20px\]{width:20px}.w-\[28rem\]{width:28rem}.w-\[64px\]{width:64px}.w-auto{width:auto}.w-fit{width:-moz-fit-content;width:fit-content}.w-full{width:100%}.flex-col{flex-direction:column}.content-center{align-content:center}.items-center{align-items:center}.justify-center{justify-content:center}.justify-between{justify-content:space-between}.gap-2{gap:.5rem}.gap-3{gap:.75rem}.gap-5{gap:1.25rem}.rounded-full{border-radius:9999px}.rounded-lg{border-radius:.5rem}.rounded-xl{border-radius:.75rem}.border-2{border-width:2px}.bg-blue-500{--tw-bg-opacity:1;background-color:rgb(59 130 246/var(--tw-bg-opacity))}.bg-orange-400{--tw-bg-opacity:1;background-color:rgb(251 146 60/var(--tw-bg-opacity))}.bg-red-500{--tw-bg-opacity:1;background-color:rgb(239 68 68/var(--tw-bg-opacity))}.bg-slate-400{--tw-bg-opacity:1;background-color:rgb(148 163 184/var(--tw-bg-opacity))}.bg-white{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity))}.p-2{padding:.5rem}.px-2{padding-left:.5rem;padding-right:.5rem}.px-5{padding-left:1.25rem;padding-right:1.25rem}.py-1{padding-top:.25rem;padding-bottom:.25rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.font-bold{font-weight:700}.font-semibold{font-weight:600}.opacity-30{opacity:.3}.transition-all{transition-property:all;transition-timing-function:cubic-bezier(.4,0,.2,1);transition-duration:.15s}:root{--primary-color:#fec856;--secondary-color:#fe9256;--light-bg-color:#fef2da}.bg-primary-light{background-color:var(--light-bg-color)}.bg-primary{background-color:var(--primary-color)}.bg-secondary,.hover-bg-secondary:hover{background-color:var(--secondary-color)}.border-primary{border-color:var(--primary-color)}.empty-3s{animation:empty 3s ease-out 0s;animation-fill-mode:forwards}.empty-5s{animation:empty 5s ease-out 0s;animation-fill-mode:forwards}@keyframes empty{0%{width:100%}to{width:0}} \ No newline at end of file diff --git a/template/gameHome.html.tmpl b/template/gameHome.html.tmpl index 847c184..99f7f99 100644 --- a/template/gameHome.html.tmpl +++ b/template/gameHome.html.tmpl @@ -1,7 +1,7 @@ {{ define "gameHome" }} -
-
- logo +
+
+ logo

RPS Elite

@@ -18,26 +18,25 @@ {{ template "opponent-panel" .OpponentInfo}} -
+
-
+
-
- - -
-
-

move 1 to 2

+
{{ end }} \ No newline at end of file diff --git a/template/history.html.tmpl b/template/history.html.tmpl new file mode 100644 index 0000000..6eff9d0 --- /dev/null +++ b/template/history.html.tmpl @@ -0,0 +1,26 @@ +{{ define "history-row" }} +
+
+ {{ if ne .OpponentMove "" }} + {{.OpponentMove}} + {{ else }} +
+ {{ end }} + +{{ if eq .Winner "" }} +

tie

+ +{{ else }} +

{{ .Winner }}

+ +{{ end }} + + {{ if ne .PlayerMove "" }} + {{.PlayerMove}} + {{ else }} +
+ {{ end }} +
+ +
+{{ end }}