Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Oct 28, 2024
1 parent fee1531 commit 4df7256
Show file tree
Hide file tree
Showing 18 changed files with 2,513 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Welcome, fedinaut! localhost.localdomain:8443 is an instance of tootik, a federa
🔭 View profile
🔖 Bookmarks
🔎 Search posts
🎲 Games
📣 New post
⚙️ Settings
📊 Status
Expand Down Expand Up @@ -109,6 +110,7 @@ This makes tootik lightweight, private and accessible:
* Mention community in a public post to start thread
* Community sends posts and replies to all members
* Bookmarks
* Games
* Full-text search within posts
* Upload of posts and user avatars, over [Titan](gemini://transjovian.org/titan)
* Account migration, in both directions
Expand Down
6 changes: 6 additions & 0 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Config struct {
MaxBookmarksPerUser int
MinBookmarkInterval time.Duration

MinCheckersInterval time.Duration

PostsPerPage int
RepliesPerPage int
MaxOffset int
Expand Down Expand Up @@ -220,6 +222,10 @@ func (c *Config) FillDefaults() {
c.MinBookmarkInterval = time.Second * 5
}

if c.MinCheckersInterval <= 0 {
c.MinCheckersInterval = time.Hour
}

if c.PostsPerPage <= 0 {
c.PostsPerPage = 30
}
Expand Down
21 changes: 21 additions & 0 deletions checkers/coord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2024 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkers

type Coord struct {
X, Y int
}
132 changes: 132 additions & 0 deletions checkers/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
Copyright 2024 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checkers

import (
"database/sql/driver"
"encoding/json"
"fmt"
)

type jsonPiece struct {
Piece
Coord
}

type jsonBoard struct {
Humans []jsonPiece
Orcs []jsonPiece
}

type stateJson struct {
jsonBoard
Turns []jsonBoard
Current Player
}

func (s *State) MarshalJSON() ([]byte, error) {
tmp := stateJson{
jsonBoard: jsonBoard{
Humans: make([]jsonPiece, 0, len(s.Humans)),
Orcs: make([]jsonPiece, 0, len(s.Orcs)),
},
Current: s.Current,
}

for pos, human := range s.Humans {
tmp.Humans = append(tmp.Humans, jsonPiece{Piece: human, Coord: pos})
}

for pos, orc := range s.Orcs {
tmp.Orcs = append(tmp.Orcs, jsonPiece{Piece: orc, Coord: pos})
}

for pos, human := range s.Humans {
tmp.Humans = append(tmp.Humans, jsonPiece{Piece: human, Coord: pos})
}

for _, turn := range s.Turns {
clone := jsonBoard{
Humans: make([]jsonPiece, 0, len(turn.Humans)),
Orcs: make([]jsonPiece, 0, len(turn.Orcs)),
}

for pos, human := range turn.Humans {
clone.Humans = append(clone.Humans, jsonPiece{Piece: human, Coord: pos})
}

for pos, orc := range turn.Orcs {
clone.Orcs = append(clone.Orcs, jsonPiece{Piece: orc, Coord: pos})
}

tmp.Turns = append(tmp.Turns, clone)
}

return json.Marshal(tmp)
}

func (s *State) UnmarshalJSON(b []byte) error {
var tmp stateJson
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}

s.Humans = make(map[Coord]Piece, 12)
for _, human := range tmp.Humans {
s.Humans[human.Coord] = human.Piece
}

s.Orcs = make(map[Coord]Piece, 12)
for _, orc := range tmp.Orcs {
s.Orcs[orc.Coord] = orc.Piece
}

s.Turns = make([]Board, 0, len(tmp.Turns))
for _, clone := range tmp.Turns {
board := Board{
Humans: make(map[Coord]Piece, 12),
Orcs: make(map[Coord]Piece, 12),
}

for _, human := range clone.Humans {
board.Humans[human.Coord] = human.Piece
}

for _, orc := range clone.Orcs {
board.Orcs[orc.Coord] = orc.Piece
}

s.Turns = append(s.Turns, board)
}

s.Current = tmp.Current

return nil
}

func (s *State) Scan(src any) error {
str, ok := src.(string)
if !ok {
return fmt.Errorf("unsupported conversion from %T to %T", src, s)
}
return json.Unmarshal([]byte(str), s)
}

func (s *State) Value() (driver.Value, error) {
buf, err := json.Marshal(s)
return string(buf), err
}
Loading

0 comments on commit 4df7256

Please sign in to comment.