@ -0,0 +1,33 @@ |
|||||
|
package constants |
||||
|
|
||||
|
// game constants
|
||||
|
|
||||
|
const GALAXYSIZE = 50 |
||||
|
const SOLARSYSTEMSIZE = 15 |
||||
|
|
||||
|
// MetalMineLevels contains the constants of productivity for each level, sorted from 0 to X levels
|
||||
|
var MetalMineLevels = []int64{ |
||||
|
0, |
||||
|
1, |
||||
|
5, |
||||
|
10, |
||||
|
// TODO this will be same strategy with all the buildings and research
|
||||
|
} |
||||
|
var CrystalMineLevels = []int64{ |
||||
|
0, |
||||
|
1, |
||||
|
5, |
||||
|
10, |
||||
|
} |
||||
|
var DeuteriumMineLevels = []int64{ |
||||
|
0, |
||||
|
1, |
||||
|
5, |
||||
|
10, |
||||
|
} |
||||
|
var EnergyMineLevels = []int64{ |
||||
|
0, |
||||
|
1, |
||||
|
5, |
||||
|
10, |
||||
|
} |
@ -1,21 +1,138 @@ |
|||||
package models |
package models |
||||
|
|
||||
import "gopkg.in/mgo.v2/bson" |
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"time" |
||||
|
|
||||
type Resource struct { |
|
||||
Value int64 |
|
||||
Max int64 |
|
||||
|
"github.com/arnaucube/gogame/database" |
||||
|
"gopkg.in/mgo.v2/bson" |
||||
|
) |
||||
|
|
||||
|
type Resources struct { |
||||
|
Metal int64 |
||||
|
Crystal int64 |
||||
|
Deuterium int64 |
||||
|
Energy int64 |
||||
|
} |
||||
|
|
||||
|
// UserDb is the data in DB
|
||||
|
type UserDb struct { |
||||
|
Id bson.ObjectId `json:"id" bson:"_id,omitempty"` |
||||
|
Name string |
||||
|
Password string |
||||
|
Email string |
||||
|
LastUpdated time.Time |
||||
|
Resources Resources |
||||
|
Planets []bson.ObjectId |
||||
} |
} |
||||
|
|
||||
|
// User is the data in memory, after getting it from DB
|
||||
type User struct { |
type User struct { |
||||
Id bson.ObjectId `json:"id" bson:"_id,omitempty"` |
|
||||
Name string |
|
||||
Password string |
|
||||
Email string |
|
||||
Resources struct { |
|
||||
Metal Resource |
|
||||
Crystal Resource |
|
||||
Deuterium Resource |
|
||||
Energy Resource |
|
||||
|
Id bson.ObjectId `json:"id" bson:"_id,omitempty"` |
||||
|
Name string |
||||
|
LastUpdated time.Time |
||||
|
db *database.Db |
||||
|
Resources Resources |
||||
|
} |
||||
|
|
||||
|
func NewUser(db *database.Db, name, password, email string) (*User, error) { |
||||
|
newUser := UserDb{ |
||||
|
Id: bson.NewObjectId(), |
||||
|
Name: name, |
||||
|
Password: password, |
||||
|
Email: email, |
||||
|
LastUpdated: time.Now(), |
||||
|
} |
||||
|
err := db.Users.Insert(newUser) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
} |
} |
||||
|
user := UserDbToUser(db, newUser) |
||||
|
return user, nil |
||||
|
} |
||||
|
|
||||
|
func UserDbToUser(db *database.Db, u UserDb) *User { |
||||
|
return &User{ |
||||
|
Id: u.Id, |
||||
|
Name: u.Name, |
||||
|
LastUpdated: u.LastUpdated, |
||||
|
db: db, |
||||
|
Resources: u.Resources, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (u *User) StoreInDb() error { |
||||
|
userDb := UserDb{ |
||||
|
Id: u.Id, |
||||
|
Name: u.Name, |
||||
|
LastUpdated: u.LastUpdated, |
||||
|
Resources: u.Resources, |
||||
|
} |
||||
|
err := u.db.Users.Update(bson.M{"_id": u.Id}, userDb) |
||||
|
return err |
||||
|
|
||||
|
} |
||||
|
|
||||
|
func (u *User) GetFromDb() error { |
||||
|
var userDb UserDb |
||||
|
err := u.db.Users.Find(bson.M{"_id": u.Id}).One(&userDb) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
u = UserDbToUser(u.db, userDb) |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func (u *User) GetPlanets() ([]Planet, error) { |
||||
|
var planets []Planet |
||||
|
err := u.db.Users.Find(bson.M{"OwnerId": u.Id}).All(&planets) |
||||
|
if err != nil { |
||||
|
return planets, err |
||||
|
} |
||||
|
return planets, nil |
||||
|
} |
||||
|
|
||||
|
// GetResources updates the values of resources and returns the value
|
||||
|
// Resource types: metal, crystal, deuterium, energy
|
||||
|
func (u *User) GetResources() (*User, error) { |
||||
|
// get current values
|
||||
|
err := u.GetFromDb() |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
// get u.LastUpdated
|
||||
|
fmt.Println(u.LastUpdated) |
||||
|
// calculate Delta time = currentTime - u.LastUpdated
|
||||
|
delta := time.Since(u.LastUpdated) |
||||
|
|
||||
|
// get planets
|
||||
|
planets, err := u.GetPlanets() |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
// get Resource-Plant level in each planet
|
||||
|
var metalGrowth, crystalGrowth, deuteriumGrowth, energyGrowth int64 |
||||
|
for _, planet := range planets { |
||||
|
// calculate growth = ResourcePlant.Level for each planet
|
||||
|
// TODO find correct formulas
|
||||
|
metalGrowth = metalGrowth + (planet.Buildings.MetalMine * int64(delta)) |
||||
|
crystalGrowth = crystalGrowth + (planet.Buildings.CrystalMine * int64(delta)) |
||||
|
deuteriumGrowth = deuteriumGrowth + (planet.Buildings.DeuteriumMine * int64(delta)) |
||||
|
energyGrowth = energyGrowth + (planet.Buildings.EnergyMine * int64(delta)) |
||||
|
} |
||||
|
// calculate newAmount = oldAmount + (growth & DeltaTime)
|
||||
|
u.Resources.Metal = u.Resources.Metal + metalGrowth |
||||
|
u.Resources.Crystal = u.Resources.Crystal + crystalGrowth |
||||
|
u.Resources.Deuterium = u.Resources.Deuterium + deuteriumGrowth |
||||
|
u.Resources.Energy = u.Resources.Energy + energyGrowth |
||||
|
|
||||
|
// store new amount to user db
|
||||
|
err = u.StoreInDb() |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
// return user
|
||||
|
return u, nil |
||||
} |
} |
@ -0,0 +1,17 @@ |
|||||
|
package models |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/arnaucube/gogame/database" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestCreateUser(t *testing.T) { |
||||
|
db, err := database.New("127.0.0.1:27017", "gogametests") |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
user, err := NewUser(db, "user00", "password00", "user00@email.com") |
||||
|
assert.Nil(t, err) |
||||
|
assert.Equal(t, user.Name, "user00") |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package gamesrv |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/arnaucube/gogame/database" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"gopkg.in/mgo.v2/bson" |
||||
|
) |
||||
|
|
||||
|
func TestCreatePlanet(t *testing.T) { |
||||
|
db, err := database.New("127.0.0.1:27017", "gogametests") |
||||
|
assert.Nil(t, err) |
||||
|
srv := New(db) |
||||
|
|
||||
|
solarSystem, planet, err := srv.CreatePlanet(bson.ObjectIdHex("5d029a6ff18ba24f406168fe")) |
||||
|
assert.Nil(t, err) |
||||
|
fmt.Println(solarSystem) |
||||
|
fmt.Println(planet) |
||||
|
} |
@ -1 +0,0 @@ |
|||||
node_modules |
|
@ -1,56 +0,0 @@ |
|||||
{ |
|
||||
"name": "gogame-test", |
|
||||
"version": "0.0.1", |
|
||||
"lockfileVersion": 1, |
|
||||
"requires": true, |
|
||||
"dependencies": { |
|
||||
"axios": { |
|
||||
"version": "0.19.0", |
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz", |
|
||||
"integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==", |
|
||||
"requires": { |
|
||||
"follow-redirects": "1.5.10", |
|
||||
"is-buffer": "^2.0.2" |
|
||||
} |
|
||||
}, |
|
||||
"debug": { |
|
||||
"version": "3.1.0", |
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", |
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", |
|
||||
"requires": { |
|
||||
"ms": "2.0.0" |
|
||||
} |
|
||||
}, |
|
||||
"follow-redirects": { |
|
||||
"version": "1.5.10", |
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", |
|
||||
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", |
|
||||
"requires": { |
|
||||
"debug": "=3.1.0" |
|
||||
} |
|
||||
}, |
|
||||
"is-buffer": { |
|
||||
"version": "2.0.3", |
|
||||
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz", |
|
||||
"integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==" |
|
||||
}, |
|
||||
"ms": { |
|
||||
"version": "2.0.0", |
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", |
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" |
|
||||
}, |
|
||||
"nan": { |
|
||||
"version": "2.14.0", |
|
||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", |
|
||||
"integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" |
|
||||
}, |
|
||||
"sleep": { |
|
||||
"version": "6.1.0", |
|
||||
"resolved": "https://registry.npmjs.org/sleep/-/sleep-6.1.0.tgz", |
|
||||
"integrity": "sha512-Z1x4JjJxsru75Tqn8F4tnOFeEu3HjtITTsumYUiuz54sGKdISgLCek9AUlXlVVrkhltRFhNUsJDJE76SFHTDIQ==", |
|
||||
"requires": { |
|
||||
"nan": "^2.13.2" |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
@ -1,22 +0,0 @@ |
|||||
{ |
|
||||
"name": "gogame-test", |
|
||||
"version": "0.0.1", |
|
||||
"description": "", |
|
||||
"main": "test.js", |
|
||||
"scripts": { |
|
||||
"test": "echo \"Error: no test specified\" && exit 1" |
|
||||
}, |
|
||||
"repository": { |
|
||||
"type": "git", |
|
||||
"url": "git+https://github.com/arnaucube/gogame.git" |
|
||||
}, |
|
||||
"author": "", |
|
||||
"license": "ISC", |
|
||||
"bugs": { |
|
||||
"url": "https://github.com/arnaucube/gogame/issues" |
|
||||
}, |
|
||||
"homepage": "https://github.com/arnaucube/gogame#readme", |
|
||||
"dependencies": { |
|
||||
"axios": "^0.19.0" |
|
||||
} |
|
||||
} |
|
@ -1,29 +0,0 @@ |
|||||
const axios = require('axios'); |
|
||||
|
|
||||
const url = 'http://127.0.0.1:5000'; |
|
||||
|
|
||||
// let newUser = {
|
|
||||
// name: 'user00',
|
|
||||
// password: 'user00password',
|
|
||||
// email: 'user00@email.com'
|
|
||||
// };
|
|
||||
// axios.post(url + '/register', newUser)
|
|
||||
// .then(function (res) {
|
|
||||
// console.log(res.data);
|
|
||||
// })
|
|
||||
// .catch(function (error) {
|
|
||||
// console.error(error.response.data);
|
|
||||
// });
|
|
||||
|
|
||||
|
|
||||
let user = { |
|
||||
email: 'user00@email.com', |
|
||||
password: 'user00password' |
|
||||
}; |
|
||||
axios.post(url + '/login', user) |
|
||||
.then(function (res) { |
|
||||
console.log(res.data); |
|
||||
}) |
|
||||
.catch(function (error) { |
|
||||
console.error(error.response.data); |
|
||||
}); |
|
@ -0,0 +1,42 @@ |
|||||
|
#!/usr/bin/env python3 |
||||
|
"""Test endpoints for gogame |
||||
|
""" |
||||
|
|
||||
|
import json |
||||
|
import requests |
||||
|
import provoj |
||||
|
import time |
||||
|
|
||||
|
import subprocess |
||||
|
subprocess.check_call(["mongo", "gogame", "--eval", "'db.dropDatabase()'"]) |
||||
|
|
||||
|
time.sleep(1) |
||||
|
|
||||
|
URL = "http://127.0.0.1:5000" |
||||
|
|
||||
|
t = provoj.NewTest("gogame") |
||||
|
|
||||
|
registerData = { |
||||
|
"name": "user00", |
||||
|
"password": "user00password", |
||||
|
"email": "user00@email.com", |
||||
|
} |
||||
|
r = requests.post(URL + "/register", json=registerData) |
||||
|
t.rStatus("post /register", r) |
||||
|
jsonR = r.json() |
||||
|
print(jsonR) |
||||
|
|
||||
|
time.sleep(1) |
||||
|
|
||||
|
loginData = { |
||||
|
"email": "user00@email.com", |
||||
|
"password": "user00password", |
||||
|
} |
||||
|
r = requests.post(URL + "/login", json=loginData) |
||||
|
t.rStatus("post /login", r) |
||||
|
jsonR = r.json() |
||||
|
print(jsonR) |
||||
|
|
||||
|
|
||||
|
t.printScores() |
||||
|
|